blob: a2e0036e38dc5bacc7c27660b12b36be760e4817 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.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"
Duncan Sandsb116fac2007-07-27 20:02:49 +000042#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043using namespace llvm;
44
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000045X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000047 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000051
Chris Lattnerd43d00c2008-01-24 08:07:48 +000052 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000053
Anton Korobeynikov2365f512007-07-14 14:06:15 +000054 RegInfo = TM.getRegisterInfo();
55
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000062 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000063 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000064 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000065
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000066 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000067 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000068 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000070 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000071 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000080 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000083 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000085
Duncan Sandsf9c98e62008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000087
Chris Lattnerddf89562008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000101
Evan Cheng25ab6902006-09-08 06:48:29 +0000102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000105 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000117 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126
Dale Johannesen73328d12007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000131
Evan Cheng02568ff2006-01-30 22:13:22 +0000132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000141 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
Evan Cheng25ab6902006-09-08 06:48:29 +0000152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165
Chris Lattner399610a2006-12-05 18:22:22 +0000166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
Chris Lattner21f66852005-12-23 05:15:23 +0000171
Dan Gohmanb00ee212008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000206
Evan Chengc35497f2006-10-30 08:02:39 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000211 setOperationAction(ISD::MEMMOVE , 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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000219 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000220
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000225 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
226 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000228 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
229 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit()) {
231 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 }
235
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000236 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000237 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000238
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000239 // These should be promoted to a larger select which is supported.
240 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
241 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000242 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000243 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
244 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000247 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000248 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000253 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
256 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
257 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000258 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000259 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000260 if (!Subtarget->is64Bit())
261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
262
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000263 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000268 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
271 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
272 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
273 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
274 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000275 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000276 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000279 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000280 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
281 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000282
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000283 if (!Subtarget->hasSSE2())
284 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
285
286
Evan Chenga844bde2008-02-02 04:07:54 +0000287 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000288 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000289 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000290 if (!Subtarget->isTargetDarwin() &&
291 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000292 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000293 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000294
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000295 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
296 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
297 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
298 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
299 if (Subtarget->is64Bit()) {
300 // FIXME: Verify
301 setExceptionPointerRegister(X86::RAX);
302 setExceptionSelectorRegister(X86::RDX);
303 } else {
304 setExceptionPointerRegister(X86::EAX);
305 setExceptionSelectorRegister(X86::EDX);
306 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000307 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000308
Duncan Sandsf7331b32007-09-11 14:10:23 +0000309 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000310
Chris Lattnerda68d302008-01-15 21:58:22 +0000311 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000312
Nate Begemanacc398c2006-01-25 18:21:52 +0000313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
314 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000315 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000316 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000317 if (Subtarget->is64Bit())
318 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
319 else
320 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
321
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000322 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000323 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 if (Subtarget->is64Bit())
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000326 if (Subtarget->isTargetCygMing())
327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
328 else
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000330
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000331 if (X86ScalarSSEf64) {
332 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000334 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
335 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336
Evan Cheng223547a2006-01-31 22:28:30 +0000337 // Use ANDPD to simulate FABS.
338 setOperationAction(ISD::FABS , MVT::f64, Custom);
339 setOperationAction(ISD::FABS , MVT::f32, Custom);
340
341 // Use XORP to simulate FNEG.
342 setOperationAction(ISD::FNEG , MVT::f64, Custom);
343 setOperationAction(ISD::FNEG , MVT::f32, Custom);
344
Evan Cheng68c47cb2007-01-05 07:55:56 +0000345 // Use ANDPD and ORPD to simulate FCOPYSIGN.
346 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
347 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
348
Evan Chengd25e9e82006-02-02 00:28:23 +0000349 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000350 setOperationAction(ISD::FSIN , MVT::f64, Expand);
351 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352 setOperationAction(ISD::FREM , MVT::f64, Expand);
353 setOperationAction(ISD::FSIN , MVT::f32, Expand);
354 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000355 setOperationAction(ISD::FREM , MVT::f32, Expand);
356
Chris Lattnera54aa942006-01-29 06:26:08 +0000357 // Expand FP immediates into loads from the stack, except for the special
358 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000359 addLegalFPImmediate(APFloat(+0.0)); // xorpd
360 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000361
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000362 // Floating truncations from f80 and extensions to f80 go through memory.
363 // If optimizing, we lie about this though and handle it in
364 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
365 if (Fast) {
366 setConvertAction(MVT::f32, MVT::f80, Expand);
367 setConvertAction(MVT::f64, MVT::f80, Expand);
368 setConvertAction(MVT::f80, MVT::f32, Expand);
369 setConvertAction(MVT::f80, MVT::f64, Expand);
370 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000371 } else if (X86ScalarSSEf32) {
372 // Use SSE for f32, x87 for f64.
373 // Set up the FP register classes.
374 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
375 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
376
377 // Use ANDPS to simulate FABS.
378 setOperationAction(ISD::FABS , MVT::f32, Custom);
379
380 // Use XORP to simulate FNEG.
381 setOperationAction(ISD::FNEG , MVT::f32, Custom);
382
383 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
384
385 // Use ANDPS and ORPS to simulate FCOPYSIGN.
386 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
387 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
388
389 // We don't support sin/cos/fmod
390 setOperationAction(ISD::FSIN , MVT::f32, Expand);
391 setOperationAction(ISD::FCOS , MVT::f32, Expand);
392 setOperationAction(ISD::FREM , MVT::f32, Expand);
393
Nate Begemane1795842008-02-14 08:57:00 +0000394 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000395 addLegalFPImmediate(APFloat(+0.0f)); // xorps
396 addLegalFPImmediate(APFloat(+0.0)); // FLD0
397 addLegalFPImmediate(APFloat(+1.0)); // FLD1
398 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
399 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
400
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000401 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
402 // this though and handle it in InstructionSelectPreprocess so that
403 // dagcombine2 can hack on these.
404 if (Fast) {
405 setConvertAction(MVT::f32, MVT::f64, Expand);
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f80, MVT::f32, Expand);
408 setConvertAction(MVT::f64, MVT::f32, Expand);
409 // And x87->x87 truncations also.
410 setConvertAction(MVT::f80, MVT::f64, Expand);
411 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412
413 if (!UnsafeFPMath) {
414 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
415 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
416 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000417 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000418 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000422
Evan Cheng68c47cb2007-01-05 07:55:56 +0000423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000424 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000427
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000428 // Floating truncations go through memory. If optimizing, we lie about
429 // this though and handle it in InstructionSelectPreprocess so that
430 // dagcombine2 can hack on these.
431 if (Fast) {
432 setConvertAction(MVT::f80, MVT::f32, Expand);
433 setConvertAction(MVT::f64, MVT::f32, Expand);
434 setConvertAction(MVT::f80, MVT::f64, Expand);
435 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000441 addLegalFPImmediate(APFloat(+0.0)); // FLD0
442 addLegalFPImmediate(APFloat(+1.0)); // FLD1
443 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
444 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000445 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
446 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
447 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
448 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000449 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000450
Dale Johannesen59a58732007-08-05 18:49:15 +0000451 // Long double always uses X87.
452 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000453 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000455 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000456 APFloat TmpFlt(+0.0);
457 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
458 addLegalFPImmediate(TmpFlt); // FLD0
459 TmpFlt.changeSign();
460 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
461 APFloat TmpFlt2(+1.0);
462 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt2); // FLD1
464 TmpFlt2.changeSign();
465 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
466 }
467
Dale Johannesen2f429012007-09-26 21:10:55 +0000468 if (!UnsafeFPMath) {
469 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
470 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
471 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000472
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000473 // Always use a library call for pow.
474 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
475 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
476 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
477
Evan Chengd30bf012006-03-01 01:11:20 +0000478 // First set operation action for all vector types to expand. Then we
479 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000480 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
481 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000482 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000484 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000485 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000486 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000487 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000488 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000494 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000495 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000496 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000497 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000498 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000505 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000509 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000510 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman89081322007-12-12 22:21:26 +0000513 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000519 }
520
Evan Chenga88973f2006-03-22 19:22:18 +0000521 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000522 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
523 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
524 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000525 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000526
Evan Chengd30bf012006-03-01 01:11:20 +0000527 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000528
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000529 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
530 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
531 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000532 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000533
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000534 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
535 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
536 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000537 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000538
Bill Wendling74027e92007-03-15 21:24:36 +0000539 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
540 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
541
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000542 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000543 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000544 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000545 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
546 setOperationAction(ISD::AND, MVT::v2i32, Promote);
547 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
548 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000549
550 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000551 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000552 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000553 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::OR, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000557
558 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000559 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000560 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000561 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000565
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000566 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000567 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000568 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000569 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000573
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000574 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
575 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000578
579 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
580 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000583
584 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
585 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000587 }
588
Evan Chenga88973f2006-03-22 19:22:18 +0000589 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000590 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
591
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000592 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
593 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
594 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
595 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000596 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
597 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000598 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000601 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000602 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000603 }
604
Evan Chenga88973f2006-03-22 19:22:18 +0000605 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000606 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
607 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
608 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
609 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
611
Evan Chengf7c378e2006-04-10 07:23:14 +0000612 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
613 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
614 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000615 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000616 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
617 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
618 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000619 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000620 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000621 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
622 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
623 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
624 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000625 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
626 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000627
Evan Chengf7c378e2006-04-10 07:23:14 +0000628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000630 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000632 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000633
Evan Cheng2c3ae372006-04-12 21:21:57 +0000634 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
635 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begeman844e0f92007-12-11 01:41:33 +0000636 // Do not attempt to custom lower non-power-of-2 vectors
637 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
638 continue;
Evan Cheng2c3ae372006-04-12 21:21:57 +0000639 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
641 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
642 }
643 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
644 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000647 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000648 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000649 if (Subtarget->is64Bit()) {
650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000652 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000653
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000654 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000655 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
656 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
657 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
658 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
659 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
660 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
661 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000662 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
663 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000664 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000666 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000667
Chris Lattnerddf89562008-01-17 19:59:44 +0000668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000669
Evan Cheng2c3ae372006-04-12 21:21:57 +0000670 // Custom lower v2i64 and v2f64 selects.
671 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000672 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000673 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000674 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000675 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000676
677 if (Subtarget->hasSSE41()) {
678 // FIXME: Do we need to handle scalar-to-vector here?
679 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
680
681 // i8 and i16 vectors are custom , because the source register and source
682 // source memory operand types are not the same width. f32 vectors are
683 // custom since the immediate controlling the insert encodes additional
684 // information.
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
689
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
694
695 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000698 }
699 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000700
Evan Cheng6be2c582006-04-05 23:38:46 +0000701 // We want to custom lower some of our intrinsics.
702 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
703
Evan Cheng206ee9d2006-07-07 08:33:52 +0000704 // We have target-specific dag combine patterns for the following nodes:
705 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000706 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000707 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000708
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000709 computeRegisterProperties();
710
Evan Cheng87ed7162006-02-14 08:25:08 +0000711 // FIXME: These should be based on subtarget info. Plus, the values should
712 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000713 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
714 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
715 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000716 allowUnalignedMemoryAccesses = true; // x86 supports it!
717}
718
Evan Cheng29286502008-01-23 23:17:41 +0000719/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
720/// the desired ByVal argument alignment.
721static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
722 if (MaxAlign == 16)
723 return;
724 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
725 if (VTy->getBitWidth() == 128)
726 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000727 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
728 unsigned EltAlign = 0;
729 getMaxByValAlign(ATy->getElementType(), EltAlign);
730 if (EltAlign > MaxAlign)
731 MaxAlign = EltAlign;
732 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
733 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
734 unsigned EltAlign = 0;
735 getMaxByValAlign(STy->getElementType(i), EltAlign);
736 if (EltAlign > MaxAlign)
737 MaxAlign = EltAlign;
738 if (MaxAlign == 16)
739 break;
740 }
741 }
742 return;
743}
744
745/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
746/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000747/// that contain SSE vectors are placed at 16-byte boundaries while the rest
748/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000749unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
750 if (Subtarget->is64Bit())
751 return getTargetData()->getABITypeAlignment(Ty);
752 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000753 if (Subtarget->hasSSE1())
754 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000755 return Align;
756}
Chris Lattner2b02a442007-02-25 08:29:00 +0000757
Evan Chengcc415862007-11-09 01:32:10 +0000758/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
759/// jumptable.
760SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
761 SelectionDAG &DAG) const {
762 if (usesGlobalOffsetTable())
763 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
764 if (!Subtarget->isPICStyleRIPRel())
765 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
766 return Table;
767}
768
Chris Lattner2b02a442007-02-25 08:29:00 +0000769//===----------------------------------------------------------------------===//
770// Return Value Calling Convention Implementation
771//===----------------------------------------------------------------------===//
772
Chris Lattner59ed56b2007-02-28 04:55:35 +0000773#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000774
775/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
776/// exists skip possible ISD:TokenFactor.
777static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000778 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000779 return Chain;
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000780 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000781 if (Chain.getNumOperands() &&
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000782 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000783 return Chain.getOperand(0);
784 }
785 return Chain;
786}
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000787
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000788/// LowerRET - Lower an ISD::RET node.
789SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
790 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
791
Chris Lattner9774c912007-02-27 05:28:59 +0000792 SmallVector<CCValAssign, 16> RVLocs;
793 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000794 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
795 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000796 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000797
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000798 // If this is the first return lowered for this function, add the regs to the
799 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000800 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000801 for (unsigned i = 0; i != RVLocs.size(); ++i)
802 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000803 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000804 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000805 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000806
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000807 // Handle tail call return.
808 Chain = GetPossiblePreceedingTailCall(Chain);
809 if (Chain.getOpcode() == X86ISD::TAILCALL) {
810 SDOperand TailCall = Chain;
811 SDOperand TargetAddress = TailCall.getOperand(1);
812 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000813 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000814 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
815 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
816 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
817 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
818 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000819 assert(StackAdjustment.getOpcode() == ISD::Constant &&
820 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000821
822 SmallVector<SDOperand,8> Operands;
823 Operands.push_back(Chain.getOperand(0));
824 Operands.push_back(TargetAddress);
825 Operands.push_back(StackAdjustment);
826 // Copy registers used by the call. Last operand is a flag so it is not
827 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000828 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000829 Operands.push_back(Chain.getOperand(i));
830 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000831 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
832 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000833 }
834
835 // Regular return.
836 SDOperand Flag;
837
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000838 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000839 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
840 RVLocs[0].getLocReg() != X86::ST0) {
841 for (unsigned i = 0; i != RVLocs.size(); ++i) {
842 CCValAssign &VA = RVLocs[i];
843 assert(VA.isRegLoc() && "Can only return in registers!");
844 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
845 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000846 Flag = Chain.getValue(1);
847 }
848 } else {
849 // We need to handle a destination of ST0 specially, because it isn't really
850 // a register.
851 SDOperand Value = Op.getOperand(1);
852
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000853 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
854 // This will get legalized into a load/store if it can't get optimized away.
855 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
856 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000857
858 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
859 SDOperand Ops[] = { Chain, Value };
860 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
861 Flag = Chain.getValue(1);
862 }
863
864 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
865 if (Flag.Val)
866 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
867 else
868 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
869}
870
871
Chris Lattner3085e152007-02-25 08:59:22 +0000872/// LowerCallResult - Lower the result values of an ISD::CALL into the
873/// appropriate copies out of appropriate physical registers. This assumes that
874/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
875/// being lowered. The returns a SDNode with the same number of values as the
876/// ISD::CALL.
877SDNode *X86TargetLowering::
878LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
879 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000880
881 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000882 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000883 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
884 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000885 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
886
Chris Lattnere32bbf62007-02-28 07:09:55 +0000887 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000888
889 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000890 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
892 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
893 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000894 InFlag = Chain.getValue(2);
895 ResultVals.push_back(Chain.getValue(0));
896 }
897 } else {
898 // Copies from the FP stack are special, as ST0 isn't a valid register
899 // before the fp stackifier runs.
900
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
902 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
903 // the specified value type.
904 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
905 if (isScalarFPTypeInSSEReg(GetResultTy))
906 GetResultTy = MVT::f80;
907 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
908
Chris Lattner3085e152007-02-25 08:59:22 +0000909 SDOperand GROps[] = { Chain, InFlag };
910 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
911 Chain = RetVal.getValue(1);
912 InFlag = RetVal.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000913
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000914 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
915 // there.
916 if (GetResultTy != RVLocs[0].getValVT())
917 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
918 // This truncation won't change the value.
919 DAG.getIntPtrConstant(1));
920
Chris Lattner3085e152007-02-25 08:59:22 +0000921 ResultVals.push_back(RetVal);
922 }
923
924 // Merge everything together with a MERGE_VALUES node.
925 ResultVals.push_back(Chain);
926 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
927 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000928}
929
Evan Cheng0d9e9762008-01-29 19:34:22 +0000930/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
931/// ISD::CALL where the results are known to be in two 64-bit registers,
932/// e.g. XMM0 and XMM1. This simplify store the two values back to the
933/// fixed stack slot allocated for StructRet.
934SDNode *X86TargetLowering::
935LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
936 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
937 MVT::ValueType VT, SelectionDAG &DAG) {
938 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
939 Chain = RetVal1.getValue(1);
940 InFlag = RetVal1.getValue(2);
941 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
942 Chain = RetVal2.getValue(1);
943 InFlag = RetVal2.getValue(2);
944 SDOperand FIN = TheCall->getOperand(5);
945 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
946 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
947 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
948 return Chain.Val;
949}
950
951/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
952/// where the results are known to be in ST0 and ST1.
953SDNode *X86TargetLowering::
954LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
955 SDNode *TheCall, SelectionDAG &DAG) {
956 SmallVector<SDOperand, 8> ResultVals;
957 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
958 SDVTList Tys = DAG.getVTList(VTs, 4);
959 SDOperand Ops[] = { Chain, InFlag };
960 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
961 Chain = RetVal.getValue(2);
962 SDOperand FIN = TheCall->getOperand(5);
963 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
964 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
965 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
966 return Chain.Val;
967}
Chris Lattner2b02a442007-02-25 08:29:00 +0000968
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000969//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000970// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000971//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000972// StdCall calling convention seems to be standard for many Windows' API
973// routines and around. It differs from C calling convention just a little:
974// callee should clean up the stack, not caller. Symbols should be also
975// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000976// For info on fast calling convention see Fast Calling Convention (tail call)
977// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000978
Evan Cheng85e38002006-04-27 05:35:28 +0000979/// AddLiveIn - This helper function adds the specified physical register to the
980/// MachineFunction as a live in value. It also creates a corresponding virtual
981/// register for it.
982static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000983 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000984 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000985 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
986 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +0000987 return VReg;
988}
989
Gordon Henriksen86737662008-01-05 16:56:59 +0000990// Determines whether a CALL node uses struct return semantics.
991static bool CallIsStructReturn(SDOperand Op) {
992 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
993 if (!NumOps)
994 return false;
995
996 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
997 return Flags->getValue() & ISD::ParamFlags::StructReturn;
998}
999
1000// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1001static bool ArgsAreStructReturn(SDOperand Op) {
1002 unsigned NumArgs = Op.Val->getNumValues() - 1;
1003 if (!NumArgs)
1004 return false;
1005
1006 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1007 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1008}
1009
1010// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1011// its own arguments. Callee pop is necessary to support tail calls.
1012bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1013 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1014 if (IsVarArg)
1015 return false;
1016
1017 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1018 default:
1019 return false;
1020 case CallingConv::X86_StdCall:
1021 return !Subtarget->is64Bit();
1022 case CallingConv::X86_FastCall:
1023 return !Subtarget->is64Bit();
1024 case CallingConv::Fast:
1025 return PerformTailCallOpt;
1026 }
1027}
1028
1029// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1030CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1031 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1032
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001033 if (Subtarget->is64Bit()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001034 if (CC == CallingConv::Fast && PerformTailCallOpt)
1035 return CC_X86_64_TailCall;
1036 else
1037 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001038 }
1039
Gordon Henriksen86737662008-01-05 16:56:59 +00001040 if (CC == CallingConv::X86_FastCall)
1041 return CC_X86_32_FastCall;
1042 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1043 return CC_X86_32_TailCall;
1044 else
1045 return CC_X86_32_C;
1046}
1047
1048// Selects the appropriate decoration to apply to a MachineFunction containing a
1049// given FORMAL_ARGUMENTS node.
1050NameDecorationStyle
1051X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1052 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1053 if (CC == CallingConv::X86_FastCall)
1054 return FastCall;
1055 else if (CC == CallingConv::X86_StdCall)
1056 return StdCall;
1057 return None;
1058}
1059
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001060
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001061// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1062// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001063// the implementation of this call is very conservative and assumes all
1064// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001065// registers would be overwritten by direct lowering.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001066// Possible improvement:
1067// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1068// indicating inreg passed arguments which also need not be lowered to a safe
1069// stack slot.
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001070static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001071 RegisterSDNode * OpReg = NULL;
1072 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1073 (Op.getOpcode()== ISD::CopyFromReg &&
1074 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
Dan Gohman6f0d0242008-02-10 18:45:23 +00001075 OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister))
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001076 return true;
1077 return false;
1078}
1079
Evan Cheng8e5712b2008-01-12 01:08:07 +00001080// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1081// by "Src" to address "Dst" with size and alignment information specified by
1082// the specific parameter attribute. The copy will be passed as a byval function
1083// parameter.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001084static SDOperand
Evan Cheng8e5712b2008-01-12 01:08:07 +00001085CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1086 unsigned Flags, SelectionDAG &DAG) {
1087 unsigned Align = 1 <<
1088 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1089 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001090 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng8e5712b2008-01-12 01:08:07 +00001091 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1092 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001093 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng8e5712b2008-01-12 01:08:07 +00001094 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001095}
1096
Rafael Espindola7effac52007-09-14 15:48:13 +00001097SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1098 const CCValAssign &VA,
1099 MachineFrameInfo *MFI,
1100 SDOperand Root, unsigned i) {
1101 // Create the nodes corresponding to a load from this parameter slot.
Evan Chenge70bb592008-01-10 02:24:25 +00001102 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1103 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1104
1105 // FIXME: For now, all byval parameter objects are marked mutable. This
1106 // can be changed with more analysis.
Rafael Espindola7effac52007-09-14 15:48:13 +00001107 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Chenge70bb592008-01-10 02:24:25 +00001108 VA.getLocMemOffset(), !isByVal);
Rafael Espindola7effac52007-09-14 15:48:13 +00001109 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chenge70bb592008-01-10 02:24:25 +00001110 if (isByVal)
Rafael Espindola7effac52007-09-14 15:48:13 +00001111 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001112 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001113 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola7effac52007-09-14 15:48:13 +00001114}
1115
Gordon Henriksen86737662008-01-05 16:56:59 +00001116SDOperand
1117X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001118 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001119 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1120
1121 const Function* Fn = MF.getFunction();
1122 if (Fn->hasExternalLinkage() &&
1123 Subtarget->isTargetCygMing() &&
1124 Fn->getName() == "main")
1125 FuncInfo->setForceFramePointer(true);
1126
1127 // Decorate the function name.
1128 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1129
Evan Cheng1bc78042006-04-26 01:20:17 +00001130 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001131 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001132 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001133 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001134 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001135
1136 assert(!(isVarArg && CC == CallingConv::Fast) &&
1137 "Var args not supported with calling convention fastcc");
1138
Chris Lattner638402b2007-02-28 07:00:42 +00001139 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001140 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001141 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001142 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001143
Chris Lattnerf39f7712007-02-28 05:46:49 +00001144 SmallVector<SDOperand, 8> ArgValues;
1145 unsigned LastVal = ~0U;
1146 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1147 CCValAssign &VA = ArgLocs[i];
1148 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1149 // places.
1150 assert(VA.getValNo() != LastVal &&
1151 "Don't support value assigned to multiple locs yet");
1152 LastVal = VA.getValNo();
1153
1154 if (VA.isRegLoc()) {
1155 MVT::ValueType RegVT = VA.getLocVT();
1156 TargetRegisterClass *RC;
1157 if (RegVT == MVT::i32)
1158 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001159 else if (Is64Bit && RegVT == MVT::i64)
1160 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001161 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001162 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001163 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001164 RC = X86::FR64RegisterClass;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001165 else {
1166 assert(MVT::isVector(RegVT));
Gordon Henriksen86737662008-01-05 16:56:59 +00001167 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1168 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1169 RegVT = MVT::i64;
1170 } else
1171 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001172 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001173
Chris Lattner82932a52007-03-02 05:12:29 +00001174 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1175 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001176
1177 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1178 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1179 // right size.
1180 if (VA.getLocInfo() == CCValAssign::SExt)
1181 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1182 DAG.getValueType(VA.getValVT()));
1183 else if (VA.getLocInfo() == CCValAssign::ZExt)
1184 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1185 DAG.getValueType(VA.getValVT()));
1186
1187 if (VA.getLocInfo() != CCValAssign::Full)
1188 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1189
Gordon Henriksen86737662008-01-05 16:56:59 +00001190 // Handle MMX values passed in GPRs.
1191 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1192 MVT::getSizeInBits(RegVT) == 64)
1193 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1194
Chris Lattnerf39f7712007-02-28 05:46:49 +00001195 ArgValues.push_back(ArgValue);
1196 } else {
1197 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001198 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001199 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001200 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001201
Chris Lattnerf39f7712007-02-28 05:46:49 +00001202 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001203 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001204 if (CC == CallingConv::Fast)
1205 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001206
Evan Cheng1bc78042006-04-26 01:20:17 +00001207 // If the function takes variable number of arguments, make a frame index for
1208 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001209 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001210 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1211 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1212 }
1213 if (Is64Bit) {
1214 static const unsigned GPR64ArgRegs[] = {
1215 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1216 };
1217 static const unsigned XMMArgRegs[] = {
1218 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1219 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1220 };
1221
1222 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1223 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1224
1225 // For X86-64, if there are vararg parameters that are passed via
1226 // registers, then we must store them to their spots on the stack so they
1227 // may be loaded by deferencing the result of va_next.
1228 VarArgsGPOffset = NumIntRegs * 8;
1229 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1230 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1231
1232 // Store the integer parameter registers.
1233 SmallVector<SDOperand, 8> MemOps;
1234 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1235 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001236 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen86737662008-01-05 16:56:59 +00001237 for (; NumIntRegs != 6; ++NumIntRegs) {
1238 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1239 X86::GR64RegisterClass);
1240 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman69de1932008-02-06 22:27:42 +00001241 SDOperand Store =
1242 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001243 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001244 RegSaveFrameIndex);
Gordon Henriksen86737662008-01-05 16:56:59 +00001245 MemOps.push_back(Store);
1246 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001247 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001248 }
1249
1250 // Now store the XMM (fp + vector) parameter registers.
1251 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001252 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen86737662008-01-05 16:56:59 +00001253 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1254 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1255 X86::VR128RegisterClass);
1256 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman69de1932008-02-06 22:27:42 +00001257 SDOperand Store =
1258 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001259 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001260 RegSaveFrameIndex);
Gordon Henriksen86737662008-01-05 16:56:59 +00001261 MemOps.push_back(Store);
1262 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001263 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001264 }
1265 if (!MemOps.empty())
1266 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1267 &MemOps[0], MemOps.size());
1268 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001269 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001270
1271 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1272 // arguments and the arguments after the retaddr has been pushed are
1273 // aligned.
1274 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1275 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1276 (StackSize & 7) == 0)
1277 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001278
Gordon Henriksenae636f82008-01-03 16:47:34 +00001279 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001280
Gordon Henriksen86737662008-01-05 16:56:59 +00001281 // Some CCs need callee pop.
1282 if (IsCalleePop(Op)) {
1283 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001284 BytesCallerReserves = 0;
1285 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001286 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001287 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001288 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001289 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001290 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001291 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001292
Gordon Henriksen86737662008-01-05 16:56:59 +00001293 if (!Is64Bit) {
1294 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1295 if (CC == CallingConv::X86_FastCall)
1296 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1297 }
Evan Cheng25caf632006-05-23 21:06:34 +00001298
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001299 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001300
Evan Cheng25caf632006-05-23 21:06:34 +00001301 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001302 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001303 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001304}
1305
Evan Chengdffbd832008-01-10 00:09:10 +00001306SDOperand
1307X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1308 const SDOperand &StackPtr,
1309 const CCValAssign &VA,
1310 SDOperand Chain,
1311 SDOperand Arg) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001312 unsigned LocMemOffset = VA.getLocMemOffset();
1313 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001314 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1315 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1316 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1317 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001318 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001319 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001320 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001321 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001322}
1323
Evan Cheng0d9e9762008-01-29 19:34:22 +00001324/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1325/// struct return call to the specified function. X86-64 ABI specifies
1326/// some SRet calls are actually returned in registers. Since current
1327/// LLVM cannot represent multi-value calls, they are represent as
1328/// calls where the results are passed in a hidden struct provided by
1329/// the caller. This function examines the type of the struct to
1330/// determine the correct way to implement the call.
1331X86::X86_64SRet
1332X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1333 // FIXME: Disabled for now.
1334 return X86::InMemory;
1335
1336 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1337 const Type *RTy = PTy->getElementType();
1338 unsigned Size = getTargetData()->getABITypeSize(RTy);
1339 if (Size != 16 && Size != 32)
1340 return X86::InMemory;
1341
1342 if (Size == 32) {
1343 const StructType *STy = dyn_cast<StructType>(RTy);
1344 if (!STy) return X86::InMemory;
1345 if (STy->getNumElements() == 2 &&
1346 STy->getElementType(0) == Type::X86_FP80Ty &&
1347 STy->getElementType(1) == Type::X86_FP80Ty)
1348 return X86::InX87;
1349 }
1350
1351 bool AllFP = true;
1352 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1353 I != E; ++I) {
1354 const Type *STy = I->get();
1355 if (!STy->isFPOrFPVector()) {
1356 AllFP = false;
1357 break;
1358 }
1359 }
1360
1361 if (AllFP)
1362 return X86::InSSE;
1363 return X86::InGPR64;
1364}
1365
1366void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1367 CCAssignFn *Fn,
1368 CCState &CCInfo) {
1369 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1370 for (unsigned i = 1; i != NumOps; ++i) {
1371 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1372 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1373 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1374 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1375 cerr << "Call operand #" << i << " has unhandled type "
1376 << MVT::getValueTypeString(ArgVT) << "\n";
1377 abort();
1378 }
1379 }
1380}
1381
Gordon Henriksen86737662008-01-05 16:56:59 +00001382SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1383 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng32fe1032006-05-25 00:59:30 +00001384 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001385 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001386 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001387 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1388 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng32fe1032006-05-25 00:59:30 +00001389 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001391 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001392
1393 assert(!(isVarArg && CC == CallingConv::Fast) &&
1394 "Var args not supported with calling convention fastcc");
1395
Chris Lattner638402b2007-02-28 07:00:42 +00001396 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001397 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001398 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng0d9e9762008-01-29 19:34:22 +00001399 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1400
1401 X86::X86_64SRet SRetMethod = X86::InMemory;
1402 if (Is64Bit && IsStructRet)
1403 // FIXME: We can't figure out type of the sret structure for indirect
1404 // calls. We need to copy more information from CallSite to the ISD::CALL
1405 // node.
1406 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1407 SRetMethod =
1408 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1409
1410 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1411 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1412 // a sret call.
1413 if (SRetMethod != X86::InMemory)
1414 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1415 else
1416 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001417
Chris Lattner423c5f42007-02-28 05:31:48 +00001418 // Get a count of how many bytes are to be pushed on the stack.
1419 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001420 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001421 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001422
Gordon Henriksen86737662008-01-05 16:56:59 +00001423 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1424 // arguments and the arguments after the retaddr has been pushed are aligned.
1425 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1426 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1427 (NumBytes & 7) == 0)
1428 NumBytes += 4;
1429
1430 int FPDiff = 0;
1431 if (IsTailCall) {
1432 // Lower arguments at fp - stackoffset + fpdiff.
1433 unsigned NumBytesCallerPushed =
1434 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1435 FPDiff = NumBytesCallerPushed - NumBytes;
1436
1437 // Set the delta of movement of the returnaddr stackslot.
1438 // But only set if delta is greater than previous delta.
1439 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1440 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1441 }
1442
Chris Lattner0bd48932008-01-17 07:00:52 +00001443 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444
Gordon Henriksen86737662008-01-05 16:56:59 +00001445 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1446 if (IsTailCall) {
1447 // Adjust the Return address stack slot.
1448 if (FPDiff) {
1449 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1450 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1451 // Load the "old" Return address.
1452 RetAddrFrIdx =
1453 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1454 // Calculate the new stack slot for the return address.
1455 int SlotSize = Is64Bit ? 8 : 4;
1456 int NewReturnAddrFI =
1457 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1458 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1459 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1460 }
1461 }
1462
Chris Lattner5a88b832007-02-25 07:10:00 +00001463 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1464 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001465
Chris Lattner423c5f42007-02-28 05:31:48 +00001466 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001467
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001468 // Walk the register/memloc assignments, inserting copies/loads. For tail
1469 // calls, lower arguments which could otherwise be possibly overwritten to the
1470 // stack slot where they would go on normal function calls.
Chris Lattner423c5f42007-02-28 05:31:48 +00001471 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1472 CCValAssign &VA = ArgLocs[i];
1473 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001474
Chris Lattner423c5f42007-02-28 05:31:48 +00001475 // Promote the value if needed.
1476 switch (VA.getLocInfo()) {
1477 default: assert(0 && "Unknown loc info!");
1478 case CCValAssign::Full: break;
1479 case CCValAssign::SExt:
1480 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1481 break;
1482 case CCValAssign::ZExt:
1483 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1484 break;
1485 case CCValAssign::AExt:
1486 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1487 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001488 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001489
1490 if (VA.isRegLoc()) {
1491 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1492 } else {
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001493 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001494 assert(VA.isMemLoc());
1495 if (StackPtr.Val == 0)
1496 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1497
1498 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1499 Arg));
1500 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001501 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001502 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001503
Evan Cheng32fe1032006-05-25 00:59:30 +00001504 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001505 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1506 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001507
Evan Cheng347d5f72006-04-28 21:29:37 +00001508 // Build a sequence of copy-to-reg nodes chained together with token chain
1509 // and flag operands which copy the outgoing args into registers.
1510 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1512 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1513 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001514 InFlag = Chain.getValue(1);
1515 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001516
1517 if (IsTailCall)
1518 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1519
Evan Chengf4684712007-02-21 21:18:14 +00001520 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1521 // GOT pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001522 // Does not work with tail call since ebx is not restored correctly by
1523 // tailcaller. TODO: at least for x86 - verify for x86-64
1524 if (!IsTailCall && !Is64Bit &&
1525 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Cheng706535d2007-01-22 21:34:25 +00001526 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001527 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1528 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1529 InFlag);
1530 InFlag = Chain.getValue(1);
1531 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001532
Gordon Henriksen86737662008-01-05 16:56:59 +00001533 if (Is64Bit && isVarArg) {
1534 // From AMD64 ABI document:
1535 // For calls that may call functions that use varargs or stdargs
1536 // (prototype-less calls or calls to functions containing ellipsis (...) in
1537 // the declaration) %al is used as hidden argument to specify the number
1538 // of SSE registers used. The contents of %al do not need to match exactly
1539 // the number of registers, but must be an ubound on the number of SSE
1540 // registers used and is in the range 0 - 8 inclusive.
1541
1542 // Count the number of XMM registers allocated.
1543 static const unsigned XMMArgRegs[] = {
1544 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1545 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1546 };
1547 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1548
1549 Chain = DAG.getCopyToReg(Chain, X86::AL,
1550 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1551 InFlag = Chain.getValue(1);
1552 }
1553
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001554 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 if (IsTailCall) {
1556 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 SDOperand FIN;
1558 int FI = 0;
1559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1560 CCValAssign &VA = ArgLocs[i];
1561 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001562 assert(VA.isMemLoc());
1563 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1565 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566 // Create frame index.
1567 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1568 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1569 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1570 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001571 SDOperand Source = Arg;
Evan Cheng8e5712b2008-01-12 01:08:07 +00001572 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001573 // Copy from stack slots to stack slot of a tail called function. This
1574 // needs to be done because if we would lower the arguments directly
1575 // to their real stack slot we might end up overwriting each other.
1576 // Get source stack slot.
Chris Lattner0bd48932008-01-17 07:00:52 +00001577 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001578 if (StackPtr.Val == 0)
1579 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1580 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1581 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands9e9cf0c2008-01-13 21:20:29 +00001582 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001583 }
1584
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001586 // Copy relative to framepointer.
1587 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1588 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001590 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001591 MemOpChains2.push_back(
1592 DAG.getStore(Chain, Source, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001593 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001594 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 }
1596 }
1597
1598 if (!MemOpChains2.empty())
1599 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001600 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001601
1602 // Store the return address to the appropriate stack slot.
1603 if (FPDiff)
1604 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1605 }
1606
Evan Cheng32fe1032006-05-25 00:59:30 +00001607 // If the callee is a GlobalAddress node (quite common, every direct call is)
1608 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001609 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001610 // We should use extra load for direct calls to dllimported functions in
1611 // non-JIT mode.
Gordon Henriksen86737662008-01-05 16:56:59 +00001612 if ((IsTailCall || !Is64Bit ||
1613 getTargetMachine().getCodeModel() != CodeModel::Large)
1614 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1615 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001616 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001617 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001618 if (IsTailCall || !Is64Bit ||
1619 getTargetMachine().getCodeModel() != CodeModel::Large)
1620 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1621 } else if (IsTailCall) {
1622 assert(Callee.getOpcode() == ISD::LOAD &&
1623 "Function destination must be loaded into virtual register");
1624 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1625
1626 Chain = DAG.getCopyToReg(Chain,
1627 DAG.getRegister(Opc, getPointerTy()) ,
1628 Callee,InFlag);
1629 Callee = DAG.getRegister(Opc, getPointerTy());
1630 // Add register as live out.
1631 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 }
1633
Chris Lattnerd96d0722007-02-25 06:40:16 +00001634 // Returns a chain & a flag for retval copy to use.
1635 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001636 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001637
1638 if (IsTailCall) {
1639 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001640 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1641 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 if (InFlag.Val)
1643 Ops.push_back(InFlag);
1644 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1645 InFlag = Chain.getValue(1);
1646
1647 // Returns a chain & a flag for retval copy to use.
1648 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1649 Ops.clear();
1650 }
1651
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001652 Ops.push_back(Chain);
1653 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001654
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 if (IsTailCall)
1656 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001657
1658 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 if (!IsTailCall && !Is64Bit &&
1660 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Chengf4684712007-02-21 21:18:14 +00001661 Subtarget->isPICStyleGOT())
1662 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksenae636f82008-01-03 16:47:34 +00001663
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 // Add argument registers to the end of the list so that they are known live
1665 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1667 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1668 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001669
Evan Cheng347d5f72006-04-28 21:29:37 +00001670 if (InFlag.Val)
1671 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001672
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 if (IsTailCall) {
1674 assert(InFlag.Val &&
1675 "Flag must be set. Depend on flag being set in LowerRET");
1676 Chain = DAG.getNode(X86ISD::TAILCALL,
1677 Op.Val->getVTList(), &Ops[0], Ops.size());
1678
1679 return SDOperand(Chain.Val, Op.ResNo);
1680 }
1681
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001682 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001683 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001684
Chris Lattner2d297092006-05-23 18:50:38 +00001685 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001686 unsigned NumBytesForCalleeToPush;
1687 if (IsCalleePop(Op))
1688 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001689 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001690 // If this is is a call to a struct-return function, the callee
1691 // pops the hidden struct pointer, so we have to push it back.
1692 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001693 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001694 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001695 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001696
Gordon Henriksenae636f82008-01-03 16:47:34 +00001697 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001698 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001699 DAG.getIntPtrConstant(NumBytes),
1700 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001701 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001702 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001703
Chris Lattner3085e152007-02-25 08:59:22 +00001704 // Handle result values, copying them out of physregs into vregs that we
1705 // return.
Evan Cheng0d9e9762008-01-29 19:34:22 +00001706 switch (SRetMethod) {
1707 default:
1708 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1709 case X86::InGPR64:
1710 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1711 X86::RAX, X86::RDX,
1712 MVT::i64, DAG), Op.ResNo);
1713 case X86::InSSE:
1714 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1715 X86::XMM0, X86::XMM1,
1716 MVT::f64, DAG), Op.ResNo);
1717 case X86::InX87:
1718 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1719 Op.ResNo);
1720 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001721}
1722
Evan Cheng25ab6902006-09-08 06:48:29 +00001723
1724//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001725// Fast Calling Convention (tail call) implementation
1726//===----------------------------------------------------------------------===//
1727
1728// Like std call, callee cleans arguments, convention except that ECX is
1729// reserved for storing the tail called function address. Only 2 registers are
1730// free for argument passing (inreg). Tail call optimization is performed
1731// provided:
1732// * tailcallopt is enabled
1733// * caller/callee are fastcc
1734// * elf/pic is disabled OR
1735// * elf/pic enabled + callee is in module + callee has
1736// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001737// To keep the stack aligned according to platform abi the function
1738// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1739// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001740// If a tail called function callee has more arguments than the caller the
1741// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001742// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001743// original REtADDR, but before the saved framepointer or the spilled registers
1744// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1745// stack layout:
1746// arg1
1747// arg2
1748// RETADDR
1749// [ new RETADDR
1750// move area ]
1751// (possible EBP)
1752// ESI
1753// EDI
1754// local1 ..
1755
1756/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1757/// for a 16 byte align requirement.
1758unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1759 SelectionDAG& DAG) {
1760 if (PerformTailCallOpt) {
1761 MachineFunction &MF = DAG.getMachineFunction();
1762 const TargetMachine &TM = MF.getTarget();
1763 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1764 unsigned StackAlignment = TFI.getStackAlignment();
1765 uint64_t AlignMask = StackAlignment - 1;
1766 int64_t Offset = StackSize;
1767 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1768 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1769 // Number smaller than 12 so just add the difference.
1770 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1771 } else {
1772 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1773 Offset = ((~AlignMask) & Offset) + StackAlignment +
1774 (StackAlignment-SlotSize);
1775 }
1776 StackSize = Offset;
1777 }
1778 return StackSize;
1779}
1780
1781/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001782/// following the call is a return. A function is eligible if caller/callee
1783/// calling conventions match, currently only fastcc supports tail calls, and
1784/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001785bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1786 SDOperand Ret,
1787 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001788 if (!PerformTailCallOpt)
1789 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001790
1791 // Check whether CALL node immediatly preceeds the RET node and whether the
1792 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001793 unsigned NumOps = Ret.getNumOperands();
1794 if ((NumOps == 1 &&
1795 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1796 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001797 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001798 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1799 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001800 MachineFunction &MF = DAG.getMachineFunction();
1801 unsigned CallerCC = MF.getFunction()->getCallingConv();
1802 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1803 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1804 SDOperand Callee = Call.getOperand(4);
1805 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001806 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1807 !Subtarget->isPICStyleGOT())
1808 return true;
1809
1810 // Can only do local tail calls with PIC.
Gordon Henriksen86737662008-01-05 16:56:59 +00001811 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1812 return G->getGlobal()->hasHiddenVisibility()
1813 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001814 }
1815 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001816
1817 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001818}
1819
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001820//===----------------------------------------------------------------------===//
1821// Other Lowering Hooks
1822//===----------------------------------------------------------------------===//
1823
1824
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001825SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001826 MachineFunction &MF = DAG.getMachineFunction();
1827 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1828 int ReturnAddrIndex = FuncInfo->getRAIndex();
1829
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001830 if (ReturnAddrIndex == 0) {
1831 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001832 if (Subtarget->is64Bit())
1833 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1834 else
1835 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001836
1837 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001838 }
1839
Evan Cheng25ab6902006-09-08 06:48:29 +00001840 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001841}
1842
1843
1844
Evan Cheng6dfa9992006-01-30 23:41:35 +00001845/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1846/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001847/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1848/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001849static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001850 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1851 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001852 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001853 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001854 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1855 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1856 // X > -1 -> X == 0, jump !sign.
1857 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001858 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001859 return true;
1860 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1861 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001862 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001863 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001864 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1865 // X < 1 -> X <= 0
1866 RHS = DAG.getConstant(0, RHS.getValueType());
1867 X86CC = X86::COND_LE;
1868 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001869 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001870 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001871
Evan Chengd9558e02006-01-06 00:43:03 +00001872 switch (SetCCOpcode) {
1873 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001874 case ISD::SETEQ: X86CC = X86::COND_E; break;
1875 case ISD::SETGT: X86CC = X86::COND_G; break;
1876 case ISD::SETGE: X86CC = X86::COND_GE; break;
1877 case ISD::SETLT: X86CC = X86::COND_L; break;
1878 case ISD::SETLE: X86CC = X86::COND_LE; break;
1879 case ISD::SETNE: X86CC = X86::COND_NE; break;
1880 case ISD::SETULT: X86CC = X86::COND_B; break;
1881 case ISD::SETUGT: X86CC = X86::COND_A; break;
1882 case ISD::SETULE: X86CC = X86::COND_BE; break;
1883 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001884 }
1885 } else {
1886 // On a floating point condition, the flags are set as follows:
1887 // ZF PF CF op
1888 // 0 | 0 | 0 | X > Y
1889 // 0 | 0 | 1 | X < Y
1890 // 1 | 0 | 0 | X == Y
1891 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001892 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001893 switch (SetCCOpcode) {
1894 default: break;
1895 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001896 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001897 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001898 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001899 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001900 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001901 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001902 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001903 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001904 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001905 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001906 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001907 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001908 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001909 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001910 case ISD::SETNE: X86CC = X86::COND_NE; break;
1911 case ISD::SETUO: X86CC = X86::COND_P; break;
1912 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001913 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001914 if (Flip)
1915 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001916 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001917
Chris Lattner7fbe9722006-10-20 17:42:20 +00001918 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001919}
1920
Evan Cheng4a460802006-01-11 00:33:36 +00001921/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1922/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001923/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001924static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001925 switch (X86CC) {
1926 default:
1927 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001928 case X86::COND_B:
1929 case X86::COND_BE:
1930 case X86::COND_E:
1931 case X86::COND_P:
1932 case X86::COND_A:
1933 case X86::COND_AE:
1934 case X86::COND_NE:
1935 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001936 return true;
1937 }
1938}
1939
Evan Cheng5ced1d82006-04-06 23:23:56 +00001940/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001941/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001942static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1943 if (Op.getOpcode() == ISD::UNDEF)
1944 return true;
1945
1946 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001947 return (Val >= Low && Val < Hi);
1948}
1949
1950/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1951/// true if Op is undef or if its value equal to the specified value.
1952static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1953 if (Op.getOpcode() == ISD::UNDEF)
1954 return true;
1955 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001956}
1957
Evan Cheng0188ecb2006-03-22 18:59:22 +00001958/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1959/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1960bool X86::isPSHUFDMask(SDNode *N) {
1961 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1962
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001963 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00001964 return false;
1965
1966 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001967 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001968 SDOperand Arg = N->getOperand(i);
1969 if (Arg.getOpcode() == ISD::UNDEF) continue;
1970 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001971 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00001972 return false;
1973 }
1974
1975 return true;
1976}
1977
1978/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001979/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001980bool X86::isPSHUFHWMask(SDNode *N) {
1981 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1982
1983 if (N->getNumOperands() != 8)
1984 return false;
1985
1986 // Lower quadword copied in order.
1987 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001988 SDOperand Arg = N->getOperand(i);
1989 if (Arg.getOpcode() == ISD::UNDEF) continue;
1990 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1991 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001992 return false;
1993 }
1994
1995 // Upper quadword shuffled.
1996 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001997 SDOperand Arg = N->getOperand(i);
1998 if (Arg.getOpcode() == ISD::UNDEF) continue;
1999 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2000 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002001 if (Val < 4 || Val > 7)
2002 return false;
2003 }
2004
2005 return true;
2006}
2007
2008/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002009/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002010bool X86::isPSHUFLWMask(SDNode *N) {
2011 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2012
2013 if (N->getNumOperands() != 8)
2014 return false;
2015
2016 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002017 for (unsigned i = 4; i != 8; ++i)
2018 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002019 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002020
2021 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002022 for (unsigned i = 0; i != 4; ++i)
2023 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002024 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002025
2026 return true;
2027}
2028
Evan Cheng14aed5e2006-03-24 01:18:28 +00002029/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2030/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002031static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002032 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002033
Evan Cheng39623da2006-04-20 08:58:49 +00002034 unsigned Half = NumElems / 2;
2035 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002036 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002037 return false;
2038 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002039 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002040 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002041
2042 return true;
2043}
2044
Evan Cheng39623da2006-04-20 08:58:49 +00002045bool X86::isSHUFPMask(SDNode *N) {
2046 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002047 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002048}
2049
Evan Cheng213d2cf2007-05-17 18:45:50 +00002050/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002051/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2052/// half elements to come from vector 1 (which would equal the dest.) and
2053/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002054static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2055 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002056
Chris Lattner5a88b832007-02-25 07:10:00 +00002057 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002058 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002059 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002060 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002061 for (unsigned i = Half; i < NumOps; ++i)
2062 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002063 return false;
2064 return true;
2065}
2066
2067static bool isCommutedSHUFP(SDNode *N) {
2068 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002069 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002070}
2071
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002072/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2073/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2074bool X86::isMOVHLPSMask(SDNode *N) {
2075 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2076
Evan Cheng2064a2b2006-03-28 06:50:32 +00002077 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002078 return false;
2079
Evan Cheng2064a2b2006-03-28 06:50:32 +00002080 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002081 return isUndefOrEqual(N->getOperand(0), 6) &&
2082 isUndefOrEqual(N->getOperand(1), 7) &&
2083 isUndefOrEqual(N->getOperand(2), 2) &&
2084 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002085}
2086
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002087/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2088/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2089/// <2, 3, 2, 3>
2090bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2091 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2092
2093 if (N->getNumOperands() != 4)
2094 return false;
2095
2096 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2097 return isUndefOrEqual(N->getOperand(0), 2) &&
2098 isUndefOrEqual(N->getOperand(1), 3) &&
2099 isUndefOrEqual(N->getOperand(2), 2) &&
2100 isUndefOrEqual(N->getOperand(3), 3);
2101}
2102
Evan Cheng5ced1d82006-04-06 23:23:56 +00002103/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2104/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2105bool X86::isMOVLPMask(SDNode *N) {
2106 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2107
2108 unsigned NumElems = N->getNumOperands();
2109 if (NumElems != 2 && NumElems != 4)
2110 return false;
2111
Evan Chengc5cdff22006-04-07 21:53:05 +00002112 for (unsigned i = 0; i < NumElems/2; ++i)
2113 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2114 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002115
Evan Chengc5cdff22006-04-07 21:53:05 +00002116 for (unsigned i = NumElems/2; i < NumElems; ++i)
2117 if (!isUndefOrEqual(N->getOperand(i), i))
2118 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002119
2120 return true;
2121}
2122
2123/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002124/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2125/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002126bool X86::isMOVHPMask(SDNode *N) {
2127 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2128
2129 unsigned NumElems = N->getNumOperands();
2130 if (NumElems != 2 && NumElems != 4)
2131 return false;
2132
Evan Chengc5cdff22006-04-07 21:53:05 +00002133 for (unsigned i = 0; i < NumElems/2; ++i)
2134 if (!isUndefOrEqual(N->getOperand(i), i))
2135 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002136
2137 for (unsigned i = 0; i < NumElems/2; ++i) {
2138 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002139 if (!isUndefOrEqual(Arg, i + NumElems))
2140 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002141 }
2142
2143 return true;
2144}
2145
Evan Cheng0038e592006-03-28 00:39:58 +00002146/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2147/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002148bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2149 bool V2IsSplat = false) {
2150 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002151 return false;
2152
Chris Lattner5a88b832007-02-25 07:10:00 +00002153 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2154 SDOperand BitI = Elts[i];
2155 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002156 if (!isUndefOrEqual(BitI, j))
2157 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002158 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002159 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002160 return false;
2161 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002162 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002163 return false;
2164 }
Evan Cheng0038e592006-03-28 00:39:58 +00002165 }
2166
2167 return true;
2168}
2169
Evan Cheng39623da2006-04-20 08:58:49 +00002170bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2171 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002172 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002173}
2174
Evan Cheng4fcb9222006-03-28 02:43:26 +00002175/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2176/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002177bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2178 bool V2IsSplat = false) {
2179 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002180 return false;
2181
Chris Lattner5a88b832007-02-25 07:10:00 +00002182 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2183 SDOperand BitI = Elts[i];
2184 SDOperand BitI1 = Elts[i+1];
2185 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002186 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002187 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002188 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002189 return false;
2190 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002191 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002192 return false;
2193 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002194 }
2195
2196 return true;
2197}
2198
Evan Cheng39623da2006-04-20 08:58:49 +00002199bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2200 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002201 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002202}
2203
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002204/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2205/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2206/// <0, 0, 1, 1>
2207bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2208 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2209
2210 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002211 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002212 return false;
2213
2214 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2215 SDOperand BitI = N->getOperand(i);
2216 SDOperand BitI1 = N->getOperand(i+1);
2217
Evan Chengc5cdff22006-04-07 21:53:05 +00002218 if (!isUndefOrEqual(BitI, j))
2219 return false;
2220 if (!isUndefOrEqual(BitI1, j))
2221 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002222 }
2223
2224 return true;
2225}
2226
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002227/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2228/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2229/// <2, 2, 3, 3>
2230bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2231 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2232
2233 unsigned NumElems = N->getNumOperands();
2234 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2235 return false;
2236
2237 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2238 SDOperand BitI = N->getOperand(i);
2239 SDOperand BitI1 = N->getOperand(i + 1);
2240
2241 if (!isUndefOrEqual(BitI, j))
2242 return false;
2243 if (!isUndefOrEqual(BitI1, j))
2244 return false;
2245 }
2246
2247 return true;
2248}
2249
Evan Cheng017dcc62006-04-21 01:05:10 +00002250/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2251/// specifies a shuffle of elements that is suitable for input to MOVSS,
2252/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002253static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002254 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002255 return false;
2256
Chris Lattner5a88b832007-02-25 07:10:00 +00002257 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002258 return false;
2259
Chris Lattner5a88b832007-02-25 07:10:00 +00002260 for (unsigned i = 1; i < NumElts; ++i) {
2261 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002262 return false;
2263 }
2264
2265 return true;
2266}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002267
Evan Cheng017dcc62006-04-21 01:05:10 +00002268bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002269 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002270 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002271}
2272
Evan Cheng017dcc62006-04-21 01:05:10 +00002273/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2274/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002275/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002276static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2277 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002278 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002279 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002280 return false;
2281
2282 if (!isUndefOrEqual(Ops[0], 0))
2283 return false;
2284
Chris Lattner5a88b832007-02-25 07:10:00 +00002285 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002286 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002287 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2288 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2289 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002290 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002291 }
2292
2293 return true;
2294}
2295
Evan Cheng8cf723d2006-09-08 01:50:06 +00002296static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2297 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002299 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2300 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002301}
2302
Evan Chengd9539472006-04-14 21:59:03 +00002303/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2304/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2305bool X86::isMOVSHDUPMask(SDNode *N) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307
2308 if (N->getNumOperands() != 4)
2309 return false;
2310
2311 // Expect 1, 1, 3, 3
2312 for (unsigned i = 0; i < 2; ++i) {
2313 SDOperand Arg = N->getOperand(i);
2314 if (Arg.getOpcode() == ISD::UNDEF) continue;
2315 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2316 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2317 if (Val != 1) return false;
2318 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002319
2320 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002321 for (unsigned i = 2; i < 4; ++i) {
2322 SDOperand Arg = N->getOperand(i);
2323 if (Arg.getOpcode() == ISD::UNDEF) continue;
2324 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2325 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2326 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002327 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002328 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002329
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002330 // Don't use movshdup if it can be done with a shufps.
2331 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002332}
2333
2334/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2335/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2336bool X86::isMOVSLDUPMask(SDNode *N) {
2337 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2338
2339 if (N->getNumOperands() != 4)
2340 return false;
2341
2342 // Expect 0, 0, 2, 2
2343 for (unsigned i = 0; i < 2; ++i) {
2344 SDOperand Arg = N->getOperand(i);
2345 if (Arg.getOpcode() == ISD::UNDEF) continue;
2346 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2347 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2348 if (Val != 0) return false;
2349 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002350
2351 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002352 for (unsigned i = 2; i < 4; ++i) {
2353 SDOperand Arg = N->getOperand(i);
2354 if (Arg.getOpcode() == ISD::UNDEF) continue;
2355 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2356 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2357 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002358 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002359 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002360
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002361 // Don't use movshdup if it can be done with a shufps.
2362 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002363}
2364
Evan Cheng49892af2007-06-19 00:02:56 +00002365/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2366/// specifies a identity operation on the LHS or RHS.
2367static bool isIdentityMask(SDNode *N, bool RHS = false) {
2368 unsigned NumElems = N->getNumOperands();
2369 for (unsigned i = 0; i < NumElems; ++i)
2370 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2371 return false;
2372 return true;
2373}
2374
Evan Chengb9df0ca2006-03-22 02:53:00 +00002375/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2376/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002377static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002378 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2379
Evan Chengb9df0ca2006-03-22 02:53:00 +00002380 // This is a splat operation if each element of the permute is the same, and
2381 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002382 unsigned NumElems = N->getNumOperands();
2383 SDOperand ElementBase;
2384 unsigned i = 0;
2385 for (; i != NumElems; ++i) {
2386 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002387 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002388 ElementBase = Elt;
2389 break;
2390 }
2391 }
2392
2393 if (!ElementBase.Val)
2394 return false;
2395
2396 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002397 SDOperand Arg = N->getOperand(i);
2398 if (Arg.getOpcode() == ISD::UNDEF) continue;
2399 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002400 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002401 }
2402
2403 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002404 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002405}
2406
Evan Chengc575ca22006-04-17 20:43:08 +00002407/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2408/// a splat of a single element and it's a 2 or 4 element mask.
2409bool X86::isSplatMask(SDNode *N) {
2410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2411
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002412 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002413 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2414 return false;
2415 return ::isSplatMask(N);
2416}
2417
Evan Chengf686d9b2006-10-27 21:08:32 +00002418/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2419/// specifies a splat of zero element.
2420bool X86::isSplatLoMask(SDNode *N) {
2421 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2422
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002423 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002424 if (!isUndefOrEqual(N->getOperand(i), 0))
2425 return false;
2426 return true;
2427}
2428
Evan Cheng63d33002006-03-22 08:01:21 +00002429/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2430/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2431/// instructions.
2432unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002433 unsigned NumOperands = N->getNumOperands();
2434 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2435 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002436 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002437 unsigned Val = 0;
2438 SDOperand Arg = N->getOperand(NumOperands-i-1);
2439 if (Arg.getOpcode() != ISD::UNDEF)
2440 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002441 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002442 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002443 if (i != NumOperands - 1)
2444 Mask <<= Shift;
2445 }
Evan Cheng63d33002006-03-22 08:01:21 +00002446
2447 return Mask;
2448}
2449
Evan Cheng506d3df2006-03-29 23:07:14 +00002450/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2451/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2452/// instructions.
2453unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2454 unsigned Mask = 0;
2455 // 8 nodes, but we only care about the last 4.
2456 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002457 unsigned Val = 0;
2458 SDOperand Arg = N->getOperand(i);
2459 if (Arg.getOpcode() != ISD::UNDEF)
2460 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002461 Mask |= (Val - 4);
2462 if (i != 4)
2463 Mask <<= 2;
2464 }
2465
2466 return Mask;
2467}
2468
2469/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2470/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2471/// instructions.
2472unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2473 unsigned Mask = 0;
2474 // 8 nodes, but we only care about the first 4.
2475 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002476 unsigned Val = 0;
2477 SDOperand Arg = N->getOperand(i);
2478 if (Arg.getOpcode() != ISD::UNDEF)
2479 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002480 Mask |= Val;
2481 if (i != 0)
2482 Mask <<= 2;
2483 }
2484
2485 return Mask;
2486}
2487
Evan Chengc21a0532006-04-05 01:47:37 +00002488/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2489/// specifies a 8 element shuffle that can be broken into a pair of
2490/// PSHUFHW and PSHUFLW.
2491static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2493
2494 if (N->getNumOperands() != 8)
2495 return false;
2496
2497 // Lower quadword shuffled.
2498 for (unsigned i = 0; i != 4; ++i) {
2499 SDOperand Arg = N->getOperand(i);
2500 if (Arg.getOpcode() == ISD::UNDEF) continue;
2501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2502 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002503 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002504 return false;
2505 }
2506
2507 // Upper quadword shuffled.
2508 for (unsigned i = 4; i != 8; ++i) {
2509 SDOperand Arg = N->getOperand(i);
2510 if (Arg.getOpcode() == ISD::UNDEF) continue;
2511 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2512 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2513 if (Val < 4 || Val > 7)
2514 return false;
2515 }
2516
2517 return true;
2518}
2519
Chris Lattner8a594482007-11-25 00:24:49 +00002520/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002521/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002522static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2523 SDOperand &V2, SDOperand &Mask,
2524 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002525 MVT::ValueType VT = Op.getValueType();
2526 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002527 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002528 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002529 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002530
2531 for (unsigned i = 0; i != NumElems; ++i) {
2532 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002533 if (Arg.getOpcode() == ISD::UNDEF) {
2534 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2535 continue;
2536 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002537 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2538 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2539 if (Val < NumElems)
2540 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2541 else
2542 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2543 }
2544
Evan Cheng9eca5e82006-10-25 21:49:50 +00002545 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002546 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002547 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002548}
2549
Evan Cheng779ccea2007-12-07 21:30:01 +00002550/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2551/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002552static
2553SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2554 MVT::ValueType MaskVT = Mask.getValueType();
2555 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2556 unsigned NumElems = Mask.getNumOperands();
2557 SmallVector<SDOperand, 8> MaskVec;
2558 for (unsigned i = 0; i != NumElems; ++i) {
2559 SDOperand Arg = Mask.getOperand(i);
2560 if (Arg.getOpcode() == ISD::UNDEF) {
2561 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2562 continue;
2563 }
2564 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2565 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2566 if (Val < NumElems)
2567 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2568 else
2569 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2570 }
2571 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2572}
2573
2574
Evan Cheng533a0aa2006-04-19 20:35:22 +00002575/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2576/// match movhlps. The lower half elements should come from upper half of
2577/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002578/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002579static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2580 unsigned NumElems = Mask->getNumOperands();
2581 if (NumElems != 4)
2582 return false;
2583 for (unsigned i = 0, e = 2; i != e; ++i)
2584 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2585 return false;
2586 for (unsigned i = 2; i != 4; ++i)
2587 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2588 return false;
2589 return true;
2590}
2591
Evan Cheng5ced1d82006-04-06 23:23:56 +00002592/// isScalarLoadToVector - Returns true if the node is a scalar load that
2593/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002594static inline bool isScalarLoadToVector(SDNode *N) {
2595 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2596 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002597 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002598 }
2599 return false;
2600}
2601
Evan Cheng533a0aa2006-04-19 20:35:22 +00002602/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2603/// match movlp{s|d}. The lower half elements should come from lower half of
2604/// V1 (and in order), and the upper half elements should come from the upper
2605/// half of V2 (and in order). And since V1 will become the source of the
2606/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002607static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002608 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002609 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002610 // Is V2 is a vector load, don't do this transformation. We will try to use
2611 // load folding shufps op.
2612 if (ISD::isNON_EXTLoad(V2))
2613 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002614
Evan Cheng533a0aa2006-04-19 20:35:22 +00002615 unsigned NumElems = Mask->getNumOperands();
2616 if (NumElems != 2 && NumElems != 4)
2617 return false;
2618 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2619 if (!isUndefOrEqual(Mask->getOperand(i), i))
2620 return false;
2621 for (unsigned i = NumElems/2; i != NumElems; ++i)
2622 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2623 return false;
2624 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002625}
2626
Evan Cheng39623da2006-04-20 08:58:49 +00002627/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2628/// all the same.
2629static bool isSplatVector(SDNode *N) {
2630 if (N->getOpcode() != ISD::BUILD_VECTOR)
2631 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002632
Evan Cheng39623da2006-04-20 08:58:49 +00002633 SDOperand SplatValue = N->getOperand(0);
2634 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2635 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002636 return false;
2637 return true;
2638}
2639
Evan Cheng8cf723d2006-09-08 01:50:06 +00002640/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2641/// to an undef.
2642static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002643 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002644 return false;
2645
2646 SDOperand V1 = N->getOperand(0);
2647 SDOperand V2 = N->getOperand(1);
2648 SDOperand Mask = N->getOperand(2);
2649 unsigned NumElems = Mask.getNumOperands();
2650 for (unsigned i = 0; i != NumElems; ++i) {
2651 SDOperand Arg = Mask.getOperand(i);
2652 if (Arg.getOpcode() != ISD::UNDEF) {
2653 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2654 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2655 return false;
2656 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2657 return false;
2658 }
2659 }
2660 return true;
2661}
2662
Evan Cheng213d2cf2007-05-17 18:45:50 +00002663/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2664/// constant +0.0.
2665static inline bool isZeroNode(SDOperand Elt) {
2666 return ((isa<ConstantSDNode>(Elt) &&
2667 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2668 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002669 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002670}
2671
2672/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2673/// to an zero vector.
2674static bool isZeroShuffle(SDNode *N) {
2675 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2676 return false;
2677
2678 SDOperand V1 = N->getOperand(0);
2679 SDOperand V2 = N->getOperand(1);
2680 SDOperand Mask = N->getOperand(2);
2681 unsigned NumElems = Mask.getNumOperands();
2682 for (unsigned i = 0; i != NumElems; ++i) {
2683 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002684 if (Arg.getOpcode() == ISD::UNDEF)
2685 continue;
2686
2687 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2688 if (Idx < NumElems) {
2689 unsigned Opc = V1.Val->getOpcode();
2690 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2691 continue;
2692 if (Opc != ISD::BUILD_VECTOR ||
2693 !isZeroNode(V1.Val->getOperand(Idx)))
2694 return false;
2695 } else if (Idx >= NumElems) {
2696 unsigned Opc = V2.Val->getOpcode();
2697 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2698 continue;
2699 if (Opc != ISD::BUILD_VECTOR ||
2700 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2701 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002702 }
2703 }
2704 return true;
2705}
2706
2707/// getZeroVector - Returns a vector of specified type with all zero elements.
2708///
2709static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2710 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002711
2712 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2713 // type. This ensures they get CSE'd.
2714 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2715 SDOperand Vec;
2716 if (MVT::getSizeInBits(VT) == 64) // MMX
2717 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2718 else // SSE
2719 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2720 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002721}
2722
Chris Lattner8a594482007-11-25 00:24:49 +00002723/// getOnesVector - Returns a vector of specified type with all bits set.
2724///
2725static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2726 assert(MVT::isVector(VT) && "Expected a vector type");
2727
2728 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2729 // type. This ensures they get CSE'd.
2730 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2731 SDOperand Vec;
2732 if (MVT::getSizeInBits(VT) == 64) // MMX
2733 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2734 else // SSE
2735 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2736 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2737}
2738
2739
Evan Cheng39623da2006-04-20 08:58:49 +00002740/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2741/// that point to V2 points to its first element.
2742static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2743 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2744
2745 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002746 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002747 unsigned NumElems = Mask.getNumOperands();
2748 for (unsigned i = 0; i != NumElems; ++i) {
2749 SDOperand Arg = Mask.getOperand(i);
2750 if (Arg.getOpcode() != ISD::UNDEF) {
2751 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2752 if (Val > NumElems) {
2753 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2754 Changed = true;
2755 }
2756 }
2757 MaskVec.push_back(Arg);
2758 }
2759
2760 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002761 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2762 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002763 return Mask;
2764}
2765
Evan Cheng017dcc62006-04-21 01:05:10 +00002766/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2767/// operation of specified width.
2768static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002769 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002770 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002771
Chris Lattner5a88b832007-02-25 07:10:00 +00002772 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002773 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2774 for (unsigned i = 1; i != NumElems; ++i)
2775 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002776 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002777}
2778
Evan Chengc575ca22006-04-17 20:43:08 +00002779/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2780/// of specified width.
2781static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2782 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002783 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002784 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002785 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2786 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2787 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2788 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002789 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002790}
2791
Evan Cheng39623da2006-04-20 08:58:49 +00002792/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2793/// of specified width.
2794static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2795 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002796 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002797 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002798 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002799 for (unsigned i = 0; i != Half; ++i) {
2800 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2801 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2802 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002803 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002804}
2805
Evan Chengc575ca22006-04-17 20:43:08 +00002806/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2807///
2808static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2809 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002810 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002811 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002812 unsigned NumElems = Mask.getNumOperands();
2813 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002814 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002815 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002816 NumElems >>= 1;
2817 }
2818 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2819
Chris Lattner8a594482007-11-25 00:24:49 +00002820 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002821 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002822 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002823 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2824}
2825
Evan Chengba05f722006-04-21 23:03:30 +00002826/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002827/// vector of zero or undef vector. This produces a shuffle where the low
2828/// element of V2 is swizzled into the zero/undef vector, landing at element
2829/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Evan Chengba05f722006-04-21 23:03:30 +00002830static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002831 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002832 bool isZero, SelectionDAG &DAG) {
2833 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002834 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002835 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner8a594482007-11-25 00:24:49 +00002836 SmallVector<SDOperand, 16> MaskVec;
2837 for (unsigned i = 0; i != NumElems; ++i)
2838 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2839 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2840 else
2841 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002842 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2843 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002844 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002845}
2846
Evan Chengc78d3b42006-04-24 18:01:45 +00002847/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2848///
2849static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2850 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002851 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002852 if (NumNonZero > 8)
2853 return SDOperand();
2854
2855 SDOperand V(0, 0);
2856 bool First = true;
2857 for (unsigned i = 0; i < 16; ++i) {
2858 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2859 if (ThisIsNonZero && First) {
2860 if (NumZero)
2861 V = getZeroVector(MVT::v8i16, DAG);
2862 else
2863 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2864 First = false;
2865 }
2866
2867 if ((i & 1) != 0) {
2868 SDOperand ThisElt(0, 0), LastElt(0, 0);
2869 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2870 if (LastIsNonZero) {
2871 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2872 }
2873 if (ThisIsNonZero) {
2874 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2875 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2876 ThisElt, DAG.getConstant(8, MVT::i8));
2877 if (LastIsNonZero)
2878 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2879 } else
2880 ThisElt = LastElt;
2881
2882 if (ThisElt.Val)
2883 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002884 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002885 }
2886 }
2887
2888 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2889}
2890
Bill Wendlinga348c562007-03-22 18:42:45 +00002891/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002892///
2893static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2894 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002895 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002896 if (NumNonZero > 4)
2897 return SDOperand();
2898
2899 SDOperand V(0, 0);
2900 bool First = true;
2901 for (unsigned i = 0; i < 8; ++i) {
2902 bool isNonZero = (NonZeros & (1 << i)) != 0;
2903 if (isNonZero) {
2904 if (First) {
2905 if (NumZero)
2906 V = getZeroVector(MVT::v8i16, DAG);
2907 else
2908 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2909 First = false;
2910 }
2911 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00002912 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00002913 }
2914 }
2915
2916 return V;
2917}
2918
Evan Cheng0db9fe62006-04-25 20:13:52 +00002919SDOperand
2920X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00002921 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2922 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2923 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2924 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2925 // eliminated on x86-32 hosts.
2926 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2927 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002928
Chris Lattner8a594482007-11-25 00:24:49 +00002929 if (ISD::isBuildVectorAllOnes(Op.Val))
2930 return getOnesVector(Op.getValueType(), DAG);
2931 return getZeroVector(Op.getValueType(), DAG);
2932 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002933
2934 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002935 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002936 unsigned EVTBits = MVT::getSizeInBits(EVT);
2937
2938 unsigned NumElems = Op.getNumOperands();
2939 unsigned NumZero = 0;
2940 unsigned NumNonZero = 0;
2941 unsigned NonZeros = 0;
Evan Chengdb2d5242007-12-12 06:45:40 +00002942 bool HasNonImms = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00002943 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002944 for (unsigned i = 0; i < NumElems; ++i) {
2945 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00002946 if (Elt.getOpcode() == ISD::UNDEF)
2947 continue;
2948 Values.insert(Elt);
2949 if (Elt.getOpcode() != ISD::Constant &&
2950 Elt.getOpcode() != ISD::ConstantFP)
2951 HasNonImms = true;
2952 if (isZeroNode(Elt))
2953 NumZero++;
2954 else {
2955 NonZeros |= (1 << i);
2956 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002957 }
2958 }
2959
Dan Gohman7f321562007-06-25 16:23:39 +00002960 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00002961 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2962 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002963 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002964
2965 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2966 if (Values.size() == 1)
2967 return SDOperand();
2968
2969 // Special case for single non-zero element.
Evan Chengdb2d5242007-12-12 06:45:40 +00002970 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002971 unsigned Idx = CountTrailingZeros_32(NonZeros);
2972 SDOperand Item = Op.getOperand(Idx);
2973 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2974 if (Idx == 0)
2975 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2976 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2977 NumZero > 0, DAG);
Evan Chengdb2d5242007-12-12 06:45:40 +00002978 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2979 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002980
2981 if (EVTBits == 32) {
2982 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2983 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2984 DAG);
2985 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002986 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002987 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002988 for (unsigned i = 0; i < NumElems; i++)
2989 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002990 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2991 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002992 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2993 DAG.getNode(ISD::UNDEF, VT), Mask);
2994 }
2995 }
2996
Dan Gohmana3941172007-07-24 22:55:08 +00002997 // A vector full of immediates; various special cases are already
2998 // handled, so this is best done with a single constant-pool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00002999 if (!HasNonImms)
Dan Gohmana3941172007-07-24 22:55:08 +00003000 return SDOperand();
3001
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003002 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003003 if (EVTBits == 64)
3004 return SDOperand();
3005
3006 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003007 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003008 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3009 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003010 if (V.Val) return V;
3011 }
3012
Bill Wendling826f36f2007-03-28 00:57:11 +00003013 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003014 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3015 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003016 if (V.Val) return V;
3017 }
3018
3019 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003020 SmallVector<SDOperand, 8> V;
3021 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003022 if (NumElems == 4 && NumZero > 0) {
3023 for (unsigned i = 0; i < 4; ++i) {
3024 bool isZero = !(NonZeros & (1 << i));
3025 if (isZero)
3026 V[i] = getZeroVector(VT, DAG);
3027 else
3028 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3029 }
3030
3031 for (unsigned i = 0; i < 2; ++i) {
3032 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3033 default: break;
3034 case 0:
3035 V[i] = V[i*2]; // Must be a zero vector.
3036 break;
3037 case 1:
3038 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3039 getMOVLMask(NumElems, DAG));
3040 break;
3041 case 2:
3042 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3043 getMOVLMask(NumElems, DAG));
3044 break;
3045 case 3:
3046 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3047 getUnpacklMask(NumElems, DAG));
3048 break;
3049 }
3050 }
3051
Evan Cheng069287d2006-05-16 07:21:53 +00003052 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003053 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003054 // FIXME: we can do the same for v4f32 case when we know both parts of
3055 // the lower half come from scalar_to_vector (loadf32). We should do
3056 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003057 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003058 return V[0];
3059 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003060 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003061 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003062 bool Reverse = (NonZeros & 0x3) == 2;
3063 for (unsigned i = 0; i < 2; ++i)
3064 if (Reverse)
3065 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3066 else
3067 MaskVec.push_back(DAG.getConstant(i, EVT));
3068 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3069 for (unsigned i = 0; i < 2; ++i)
3070 if (Reverse)
3071 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3072 else
3073 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003074 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3075 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3077 }
3078
3079 if (Values.size() > 2) {
3080 // Expand into a number of unpckl*.
3081 // e.g. for v4f32
3082 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3083 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3084 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3085 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3086 for (unsigned i = 0; i < NumElems; ++i)
3087 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3088 NumElems >>= 1;
3089 while (NumElems != 0) {
3090 for (unsigned i = 0; i < NumElems; ++i)
3091 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3092 UnpckMask);
3093 NumElems >>= 1;
3094 }
3095 return V[0];
3096 }
3097
3098 return SDOperand();
3099}
3100
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003101static
3102SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3103 SDOperand PermMask, SelectionDAG &DAG,
3104 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003105 SDOperand NewV;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003106 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3107 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng14b32e12007-12-11 01:46:18 +00003108 MVT::ValueType PtrVT = TLI.getPointerTy();
3109 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3110 PermMask.Val->op_end());
3111
3112 // First record which half of which vector the low elements come from.
3113 SmallVector<unsigned, 4> LowQuad(4);
3114 for (unsigned i = 0; i < 4; ++i) {
3115 SDOperand Elt = MaskElts[i];
3116 if (Elt.getOpcode() == ISD::UNDEF)
3117 continue;
3118 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3119 int QuadIdx = EltIdx / 4;
3120 ++LowQuad[QuadIdx];
3121 }
3122 int BestLowQuad = -1;
3123 unsigned MaxQuad = 1;
3124 for (unsigned i = 0; i < 4; ++i) {
3125 if (LowQuad[i] > MaxQuad) {
3126 BestLowQuad = i;
3127 MaxQuad = LowQuad[i];
3128 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003129 }
3130
Evan Cheng14b32e12007-12-11 01:46:18 +00003131 // Record which half of which vector the high elements come from.
3132 SmallVector<unsigned, 4> HighQuad(4);
3133 for (unsigned i = 4; i < 8; ++i) {
3134 SDOperand Elt = MaskElts[i];
3135 if (Elt.getOpcode() == ISD::UNDEF)
3136 continue;
3137 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3138 int QuadIdx = EltIdx / 4;
3139 ++HighQuad[QuadIdx];
3140 }
3141 int BestHighQuad = -1;
3142 MaxQuad = 1;
3143 for (unsigned i = 0; i < 4; ++i) {
3144 if (HighQuad[i] > MaxQuad) {
3145 BestHighQuad = i;
3146 MaxQuad = HighQuad[i];
3147 }
3148 }
3149
3150 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3151 if (BestLowQuad != -1 || BestHighQuad != -1) {
3152 // First sort the 4 chunks in order using shufpd.
3153 SmallVector<SDOperand, 8> MaskVec;
3154 if (BestLowQuad != -1)
3155 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3156 else
3157 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3158 if (BestHighQuad != -1)
3159 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3160 else
3161 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3162 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3163 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3164 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3165 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3166 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3167
3168 // Now sort high and low parts separately.
3169 BitVector InOrder(8);
3170 if (BestLowQuad != -1) {
3171 // Sort lower half in order using PSHUFLW.
3172 MaskVec.clear();
3173 bool AnyOutOrder = false;
3174 for (unsigned i = 0; i != 4; ++i) {
3175 SDOperand Elt = MaskElts[i];
3176 if (Elt.getOpcode() == ISD::UNDEF) {
3177 MaskVec.push_back(Elt);
3178 InOrder.set(i);
3179 } else {
3180 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3181 if (EltIdx != i)
3182 AnyOutOrder = true;
3183 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3184 // If this element is in the right place after this shuffle, then
3185 // remember it.
3186 if ((int)(EltIdx / 4) == BestLowQuad)
3187 InOrder.set(i);
3188 }
3189 }
3190 if (AnyOutOrder) {
3191 for (unsigned i = 4; i != 8; ++i)
3192 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3193 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3194 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3195 }
3196 }
3197
3198 if (BestHighQuad != -1) {
3199 // Sort high half in order using PSHUFHW if possible.
3200 MaskVec.clear();
3201 for (unsigned i = 0; i != 4; ++i)
3202 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3203 bool AnyOutOrder = false;
3204 for (unsigned i = 4; i != 8; ++i) {
3205 SDOperand Elt = MaskElts[i];
3206 if (Elt.getOpcode() == ISD::UNDEF) {
3207 MaskVec.push_back(Elt);
3208 InOrder.set(i);
3209 } else {
3210 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3211 if (EltIdx != i)
3212 AnyOutOrder = true;
3213 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3214 // If this element is in the right place after this shuffle, then
3215 // remember it.
3216 if ((int)(EltIdx / 4) == BestHighQuad)
3217 InOrder.set(i);
3218 }
3219 }
3220 if (AnyOutOrder) {
3221 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3222 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3223 }
3224 }
3225
3226 // The other elements are put in the right place using pextrw and pinsrw.
3227 for (unsigned i = 0; i != 8; ++i) {
3228 if (InOrder[i])
3229 continue;
3230 SDOperand Elt = MaskElts[i];
3231 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3232 if (EltIdx == i)
3233 continue;
3234 SDOperand ExtOp = (EltIdx < 8)
3235 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3236 DAG.getConstant(EltIdx, PtrVT))
3237 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3238 DAG.getConstant(EltIdx - 8, PtrVT));
3239 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3240 DAG.getConstant(i, PtrVT));
3241 }
3242 return NewV;
3243 }
3244
3245 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3246 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003247 // First, let's find out how many elements are already in the right order.
3248 unsigned V1InOrder = 0;
3249 unsigned V1FromV1 = 0;
3250 unsigned V2InOrder = 0;
3251 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003252 SmallVector<SDOperand, 8> V1Elts;
3253 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003254 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003255 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003256 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003257 V1Elts.push_back(Elt);
3258 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003259 ++V1InOrder;
3260 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003261 continue;
3262 }
3263 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3264 if (EltIdx == i) {
3265 V1Elts.push_back(Elt);
3266 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3267 ++V1InOrder;
3268 } else if (EltIdx == i+8) {
3269 V1Elts.push_back(Elt);
3270 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3271 ++V2InOrder;
3272 } else if (EltIdx < 8) {
3273 V1Elts.push_back(Elt);
3274 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003275 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003276 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3277 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003278 }
3279 }
3280
3281 if (V2InOrder > V1InOrder) {
3282 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3283 std::swap(V1, V2);
3284 std::swap(V1Elts, V2Elts);
3285 std::swap(V1FromV1, V2FromV2);
3286 }
3287
Evan Cheng14b32e12007-12-11 01:46:18 +00003288 if ((V1FromV1 + V1InOrder) != 8) {
3289 // Some elements are from V2.
3290 if (V1FromV1) {
3291 // If there are elements that are from V1 but out of place,
3292 // then first sort them in place
3293 SmallVector<SDOperand, 8> MaskVec;
3294 for (unsigned i = 0; i < 8; ++i) {
3295 SDOperand Elt = V1Elts[i];
3296 if (Elt.getOpcode() == ISD::UNDEF) {
3297 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3298 continue;
3299 }
3300 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3301 if (EltIdx >= 8)
3302 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3303 else
3304 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3305 }
3306 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3307 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003308 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003309
3310 NewV = V1;
3311 for (unsigned i = 0; i < 8; ++i) {
3312 SDOperand Elt = V1Elts[i];
3313 if (Elt.getOpcode() == ISD::UNDEF)
3314 continue;
3315 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3316 if (EltIdx < 8)
3317 continue;
3318 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3319 DAG.getConstant(EltIdx - 8, PtrVT));
3320 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3321 DAG.getConstant(i, PtrVT));
3322 }
3323 return NewV;
3324 } else {
3325 // All elements are from V1.
3326 NewV = V1;
3327 for (unsigned i = 0; i < 8; ++i) {
3328 SDOperand Elt = V1Elts[i];
3329 if (Elt.getOpcode() == ISD::UNDEF)
3330 continue;
3331 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3332 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3333 DAG.getConstant(EltIdx, PtrVT));
3334 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3335 DAG.getConstant(i, PtrVT));
3336 }
3337 return NewV;
3338 }
3339}
3340
Evan Cheng7a831ce2007-12-15 03:00:47 +00003341/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3342/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3343/// done when every pair / quad of shuffle mask elements point to elements in
3344/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003345/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3346static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003347SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3348 MVT::ValueType VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003349 SDOperand PermMask, SelectionDAG &DAG,
3350 TargetLowering &TLI) {
3351 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003352 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3353 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3354 MVT::ValueType NewVT = MaskVT;
3355 switch (VT) {
3356 case MVT::v4f32: NewVT = MVT::v2f64; break;
3357 case MVT::v4i32: NewVT = MVT::v2i64; break;
3358 case MVT::v8i16: NewVT = MVT::v4i32; break;
3359 case MVT::v16i8: NewVT = MVT::v4i32; break;
3360 default: assert(false && "Unexpected!");
3361 }
3362
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003363 if (NewWidth == 2) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00003364 if (MVT::isInteger(VT))
3365 NewVT = MVT::v2i64;
3366 else
3367 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003368 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003369 unsigned Scale = NumElems / NewWidth;
3370 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003371 for (unsigned i = 0; i < NumElems; i += Scale) {
3372 unsigned StartIdx = ~0U;
3373 for (unsigned j = 0; j < Scale; ++j) {
3374 SDOperand Elt = PermMask.getOperand(i+j);
3375 if (Elt.getOpcode() == ISD::UNDEF)
3376 continue;
3377 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3378 if (StartIdx == ~0U)
3379 StartIdx = EltIdx - (EltIdx % Scale);
3380 if (EltIdx != StartIdx + j)
3381 return SDOperand();
3382 }
3383 if (StartIdx == ~0U)
3384 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3385 else
3386 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003387 }
3388
Evan Cheng7a831ce2007-12-15 03:00:47 +00003389 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3390 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3391 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3392 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3393 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003394}
3395
Evan Cheng0db9fe62006-04-25 20:13:52 +00003396SDOperand
3397X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3398 SDOperand V1 = Op.getOperand(0);
3399 SDOperand V2 = Op.getOperand(1);
3400 SDOperand PermMask = Op.getOperand(2);
3401 MVT::ValueType VT = Op.getValueType();
3402 unsigned NumElems = PermMask.getNumOperands();
3403 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3404 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003405 bool V1IsSplat = false;
3406 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003407
Evan Cheng8cf723d2006-09-08 01:50:06 +00003408 if (isUndefShuffle(Op.Val))
3409 return DAG.getNode(ISD::UNDEF, VT);
3410
Evan Cheng213d2cf2007-05-17 18:45:50 +00003411 if (isZeroShuffle(Op.Val))
3412 return getZeroVector(VT, DAG);
3413
Evan Cheng49892af2007-06-19 00:02:56 +00003414 if (isIdentityMask(PermMask.Val))
3415 return V1;
3416 else if (isIdentityMask(PermMask.Val, true))
3417 return V2;
3418
Evan Cheng0db9fe62006-04-25 20:13:52 +00003419 if (isSplatMask(PermMask.Val)) {
3420 if (NumElems <= 4) return Op;
3421 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003422 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003423 }
3424
Evan Cheng7a831ce2007-12-15 03:00:47 +00003425 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3426 // do it!
3427 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3428 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3429 if (NewOp.Val)
3430 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3431 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3432 // FIXME: Figure out a cleaner way to do this.
3433 // Try to make use of movq to zero out the top part.
3434 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3435 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3436 if (NewOp.Val) {
3437 SDOperand NewV1 = NewOp.getOperand(0);
3438 SDOperand NewV2 = NewOp.getOperand(1);
3439 SDOperand NewMask = NewOp.getOperand(2);
3440 if (isCommutedMOVL(NewMask.Val, true, false)) {
3441 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3442 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3443 NewV1, NewV2, getMOVLMask(2, DAG));
3444 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3445 }
3446 }
3447 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3448 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3449 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3450 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3451 }
3452 }
3453
Evan Cheng9bbbb982006-10-25 20:48:19 +00003454 if (X86::isMOVLMask(PermMask.Val))
3455 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003456
Evan Cheng9bbbb982006-10-25 20:48:19 +00003457 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3458 X86::isMOVSLDUPMask(PermMask.Val) ||
3459 X86::isMOVHLPSMask(PermMask.Val) ||
3460 X86::isMOVHPMask(PermMask.Val) ||
3461 X86::isMOVLPMask(PermMask.Val))
3462 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003463
Evan Cheng9bbbb982006-10-25 20:48:19 +00003464 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3465 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003466 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003467
Evan Cheng9eca5e82006-10-25 21:49:50 +00003468 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003469 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3470 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003471 V1IsSplat = isSplatVector(V1.Val);
3472 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003473
3474 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003475 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003476 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003477 std::swap(V1IsSplat, V2IsSplat);
3478 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003479 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003480 }
3481
Evan Cheng7a831ce2007-12-15 03:00:47 +00003482 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003483 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3484 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003485 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003486 if (V2IsSplat) {
3487 // V2 is a splat, so the mask may be malformed. That is, it may point
3488 // to any V2 element. The instruction selectior won't like this. Get
3489 // a corrected mask and commute to form a proper MOVS{S|D}.
3490 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3491 if (NewMask.Val != PermMask.Val)
3492 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003493 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003494 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003495 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003496
Evan Chengd9b8e402006-10-16 06:36:00 +00003497 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003498 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003499 X86::isUNPCKLMask(PermMask.Val) ||
3500 X86::isUNPCKHMask(PermMask.Val))
3501 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003502
Evan Cheng9bbbb982006-10-25 20:48:19 +00003503 if (V2IsSplat) {
3504 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003505 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003506 // new vector_shuffle with the corrected mask.
3507 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3508 if (NewMask.Val != PermMask.Val) {
3509 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3510 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3511 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3512 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3513 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3514 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003515 }
3516 }
3517 }
3518
3519 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003520 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3521 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3522
3523 if (Commuted) {
3524 // Commute is back and try unpck* again.
3525 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3526 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003527 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003528 X86::isUNPCKLMask(PermMask.Val) ||
3529 X86::isUNPCKHMask(PermMask.Val))
3530 return Op;
3531 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003532
3533 // If VT is integer, try PSHUF* first, then SHUFP*.
3534 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003535 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3536 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3537 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3538 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003539 X86::isPSHUFHWMask(PermMask.Val) ||
3540 X86::isPSHUFLWMask(PermMask.Val)) {
3541 if (V2.getOpcode() != ISD::UNDEF)
3542 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3543 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3544 return Op;
3545 }
3546
Chris Lattner07c70cd2007-05-17 17:13:13 +00003547 if (X86::isSHUFPMask(PermMask.Val) &&
3548 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003549 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003550 } else {
3551 // Floating point cases in the other order.
3552 if (X86::isSHUFPMask(PermMask.Val))
3553 return Op;
3554 if (X86::isPSHUFDMask(PermMask.Val) ||
3555 X86::isPSHUFHWMask(PermMask.Val) ||
3556 X86::isPSHUFLWMask(PermMask.Val)) {
3557 if (V2.getOpcode() != ISD::UNDEF)
3558 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3559 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3560 return Op;
3561 }
3562 }
3563
Evan Cheng14b32e12007-12-11 01:46:18 +00003564 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3565 if (VT == MVT::v8i16) {
3566 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3567 if (NewOp.Val)
3568 return NewOp;
3569 }
3570
3571 // Handle all 4 wide cases with a number of shuffles.
3572 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003573 // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003574 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003575 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003576 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003577 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003578 SmallVector<SDOperand, 8> Mask1(NumElems,
3579 DAG.getNode(ISD::UNDEF, MaskEVT));
3580 SmallVector<SDOperand, 8> Mask2(NumElems,
3581 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003582 unsigned NumHi = 0;
3583 unsigned NumLo = 0;
3584 // If no more than two elements come from either vector. This can be
3585 // implemented with two shuffles. First shuffle gather the elements.
3586 // The second shuffle, which takes the first shuffle as both of its
3587 // vector operands, put the elements into the right order.
3588 for (unsigned i = 0; i != NumElems; ++i) {
3589 SDOperand Elt = PermMask.getOperand(i);
3590 if (Elt.getOpcode() == ISD::UNDEF) {
3591 Locs[i] = std::make_pair(-1, -1);
3592 } else {
3593 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3594 if (Val < NumElems) {
3595 Locs[i] = std::make_pair(0, NumLo);
3596 Mask1[NumLo] = Elt;
3597 NumLo++;
3598 } else {
3599 Locs[i] = std::make_pair(1, NumHi);
3600 if (2+NumHi < NumElems)
3601 Mask1[2+NumHi] = Elt;
3602 NumHi++;
3603 }
3604 }
3605 }
3606 if (NumLo <= 2 && NumHi <= 2) {
3607 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003608 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3609 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003610 for (unsigned i = 0; i != NumElems; ++i) {
3611 if (Locs[i].first == -1)
3612 continue;
3613 else {
3614 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3615 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3616 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3617 }
3618 }
3619
3620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003621 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3622 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003623 }
3624
3625 // Break it into (shuffle shuffle_hi, shuffle_lo).
3626 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003627 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3628 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3629 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003630 unsigned MaskIdx = 0;
3631 unsigned LoIdx = 0;
3632 unsigned HiIdx = NumElems/2;
3633 for (unsigned i = 0; i != NumElems; ++i) {
3634 if (i == NumElems/2) {
3635 MaskPtr = &HiMask;
3636 MaskIdx = 1;
3637 LoIdx = 0;
3638 HiIdx = NumElems/2;
3639 }
3640 SDOperand Elt = PermMask.getOperand(i);
3641 if (Elt.getOpcode() == ISD::UNDEF) {
3642 Locs[i] = std::make_pair(-1, -1);
3643 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3644 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3645 (*MaskPtr)[LoIdx] = Elt;
3646 LoIdx++;
3647 } else {
3648 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3649 (*MaskPtr)[HiIdx] = Elt;
3650 HiIdx++;
3651 }
3652 }
3653
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003654 SDOperand LoShuffle =
3655 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003656 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3657 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003658 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003659 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003660 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3661 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003662 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003663 for (unsigned i = 0; i != NumElems; ++i) {
3664 if (Locs[i].first == -1) {
3665 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3666 } else {
3667 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3668 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3669 }
3670 }
3671 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003672 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3673 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 }
3675
3676 return SDOperand();
3677}
3678
3679SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00003680X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3681 SelectionDAG &DAG) {
3682 MVT::ValueType VT = Op.getValueType();
3683 if (MVT::getSizeInBits(VT) == 8) {
3684 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3685 Op.getOperand(0), Op.getOperand(1));
3686 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3687 DAG.getValueType(VT));
3688 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3689 } else if (MVT::getSizeInBits(VT) == 16) {
3690 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3691 Op.getOperand(0), Op.getOperand(1));
3692 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3693 DAG.getValueType(VT));
3694 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3695 }
3696 return SDOperand();
3697}
3698
3699
3700SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00003701X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3702 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3703 return SDOperand();
3704
Nate Begeman14d12ca2008-02-11 04:19:36 +00003705 if (Subtarget->hasSSE41())
3706 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3707
Evan Cheng0db9fe62006-04-25 20:13:52 +00003708 MVT::ValueType VT = Op.getValueType();
3709 // TODO: handle v16i8.
3710 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003711 SDOperand Vec = Op.getOperand(0);
3712 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3713 if (Idx == 0)
3714 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3715 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3716 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3717 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003718 // Transform it so it match pextrw which produces a 32-bit result.
3719 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3720 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3721 Op.getOperand(0), Op.getOperand(1));
3722 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3723 DAG.getValueType(VT));
3724 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3725 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003726 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3727 if (Idx == 0)
3728 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 // SHUFPS the element to the lowest double word, then movss.
3730 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003731 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003732 IdxVec.
3733 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3734 IdxVec.
3735 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3736 IdxVec.
3737 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3738 IdxVec.
3739 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003740 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3741 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003742 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003743 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003744 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00003746 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003747 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00003748 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3749 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3750 // to match extract_elt for f64.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003751 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3752 if (Idx == 0)
3753 return Op;
3754
3755 // UNPCKHPD the element to the lowest double word, then movsd.
3756 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3757 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3758 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003759 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003760 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003761 IdxVec.
3762 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003763 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3764 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003765 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003766 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3767 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3768 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00003769 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 }
3771
3772 return SDOperand();
3773}
3774
3775SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00003776X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3777 MVT::ValueType VT = Op.getValueType();
3778 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3779
3780 SDOperand N0 = Op.getOperand(0);
3781 SDOperand N1 = Op.getOperand(1);
3782 SDOperand N2 = Op.getOperand(2);
3783
3784 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3785 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3786 : X86ISD::PINSRW;
3787 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3788 // argument.
3789 if (N1.getValueType() != MVT::i32)
3790 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3791 if (N2.getValueType() != MVT::i32)
3792 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3793 return DAG.getNode(Opc, VT, N0, N1, N2);
3794 } else if (EVT == MVT::f32) {
3795 // Bits [7:6] of the constant are the source select. This will always be
3796 // zero here. The DAG Combiner may combine an extract_elt index into these
3797 // bits. For example (insert (extract, 3), 2) could be matched by putting
3798 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3799 // Bits [5:4] of the constant are the destination select. This is the
3800 // value of the incoming immediate.
3801 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3802 // combine either bitwise AND or insert of float 0.0 to set these bits.
3803 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3804 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3805 }
3806 return SDOperand();
3807}
3808
3809SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003811 MVT::ValueType VT = Op.getValueType();
Evan Cheng794405e2007-12-12 07:55:34 +00003812 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begeman14d12ca2008-02-11 04:19:36 +00003813
3814 if (Subtarget->hasSSE41())
3815 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3816
Evan Cheng794405e2007-12-12 07:55:34 +00003817 if (EVT == MVT::i8)
3818 return SDOperand();
3819
Evan Cheng0db9fe62006-04-25 20:13:52 +00003820 SDOperand N0 = Op.getOperand(0);
3821 SDOperand N1 = Op.getOperand(1);
3822 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00003823
3824 if (MVT::getSizeInBits(EVT) == 16) {
3825 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3826 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003827 if (N1.getValueType() != MVT::i32)
3828 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3829 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00003830 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003831 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003832 }
Nate Begeman219f67f2008-01-05 20:51:30 +00003833 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003834}
3835
3836SDOperand
3837X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3838 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengefec7512008-02-18 23:04:32 +00003839 MVT::ValueType VT = MVT::v2i32;
3840 switch (Op.getValueType()) {
3841 default: break;
3842 case MVT::v16i8:
3843 case MVT::v8i16:
3844 VT = MVT::v4i32;
3845 break;
3846 }
3847 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3848 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849}
3850
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003851// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3853// one of the above mentioned nodes. It has to be wrapped because otherwise
3854// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3855// be used to form addressing mode. These wrapped nodes will be selected
3856// into MOV32ri.
3857SDOperand
3858X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3859 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003860 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3861 getPointerTy(),
3862 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003863 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003864 // With PIC, the address is actually $g + Offset.
3865 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3866 !Subtarget->isPICStyleRIPRel()) {
3867 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3868 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3869 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 }
3871
3872 return Result;
3873}
3874
3875SDOperand
3876X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3877 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003878 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga844bde2008-02-02 04:07:54 +00003879 // If it's a debug information descriptor, don't mess with it.
3880 if (DAG.isVerifiedDebugInfoDesc(Op))
3881 return Result;
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003882 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003883 // With PIC, the address is actually $g + Offset.
3884 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3885 !Subtarget->isPICStyleRIPRel()) {
3886 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3887 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3888 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003889 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003890
3891 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3892 // load the value at address GV, not the value of GV itself. This means that
3893 // the GlobalAddress must be in the base or index register of the address, not
3894 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003895 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003896 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00003897 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00003898 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003899
3900 return Result;
3901}
3902
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003903// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3904static SDOperand
3905LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3906 const MVT::ValueType PtrVT) {
3907 SDOperand InFlag;
3908 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3909 DAG.getNode(X86ISD::GlobalBaseReg,
3910 PtrVT), InFlag);
3911 InFlag = Chain.getValue(1);
3912
3913 // emit leal symbol@TLSGD(,%ebx,1), %eax
3914 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3915 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3916 GA->getValueType(0),
3917 GA->getOffset());
3918 SDOperand Ops[] = { Chain, TGA, InFlag };
3919 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3920 InFlag = Result.getValue(2);
3921 Chain = Result.getValue(1);
3922
3923 // call ___tls_get_addr. This function receives its argument in
3924 // the register EAX.
3925 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3926 InFlag = Chain.getValue(1);
3927
3928 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3929 SDOperand Ops1[] = { Chain,
3930 DAG.getTargetExternalSymbol("___tls_get_addr",
3931 PtrVT),
3932 DAG.getRegister(X86::EAX, PtrVT),
3933 DAG.getRegister(X86::EBX, PtrVT),
3934 InFlag };
3935 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3936 InFlag = Chain.getValue(1);
3937
3938 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3939}
3940
3941// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3942// "local exec" model.
3943static SDOperand
3944LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3945 const MVT::ValueType PtrVT) {
3946 // Get the Thread Pointer
3947 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3948 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3949 // exec)
3950 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3951 GA->getValueType(0),
3952 GA->getOffset());
3953 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003954
3955 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00003956 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00003957 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003958
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003959 // The address of the thread local variable is the add of the thread
3960 // pointer with the offset of the variable.
3961 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3962}
3963
3964SDOperand
3965X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3966 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003967 // TODO: implement the "initial exec"model for pic executables
3968 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3969 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003970 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3971 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3972 // otherwise use the "Local Exec"TLS Model
3973 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3974 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3975 else
3976 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3977}
3978
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979SDOperand
3980X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3981 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003982 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003983 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003984 // With PIC, the address is actually $g + Offset.
3985 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3986 !Subtarget->isPICStyleRIPRel()) {
3987 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3988 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3989 Result);
3990 }
3991
3992 return Result;
3993}
3994
3995SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3996 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3997 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3998 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3999 // With PIC, the address is actually $g + Offset.
4000 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4001 !Subtarget->isPICStyleRIPRel()) {
4002 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4003 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4004 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004005 }
4006
4007 return Result;
4008}
4009
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004010/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4011/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004012SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004013 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4014 "Not an i64 shift!");
4015 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4016 SDOperand ShOpLo = Op.getOperand(0);
4017 SDOperand ShOpHi = Op.getOperand(1);
4018 SDOperand ShAmt = Op.getOperand(2);
4019 SDOperand Tmp1 = isSRA ?
4020 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4021 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00004022
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004023 SDOperand Tmp2, Tmp3;
4024 if (Op.getOpcode() == ISD::SHL_PARTS) {
4025 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4026 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4027 } else {
4028 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4029 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4030 }
Evan Chenge3413162006-01-09 18:33:28 +00004031
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004032 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4033 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4034 DAG.getConstant(32, MVT::i8));
4035 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4036 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004037
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004038 SDOperand Hi, Lo;
4039 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4040 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4041 SmallVector<SDOperand, 4> Ops;
4042 if (Op.getOpcode() == ISD::SHL_PARTS) {
4043 Ops.push_back(Tmp2);
4044 Ops.push_back(Tmp3);
4045 Ops.push_back(CC);
4046 Ops.push_back(Cond);
4047 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00004048
Evan Chenge3413162006-01-09 18:33:28 +00004049 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004050 Ops.push_back(Tmp3);
4051 Ops.push_back(Tmp1);
4052 Ops.push_back(CC);
4053 Ops.push_back(Cond);
4054 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4055 } else {
4056 Ops.push_back(Tmp2);
4057 Ops.push_back(Tmp3);
4058 Ops.push_back(CC);
4059 Ops.push_back(Cond);
4060 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4061
4062 Ops.clear();
4063 Ops.push_back(Tmp3);
4064 Ops.push_back(Tmp1);
4065 Ops.push_back(CC);
4066 Ops.push_back(Cond);
4067 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4068 }
4069
4070 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4071 Ops.clear();
4072 Ops.push_back(Lo);
4073 Ops.push_back(Hi);
4074 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004075}
Evan Chenga3195e82006-01-12 22:54:21 +00004076
Evan Cheng0db9fe62006-04-25 20:13:52 +00004077SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4078 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4079 Op.getOperand(0).getValueType() >= MVT::i16 &&
4080 "Unknown SINT_TO_FP to lower!");
4081
4082 SDOperand Result;
4083 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4084 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4085 MachineFunction &MF = DAG.getMachineFunction();
4086 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4087 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00004088 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004089 StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004090 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004091 SSFI);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004093 // These are really Legal; caller falls through into that case.
Chris Lattner78631162008-01-16 06:24:21 +00004094 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004095 return Result;
Chris Lattner1956d152008-01-16 06:19:45 +00004096 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen73328d12007-09-19 23:55:34 +00004097 Subtarget->is64Bit())
4098 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004099
Evan Cheng0db9fe62006-04-25 20:13:52 +00004100 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004101 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004102 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004103 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004104 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4105 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004106 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004107 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004108 Ops.push_back(Chain);
4109 Ops.push_back(StackSlot);
4110 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004111 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004112 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004113
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004114 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004115 Chain = Result.getValue(1);
4116 SDOperand InFlag = Result.getValue(2);
4117
4118 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4119 // shouldn't be necessary except that RFP cannot be live across
4120 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004121 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004122 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004123 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004124 Tys = DAG.getVTList(MVT::Other);
4125 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004126 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004127 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004128 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004129 Ops.push_back(DAG.getValueType(Op.getValueType()));
4130 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004131 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004132 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004133 PseudoSourceValue::getFixedStack(), SSFI);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004134 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004135
Evan Cheng0db9fe62006-04-25 20:13:52 +00004136 return Result;
4137}
4138
Chris Lattner27a6c732007-11-24 07:07:01 +00004139std::pair<SDOperand,SDOperand> X86TargetLowering::
4140FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004141 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4142 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004143
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004144 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004145 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004146 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattner27a6c732007-11-24 07:07:01 +00004147 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004148 if (Subtarget->is64Bit() &&
4149 Op.getValueType() == MVT::i64 &&
4150 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004151 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004152
Evan Cheng87c89352007-10-15 20:11:21 +00004153 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4154 // stack slot.
4155 MachineFunction &MF = DAG.getMachineFunction();
4156 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4157 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4158 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159 unsigned Opc;
4160 switch (Op.getValueType()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004161 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4162 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4163 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4164 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004165 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004166
Evan Cheng0db9fe62006-04-25 20:13:52 +00004167 SDOperand Chain = DAG.getEntryNode();
4168 SDOperand Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004169 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004171 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004172 PseudoSourceValue::getFixedStack(), SSFI);
Dale Johannesen849f2142007-07-03 00:53:03 +00004173 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004174 SDOperand Ops[] = {
4175 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4176 };
4177 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004178 Chain = Value.getValue(1);
4179 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4180 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4181 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004182
Evan Cheng0db9fe62006-04-25 20:13:52 +00004183 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004184 SDOperand Ops[] = { Chain, Value, StackSlot };
4185 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004186
Chris Lattner27a6c732007-11-24 07:07:01 +00004187 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188}
4189
Chris Lattner27a6c732007-11-24 07:07:01 +00004190SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004191 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4192 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4193 if (FIST.Val == 0) return SDOperand();
4194
4195 // Load the result.
4196 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4197}
4198
4199SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4200 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4201 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4202 if (FIST.Val == 0) return 0;
4203
4204 // Return an i64 load from the stack slot.
4205 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4206
4207 // Use a MERGE_VALUES node to drop the chain result value.
4208 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4209}
4210
Evan Cheng0db9fe62006-04-25 20:13:52 +00004211SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4212 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004213 MVT::ValueType EltVT = VT;
4214 if (MVT::isVector(VT))
4215 EltVT = MVT::getVectorElementType(VT);
4216 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004217 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004218 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004219 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004220 CV.push_back(C);
4221 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004222 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004223 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004224 CV.push_back(C);
4225 CV.push_back(C);
4226 CV.push_back(C);
4227 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004228 }
Dan Gohmand3006222007-07-27 17:16:43 +00004229 Constant *C = ConstantVector::get(CV);
4230 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004231 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004232 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004233 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004234 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4235}
4236
4237SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4238 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004239 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004240 unsigned EltNum = 1;
4241 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004242 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004243 EltNum = MVT::getVectorNumElements(VT);
4244 }
Dan Gohman20382522007-07-10 00:05:58 +00004245 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004246 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004247 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004248 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004249 CV.push_back(C);
4250 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004252 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004253 CV.push_back(C);
4254 CV.push_back(C);
4255 CV.push_back(C);
4256 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004257 }
Dan Gohmand3006222007-07-27 17:16:43 +00004258 Constant *C = ConstantVector::get(CV);
4259 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004260 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004261 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004262 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004263 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004264 return DAG.getNode(ISD::BIT_CONVERT, VT,
4265 DAG.getNode(ISD::XOR, MVT::v2i64,
4266 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4267 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4268 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004269 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4270 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004271}
4272
Evan Cheng68c47cb2007-01-05 07:55:56 +00004273SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004274 SDOperand Op0 = Op.getOperand(0);
4275 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004276 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004277 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004278 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004279
4280 // If second operand is smaller, extend it first.
4281 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4282 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4283 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004284 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004285 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004286 // And if it is bigger, shrink it first.
4287 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004288 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004289 SrcVT = VT;
4290 SrcTy = MVT::getTypeForValueType(SrcVT);
4291 }
4292
4293 // At this point the operands and the result should have the same
4294 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004295
Evan Cheng68c47cb2007-01-05 07:55:56 +00004296 // First get the sign bit of second operand.
4297 std::vector<Constant*> CV;
4298 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004299 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4300 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004301 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004302 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4303 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4304 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4305 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004306 }
Dan Gohmand3006222007-07-27 17:16:43 +00004307 Constant *C = ConstantVector::get(CV);
4308 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004309 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004310 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004311 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004312 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004313
4314 // Shift sign bit right or left if the two operands have different types.
4315 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4316 // Op0 is MVT::f32, Op1 is MVT::f64.
4317 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4318 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4319 DAG.getConstant(32, MVT::i32));
4320 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4321 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004322 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004323 }
4324
Evan Cheng73d6cf12007-01-05 21:37:56 +00004325 // Clear first operand sign bit.
4326 CV.clear();
4327 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004328 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4329 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004330 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004331 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4332 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4333 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4334 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004335 }
Dan Gohmand3006222007-07-27 17:16:43 +00004336 C = ConstantVector::get(CV);
4337 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004338 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004339 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004340 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004341 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4342
4343 // Or the value with the sign bit.
4344 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004345}
4346
Evan Chenge5f62042007-09-29 00:00:36 +00004347SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004348 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004349 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004350 SDOperand Op0 = Op.getOperand(0);
4351 SDOperand Op1 = Op.getOperand(1);
4352 SDOperand CC = Op.getOperand(2);
4353 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4354 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4355 unsigned X86CC;
4356
Evan Cheng0488db92007-09-25 01:57:46 +00004357 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004358 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004359 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4360 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004361 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004362 }
Evan Cheng0488db92007-09-25 01:57:46 +00004363
4364 assert(isFP && "Illegal integer SetCC!");
4365
Evan Chenge5f62042007-09-29 00:00:36 +00004366 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004367 switch (SetCCOpcode) {
4368 default: assert(false && "Illegal floating point SetCC!");
4369 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004370 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004371 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004372 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004373 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4374 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4375 }
4376 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004377 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004378 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004379 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004380 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4381 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4382 }
4383 }
4384}
4385
4386
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004388 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004389 SDOperand Cond = Op.getOperand(0);
4390 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004391
Evan Cheng734503b2006-09-11 02:19:56 +00004392 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004393 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004394
Evan Cheng3f41d662007-10-08 22:16:29 +00004395 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4396 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004397 if (Cond.getOpcode() == X86ISD::SETCC) {
4398 CC = Cond.getOperand(0);
4399
Evan Cheng734503b2006-09-11 02:19:56 +00004400 SDOperand Cmp = Cond.getOperand(1);
4401 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004402 MVT::ValueType VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004403
Evan Cheng3f41d662007-10-08 22:16:29 +00004404 bool IllegalFPCMov = false;
Chris Lattner1956d152008-01-16 06:19:45 +00004405 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattner78631162008-01-16 06:24:21 +00004406 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004407 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004408
Evan Chenge5f62042007-09-29 00:00:36 +00004409 if ((Opc == X86ISD::CMP ||
4410 Opc == X86ISD::COMI ||
4411 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004412 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004413 addTest = false;
4414 }
4415 }
4416
4417 if (addTest) {
4418 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004419 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004420 }
4421
4422 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4423 MVT::Flag);
4424 SmallVector<SDOperand, 4> Ops;
4425 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4426 // condition is true.
4427 Ops.push_back(Op.getOperand(2));
4428 Ops.push_back(Op.getOperand(1));
4429 Ops.push_back(CC);
4430 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004431 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004432}
4433
Evan Cheng0db9fe62006-04-25 20:13:52 +00004434SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004435 bool addTest = true;
4436 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004437 SDOperand Cond = Op.getOperand(1);
4438 SDOperand Dest = Op.getOperand(2);
4439 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004440
Evan Cheng0db9fe62006-04-25 20:13:52 +00004441 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004442 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004443
Evan Cheng3f41d662007-10-08 22:16:29 +00004444 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4445 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004446 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004447 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004448
Evan Cheng734503b2006-09-11 02:19:56 +00004449 SDOperand Cmp = Cond.getOperand(1);
4450 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004451 if (Opc == X86ISD::CMP ||
4452 Opc == X86ISD::COMI ||
4453 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004454 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004455 addTest = false;
4456 }
4457 }
4458
4459 if (addTest) {
4460 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004461 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004462 }
Evan Chenge5f62042007-09-29 00:00:36 +00004463 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004464 Chain, Op.getOperand(2), CC, Cond);
4465}
4466
Anton Korobeynikove060b532007-04-17 19:34:00 +00004467
4468// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4469// Calls to _alloca is needed to probe the stack when allocating more than 4k
4470// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4471// that the guard pages used by the OS virtual memory manager are allocated in
4472// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004473SDOperand
4474X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4475 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004476 assert(Subtarget->isTargetCygMing() &&
4477 "This should be used only on Cygwin/Mingw targets");
4478
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004479 // Get the inputs.
4480 SDOperand Chain = Op.getOperand(0);
4481 SDOperand Size = Op.getOperand(1);
4482 // FIXME: Ensure alignment here
4483
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004484 SDOperand Flag;
4485
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004486 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner0bd48932008-01-17 07:00:52 +00004487 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004488
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004489 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4490 Flag = Chain.getValue(1);
4491
4492 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4493 SDOperand Ops[] = { Chain,
4494 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4495 DAG.getRegister(X86::EAX, IntPtr),
4496 Flag };
4497 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4498 Flag = Chain.getValue(1);
4499
4500 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004501
4502 std::vector<MVT::ValueType> Tys;
4503 Tys.push_back(SPTy);
4504 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004505 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4506 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004507}
4508
Evan Cheng0db9fe62006-04-25 20:13:52 +00004509SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4510 SDOperand InFlag(0, 0);
4511 SDOperand Chain = Op.getOperand(0);
4512 unsigned Align =
4513 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4514 if (Align == 0) Align = 1;
4515
4516 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004517 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004518 // The libc version is likely to be faster for these cases. It can use the
4519 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004520 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004521 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004523 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004524 TargetLowering::ArgListTy Args;
4525 TargetLowering::ArgListEntry Entry;
4526 Entry.Node = Op.getOperand(1);
4527 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004528 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004529 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004530 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4531 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004532 Args.push_back(Entry);
4533 Entry.Node = Op.getOperand(3);
4534 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004536 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4537 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004539 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004540
Evan Cheng0db9fe62006-04-25 20:13:52 +00004541 MVT::ValueType AVT;
4542 SDOperand Count;
4543 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4544 unsigned BytesLeft = 0;
4545 bool TwoRepStos = false;
4546 if (ValC) {
4547 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004548 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004549
Evan Cheng0db9fe62006-04-25 20:13:52 +00004550 // If the value is a constant, then we can potentially use larger sets.
4551 switch (Align & 3) {
4552 case 2: // WORD aligned
4553 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004555 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004557 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004559 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560 Val = (Val << 8) | Val;
4561 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004562 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4563 AVT = MVT::i64;
4564 ValReg = X86::RAX;
4565 Val = (Val << 32) | Val;
4566 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567 break;
4568 default: // Byte aligned
4569 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004570 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004571 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004572 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004573 }
4574
Evan Cheng25ab6902006-09-08 06:48:29 +00004575 if (AVT > MVT::i8) {
4576 if (I) {
4577 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner0bd48932008-01-17 07:00:52 +00004578 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Evan Cheng25ab6902006-09-08 06:48:29 +00004579 BytesLeft = I->getValue() % UBytes;
4580 } else {
4581 assert(AVT >= MVT::i32 &&
4582 "Do not use rep;stos if not at least DWORD aligned");
4583 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4584 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4585 TwoRepStos = true;
4586 }
4587 }
4588
Evan Cheng0db9fe62006-04-25 20:13:52 +00004589 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4590 InFlag);
4591 InFlag = Chain.getValue(1);
4592 } else {
4593 AVT = MVT::i8;
4594 Count = Op.getOperand(3);
4595 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4596 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004597 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004598
Evan Cheng25ab6902006-09-08 06:48:29 +00004599 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4600 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004602 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4603 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004605
Chris Lattnerd96d0722007-02-25 06:40:16 +00004606 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004607 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004608 Ops.push_back(Chain);
4609 Ops.push_back(DAG.getValueType(AVT));
4610 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004611 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004612
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 if (TwoRepStos) {
4614 InFlag = Chain.getValue(1);
4615 Count = Op.getOperand(3);
4616 MVT::ValueType CVT = Count.getValueType();
4617 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004618 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4619 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4620 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004621 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004622 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623 Ops.clear();
4624 Ops.push_back(Chain);
4625 Ops.push_back(DAG.getValueType(MVT::i8));
4626 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004627 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004628 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004629 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004630 SDOperand Value;
4631 unsigned Val = ValC->getValue() & 255;
4632 unsigned Offset = I->getValue() - BytesLeft;
4633 SDOperand DstAddr = Op.getOperand(1);
4634 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004635 if (BytesLeft >= 4) {
4636 Val = (Val << 8) | Val;
4637 Val = (Val << 16) | Val;
4638 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004639 Chain = DAG.getStore(Chain, Value,
4640 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4641 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004642 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004643 BytesLeft -= 4;
4644 Offset += 4;
4645 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646 if (BytesLeft >= 2) {
4647 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004648 Chain = DAG.getStore(Chain, Value,
4649 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4650 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004651 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 BytesLeft -= 2;
4653 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004654 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 if (BytesLeft == 1) {
4656 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004657 Chain = DAG.getStore(Chain, Value,
4658 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4659 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004660 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004661 }
Evan Cheng386031a2006-03-24 07:29:27 +00004662 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004663
Evan Cheng0db9fe62006-04-25 20:13:52 +00004664 return Chain;
4665}
Evan Cheng11e15b32006-04-03 20:53:28 +00004666
Rafael Espindola068317b2007-09-28 12:53:01 +00004667SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4668 SDOperand Dest,
4669 SDOperand Source,
4670 unsigned Size,
4671 unsigned Align,
4672 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004673 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004674 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004675 switch (Align & 3) {
4676 case 2: // WORD aligned
4677 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004678 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004679 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004681 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4682 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 break;
4684 default: // Byte aligned
4685 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004686 break;
4687 }
4688
Rafael Espindola068317b2007-09-28 12:53:01 +00004689 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner0bd48932008-01-17 07:00:52 +00004690 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindola068317b2007-09-28 12:53:01 +00004691 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004692
Evan Cheng0db9fe62006-04-25 20:13:52 +00004693 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004694 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4695 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004696 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004697 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004698 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004699 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004700 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004701 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702 InFlag = Chain.getValue(1);
4703
Chris Lattnerd96d0722007-02-25 06:40:16 +00004704 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004705 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004706 Ops.push_back(Chain);
4707 Ops.push_back(DAG.getValueType(AVT));
4708 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004709 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004710
Rafael Espindola068317b2007-09-28 12:53:01 +00004711 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004712 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004713 unsigned Offset = Size - BytesLeft;
4714 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004715 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004716 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004717 MVT::ValueType SrcVT = SrcAddr.getValueType();
4718 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004719 if (BytesLeft >= 4) {
4720 Value = DAG.getLoad(MVT::i32, Chain,
4721 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4722 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004723 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004724 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004725 Chain = DAG.getStore(Chain, Value,
4726 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4727 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004728 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004729 BytesLeft -= 4;
4730 Offset += 4;
4731 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004732 if (BytesLeft >= 2) {
4733 Value = DAG.getLoad(MVT::i16, Chain,
4734 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4735 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004736 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004737 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004738 Chain = DAG.getStore(Chain, Value,
4739 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4740 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004741 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004742 BytesLeft -= 2;
4743 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004744 }
4745
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746 if (BytesLeft == 1) {
4747 Value = DAG.getLoad(MVT::i8, Chain,
4748 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4749 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004750 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004751 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004752 Chain = DAG.getStore(Chain, Value,
4753 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4754 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004755 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004756 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004757 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004758
4759 return Chain;
4760}
4761
Chris Lattner27a6c732007-11-24 07:07:01 +00004762/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4763SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00004764 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00004765 SDOperand TheChain = N->getOperand(0);
4766 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004767 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004768 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4769 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4770 MVT::i64, rax.getValue(2));
4771 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004772 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004773 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00004774 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00004775 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004776
4777 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner27a6c732007-11-24 07:07:01 +00004778 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004779 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004780
Chris Lattner27a6c732007-11-24 07:07:01 +00004781 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4782 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4783 MVT::i32, eax.getValue(2));
4784 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4785 SDOperand Ops[] = { eax, edx };
4786 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4787
4788 // Use a MERGE_VALUES to return the value and chain.
4789 Ops[1] = edx.getValue(1);
4790 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4791 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792}
4793
4794SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00004795 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004796
Evan Cheng25ab6902006-09-08 06:48:29 +00004797 if (!Subtarget->is64Bit()) {
4798 // vastart just stores the address of the VarArgsFrameIndex slot into the
4799 // memory location argument.
4800 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004801 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004802 }
4803
4804 // __va_list_tag:
4805 // gp_offset (0 - 6 * 8)
4806 // fp_offset (48 - 48 + 8 * 16)
4807 // overflow_arg_area (point to parameters coming in memory).
4808 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004809 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004810 SDOperand FIN = Op.getOperand(1);
4811 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004812 SDOperand Store = DAG.getStore(Op.getOperand(0),
4813 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00004814 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004815 MemOps.push_back(Store);
4816
4817 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00004818 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00004819 Store = DAG.getStore(Op.getOperand(0),
4820 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00004821 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004822 MemOps.push_back(Store);
4823
4824 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00004825 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng25ab6902006-09-08 06:48:29 +00004826 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004827 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004828 MemOps.push_back(Store);
4829
4830 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00004831 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Evan Cheng25ab6902006-09-08 06:48:29 +00004832 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004833 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004834 MemOps.push_back(Store);
4835 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836}
4837
Evan Chengae642192007-03-02 23:16:35 +00004838SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4839 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4840 SDOperand Chain = Op.getOperand(0);
4841 SDOperand DstPtr = Op.getOperand(1);
4842 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00004843 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4844 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00004845
Dan Gohman69de1932008-02-06 22:27:42 +00004846 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00004847 Chain = SrcPtr.getValue(1);
4848 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman69de1932008-02-06 22:27:42 +00004849 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00004850 Chain = Val.getValue(1);
Dan Gohman69de1932008-02-06 22:27:42 +00004851 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00004852 if (i == 2)
4853 break;
4854 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner0bd48932008-01-17 07:00:52 +00004855 DAG.getIntPtrConstant(8));
Evan Chengae642192007-03-02 23:16:35 +00004856 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner0bd48932008-01-17 07:00:52 +00004857 DAG.getIntPtrConstant(8));
Evan Chengae642192007-03-02 23:16:35 +00004858 }
4859 return Chain;
4860}
4861
Evan Cheng0db9fe62006-04-25 20:13:52 +00004862SDOperand
4863X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4864 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4865 switch (IntNo) {
4866 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004867 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004868 case Intrinsic::x86_sse_comieq_ss:
4869 case Intrinsic::x86_sse_comilt_ss:
4870 case Intrinsic::x86_sse_comile_ss:
4871 case Intrinsic::x86_sse_comigt_ss:
4872 case Intrinsic::x86_sse_comige_ss:
4873 case Intrinsic::x86_sse_comineq_ss:
4874 case Intrinsic::x86_sse_ucomieq_ss:
4875 case Intrinsic::x86_sse_ucomilt_ss:
4876 case Intrinsic::x86_sse_ucomile_ss:
4877 case Intrinsic::x86_sse_ucomigt_ss:
4878 case Intrinsic::x86_sse_ucomige_ss:
4879 case Intrinsic::x86_sse_ucomineq_ss:
4880 case Intrinsic::x86_sse2_comieq_sd:
4881 case Intrinsic::x86_sse2_comilt_sd:
4882 case Intrinsic::x86_sse2_comile_sd:
4883 case Intrinsic::x86_sse2_comigt_sd:
4884 case Intrinsic::x86_sse2_comige_sd:
4885 case Intrinsic::x86_sse2_comineq_sd:
4886 case Intrinsic::x86_sse2_ucomieq_sd:
4887 case Intrinsic::x86_sse2_ucomilt_sd:
4888 case Intrinsic::x86_sse2_ucomile_sd:
4889 case Intrinsic::x86_sse2_ucomigt_sd:
4890 case Intrinsic::x86_sse2_ucomige_sd:
4891 case Intrinsic::x86_sse2_ucomineq_sd: {
4892 unsigned Opc = 0;
4893 ISD::CondCode CC = ISD::SETCC_INVALID;
4894 switch (IntNo) {
4895 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004896 case Intrinsic::x86_sse_comieq_ss:
4897 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004898 Opc = X86ISD::COMI;
4899 CC = ISD::SETEQ;
4900 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004901 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004902 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004903 Opc = X86ISD::COMI;
4904 CC = ISD::SETLT;
4905 break;
4906 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004907 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004908 Opc = X86ISD::COMI;
4909 CC = ISD::SETLE;
4910 break;
4911 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004912 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004913 Opc = X86ISD::COMI;
4914 CC = ISD::SETGT;
4915 break;
4916 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004917 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004918 Opc = X86ISD::COMI;
4919 CC = ISD::SETGE;
4920 break;
4921 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004922 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923 Opc = X86ISD::COMI;
4924 CC = ISD::SETNE;
4925 break;
4926 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004927 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 Opc = X86ISD::UCOMI;
4929 CC = ISD::SETEQ;
4930 break;
4931 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004932 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 Opc = X86ISD::UCOMI;
4934 CC = ISD::SETLT;
4935 break;
4936 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004937 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 Opc = X86ISD::UCOMI;
4939 CC = ISD::SETLE;
4940 break;
4941 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004942 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004943 Opc = X86ISD::UCOMI;
4944 CC = ISD::SETGT;
4945 break;
4946 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004947 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948 Opc = X86ISD::UCOMI;
4949 CC = ISD::SETGE;
4950 break;
4951 case Intrinsic::x86_sse_ucomineq_ss:
4952 case Intrinsic::x86_sse2_ucomineq_sd:
4953 Opc = X86ISD::UCOMI;
4954 CC = ISD::SETNE;
4955 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004956 }
Evan Cheng734503b2006-09-11 02:19:56 +00004957
Evan Cheng0db9fe62006-04-25 20:13:52 +00004958 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004959 SDOperand LHS = Op.getOperand(1);
4960 SDOperand RHS = Op.getOperand(2);
4961 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004962
Evan Chenge5f62042007-09-29 00:00:36 +00004963 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4964 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4965 DAG.getConstant(X86CC, MVT::i8), Cond);
4966 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004967 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004968 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004969}
Evan Cheng72261582005-12-20 06:22:03 +00004970
Nate Begemanbcc5f362007-01-29 22:58:52 +00004971SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4972 // Depths > 0 not supported yet!
4973 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4974 return SDOperand();
4975
4976 // Just load the return address
4977 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4978 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4979}
4980
4981SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4982 // Depths > 0 not supported yet!
4983 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4984 return SDOperand();
4985
4986 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4987 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner0bd48932008-01-17 07:00:52 +00004988 DAG.getIntPtrConstant(4));
Nate Begemanbcc5f362007-01-29 22:58:52 +00004989}
4990
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004991SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4992 SelectionDAG &DAG) {
4993 // Is not yet supported on x86-64
4994 if (Subtarget->is64Bit())
4995 return SDOperand();
4996
Chris Lattner0bd48932008-01-17 07:00:52 +00004997 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004998}
4999
5000SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5001{
5002 assert(!Subtarget->is64Bit() &&
5003 "Lowering of eh_return builtin is not supported yet on x86-64");
5004
5005 MachineFunction &MF = DAG.getMachineFunction();
5006 SDOperand Chain = Op.getOperand(0);
5007 SDOperand Offset = Op.getOperand(1);
5008 SDOperand Handler = Op.getOperand(2);
5009
5010 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5011 getPointerTy());
5012
5013 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00005014 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005015 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5016 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5017 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00005018 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005019
5020 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5021 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5022}
5023
Duncan Sandsb116fac2007-07-27 20:02:49 +00005024SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5025 SelectionDAG &DAG) {
5026 SDOperand Root = Op.getOperand(0);
5027 SDOperand Trmp = Op.getOperand(1); // trampoline
5028 SDOperand FPtr = Op.getOperand(2); // nested function
5029 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5030
Dan Gohman69de1932008-02-06 22:27:42 +00005031 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005032
Duncan Sands339e14f2008-01-16 22:55:25 +00005033 const X86InstrInfo *TII =
5034 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5035
Duncan Sandsb116fac2007-07-27 20:02:49 +00005036 if (Subtarget->is64Bit()) {
Duncan Sands339e14f2008-01-16 22:55:25 +00005037 SDOperand OutChains[6];
5038
5039 // Large code-model.
5040
5041 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5042 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5043
5044 const unsigned char N86R10 =
Dan Gohman60783302008-02-08 03:29:40 +00005045 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005046 const unsigned char N86R11 =
Dan Gohman60783302008-02-08 03:29:40 +00005047 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005048
5049 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5050
5051 // Load the pointer to the nested function into R11.
5052 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5053 SDOperand Addr = Trmp;
5054 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005055 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005056
5057 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005058 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005059
5060 // Load the 'nest' parameter value into R10.
5061 // R10 is specified in X86CallingConv.td
5062 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5063 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5064 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005065 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005066
5067 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005068 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005069
5070 // Jump to the nested function.
5071 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5072 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5073 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005074 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005075
5076 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5077 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5078 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005079 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005080
5081 SDOperand Ops[] =
5082 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5083 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005084 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005085 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005086 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5087 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005088 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005089
5090 switch (CC) {
5091 default:
5092 assert(0 && "Unsupported calling convention");
5093 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005094 case CallingConv::X86_StdCall: {
5095 // Pass 'nest' parameter in ECX.
5096 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005097 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005098
5099 // Check that ECX wasn't needed by an 'inreg' parameter.
5100 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00005101 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005102
5103 if (Attrs && !Func->isVarArg()) {
5104 unsigned InRegCount = 0;
5105 unsigned Idx = 1;
5106
5107 for (FunctionType::param_iterator I = FTy->param_begin(),
5108 E = FTy->param_end(); I != E; ++I, ++Idx)
5109 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5110 // FIXME: should only count parameters that are lowered to integers.
5111 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5112
5113 if (InRegCount > 2) {
5114 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5115 abort();
5116 }
5117 }
5118 break;
5119 }
5120 case CallingConv::X86_FastCall:
5121 // Pass 'nest' parameter in EAX.
5122 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005123 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005124 break;
5125 }
5126
5127 SDOperand OutChains[4];
5128 SDOperand Addr, Disp;
5129
5130 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5131 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5132
Duncan Sands339e14f2008-01-16 22:55:25 +00005133 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5134 const unsigned char N86Reg =
Dan Gohman60783302008-02-08 03:29:40 +00005135 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005136 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005137 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005138
5139 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005140 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005141
Duncan Sands339e14f2008-01-16 22:55:25 +00005142 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005143 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5144 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005145 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005146
5147 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005148 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005149
Duncan Sandsf7331b32007-09-11 14:10:23 +00005150 SDOperand Ops[] =
5151 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5152 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005153 }
5154}
5155
Dan Gohman1a024862008-01-31 00:41:03 +00005156SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005157 /*
5158 The rounding mode is in bits 11:10 of FPSR, and has the following
5159 settings:
5160 00 Round to nearest
5161 01 Round to -inf
5162 10 Round to +inf
5163 11 Round to 0
5164
5165 FLT_ROUNDS, on the other hand, expects the following:
5166 -1 Undefined
5167 0 Round to 0
5168 1 Round to nearest
5169 2 Round to +inf
5170 3 Round to -inf
5171
5172 To perform the conversion, we do:
5173 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5174 */
5175
5176 MachineFunction &MF = DAG.getMachineFunction();
5177 const TargetMachine &TM = MF.getTarget();
5178 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5179 unsigned StackAlignment = TFI.getStackAlignment();
5180 MVT::ValueType VT = Op.getValueType();
5181
5182 // Save FP Control Word to stack slot
5183 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5184 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5185
5186 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5187 DAG.getEntryNode(), StackSlot);
5188
5189 // Load FP Control Word from stack slot
5190 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5191
5192 // Transform as necessary
5193 SDOperand CWD1 =
5194 DAG.getNode(ISD::SRL, MVT::i16,
5195 DAG.getNode(ISD::AND, MVT::i16,
5196 CWD, DAG.getConstant(0x800, MVT::i16)),
5197 DAG.getConstant(11, MVT::i8));
5198 SDOperand CWD2 =
5199 DAG.getNode(ISD::SRL, MVT::i16,
5200 DAG.getNode(ISD::AND, MVT::i16,
5201 CWD, DAG.getConstant(0x400, MVT::i16)),
5202 DAG.getConstant(9, MVT::i8));
5203
5204 SDOperand RetVal =
5205 DAG.getNode(ISD::AND, MVT::i16,
5206 DAG.getNode(ISD::ADD, MVT::i16,
5207 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5208 DAG.getConstant(1, MVT::i16)),
5209 DAG.getConstant(3, MVT::i16));
5210
5211
5212 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5213 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5214}
5215
Evan Cheng18efe262007-12-14 02:13:44 +00005216SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5217 MVT::ValueType VT = Op.getValueType();
5218 MVT::ValueType OpVT = VT;
5219 unsigned NumBits = MVT::getSizeInBits(VT);
5220
5221 Op = Op.getOperand(0);
5222 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005223 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005224 OpVT = MVT::i32;
5225 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5226 }
Evan Cheng18efe262007-12-14 02:13:44 +00005227
Evan Cheng152804e2007-12-14 08:30:15 +00005228 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5229 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5230 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5231
5232 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5233 SmallVector<SDOperand, 4> Ops;
5234 Ops.push_back(Op);
5235 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5236 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5237 Ops.push_back(Op.getValue(1));
5238 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5239
5240 // Finally xor with NumBits-1.
5241 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5242
Evan Cheng18efe262007-12-14 02:13:44 +00005243 if (VT == MVT::i8)
5244 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5245 return Op;
5246}
5247
5248SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5249 MVT::ValueType VT = Op.getValueType();
5250 MVT::ValueType OpVT = VT;
Evan Cheng152804e2007-12-14 08:30:15 +00005251 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng18efe262007-12-14 02:13:44 +00005252
5253 Op = Op.getOperand(0);
5254 if (VT == MVT::i8) {
5255 OpVT = MVT::i32;
5256 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5257 }
Evan Cheng152804e2007-12-14 08:30:15 +00005258
5259 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5260 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5261 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5262
5263 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5264 SmallVector<SDOperand, 4> Ops;
5265 Ops.push_back(Op);
5266 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5267 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5268 Ops.push_back(Op.getValue(1));
5269 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5270
Evan Cheng18efe262007-12-14 02:13:44 +00005271 if (VT == MVT::i8)
5272 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5273 return Op;
5274}
5275
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276/// LowerOperation - Provide custom lowering hooks for some operations.
5277///
5278SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5279 switch (Op.getOpcode()) {
5280 default: assert(0 && "Should not custom lower this!");
5281 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5282 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5283 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5284 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5285 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5286 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5287 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005288 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005289 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5290 case ISD::SHL_PARTS:
5291 case ISD::SRA_PARTS:
5292 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5293 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5294 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5295 case ISD::FABS: return LowerFABS(Op, DAG);
5296 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005297 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005298 case ISD::SETCC: return LowerSETCC(Op, DAG);
5299 case ISD::SELECT: return LowerSELECT(Op, DAG);
5300 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005301 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005302 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005303 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005304 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005305 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5306 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005307 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005308 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005309 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005310 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5311 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005312 case ISD::FRAME_TO_ARGS_OFFSET:
5313 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005314 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005315 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005316 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005317 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005318 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5319 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005320
5321 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5322 case ISD::READCYCLECOUNTER:
5323 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005324 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005325}
5326
5327/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5328SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5329 switch (N->getOpcode()) {
5330 default: assert(0 && "Should not custom lower this!");
5331 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5332 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5333 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334}
5335
Evan Cheng72261582005-12-20 06:22:03 +00005336const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5337 switch (Opcode) {
5338 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005339 case X86ISD::BSF: return "X86ISD::BSF";
5340 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005341 case X86ISD::SHLD: return "X86ISD::SHLD";
5342 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005343 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005344 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005345 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005346 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005347 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005348 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005349 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5350 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5351 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005352 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005353 case X86ISD::FST: return "X86ISD::FST";
5354 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng0d9e9762008-01-29 19:34:22 +00005355 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Evan Chengb077b842005-12-21 02:39:21 +00005356 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005357 case X86ISD::CALL: return "X86ISD::CALL";
5358 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5359 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5360 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005361 case X86ISD::COMI: return "X86ISD::COMI";
5362 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005363 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005364 case X86ISD::CMOV: return "X86ISD::CMOV";
5365 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005366 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005367 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5368 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005369 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005370 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005371 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00005372 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005373 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5374 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00005375 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005376 case X86ISD::FMAX: return "X86ISD::FMAX";
5377 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005378 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5379 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005380 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5381 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005382 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005383 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005384 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng72261582005-12-20 06:22:03 +00005385 }
5386}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005387
Chris Lattnerc9addb72007-03-30 23:15:24 +00005388// isLegalAddressingMode - Return true if the addressing mode represented
5389// by AM is legal for this target, for a load/store of the specified type.
5390bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5391 const Type *Ty) const {
5392 // X86 supports extremely general addressing modes.
5393
5394 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5395 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5396 return false;
5397
5398 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005399 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005400 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5401 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005402
5403 // X86-64 only supports addr of globals in small code model.
5404 if (Subtarget->is64Bit()) {
5405 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5406 return false;
5407 // If lower 4G is not available, then we must use rip-relative addressing.
5408 if (AM.BaseOffs || AM.Scale > 1)
5409 return false;
5410 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005411 }
5412
5413 switch (AM.Scale) {
5414 case 0:
5415 case 1:
5416 case 2:
5417 case 4:
5418 case 8:
5419 // These scales always work.
5420 break;
5421 case 3:
5422 case 5:
5423 case 9:
5424 // These scales are formed with basereg+scalereg. Only accept if there is
5425 // no basereg yet.
5426 if (AM.HasBaseReg)
5427 return false;
5428 break;
5429 default: // Other stuff never works.
5430 return false;
5431 }
5432
5433 return true;
5434}
5435
5436
Evan Cheng2bd122c2007-10-26 01:56:11 +00005437bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5438 if (!Ty1->isInteger() || !Ty2->isInteger())
5439 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005440 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5441 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5442 if (NumBits1 <= NumBits2)
5443 return false;
5444 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005445}
5446
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005447bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5448 MVT::ValueType VT2) const {
5449 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5450 return false;
5451 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5452 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5453 if (NumBits1 <= NumBits2)
5454 return false;
5455 return Subtarget->is64Bit() || NumBits1 < 64;
5456}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005457
Evan Cheng60c07e12006-07-05 22:17:51 +00005458/// isShuffleMaskLegal - Targets can use this to indicate that they only
5459/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5460/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5461/// are assumed to be legal.
5462bool
5463X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5464 // Only do shuffles on 128-bit vector types for now.
5465 if (MVT::getSizeInBits(VT) == 64) return false;
5466 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005467 isIdentityMask(Mask.Val) ||
5468 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005469 isSplatMask(Mask.Val) ||
5470 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5471 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005472 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005473 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005474 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005475}
5476
5477bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5478 MVT::ValueType EVT,
5479 SelectionDAG &DAG) const {
5480 unsigned NumElts = BVOps.size();
5481 // Only do shuffles on 128-bit vector types for now.
5482 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5483 if (NumElts == 2) return true;
5484 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005485 return (isMOVLMask(&BVOps[0], 4) ||
5486 isCommutedMOVL(&BVOps[0], 4, true) ||
5487 isSHUFPMask(&BVOps[0], 4) ||
5488 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005489 }
5490 return false;
5491}
5492
5493//===----------------------------------------------------------------------===//
5494// X86 Scheduler Hooks
5495//===----------------------------------------------------------------------===//
5496
5497MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005498X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5499 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005500 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005501 switch (MI->getOpcode()) {
5502 default: assert(false && "Unexpected instr type to insert");
5503 case X86::CMOV_FR32:
5504 case X86::CMOV_FR64:
5505 case X86::CMOV_V4F32:
5506 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005507 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005508 // To "insert" a SELECT_CC instruction, we actually have to insert the
5509 // diamond control-flow pattern. The incoming instruction knows the
5510 // destination vreg to set, the condition code register to branch on, the
5511 // true/false values to select between, and a branch opcode to use.
5512 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5513 ilist<MachineBasicBlock>::iterator It = BB;
5514 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005515
Evan Cheng60c07e12006-07-05 22:17:51 +00005516 // thisMBB:
5517 // ...
5518 // TrueVal = ...
5519 // cmpTY ccX, r1, r2
5520 // bCC copy1MBB
5521 // fallthrough --> copy0MBB
5522 MachineBasicBlock *thisMBB = BB;
5523 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5524 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005525 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005526 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005527 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005528 MachineFunction *F = BB->getParent();
5529 F->getBasicBlockList().insert(It, copy0MBB);
5530 F->getBasicBlockList().insert(It, sinkMBB);
5531 // Update machine-CFG edges by first adding all successors of the current
5532 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005533 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005534 e = BB->succ_end(); i != e; ++i)
5535 sinkMBB->addSuccessor(*i);
5536 // Next, remove all successors of the current block, and add the true
5537 // and fallthrough blocks as its successors.
5538 while(!BB->succ_empty())
5539 BB->removeSuccessor(BB->succ_begin());
5540 BB->addSuccessor(copy0MBB);
5541 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005542
Evan Cheng60c07e12006-07-05 22:17:51 +00005543 // copy0MBB:
5544 // %FalseValue = ...
5545 // # fallthrough to sinkMBB
5546 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005547
Evan Cheng60c07e12006-07-05 22:17:51 +00005548 // Update machine-CFG edges
5549 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005550
Evan Cheng60c07e12006-07-05 22:17:51 +00005551 // sinkMBB:
5552 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5553 // ...
5554 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005555 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005556 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5557 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5558
5559 delete MI; // The pseudo instruction is gone now.
5560 return BB;
5561 }
5562
Dale Johannesen849f2142007-07-03 00:53:03 +00005563 case X86::FP32_TO_INT16_IN_MEM:
5564 case X86::FP32_TO_INT32_IN_MEM:
5565 case X86::FP32_TO_INT64_IN_MEM:
5566 case X86::FP64_TO_INT16_IN_MEM:
5567 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005568 case X86::FP64_TO_INT64_IN_MEM:
5569 case X86::FP80_TO_INT16_IN_MEM:
5570 case X86::FP80_TO_INT32_IN_MEM:
5571 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005572 // Change the floating point control register to use "round towards zero"
5573 // mode when truncating to an integer value.
5574 MachineFunction *F = BB->getParent();
5575 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005576 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005577
5578 // Load the old value of the high byte of the control word...
5579 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00005580 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005581 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005582
5583 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005584 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5585 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005586
5587 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005588 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005589
5590 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005591 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5592 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005593
5594 // Get the X86 opcode to use.
5595 unsigned Opc;
5596 switch (MI->getOpcode()) {
5597 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005598 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5599 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5600 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5601 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5602 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5603 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005604 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5605 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5606 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005607 }
5608
5609 X86AddressMode AM;
5610 MachineOperand &Op = MI->getOperand(0);
5611 if (Op.isRegister()) {
5612 AM.BaseType = X86AddressMode::RegBase;
5613 AM.Base.Reg = Op.getReg();
5614 } else {
5615 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00005616 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00005617 }
5618 Op = MI->getOperand(1);
5619 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005620 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005621 Op = MI->getOperand(2);
5622 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005623 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005624 Op = MI->getOperand(3);
5625 if (Op.isGlobalAddress()) {
5626 AM.GV = Op.getGlobal();
5627 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005628 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005629 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005630 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5631 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005632
5633 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005634 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005635
5636 delete MI; // The pseudo instruction is gone now.
5637 return BB;
5638 }
5639 }
5640}
5641
5642//===----------------------------------------------------------------------===//
5643// X86 Optimization Hooks
5644//===----------------------------------------------------------------------===//
5645
Nate Begeman368e18d2006-02-16 21:11:51 +00005646void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005647 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005648 APInt &KnownZero,
5649 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005650 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005651 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005652 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005653 assert((Opc >= ISD::BUILTIN_OP_END ||
5654 Opc == ISD::INTRINSIC_WO_CHAIN ||
5655 Opc == ISD::INTRINSIC_W_CHAIN ||
5656 Opc == ISD::INTRINSIC_VOID) &&
5657 "Should use MaskedValueIsZero if you don't know whether Op"
5658 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005659
Dan Gohmanf4f92f52008-02-13 23:07:24 +00005660 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005661 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005662 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005663 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005664 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5665 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00005666 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005667 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005668}
Chris Lattner259e97c2006-01-31 19:43:35 +00005669
Evan Cheng206ee9d2006-07-07 08:33:52 +00005670/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5671/// element of the result of the vector shuffle.
5672static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5673 MVT::ValueType VT = N->getValueType(0);
5674 SDOperand PermMask = N->getOperand(2);
5675 unsigned NumElems = PermMask.getNumOperands();
5676 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5677 i %= NumElems;
5678 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5679 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005680 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005681 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5682 SDOperand Idx = PermMask.getOperand(i);
5683 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005684 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005685 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5686 }
5687 return SDOperand();
5688}
5689
5690/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5691/// node is a GlobalAddress + an offset.
5692static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005693 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005694 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005695 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5696 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5697 return true;
5698 }
Evan Cheng0085a282006-11-30 21:55:46 +00005699 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005700 SDOperand N1 = N->getOperand(0);
5701 SDOperand N2 = N->getOperand(1);
5702 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5703 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5704 if (V) {
5705 Offset += V->getSignExtended();
5706 return true;
5707 }
5708 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5709 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5710 if (V) {
5711 Offset += V->getSignExtended();
5712 return true;
5713 }
5714 }
5715 }
5716 return false;
5717}
5718
5719/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5720/// + Dist * Size.
5721static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5722 MachineFrameInfo *MFI) {
5723 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5724 return false;
5725
5726 SDOperand Loc = N->getOperand(1);
5727 SDOperand BaseLoc = Base->getOperand(1);
5728 if (Loc.getOpcode() == ISD::FrameIndex) {
5729 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5730 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005731 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5732 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005733 int FS = MFI->getObjectSize(FI);
5734 int BFS = MFI->getObjectSize(BFI);
5735 if (FS != BFS || FS != Size) return false;
5736 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5737 } else {
5738 GlobalValue *GV1 = NULL;
5739 GlobalValue *GV2 = NULL;
5740 int64_t Offset1 = 0;
5741 int64_t Offset2 = 0;
5742 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5743 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5744 if (isGA1 && isGA2 && GV1 == GV2)
5745 return Offset1 == (Offset2 + Dist*Size);
5746 }
5747
5748 return false;
5749}
5750
Evan Cheng1e60c092006-07-10 21:37:44 +00005751static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5752 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005753 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00005754 int64_t Offset = 0;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005755 if (isGAPlusOffset(Base, GV, Offset))
5756 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00005757 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00005758 return false;
5759}
5760
5761
5762/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5763/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5764/// if the load addresses are consecutive, non-overlapping, and in the right
5765/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005766static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5767 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005768 MachineFunction &MF = DAG.getMachineFunction();
5769 MachineFrameInfo *MFI = MF.getFrameInfo();
5770 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005771 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005772 SDOperand PermMask = N->getOperand(2);
5773 int NumElems = (int)PermMask.getNumOperands();
5774 SDNode *Base = NULL;
5775 for (int i = 0; i < NumElems; ++i) {
5776 SDOperand Idx = PermMask.getOperand(i);
5777 if (Idx.getOpcode() == ISD::UNDEF) {
5778 if (!Base) return SDOperand();
5779 } else {
5780 SDOperand Arg =
5781 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005782 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005783 return SDOperand();
5784 if (!Base)
5785 Base = Arg.Val;
5786 else if (!isConsecutiveLoad(Arg.Val, Base,
5787 i, MVT::getSizeInBits(EVT)/8,MFI))
5788 return SDOperand();
5789 }
5790 }
5791
Evan Cheng1e60c092006-07-10 21:37:44 +00005792 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005793 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005794 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005795 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005796 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005797 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005798 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5799 LD->getSrcValueOffset(), LD->isVolatile(),
5800 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005801 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005802}
5803
Chris Lattner83e6c992006-10-04 06:57:07 +00005804/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5805static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5806 const X86Subtarget *Subtarget) {
5807 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005808
Chris Lattner83e6c992006-10-04 06:57:07 +00005809 // If we have SSE[12] support, try to form min/max nodes.
5810 if (Subtarget->hasSSE2() &&
5811 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5812 if (Cond.getOpcode() == ISD::SETCC) {
5813 // Get the LHS/RHS of the select.
5814 SDOperand LHS = N->getOperand(1);
5815 SDOperand RHS = N->getOperand(2);
5816 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005817
Evan Cheng8ca29322006-11-10 21:43:37 +00005818 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005819 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005820 switch (CC) {
5821 default: break;
5822 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5823 case ISD::SETULE:
5824 case ISD::SETLE:
5825 if (!UnsafeFPMath) break;
5826 // FALL THROUGH.
5827 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5828 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005829 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005830 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005831
Chris Lattner1907a7b2006-10-05 04:11:26 +00005832 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5833 case ISD::SETUGT:
5834 case ISD::SETGT:
5835 if (!UnsafeFPMath) break;
5836 // FALL THROUGH.
5837 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5838 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005839 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005840 break;
5841 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005842 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005843 switch (CC) {
5844 default: break;
5845 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5846 case ISD::SETUGT:
5847 case ISD::SETGT:
5848 if (!UnsafeFPMath) break;
5849 // FALL THROUGH.
5850 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5851 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005852 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005853 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005854
Chris Lattner1907a7b2006-10-05 04:11:26 +00005855 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5856 case ISD::SETULE:
5857 case ISD::SETLE:
5858 if (!UnsafeFPMath) break;
5859 // FALL THROUGH.
5860 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5861 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005862 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005863 break;
5864 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005865 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005866
Evan Cheng8ca29322006-11-10 21:43:37 +00005867 if (Opcode)
5868 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005869 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005870
Chris Lattner83e6c992006-10-04 06:57:07 +00005871 }
5872
5873 return SDOperand();
5874}
5875
Chris Lattner149a4e52008-02-22 02:09:43 +00005876/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5877static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5878 const X86Subtarget *Subtarget) {
5879 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5880 // the FP state in cases where an emms may be missing.
5881 if (MVT::isVector(St->getValue().getValueType()) &&
5882 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
5883 // Must be a store of a load.
5884 isa<LoadSDNode>(St->getChain()) &&
5885 St->getChain().Val == St->getValue().Val &&
5886 St->getValue().hasOneUse() && St->getChain().hasOneUse() &&
5887 !St->isVolatile() && !cast<LoadSDNode>(St->getChain())->isVolatile()) {
5888 LoadSDNode *Ld = cast<LoadSDNode>(St->getChain());
5889
5890 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
5891 if (Subtarget->is64Bit()) {
5892 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(), Ld->getBasePtr(),
5893 Ld->getSrcValue(), Ld->getSrcValueOffset(),
5894 Ld->isVolatile(), Ld->getAlignment());
5895 return DAG.getStore(NewLd.getValue(1), NewLd, St->getBasePtr(),
5896 St->getSrcValue(), St->getSrcValueOffset(),
5897 St->isVolatile(), St->getAlignment());
5898 }
5899
Chris Lattnerd2b1fb22008-02-22 05:18:04 +00005900 // Otherwise, lower to two 32-bit copies.
5901 SDOperand LoAddr = Ld->getBasePtr();
5902 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
5903 DAG.getConstant(MVT::i32, 4));
5904
5905 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
5906 Ld->getSrcValue(), Ld->getSrcValueOffset(),
5907 Ld->isVolatile(), Ld->getAlignment());
5908 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
5909 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
5910 Ld->isVolatile(),
5911 MinAlign(Ld->getAlignment(), 4));
5912
5913 LoAddr = St->getBasePtr();
5914 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
5915 DAG.getConstant(MVT::i32, 4));
5916
5917 SDOperand LoSt = DAG.getStore(LoLd.getValue(1), LoLd, LoAddr,
5918 St->getSrcValue(), St->getSrcValueOffset(),
5919 St->isVolatile(), St->getAlignment());
5920 SDOperand HiSt = DAG.getStore(HiLd.getValue(1), HiLd, HiAddr,
5921 St->getSrcValue(), St->getSrcValueOffset()+4,
5922 St->isVolatile(),
5923 MinAlign(St->getAlignment(), 4));
5924 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00005925 }
5926 return SDOperand();
5927}
5928
Chris Lattner6cf73262008-01-25 06:14:17 +00005929/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5930/// X86ISD::FXOR nodes.
Chris Lattneraf723b92008-01-25 05:46:26 +00005931static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00005932 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5933 // F[X]OR(0.0, x) -> x
5934 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00005935 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5936 if (C->getValueAPF().isPosZero())
5937 return N->getOperand(1);
5938 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5939 if (C->getValueAPF().isPosZero())
5940 return N->getOperand(0);
5941 return SDOperand();
5942}
5943
5944/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5945static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5946 // FAND(0.0, x) -> 0.0
5947 // FAND(x, 0.0) -> 0.0
5948 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5949 if (C->getValueAPF().isPosZero())
5950 return N->getOperand(0);
5951 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5952 if (C->getValueAPF().isPosZero())
5953 return N->getOperand(1);
5954 return SDOperand();
5955}
5956
Chris Lattner83e6c992006-10-04 06:57:07 +00005957
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005958SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005959 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005960 SelectionDAG &DAG = DCI.DAG;
5961 switch (N->getOpcode()) {
5962 default: break;
Chris Lattneraf723b92008-01-25 05:46:26 +00005963 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5964 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattner149a4e52008-02-22 02:09:43 +00005965 case ISD::STORE:
5966 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00005967 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00005968 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5969 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005970 }
5971
5972 return SDOperand();
5973}
5974
Evan Cheng60c07e12006-07-05 22:17:51 +00005975//===----------------------------------------------------------------------===//
5976// X86 Inline Assembly Support
5977//===----------------------------------------------------------------------===//
5978
Chris Lattnerf4dff842006-07-11 02:54:03 +00005979/// getConstraintType - Given a constraint letter, return the type of
5980/// constraint it is for this target.
5981X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005982X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5983 if (Constraint.size() == 1) {
5984 switch (Constraint[0]) {
5985 case 'A':
5986 case 'r':
5987 case 'R':
5988 case 'l':
5989 case 'q':
5990 case 'Q':
5991 case 'x':
5992 case 'Y':
5993 return C_RegisterClass;
5994 default:
5995 break;
5996 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005997 }
Chris Lattner4234f572007-03-25 02:14:49 +00005998 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005999}
6000
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006001/// LowerXConstraint - try to replace an X constraint, which matches anything,
6002/// with another that has more specific requirements based on the type of the
6003/// corresponding operand.
6004void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6005 std::string& s) const {
6006 if (MVT::isFloatingPoint(ConstraintVT)) {
6007 if (Subtarget->hasSSE2())
6008 s = "Y";
6009 else if (Subtarget->hasSSE1())
6010 s = "x";
6011 else
6012 s = "f";
6013 } else
6014 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6015}
6016
Chris Lattner48884cd2007-08-25 00:47:38 +00006017/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6018/// vector. If it is invalid, don't add anything to Ops.
6019void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6020 char Constraint,
6021 std::vector<SDOperand>&Ops,
6022 SelectionDAG &DAG) {
6023 SDOperand Result(0, 0);
6024
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006025 switch (Constraint) {
6026 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006027 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006029 if (C->getValue() <= 31) {
6030 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6031 break;
6032 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006033 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006034 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006035 case 'N':
6036 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006037 if (C->getValue() <= 255) {
6038 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6039 break;
6040 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006041 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006042 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006043 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006044 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006045 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6046 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6047 break;
6048 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006049
Chris Lattnerdc43a882007-05-03 16:52:29 +00006050 // If we are in non-pic codegen mode, we allow the address of a global (with
6051 // an optional displacement) to be used with 'i'.
6052 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6053 int64_t Offset = 0;
6054
6055 // Match either (GA) or (GA+C)
6056 if (GA) {
6057 Offset = GA->getOffset();
6058 } else if (Op.getOpcode() == ISD::ADD) {
6059 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6060 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6061 if (C && GA) {
6062 Offset = GA->getOffset()+C->getValue();
6063 } else {
6064 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6065 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6066 if (C && GA)
6067 Offset = GA->getOffset()+C->getValue();
6068 else
6069 C = 0, GA = 0;
6070 }
6071 }
6072
6073 if (GA) {
6074 // If addressing this global requires a load (e.g. in PIC mode), we can't
6075 // match.
6076 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6077 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006078 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006079
Chris Lattnerdc43a882007-05-03 16:52:29 +00006080 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6081 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006082 Result = Op;
6083 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006084 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006085
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006086 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006087 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006088 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006089 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006090
6091 if (Result.Val) {
6092 Ops.push_back(Result);
6093 return;
6094 }
6095 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006096}
6097
Chris Lattner259e97c2006-01-31 19:43:35 +00006098std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006099getRegClassForInlineAsmConstraint(const std::string &Constraint,
6100 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006101 if (Constraint.size() == 1) {
6102 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006103 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006104 default: break; // Unknown constraint letter
6105 case 'A': // EAX/EDX
6106 if (VT == MVT::i32 || VT == MVT::i64)
6107 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6108 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006109 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6110 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006111 if (VT == MVT::i32)
6112 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6113 else if (VT == MVT::i16)
6114 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6115 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006116 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006117 else if (VT == MVT::i64)
6118 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6119 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006120 }
6121 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006122
Chris Lattner1efa40f2006-02-22 00:56:39 +00006123 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006124}
Chris Lattnerf76d1802006-07-31 23:26:50 +00006125
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006126std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00006127X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6128 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00006129 // First, see if this is a constraint that directly corresponds to an LLVM
6130 // register class.
6131 if (Constraint.size() == 1) {
6132 // GCC Constraint Letters
6133 switch (Constraint[0]) {
6134 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006135 case 'r': // GENERAL_REGS
6136 case 'R': // LEGACY_REGS
6137 case 'l': // INDEX_REGS
6138 if (VT == MVT::i64 && Subtarget->is64Bit())
6139 return std::make_pair(0U, X86::GR64RegisterClass);
6140 if (VT == MVT::i32)
6141 return std::make_pair(0U, X86::GR32RegisterClass);
6142 else if (VT == MVT::i16)
6143 return std::make_pair(0U, X86::GR16RegisterClass);
6144 else if (VT == MVT::i8)
6145 return std::make_pair(0U, X86::GR8RegisterClass);
6146 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00006147 case 'y': // MMX_REGS if MMX allowed.
6148 if (!Subtarget->hasMMX()) break;
6149 return std::make_pair(0U, X86::VR64RegisterClass);
6150 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006151 case 'Y': // SSE_REGS if SSE2 allowed
6152 if (!Subtarget->hasSSE2()) break;
6153 // FALL THROUGH.
6154 case 'x': // SSE_REGS if SSE1 allowed
6155 if (!Subtarget->hasSSE1()) break;
6156
6157 switch (VT) {
6158 default: break;
6159 // Scalar SSE types.
6160 case MVT::f32:
6161 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00006162 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006163 case MVT::f64:
6164 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00006165 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006166 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00006167 case MVT::v16i8:
6168 case MVT::v8i16:
6169 case MVT::v4i32:
6170 case MVT::v2i64:
6171 case MVT::v4f32:
6172 case MVT::v2f64:
6173 return std::make_pair(0U, X86::VR128RegisterClass);
6174 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006175 break;
6176 }
6177 }
6178
Chris Lattnerf76d1802006-07-31 23:26:50 +00006179 // Use the default implementation in TargetLowering to convert the register
6180 // constraint into a member of a register class.
6181 std::pair<unsigned, const TargetRegisterClass*> Res;
6182 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006183
6184 // Not found as a standard register?
6185 if (Res.second == 0) {
6186 // GCC calls "st(0)" just plain "st".
6187 if (StringsEqualNoCase("{st}", Constraint)) {
6188 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006189 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006190 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006191
Chris Lattner1a60aa72006-10-31 19:42:44 +00006192 return Res;
6193 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006194
Chris Lattnerf76d1802006-07-31 23:26:50 +00006195 // Otherwise, check to see if this is a register class of the wrong value
6196 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6197 // turn into {ax},{dx}.
6198 if (Res.second->hasType(VT))
6199 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006200
Chris Lattnerf76d1802006-07-31 23:26:50 +00006201 // All of the single-register GCC register classes map their values onto
6202 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6203 // really want an 8-bit or 32-bit register, map to the appropriate register
6204 // class and return the appropriate register.
6205 if (Res.second != X86::GR16RegisterClass)
6206 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006207
Chris Lattnerf76d1802006-07-31 23:26:50 +00006208 if (VT == MVT::i8) {
6209 unsigned DestReg = 0;
6210 switch (Res.first) {
6211 default: break;
6212 case X86::AX: DestReg = X86::AL; break;
6213 case X86::DX: DestReg = X86::DL; break;
6214 case X86::CX: DestReg = X86::CL; break;
6215 case X86::BX: DestReg = X86::BL; break;
6216 }
6217 if (DestReg) {
6218 Res.first = DestReg;
6219 Res.second = Res.second = X86::GR8RegisterClass;
6220 }
6221 } else if (VT == MVT::i32) {
6222 unsigned DestReg = 0;
6223 switch (Res.first) {
6224 default: break;
6225 case X86::AX: DestReg = X86::EAX; break;
6226 case X86::DX: DestReg = X86::EDX; break;
6227 case X86::CX: DestReg = X86::ECX; break;
6228 case X86::BX: DestReg = X86::EBX; break;
6229 case X86::SI: DestReg = X86::ESI; break;
6230 case X86::DI: DestReg = X86::EDI; break;
6231 case X86::BP: DestReg = X86::EBP; break;
6232 case X86::SP: DestReg = X86::ESP; break;
6233 }
6234 if (DestReg) {
6235 Res.first = DestReg;
6236 Res.second = Res.second = X86::GR32RegisterClass;
6237 }
Evan Cheng25ab6902006-09-08 06:48:29 +00006238 } else if (VT == MVT::i64) {
6239 unsigned DestReg = 0;
6240 switch (Res.first) {
6241 default: break;
6242 case X86::AX: DestReg = X86::RAX; break;
6243 case X86::DX: DestReg = X86::RDX; break;
6244 case X86::CX: DestReg = X86::RCX; break;
6245 case X86::BX: DestReg = X86::RBX; break;
6246 case X86::SI: DestReg = X86::RSI; break;
6247 case X86::DI: DestReg = X86::RDI; break;
6248 case X86::BP: DestReg = X86::RBP; break;
6249 case X86::SP: DestReg = X86::RSP; break;
6250 }
6251 if (DestReg) {
6252 Res.first = DestReg;
6253 Res.second = Res.second = X86::GR64RegisterClass;
6254 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00006255 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006256
Chris Lattnerf76d1802006-07-31 23:26:50 +00006257 return Res;
6258}