blob: 85788ef90c735f53622ffcc3ead78399ea5e5fce [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Evan Cheng10e86422008-04-25 19:11:04 +000043// Forward declarations.
44static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
45
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000048 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000052
Chris Lattnerd43d00c2008-01-24 08:07:48 +000053 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
56
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057 // Set up the TargetLowering object.
58
59 // X86 is weird, it always uses i8 for shift amounts and setcc results.
60 setShiftAmountType(MVT::i8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000061 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);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 }
236
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000243 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000259 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000260 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
263
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000264 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
277 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Evan Chengd2cde682008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000290
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang63307c32008-05-05 19:05:59 +0000294 // Expand certain atomics
Mon P Wang28873102008-06-25 08:15:39 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i64, Custom);
299 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000300
Dan Gohman7f460202008-06-30 20:59:49 +0000301 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
302 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000303 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000304 if (!Subtarget->isTargetDarwin() &&
305 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000306 !Subtarget->isTargetCygMing()) {
307 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
308 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
309 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000310
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000311 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
312 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
315 if (Subtarget->is64Bit()) {
316 // FIXME: Verify
317 setExceptionPointerRegister(X86::RAX);
318 setExceptionSelectorRegister(X86::RDX);
319 } else {
320 setExceptionPointerRegister(X86::EAX);
321 setExceptionSelectorRegister(X86::EDX);
322 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000323 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000324
Duncan Sandsf7331b32007-09-11 14:10:23 +0000325 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000326
Chris Lattnerda68d302008-01-15 21:58:22 +0000327 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000328
Nate Begemanacc398c2006-01-25 18:21:52 +0000329 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
330 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000331 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000332 if (Subtarget->is64Bit()) {
333 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000334 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000335 } else {
336 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000337 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000338 }
Evan Chengae642192007-03-02 23:16:35 +0000339
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000340 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000341 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit())
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000344 if (Subtarget->isTargetCygMing())
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
346 else
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000348
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000349 if (X86ScalarSSEf64) {
350 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000351 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000352 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
353 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000354
Evan Cheng223547a2006-01-31 22:28:30 +0000355 // Use ANDPD to simulate FABS.
356 setOperationAction(ISD::FABS , MVT::f64, Custom);
357 setOperationAction(ISD::FABS , MVT::f32, Custom);
358
359 // Use XORP to simulate FNEG.
360 setOperationAction(ISD::FNEG , MVT::f64, Custom);
361 setOperationAction(ISD::FNEG , MVT::f32, Custom);
362
Evan Cheng68c47cb2007-01-05 07:55:56 +0000363 // Use ANDPD and ORPD to simulate FCOPYSIGN.
364 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
365 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
366
Evan Chengd25e9e82006-02-02 00:28:23 +0000367 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000368 setOperationAction(ISD::FSIN , MVT::f64, Expand);
369 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000370 setOperationAction(ISD::FSIN , MVT::f32, Expand);
371 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000372
Chris Lattnera54aa942006-01-29 06:26:08 +0000373 // Expand FP immediates into loads from the stack, except for the special
374 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000375 addLegalFPImmediate(APFloat(+0.0)); // xorpd
376 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000377
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000378 // Floating truncations from f80 and extensions to f80 go through memory.
379 // If optimizing, we lie about this though and handle it in
380 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
381 if (Fast) {
382 setConvertAction(MVT::f32, MVT::f80, Expand);
383 setConvertAction(MVT::f64, MVT::f80, Expand);
384 setConvertAction(MVT::f80, MVT::f32, Expand);
385 setConvertAction(MVT::f80, MVT::f64, Expand);
386 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000387 } else if (X86ScalarSSEf32) {
388 // Use SSE for f32, x87 for f64.
389 // Set up the FP register classes.
390 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
391 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
392
393 // Use ANDPS to simulate FABS.
394 setOperationAction(ISD::FABS , MVT::f32, Custom);
395
396 // Use XORP to simulate FNEG.
397 setOperationAction(ISD::FNEG , MVT::f32, Custom);
398
399 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
400
401 // Use ANDPS and ORPS to simulate FCOPYSIGN.
402 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
403 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
404
405 // We don't support sin/cos/fmod
406 setOperationAction(ISD::FSIN , MVT::f32, Expand);
407 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000408
Nate Begemane1795842008-02-14 08:57:00 +0000409 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000410 addLegalFPImmediate(APFloat(+0.0f)); // xorps
411 addLegalFPImmediate(APFloat(+0.0)); // FLD0
412 addLegalFPImmediate(APFloat(+1.0)); // FLD1
413 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
414 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
415
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000416 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
417 // this though and handle it in InstructionSelectPreprocess so that
418 // dagcombine2 can hack on these.
419 if (Fast) {
420 setConvertAction(MVT::f32, MVT::f64, Expand);
421 setConvertAction(MVT::f32, MVT::f80, Expand);
422 setConvertAction(MVT::f80, MVT::f32, Expand);
423 setConvertAction(MVT::f64, MVT::f32, Expand);
424 // And x87->x87 truncations also.
425 setConvertAction(MVT::f80, MVT::f64, Expand);
426 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427
428 if (!UnsafeFPMath) {
429 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
430 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
431 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000433 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000435 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
436 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000437
Evan Cheng68c47cb2007-01-05 07:55:56 +0000438 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000439 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000442
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000443 // Floating truncations go through memory. If optimizing, we lie about
444 // this though and handle it in InstructionSelectPreprocess so that
445 // dagcombine2 can hack on these.
446 if (Fast) {
447 setConvertAction(MVT::f80, MVT::f32, Expand);
448 setConvertAction(MVT::f64, MVT::f32, Expand);
449 setConvertAction(MVT::f80, MVT::f64, Expand);
450 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000451
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000464 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000465
Dale Johannesen59a58732007-08-05 18:49:15 +0000466 // Long double always uses X87.
467 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000468 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000470 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000471 APFloat TmpFlt(+0.0);
472 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
473 addLegalFPImmediate(TmpFlt); // FLD0
474 TmpFlt.changeSign();
475 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
476 APFloat TmpFlt2(+1.0);
477 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
478 addLegalFPImmediate(TmpFlt2); // FLD1
479 TmpFlt2.changeSign();
480 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
481 }
482
Dale Johannesen2f429012007-09-26 21:10:55 +0000483 if (!UnsafeFPMath) {
484 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
485 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
486 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000487
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000488 // Always use a library call for pow.
489 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
490 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
491 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
492
Evan Chengd30bf012006-03-01 01:11:20 +0000493 // First set operation action for all vector types to expand. Then we
494 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000495 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
496 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000497 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
498 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
499 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
500 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
501 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
502 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000535 }
536
Evan Chenga88973f2006-03-22 19:22:18 +0000537 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000538 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
539 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
540 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000541 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000542 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000543
Evan Chengd30bf012006-03-01 01:11:20 +0000544 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000545
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000546 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
547 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
548 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000549 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000550
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000551 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
552 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
553 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000554 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000555
Bill Wendling74027e92007-03-15 21:24:36 +0000556 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
557 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
558
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000559 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000560 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000561 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000562 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
563 setOperationAction(ISD::AND, MVT::v2i32, Promote);
564 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
565 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000566
567 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000568 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000569 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000570 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
571 setOperationAction(ISD::OR, MVT::v2i32, Promote);
572 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
573 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000574
575 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000576 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000577 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000578 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
579 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
580 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
581 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000582
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000583 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000584 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000585 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000586 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
587 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
588 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000589 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000591 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000592
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000593 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
594 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
595 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000596 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000597 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000598
599 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
601 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000603
604 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
605 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000607
608 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000609 }
610
Evan Chenga88973f2006-03-22 19:22:18 +0000611 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000612 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
613
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000614 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
615 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
616 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
617 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000618 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
619 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000620 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
621 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000623 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000624 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000625 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000626 }
627
Evan Chenga88973f2006-03-22 19:22:18 +0000628 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000629 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
630 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
631 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
632 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
633 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
634
Evan Chengf7c378e2006-04-10 07:23:14 +0000635 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
636 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
637 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000638 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000639 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
640 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
641 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000642 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000643 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000644 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
645 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
646 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
647 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000648 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
649 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000650
Nate Begeman30a0de92008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
652 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
653 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
654 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000655
Evan Chengf7c378e2006-04-10 07:23:14 +0000656 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
657 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000659 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000660 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000661
Evan Cheng2c3ae372006-04-12 21:21:57 +0000662 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000663 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
664 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000665 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000666 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000667 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000668 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000671 }
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000676 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000677 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000678 if (Subtarget->is64Bit()) {
679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000680 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000681 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000682
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000683 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000684 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
686 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
687 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
688 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
689 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
690 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
691 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
692 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
693 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
694 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000695 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000696
Chris Lattnerddf89562008-01-17 19:59:44 +0000697 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000698
Evan Cheng2c3ae372006-04-12 21:21:57 +0000699 // Custom lower v2i64 and v2f64 selects.
700 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000701 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000702 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000703 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000704
Evan Cheng470a6ad2006-02-22 02:26:30 +0000705 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000706
707 if (Subtarget->hasSSE41()) {
708 // FIXME: Do we need to handle scalar-to-vector here?
709 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000710 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000711
712 // i8 and i16 vectors are custom , because the source register and source
713 // source memory operand types are not the same width. f32 vectors are
714 // custom since the immediate controlling the insert encodes additional
715 // information.
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
720
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000725
726 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000727 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000729 }
730 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000731
Nate Begeman30a0de92008-07-17 16:51:19 +0000732 if (Subtarget->hasSSE42()) {
733 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
734 }
735
Evan Cheng6be2c582006-04-05 23:38:46 +0000736 // We want to custom lower some of our intrinsics.
737 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
738
Evan Cheng206ee9d2006-07-07 08:33:52 +0000739 // We have target-specific dag combine patterns for the following nodes:
740 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000741 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000742 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000743 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000744
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000745 computeRegisterProperties();
746
Evan Cheng87ed7162006-02-14 08:25:08 +0000747 // FIXME: These should be based on subtarget info. Plus, the values should
748 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000749 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
750 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
751 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000752 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000753 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000754}
755
Scott Michel5b8f82e2008-03-10 15:42:14 +0000756
Duncan Sands83ec4b62008-06-06 12:08:01 +0000757MVT X86TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000758 return MVT::i8;
759}
760
761
Evan Cheng29286502008-01-23 23:17:41 +0000762/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
763/// the desired ByVal argument alignment.
764static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
765 if (MaxAlign == 16)
766 return;
767 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
768 if (VTy->getBitWidth() == 128)
769 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000770 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
771 unsigned EltAlign = 0;
772 getMaxByValAlign(ATy->getElementType(), EltAlign);
773 if (EltAlign > MaxAlign)
774 MaxAlign = EltAlign;
775 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
776 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
777 unsigned EltAlign = 0;
778 getMaxByValAlign(STy->getElementType(i), EltAlign);
779 if (EltAlign > MaxAlign)
780 MaxAlign = EltAlign;
781 if (MaxAlign == 16)
782 break;
783 }
784 }
785 return;
786}
787
788/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
789/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000790/// that contain SSE vectors are placed at 16-byte boundaries while the rest
791/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000792unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
793 if (Subtarget->is64Bit())
794 return getTargetData()->getABITypeAlignment(Ty);
795 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000796 if (Subtarget->hasSSE1())
797 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000798 return Align;
799}
Chris Lattner2b02a442007-02-25 08:29:00 +0000800
Evan Chengf0df0312008-05-15 08:39:06 +0000801/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000802/// and store operations as a result of memset, memcpy, and memmove
803/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000804/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000806X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
807 bool isSrcConst, bool isSrcStr) const {
808 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
809 return MVT::v4i32;
810 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
811 return MVT::v4f32;
812 if (Subtarget->is64Bit() && Size >= 8)
813 return MVT::i64;
814 return MVT::i32;
815}
816
817
Evan Chengcc415862007-11-09 01:32:10 +0000818/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
819/// jumptable.
820SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
821 SelectionDAG &DAG) const {
822 if (usesGlobalOffsetTable())
823 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
824 if (!Subtarget->isPICStyleRIPRel())
825 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
826 return Table;
827}
828
Chris Lattner2b02a442007-02-25 08:29:00 +0000829//===----------------------------------------------------------------------===//
830// Return Value Calling Convention Implementation
831//===----------------------------------------------------------------------===//
832
Chris Lattner59ed56b2007-02-28 04:55:35 +0000833#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000834
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000835/// LowerRET - Lower an ISD::RET node.
836SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
837 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
838
Chris Lattner9774c912007-02-27 05:28:59 +0000839 SmallVector<CCValAssign, 16> RVLocs;
840 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000841 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
842 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000843 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000844
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000845 // If this is the first return lowered for this function, add the regs to the
846 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000847 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000848 for (unsigned i = 0; i != RVLocs.size(); ++i)
849 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000850 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000851 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000852 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000853
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000854 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000855 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000856 if (Chain.getOpcode() == X86ISD::TAILCALL) {
857 SDOperand TailCall = Chain;
858 SDOperand TargetAddress = TailCall.getOperand(1);
859 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000860 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000861 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
862 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
863 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
864 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
865 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000866 assert(StackAdjustment.getOpcode() == ISD::Constant &&
867 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000868
869 SmallVector<SDOperand,8> Operands;
870 Operands.push_back(Chain.getOperand(0));
871 Operands.push_back(TargetAddress);
872 Operands.push_back(StackAdjustment);
873 // Copy registers used by the call. Last operand is a flag so it is not
874 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000875 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000876 Operands.push_back(Chain.getOperand(i));
877 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000878 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
879 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000880 }
881
882 // Regular return.
883 SDOperand Flag;
884
Chris Lattner447ff682008-03-11 03:23:40 +0000885 SmallVector<SDOperand, 6> RetOps;
886 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
887 // Operand #1 = Bytes To Pop
888 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
889
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000890 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
892 CCValAssign &VA = RVLocs[i];
893 assert(VA.isRegLoc() && "Can only return in registers!");
894 SDOperand ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000895
Chris Lattner447ff682008-03-11 03:23:40 +0000896 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
897 // the RET instruction and handled by the FP Stackifier.
898 if (RVLocs[i].getLocReg() == X86::ST0 ||
899 RVLocs[i].getLocReg() == X86::ST1) {
900 // If this is a copy from an xmm register to ST(0), use an FPExtend to
901 // change the value to the FP stack register class.
902 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
903 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
904 RetOps.push_back(ValToCopy);
905 // Don't emit a copytoreg.
906 continue;
907 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000908
Chris Lattner8e6da152008-03-10 21:08:41 +0000909 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000910 Flag = Chain.getValue(1);
911 }
Dan Gohman61a92132008-04-21 23:59:07 +0000912
913 // The x86-64 ABI for returning structs by value requires that we copy
914 // the sret argument into %rax for the return. We saved the argument into
915 // a virtual register in the entry block, so now we copy the value out
916 // and into %rax.
917 if (Subtarget->is64Bit() &&
918 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
919 MachineFunction &MF = DAG.getMachineFunction();
920 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
921 unsigned Reg = FuncInfo->getSRetReturnReg();
922 if (!Reg) {
923 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
924 FuncInfo->setSRetReturnReg(Reg);
925 }
926 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
927
928 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
929 Flag = Chain.getValue(1);
930 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000931
Chris Lattner447ff682008-03-11 03:23:40 +0000932 RetOps[0] = Chain; // Update chain.
933
934 // Add the flag if we have it.
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000935 if (Flag.Val)
Chris Lattner447ff682008-03-11 03:23:40 +0000936 RetOps.push_back(Flag);
937
938 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000939}
940
941
Chris Lattner3085e152007-02-25 08:59:22 +0000942/// LowerCallResult - Lower the result values of an ISD::CALL into the
943/// appropriate copies out of appropriate physical registers. This assumes that
944/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
945/// being lowered. The returns a SDNode with the same number of values as the
946/// ISD::CALL.
947SDNode *X86TargetLowering::
948LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
949 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000950
951 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000952 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000953 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
954 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000955 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
956
Chris Lattnere32bbf62007-02-28 07:09:55 +0000957 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000958
959 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +0000960 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000961 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +0000962
963 // If this is a call to a function that returns an fp value on the floating
964 // point stack, but where we prefer to use the value in xmm registers, copy
965 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
966 if (RVLocs[i].getLocReg() == X86::ST0 &&
967 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
968 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +0000969 }
Chris Lattner3085e152007-02-25 08:59:22 +0000970
Chris Lattner8e6da152008-03-10 21:08:41 +0000971 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
972 CopyVT, InFlag).getValue(1);
973 SDOperand Val = Chain.getValue(0);
974 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000975
Chris Lattner8e6da152008-03-10 21:08:41 +0000976 if (CopyVT != RVLocs[i].getValVT()) {
977 // Round the F80 the right size, which also moves to the appropriate xmm
978 // register.
979 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
980 // This truncation won't change the value.
981 DAG.getIntPtrConstant(1));
982 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000983
Chris Lattner8e6da152008-03-10 21:08:41 +0000984 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +0000985 }
Duncan Sands4bdcb612008-07-02 17:40:58 +0000986
Chris Lattner3085e152007-02-25 08:59:22 +0000987 // Merge everything together with a MERGE_VALUES node.
988 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +0000989 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
990 ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000991}
992
993
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000994//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000995// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000996//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000997// StdCall calling convention seems to be standard for many Windows' API
998// routines and around. It differs from C calling convention just a little:
999// callee should clean up the stack, not caller. Symbols should be also
1000// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001001// For info on fast calling convention see Fast Calling Convention (tail call)
1002// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001003
Evan Cheng85e38002006-04-27 05:35:28 +00001004/// AddLiveIn - This helper function adds the specified physical register to the
1005/// MachineFunction as a live in value. It also creates a corresponding virtual
1006/// register for it.
1007static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001008 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001009 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001010 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1011 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001012 return VReg;
1013}
1014
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001015/// CallIsStructReturn - Determines whether a CALL node uses struct return
1016/// semantics.
Gordon Henriksen86737662008-01-05 16:56:59 +00001017static bool CallIsStructReturn(SDOperand Op) {
1018 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1019 if (!NumOps)
1020 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001021
1022 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001023}
1024
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001025/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1026/// return semantics.
Gordon Henriksen86737662008-01-05 16:56:59 +00001027static bool ArgsAreStructReturn(SDOperand Op) {
1028 unsigned NumArgs = Op.Val->getNumValues() - 1;
1029 if (!NumArgs)
1030 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001031
1032 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001033}
1034
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001035/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1036/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001037/// calls.
Gordon Henriksen86737662008-01-05 16:56:59 +00001038bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1039 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1040 if (IsVarArg)
1041 return false;
1042
1043 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1044 default:
1045 return false;
1046 case CallingConv::X86_StdCall:
1047 return !Subtarget->is64Bit();
1048 case CallingConv::X86_FastCall:
1049 return !Subtarget->is64Bit();
1050 case CallingConv::Fast:
1051 return PerformTailCallOpt;
1052 }
1053}
1054
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001055/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1056/// FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001057CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1058 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1059
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001060 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001061 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001062 return CC_X86_Win64_C;
1063 else {
1064 if (CC == CallingConv::Fast && PerformTailCallOpt)
1065 return CC_X86_64_TailCall;
1066 else
1067 return CC_X86_64_C;
1068 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001069 }
1070
Gordon Henriksen86737662008-01-05 16:56:59 +00001071 if (CC == CallingConv::X86_FastCall)
1072 return CC_X86_32_FastCall;
1073 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1074 return CC_X86_32_TailCall;
1075 else
1076 return CC_X86_32_C;
1077}
1078
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001079/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1080/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001081NameDecorationStyle
1082X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1083 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1084 if (CC == CallingConv::X86_FastCall)
1085 return FastCall;
1086 else if (CC == CallingConv::X86_StdCall)
1087 return StdCall;
1088 return None;
1089}
1090
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001091
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001092/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1093/// in a register before calling.
1094bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1095 return !IsTailCall && !Is64Bit &&
1096 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1097 Subtarget->isPICStyleGOT();
1098}
1099
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001100/// CallRequiresFnAddressInReg - Check whether the call requires the function
1101/// address to be loaded in a register.
1102bool
1103X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1104 return !Is64Bit && IsTailCall &&
1105 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1106 Subtarget->isPICStyleGOT();
1107}
1108
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001109/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1110/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001111/// the specific parameter attribute. The copy will be passed as a byval
1112/// function parameter.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001113static SDOperand
Evan Cheng8e5712b2008-01-12 01:08:07 +00001114CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001115 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001116 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001117 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001118 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001119}
1120
Rafael Espindola7effac52007-09-14 15:48:13 +00001121SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1122 const CCValAssign &VA,
1123 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001124 unsigned CC,
Rafael Espindola7effac52007-09-14 15:48:13 +00001125 SDOperand Root, unsigned i) {
1126 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001127 ISD::ArgFlagsTy Flags =
1128 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001129 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001130 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001131
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001132 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1133 // changed with more analysis.
1134 // In case of tail call optimization mark all arguments mutable. Since they
1135 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001136 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001137 VA.getLocMemOffset(), isImmutable);
Rafael Espindola7effac52007-09-14 15:48:13 +00001138 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001139 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001140 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001141 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001142 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001143}
1144
Gordon Henriksen86737662008-01-05 16:56:59 +00001145SDOperand
1146X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001147 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001148 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1149
1150 const Function* Fn = MF.getFunction();
1151 if (Fn->hasExternalLinkage() &&
1152 Subtarget->isTargetCygMing() &&
1153 Fn->getName() == "main")
1154 FuncInfo->setForceFramePointer(true);
1155
1156 // Decorate the function name.
1157 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1158
Evan Cheng1bc78042006-04-26 01:20:17 +00001159 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001160 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001161 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001162 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001163 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001164 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001165
1166 assert(!(isVarArg && CC == CallingConv::Fast) &&
1167 "Var args not supported with calling convention fastcc");
1168
Chris Lattner638402b2007-02-28 07:00:42 +00001169 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001170 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001171 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001172 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001173
Chris Lattnerf39f7712007-02-28 05:46:49 +00001174 SmallVector<SDOperand, 8> ArgValues;
1175 unsigned LastVal = ~0U;
1176 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1177 CCValAssign &VA = ArgLocs[i];
1178 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1179 // places.
1180 assert(VA.getValNo() != LastVal &&
1181 "Don't support value assigned to multiple locs yet");
1182 LastVal = VA.getValNo();
1183
1184 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001185 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001186 TargetRegisterClass *RC;
1187 if (RegVT == MVT::i32)
1188 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001189 else if (Is64Bit && RegVT == MVT::i64)
1190 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001191 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001192 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001193 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001194 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001195 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001196 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001197 else if (RegVT.isVector()) {
1198 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001199 if (!Is64Bit)
1200 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1201 else {
1202 // Darwin calling convention passes MMX values in either GPRs or
1203 // XMMs in x86-64. Other targets pass them in memory.
1204 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1205 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1206 RegVT = MVT::v2i64;
1207 } else {
1208 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1209 RegVT = MVT::i64;
1210 }
1211 }
1212 } else {
1213 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001214 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001215
Chris Lattner82932a52007-03-02 05:12:29 +00001216 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1217 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001218
1219 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1220 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1221 // right size.
1222 if (VA.getLocInfo() == CCValAssign::SExt)
1223 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1224 DAG.getValueType(VA.getValVT()));
1225 else if (VA.getLocInfo() == CCValAssign::ZExt)
1226 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228
1229 if (VA.getLocInfo() != CCValAssign::Full)
1230 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1231
Gordon Henriksen86737662008-01-05 16:56:59 +00001232 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001233 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001234 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001235 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1236 else if (RC == X86::VR128RegisterClass) {
1237 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1238 DAG.getConstant(0, MVT::i64));
1239 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1240 }
1241 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001242
Chris Lattnerf39f7712007-02-28 05:46:49 +00001243 ArgValues.push_back(ArgValue);
1244 } else {
1245 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001246 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001247 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001248 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001249
Dan Gohman61a92132008-04-21 23:59:07 +00001250 // The x86-64 ABI for returning structs by value requires that we copy
1251 // the sret argument into %rax for the return. Save the argument into
1252 // a virtual register so that we can access it from the return points.
1253 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1254 MachineFunction &MF = DAG.getMachineFunction();
1255 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1256 unsigned Reg = FuncInfo->getSRetReturnReg();
1257 if (!Reg) {
1258 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1259 FuncInfo->setSRetReturnReg(Reg);
1260 }
1261 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1262 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1263 }
1264
Chris Lattnerf39f7712007-02-28 05:46:49 +00001265 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001266 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001267 if (CC == CallingConv::Fast)
1268 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001269
Evan Cheng1bc78042006-04-26 01:20:17 +00001270 // If the function takes variable number of arguments, make a frame index for
1271 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001272 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001273 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1274 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1275 }
1276 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001277 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1278
1279 // FIXME: We should really autogenerate these arrays
1280 static const unsigned GPR64ArgRegsWin64[] = {
1281 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001282 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001283 static const unsigned XMMArgRegsWin64[] = {
1284 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1285 };
1286 static const unsigned GPR64ArgRegs64Bit[] = {
1287 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1288 };
1289 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001290 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1291 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1292 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001293 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1294
1295 if (IsWin64) {
1296 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1297 GPR64ArgRegs = GPR64ArgRegsWin64;
1298 XMMArgRegs = XMMArgRegsWin64;
1299 } else {
1300 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1301 GPR64ArgRegs = GPR64ArgRegs64Bit;
1302 XMMArgRegs = XMMArgRegs64Bit;
1303 }
1304 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1305 TotalNumIntRegs);
1306 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1307 TotalNumXMMRegs);
1308
Gordon Henriksen86737662008-01-05 16:56:59 +00001309 // For X86-64, if there are vararg parameters that are passed via
1310 // registers, then we must store them to their spots on the stack so they
1311 // may be loaded by deferencing the result of va_next.
1312 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001313 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1314 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1315 TotalNumXMMRegs * 16, 16);
1316
Gordon Henriksen86737662008-01-05 16:56:59 +00001317 // Store the integer parameter registers.
1318 SmallVector<SDOperand, 8> MemOps;
1319 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1320 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001321 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001322 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001323 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1324 X86::GR64RegisterClass);
1325 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman69de1932008-02-06 22:27:42 +00001326 SDOperand Store =
1327 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001328 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001329 MemOps.push_back(Store);
1330 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001331 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001332 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001333
Gordon Henriksen86737662008-01-05 16:56:59 +00001334 // Now store the XMM (fp + vector) parameter registers.
1335 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001336 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001337 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001338 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1339 X86::VR128RegisterClass);
1340 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman69de1932008-02-06 22:27:42 +00001341 SDOperand Store =
1342 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001343 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001344 MemOps.push_back(Store);
1345 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001346 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001347 }
1348 if (!MemOps.empty())
1349 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1350 &MemOps[0], MemOps.size());
1351 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001352 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001353
1354 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1355 // arguments and the arguments after the retaddr has been pushed are
1356 // aligned.
1357 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1358 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1359 (StackSize & 7) == 0)
1360 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001361
Gordon Henriksenae636f82008-01-03 16:47:34 +00001362 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001363
Gordon Henriksen86737662008-01-05 16:56:59 +00001364 // Some CCs need callee pop.
1365 if (IsCalleePop(Op)) {
1366 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001367 BytesCallerReserves = 0;
1368 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001369 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001370 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001371 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001372 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001373 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001374 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001375
Gordon Henriksen86737662008-01-05 16:56:59 +00001376 if (!Is64Bit) {
1377 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1378 if (CC == CallingConv::X86_FastCall)
1379 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1380 }
Evan Cheng25caf632006-05-23 21:06:34 +00001381
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001382 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001383
Evan Cheng25caf632006-05-23 21:06:34 +00001384 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +00001385 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1386 ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001387}
1388
Evan Chengdffbd832008-01-10 00:09:10 +00001389SDOperand
1390X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1391 const SDOperand &StackPtr,
1392 const CCValAssign &VA,
1393 SDOperand Chain,
1394 SDOperand Arg) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001395 unsigned LocMemOffset = VA.getLocMemOffset();
1396 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001397 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001398 ISD::ArgFlagsTy Flags =
1399 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1400 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001401 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001402 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001403 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001404 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001405}
1406
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001407/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1408/// optimization is performed and it is required.
1409SDOperand
1410X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1411 SDOperand &OutRetAddr,
1412 SDOperand Chain,
1413 bool IsTailCall,
1414 bool Is64Bit,
1415 int FPDiff) {
1416 if (!IsTailCall || FPDiff==0) return Chain;
1417
1418 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001419 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001420 OutRetAddr = getReturnAddressFrameIndex(DAG);
1421 // Load the "old" Return address.
1422 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1423 return SDOperand(OutRetAddr.Val, 1);
1424}
1425
1426/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1427/// optimization is performed and it is required (FPDiff!=0).
1428static SDOperand
1429EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1430 SDOperand Chain, SDOperand RetAddrFrIdx,
1431 bool Is64Bit, int FPDiff) {
1432 // Store the return address to the appropriate stack slot.
1433 if (!FPDiff) return Chain;
1434 // Calculate the new stack slot for the return address.
1435 int SlotSize = Is64Bit ? 8 : 4;
1436 int NewReturnAddrFI =
1437 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001438 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001439 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1440 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001441 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001442 return Chain;
1443}
1444
Gordon Henriksen86737662008-01-05 16:56:59 +00001445SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1446 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng32fe1032006-05-25 00:59:30 +00001447 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001448 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001449 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001450 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1451 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng32fe1032006-05-25 00:59:30 +00001452 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001453 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001454 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001455
1456 assert(!(isVarArg && CC == CallingConv::Fast) &&
1457 "Var args not supported with calling convention fastcc");
1458
Chris Lattner638402b2007-02-28 07:00:42 +00001459 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001460 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001461 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner920c37a2008-03-21 06:50:21 +00001462 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001463
Chris Lattner423c5f42007-02-28 05:31:48 +00001464 // Get a count of how many bytes are to be pushed on the stack.
1465 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001466 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001467 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001468
Gordon Henriksen86737662008-01-05 16:56:59 +00001469 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1470 // arguments and the arguments after the retaddr has been pushed are aligned.
1471 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1472 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1473 (NumBytes & 7) == 0)
1474 NumBytes += 4;
1475
1476 int FPDiff = 0;
1477 if (IsTailCall) {
1478 // Lower arguments at fp - stackoffset + fpdiff.
1479 unsigned NumBytesCallerPushed =
1480 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1481 FPDiff = NumBytesCallerPushed - NumBytes;
1482
1483 // Set the delta of movement of the returnaddr stackslot.
1484 // But only set if delta is greater than previous delta.
1485 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1486 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1487 }
1488
Chris Lattner0bd48932008-01-17 07:00:52 +00001489 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001490
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001491 SDOperand RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001492 // Load return adress for tail calls.
1493 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1494 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001495
Chris Lattner5a88b832007-02-25 07:10:00 +00001496 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1497 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattner423c5f42007-02-28 05:31:48 +00001498 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001499
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001500 // Walk the register/memloc assignments, inserting copies/loads. In the case
1501 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001502 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1503 CCValAssign &VA = ArgLocs[i];
1504 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001505 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1506 getArgFlags().isByVal();
1507
Chris Lattner423c5f42007-02-28 05:31:48 +00001508 // Promote the value if needed.
1509 switch (VA.getLocInfo()) {
1510 default: assert(0 && "Unknown loc info!");
1511 case CCValAssign::Full: break;
1512 case CCValAssign::SExt:
1513 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1514 break;
1515 case CCValAssign::ZExt:
1516 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1517 break;
1518 case CCValAssign::AExt:
1519 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1520 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001521 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001522
1523 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001524 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001525 MVT RegVT = VA.getLocVT();
1526 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001527 switch (VA.getLocReg()) {
1528 default:
1529 break;
1530 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1531 case X86::R8: {
1532 // Special case: passing MMX values in GPR registers.
1533 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1534 break;
1535 }
1536 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1537 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1538 // Special case: passing MMX values in XMM registers.
1539 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1540 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1541 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1542 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1543 getMOVLMask(2, DAG));
1544 break;
1545 }
1546 }
1547 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001548 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1549 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001550 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001551 assert(VA.isMemLoc());
1552 if (StackPtr.Val == 0)
1553 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1554
1555 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1556 Arg));
1557 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001559 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001560
Evan Cheng32fe1032006-05-25 00:59:30 +00001561 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001562 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1563 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001564
Evan Cheng347d5f72006-04-28 21:29:37 +00001565 // Build a sequence of copy-to-reg nodes chained together with token chain
1566 // and flag operands which copy the outgoing args into registers.
1567 SDOperand InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001568 // Tail call byval lowering might overwrite argument registers so in case of
1569 // tail call optimization the copies to registers are lowered later.
1570 if (!IsTailCall)
1571 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1572 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1573 InFlag);
1574 InFlag = Chain.getValue(1);
1575 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001576
Evan Chengf4684712007-02-21 21:18:14 +00001577 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001578 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001579 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1580 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1581 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1582 InFlag);
1583 InFlag = Chain.getValue(1);
1584 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001585 // If we are tail calling and generating PIC/GOT style code load the address
1586 // of the callee into ecx. The value in ecx is used as target of the tail
1587 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1588 // calls on PIC/GOT architectures. Normally we would just put the address of
1589 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1590 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001591 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001592 // Note: The actual moving to ecx is done further down.
1593 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1594 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1595 !G->getGlobal()->hasProtectedVisibility())
1596 Callee = LowerGlobalAddress(Callee, DAG);
1597 else if (isa<ExternalSymbolSDNode>(Callee))
1598 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001599 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001600
Gordon Henriksen86737662008-01-05 16:56:59 +00001601 if (Is64Bit && isVarArg) {
1602 // From AMD64 ABI document:
1603 // For calls that may call functions that use varargs or stdargs
1604 // (prototype-less calls or calls to functions containing ellipsis (...) in
1605 // the declaration) %al is used as hidden argument to specify the number
1606 // of SSE registers used. The contents of %al do not need to match exactly
1607 // the number of registers, but must be an ubound on the number of SSE
1608 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001609
1610 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001611 // Count the number of XMM registers allocated.
1612 static const unsigned XMMArgRegs[] = {
1613 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1614 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1615 };
1616 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1617
1618 Chain = DAG.getCopyToReg(Chain, X86::AL,
1619 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1620 InFlag = Chain.getValue(1);
1621 }
1622
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001623
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001624 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001625 if (IsTailCall) {
1626 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen86737662008-01-05 16:56:59 +00001627 SDOperand FIN;
1628 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001629 // Do not flag preceeding copytoreg stuff together with the following stuff.
1630 InFlag = SDOperand();
Gordon Henriksen86737662008-01-05 16:56:59 +00001631 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1632 CCValAssign &VA = ArgLocs[i];
1633 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001634 assert(VA.isMemLoc());
1635 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001636 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001637 ISD::ArgFlagsTy Flags =
1638 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 // Create frame index.
1640 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001641 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001643 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001644
Duncan Sands276dcbd2008-03-21 09:14:45 +00001645 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001646 // Copy relative to framepointer.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001647 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1648 if (StackPtr.Val == 0)
1649 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1650 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1651
1652 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001653 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001655 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001656 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001657 DAG.getStore(Chain, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001658 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001659 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 }
1661 }
1662
1663 if (!MemOpChains2.empty())
1664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001665 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001666
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001667 // Copy arguments to their registers.
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1669 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1670 InFlag);
1671 InFlag = Chain.getValue(1);
1672 }
1673 InFlag =SDOperand();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001674
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001676 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1677 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 }
1679
Evan Cheng32fe1032006-05-25 00:59:30 +00001680 // If the callee is a GlobalAddress node (quite common, every direct call is)
1681 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001683 // We should use extra load for direct calls to dllimported functions in
1684 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001685 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1686 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001687 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng817a6a92008-07-16 01:34:02 +00001689 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 } else if (IsTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1692
1693 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001694 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 Callee,InFlag);
1696 Callee = DAG.getRegister(Opc, getPointerTy());
1697 // Add register as live out.
1698 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699 }
1700
Chris Lattnerd96d0722007-02-25 06:40:16 +00001701 // Returns a chain & a flag for retval copy to use.
1702 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001703 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001704
1705 if (IsTailCall) {
1706 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001707 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1708 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 if (InFlag.Val)
1710 Ops.push_back(InFlag);
1711 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1713
1714 // Returns a chain & a flag for retval copy to use.
1715 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1716 Ops.clear();
1717 }
1718
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001721
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 if (IsTailCall)
1723 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001724
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 // Add argument registers to the end of the list so that they are known live
1726 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001727 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1728 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1729 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001730
Evan Cheng586ccac2008-03-18 23:36:35 +00001731 // Add an implicit use GOT pointer in EBX.
1732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1736
1737 // Add an implicit use of AL for x86 vararg functions.
1738 if (Is64Bit && isVarArg)
1739 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1740
Evan Cheng347d5f72006-04-28 21:29:37 +00001741 if (InFlag.Val)
1742 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001743
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 if (IsTailCall) {
1745 assert(InFlag.Val &&
1746 "Flag must be set. Depend on flag being set in LowerRET");
1747 Chain = DAG.getNode(X86ISD::TAILCALL,
1748 Op.Val->getVTList(), &Ops[0], Ops.size());
1749
1750 return SDOperand(Chain.Val, Op.ResNo);
1751 }
1752
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001753 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001754 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001755
Chris Lattner2d297092006-05-23 18:50:38 +00001756 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 unsigned NumBytesForCalleeToPush;
1758 if (IsCalleePop(Op))
1759 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001760 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 // If this is is a call to a struct-return function, the callee
1762 // pops the hidden struct pointer, so we have to push it back.
1763 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001767
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001769 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001770 DAG.getIntPtrConstant(NumBytes),
1771 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001772 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001773 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001774
Chris Lattner3085e152007-02-25 08:59:22 +00001775 // Handle result values, copying them out of physregs into vregs that we
1776 // return.
Chris Lattner920c37a2008-03-21 06:50:21 +00001777 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001778}
1779
Evan Cheng25ab6902006-09-08 06:48:29 +00001780
1781//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001782// Fast Calling Convention (tail call) implementation
1783//===----------------------------------------------------------------------===//
1784
1785// Like std call, callee cleans arguments, convention except that ECX is
1786// reserved for storing the tail called function address. Only 2 registers are
1787// free for argument passing (inreg). Tail call optimization is performed
1788// provided:
1789// * tailcallopt is enabled
1790// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001791// On X86_64 architecture with GOT-style position independent code only local
1792// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001793// To keep the stack aligned according to platform abi the function
1794// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1795// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001796// If a tail called function callee has more arguments than the caller the
1797// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001798// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001799// original REtADDR, but before the saved framepointer or the spilled registers
1800// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1801// stack layout:
1802// arg1
1803// arg2
1804// RETADDR
1805// [ new RETADDR
1806// move area ]
1807// (possible EBP)
1808// ESI
1809// EDI
1810// local1 ..
1811
1812/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1813/// for a 16 byte align requirement.
1814unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1815 SelectionDAG& DAG) {
1816 if (PerformTailCallOpt) {
1817 MachineFunction &MF = DAG.getMachineFunction();
1818 const TargetMachine &TM = MF.getTarget();
1819 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1820 unsigned StackAlignment = TFI.getStackAlignment();
1821 uint64_t AlignMask = StackAlignment - 1;
1822 int64_t Offset = StackSize;
1823 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1824 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1825 // Number smaller than 12 so just add the difference.
1826 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1827 } else {
1828 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1829 Offset = ((~AlignMask) & Offset) + StackAlignment +
1830 (StackAlignment-SlotSize);
1831 }
1832 StackSize = Offset;
1833 }
1834 return StackSize;
1835}
1836
1837/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001838/// following the call is a return. A function is eligible if caller/callee
1839/// calling conventions match, currently only fastcc supports tail calls, and
1840/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001841bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1842 SDOperand Ret,
1843 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001844 if (!PerformTailCallOpt)
1845 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001846
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001847 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001848 MachineFunction &MF = DAG.getMachineFunction();
1849 unsigned CallerCC = MF.getFunction()->getCallingConv();
1850 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1851 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1852 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001853 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001854 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001855 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001856 return true;
1857
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001858 // Can only do local tail calls (in same module, hidden or protected) on
1859 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1861 return G->getGlobal()->hasHiddenVisibility()
1862 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001863 }
1864 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001865
1866 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001867}
1868
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001869//===----------------------------------------------------------------------===//
1870// Other Lowering Hooks
1871//===----------------------------------------------------------------------===//
1872
1873
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001874SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001875 MachineFunction &MF = DAG.getMachineFunction();
1876 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1877 int ReturnAddrIndex = FuncInfo->getRAIndex();
1878
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001879 if (ReturnAddrIndex == 0) {
1880 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001881 if (Subtarget->is64Bit())
1882 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1883 else
1884 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001885
1886 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001887 }
1888
Evan Cheng25ab6902006-09-08 06:48:29 +00001889 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001890}
1891
1892
1893
Evan Cheng6dfa9992006-01-30 23:41:35 +00001894/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1895/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001896/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1897/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001898static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001899 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1900 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001901 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001902 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001903 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1904 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1905 // X > -1 -> X == 0, jump !sign.
1906 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001907 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001908 return true;
1909 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1910 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001911 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001912 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001913 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1914 // X < 1 -> X <= 0
1915 RHS = DAG.getConstant(0, RHS.getValueType());
1916 X86CC = X86::COND_LE;
1917 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001918 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001919 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001920
Evan Chengd9558e02006-01-06 00:43:03 +00001921 switch (SetCCOpcode) {
1922 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001923 case ISD::SETEQ: X86CC = X86::COND_E; break;
1924 case ISD::SETGT: X86CC = X86::COND_G; break;
1925 case ISD::SETGE: X86CC = X86::COND_GE; break;
1926 case ISD::SETLT: X86CC = X86::COND_L; break;
1927 case ISD::SETLE: X86CC = X86::COND_LE; break;
1928 case ISD::SETNE: X86CC = X86::COND_NE; break;
1929 case ISD::SETULT: X86CC = X86::COND_B; break;
1930 case ISD::SETUGT: X86CC = X86::COND_A; break;
1931 case ISD::SETULE: X86CC = X86::COND_BE; break;
1932 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001933 }
1934 } else {
1935 // On a floating point condition, the flags are set as follows:
1936 // ZF PF CF op
1937 // 0 | 0 | 0 | X > Y
1938 // 0 | 0 | 1 | X < Y
1939 // 1 | 0 | 0 | X == Y
1940 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001941 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001942 switch (SetCCOpcode) {
1943 default: break;
1944 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001945 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001946 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001947 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001948 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001949 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001950 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001951 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001952 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001953 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001954 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001955 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001956 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001957 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001958 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001959 case ISD::SETNE: X86CC = X86::COND_NE; break;
1960 case ISD::SETUO: X86CC = X86::COND_P; break;
1961 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001962 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001963 if (Flip)
1964 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001965 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001966
Chris Lattner7fbe9722006-10-20 17:42:20 +00001967 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001968}
1969
Evan Cheng4a460802006-01-11 00:33:36 +00001970/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1971/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001972/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001973static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001974 switch (X86CC) {
1975 default:
1976 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001977 case X86::COND_B:
1978 case X86::COND_BE:
1979 case X86::COND_E:
1980 case X86::COND_P:
1981 case X86::COND_A:
1982 case X86::COND_AE:
1983 case X86::COND_NE:
1984 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001985 return true;
1986 }
1987}
1988
Evan Cheng5ced1d82006-04-06 23:23:56 +00001989/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001990/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001991static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1992 if (Op.getOpcode() == ISD::UNDEF)
1993 return true;
1994
1995 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001996 return (Val >= Low && Val < Hi);
1997}
1998
1999/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2000/// true if Op is undef or if its value equal to the specified value.
2001static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2002 if (Op.getOpcode() == ISD::UNDEF)
2003 return true;
2004 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002005}
2006
Evan Cheng0188ecb2006-03-22 18:59:22 +00002007/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2008/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2009bool X86::isPSHUFDMask(SDNode *N) {
2010 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2011
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002012 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002013 return false;
2014
2015 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002016 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002017 SDOperand Arg = N->getOperand(i);
2018 if (Arg.getOpcode() == ISD::UNDEF) continue;
2019 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002020 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002021 return false;
2022 }
2023
2024 return true;
2025}
2026
2027/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002028/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002029bool X86::isPSHUFHWMask(SDNode *N) {
2030 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2031
2032 if (N->getNumOperands() != 8)
2033 return false;
2034
2035 // Lower quadword copied in order.
2036 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002037 SDOperand Arg = N->getOperand(i);
2038 if (Arg.getOpcode() == ISD::UNDEF) continue;
2039 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2040 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002041 return false;
2042 }
2043
2044 // Upper quadword shuffled.
2045 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002046 SDOperand Arg = N->getOperand(i);
2047 if (Arg.getOpcode() == ISD::UNDEF) continue;
2048 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2049 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002050 if (Val < 4 || Val > 7)
2051 return false;
2052 }
2053
2054 return true;
2055}
2056
2057/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002058/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002059bool X86::isPSHUFLWMask(SDNode *N) {
2060 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2061
2062 if (N->getNumOperands() != 8)
2063 return false;
2064
2065 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002066 for (unsigned i = 4; i != 8; ++i)
2067 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002068 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002069
2070 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002071 for (unsigned i = 0; i != 4; ++i)
2072 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002073 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002074
2075 return true;
2076}
2077
Evan Cheng14aed5e2006-03-24 01:18:28 +00002078/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2079/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002080static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002081 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002082
Evan Cheng39623da2006-04-20 08:58:49 +00002083 unsigned Half = NumElems / 2;
2084 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002085 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002086 return false;
2087 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002088 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002089 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002090
2091 return true;
2092}
2093
Evan Cheng39623da2006-04-20 08:58:49 +00002094bool X86::isSHUFPMask(SDNode *N) {
2095 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002096 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002097}
2098
Evan Cheng213d2cf2007-05-17 18:45:50 +00002099/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002100/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2101/// half elements to come from vector 1 (which would equal the dest.) and
2102/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002103static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002104 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002105
Chris Lattner5a88b832007-02-25 07:10:00 +00002106 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002107 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002108 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002109 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002110 for (unsigned i = Half; i < NumOps; ++i)
2111 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002112 return false;
2113 return true;
2114}
2115
2116static bool isCommutedSHUFP(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002118 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002119}
2120
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002121/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2122/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2123bool X86::isMOVHLPSMask(SDNode *N) {
2124 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2125
Evan Cheng2064a2b2006-03-28 06:50:32 +00002126 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002127 return false;
2128
Evan Cheng2064a2b2006-03-28 06:50:32 +00002129 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002130 return isUndefOrEqual(N->getOperand(0), 6) &&
2131 isUndefOrEqual(N->getOperand(1), 7) &&
2132 isUndefOrEqual(N->getOperand(2), 2) &&
2133 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002134}
2135
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002136/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2137/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2138/// <2, 3, 2, 3>
2139bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141
2142 if (N->getNumOperands() != 4)
2143 return false;
2144
2145 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2146 return isUndefOrEqual(N->getOperand(0), 2) &&
2147 isUndefOrEqual(N->getOperand(1), 3) &&
2148 isUndefOrEqual(N->getOperand(2), 2) &&
2149 isUndefOrEqual(N->getOperand(3), 3);
2150}
2151
Evan Cheng5ced1d82006-04-06 23:23:56 +00002152/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2153/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2154bool X86::isMOVLPMask(SDNode *N) {
2155 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2156
2157 unsigned NumElems = N->getNumOperands();
2158 if (NumElems != 2 && NumElems != 4)
2159 return false;
2160
Evan Chengc5cdff22006-04-07 21:53:05 +00002161 for (unsigned i = 0; i < NumElems/2; ++i)
2162 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002164
Evan Chengc5cdff22006-04-07 21:53:05 +00002165 for (unsigned i = NumElems/2; i < NumElems; ++i)
2166 if (!isUndefOrEqual(N->getOperand(i), i))
2167 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002168
2169 return true;
2170}
2171
2172/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002173/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2174/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002175bool X86::isMOVHPMask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178 unsigned NumElems = N->getNumOperands();
2179 if (NumElems != 2 && NumElems != 4)
2180 return false;
2181
Evan Chengc5cdff22006-04-07 21:53:05 +00002182 for (unsigned i = 0; i < NumElems/2; ++i)
2183 if (!isUndefOrEqual(N->getOperand(i), i))
2184 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002185
2186 for (unsigned i = 0; i < NumElems/2; ++i) {
2187 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002188 if (!isUndefOrEqual(Arg, i + NumElems))
2189 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002190 }
2191
2192 return true;
2193}
2194
Evan Cheng0038e592006-03-28 00:39:58 +00002195/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2196/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002197bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002198 bool V2IsSplat = false) {
2199 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002200 return false;
2201
Chris Lattner5a88b832007-02-25 07:10:00 +00002202 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2203 SDOperand BitI = Elts[i];
2204 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002205 if (!isUndefOrEqual(BitI, j))
2206 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002207 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002208 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002209 return false;
2210 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002211 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002212 return false;
2213 }
Evan Cheng0038e592006-03-28 00:39:58 +00002214 }
2215
2216 return true;
2217}
2218
Evan Cheng39623da2006-04-20 08:58:49 +00002219bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2220 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002221 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002222}
2223
Evan Cheng4fcb9222006-03-28 02:43:26 +00002224/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2225/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002226bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002227 bool V2IsSplat = false) {
2228 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002229 return false;
2230
Chris Lattner5a88b832007-02-25 07:10:00 +00002231 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2232 SDOperand BitI = Elts[i];
2233 SDOperand BitI1 = Elts[i+1];
2234 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002235 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002236 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002237 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002238 return false;
2239 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002240 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002241 return false;
2242 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002243 }
2244
2245 return true;
2246}
2247
Evan Cheng39623da2006-04-20 08:58:49 +00002248bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2249 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002250 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002251}
2252
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002253/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2254/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2255/// <0, 0, 1, 1>
2256bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2258
2259 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002260 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002261 return false;
2262
2263 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2264 SDOperand BitI = N->getOperand(i);
2265 SDOperand BitI1 = N->getOperand(i+1);
2266
Evan Chengc5cdff22006-04-07 21:53:05 +00002267 if (!isUndefOrEqual(BitI, j))
2268 return false;
2269 if (!isUndefOrEqual(BitI1, j))
2270 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002271 }
2272
2273 return true;
2274}
2275
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002276/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2277/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2278/// <2, 2, 3, 3>
2279bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2280 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2281
2282 unsigned NumElems = N->getNumOperands();
2283 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2284 return false;
2285
2286 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2287 SDOperand BitI = N->getOperand(i);
2288 SDOperand BitI1 = N->getOperand(i + 1);
2289
2290 if (!isUndefOrEqual(BitI, j))
2291 return false;
2292 if (!isUndefOrEqual(BitI1, j))
2293 return false;
2294 }
2295
2296 return true;
2297}
2298
Evan Cheng017dcc62006-04-21 01:05:10 +00002299/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2300/// specifies a shuffle of elements that is suitable for input to MOVSS,
2301/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002302static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002303 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002304 return false;
2305
Chris Lattner5a88b832007-02-25 07:10:00 +00002306 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002307 return false;
2308
Chris Lattner5a88b832007-02-25 07:10:00 +00002309 for (unsigned i = 1; i < NumElts; ++i) {
2310 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002311 return false;
2312 }
2313
2314 return true;
2315}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002316
Evan Cheng017dcc62006-04-21 01:05:10 +00002317bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002318 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002319 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002320}
2321
Evan Cheng017dcc62006-04-21 01:05:10 +00002322/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2323/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002324/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002325static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002326 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002327 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002328 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002329 return false;
2330
2331 if (!isUndefOrEqual(Ops[0], 0))
2332 return false;
2333
Chris Lattner5a88b832007-02-25 07:10:00 +00002334 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002335 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002336 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2337 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2338 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002339 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002340 }
2341
2342 return true;
2343}
2344
Evan Cheng8cf723d2006-09-08 01:50:06 +00002345static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2346 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002347 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002348 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2349 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002350}
2351
Evan Chengd9539472006-04-14 21:59:03 +00002352/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2353/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2354bool X86::isMOVSHDUPMask(SDNode *N) {
2355 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2356
2357 if (N->getNumOperands() != 4)
2358 return false;
2359
2360 // Expect 1, 1, 3, 3
2361 for (unsigned i = 0; i < 2; ++i) {
2362 SDOperand Arg = N->getOperand(i);
2363 if (Arg.getOpcode() == ISD::UNDEF) continue;
2364 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2365 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2366 if (Val != 1) return false;
2367 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002368
2369 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002370 for (unsigned i = 2; i < 4; ++i) {
2371 SDOperand Arg = N->getOperand(i);
2372 if (Arg.getOpcode() == ISD::UNDEF) continue;
2373 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2374 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2375 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002376 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002377 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002378
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002379 // Don't use movshdup if it can be done with a shufps.
2380 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002381}
2382
2383/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2384/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2385bool X86::isMOVSLDUPMask(SDNode *N) {
2386 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2387
2388 if (N->getNumOperands() != 4)
2389 return false;
2390
2391 // Expect 0, 0, 2, 2
2392 for (unsigned i = 0; i < 2; ++i) {
2393 SDOperand Arg = N->getOperand(i);
2394 if (Arg.getOpcode() == ISD::UNDEF) continue;
2395 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2396 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2397 if (Val != 0) return false;
2398 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002399
2400 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002401 for (unsigned i = 2; i < 4; ++i) {
2402 SDOperand Arg = N->getOperand(i);
2403 if (Arg.getOpcode() == ISD::UNDEF) continue;
2404 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2405 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2406 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002407 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002408 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002409
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002410 // Don't use movshdup if it can be done with a shufps.
2411 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002412}
2413
Evan Cheng49892af2007-06-19 00:02:56 +00002414/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2415/// specifies a identity operation on the LHS or RHS.
2416static bool isIdentityMask(SDNode *N, bool RHS = false) {
2417 unsigned NumElems = N->getNumOperands();
2418 for (unsigned i = 0; i < NumElems; ++i)
2419 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2420 return false;
2421 return true;
2422}
2423
Evan Chengb9df0ca2006-03-22 02:53:00 +00002424/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2425/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002426static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428
Evan Chengb9df0ca2006-03-22 02:53:00 +00002429 // This is a splat operation if each element of the permute is the same, and
2430 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002431 unsigned NumElems = N->getNumOperands();
2432 SDOperand ElementBase;
2433 unsigned i = 0;
2434 for (; i != NumElems; ++i) {
2435 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002436 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002437 ElementBase = Elt;
2438 break;
2439 }
2440 }
2441
2442 if (!ElementBase.Val)
2443 return false;
2444
2445 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002446 SDOperand Arg = N->getOperand(i);
2447 if (Arg.getOpcode() == ISD::UNDEF) continue;
2448 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002449 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002450 }
2451
2452 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002453 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002454}
2455
Evan Chengc575ca22006-04-17 20:43:08 +00002456/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2457/// a splat of a single element and it's a 2 or 4 element mask.
2458bool X86::isSplatMask(SDNode *N) {
2459 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2460
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002461 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002462 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2463 return false;
2464 return ::isSplatMask(N);
2465}
2466
Evan Chengf686d9b2006-10-27 21:08:32 +00002467/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2468/// specifies a splat of zero element.
2469bool X86::isSplatLoMask(SDNode *N) {
2470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2471
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002472 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002473 if (!isUndefOrEqual(N->getOperand(i), 0))
2474 return false;
2475 return true;
2476}
2477
Evan Cheng63d33002006-03-22 08:01:21 +00002478/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2479/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2480/// instructions.
2481unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002482 unsigned NumOperands = N->getNumOperands();
2483 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2484 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002485 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002486 unsigned Val = 0;
2487 SDOperand Arg = N->getOperand(NumOperands-i-1);
2488 if (Arg.getOpcode() != ISD::UNDEF)
2489 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002490 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002491 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002492 if (i != NumOperands - 1)
2493 Mask <<= Shift;
2494 }
Evan Cheng63d33002006-03-22 08:01:21 +00002495
2496 return Mask;
2497}
2498
Evan Cheng506d3df2006-03-29 23:07:14 +00002499/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2500/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2501/// instructions.
2502unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2503 unsigned Mask = 0;
2504 // 8 nodes, but we only care about the last 4.
2505 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002506 unsigned Val = 0;
2507 SDOperand Arg = N->getOperand(i);
2508 if (Arg.getOpcode() != ISD::UNDEF)
2509 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002510 Mask |= (Val - 4);
2511 if (i != 4)
2512 Mask <<= 2;
2513 }
2514
2515 return Mask;
2516}
2517
2518/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2519/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2520/// instructions.
2521unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2522 unsigned Mask = 0;
2523 // 8 nodes, but we only care about the first 4.
2524 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002525 unsigned Val = 0;
2526 SDOperand Arg = N->getOperand(i);
2527 if (Arg.getOpcode() != ISD::UNDEF)
2528 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002529 Mask |= Val;
2530 if (i != 0)
2531 Mask <<= 2;
2532 }
2533
2534 return Mask;
2535}
2536
Evan Chengc21a0532006-04-05 01:47:37 +00002537/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2538/// specifies a 8 element shuffle that can be broken into a pair of
2539/// PSHUFHW and PSHUFLW.
2540static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2542
2543 if (N->getNumOperands() != 8)
2544 return false;
2545
2546 // Lower quadword shuffled.
2547 for (unsigned i = 0; i != 4; ++i) {
2548 SDOperand Arg = N->getOperand(i);
2549 if (Arg.getOpcode() == ISD::UNDEF) continue;
2550 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2551 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002552 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002553 return false;
2554 }
2555
2556 // Upper quadword shuffled.
2557 for (unsigned i = 4; i != 8; ++i) {
2558 SDOperand Arg = N->getOperand(i);
2559 if (Arg.getOpcode() == ISD::UNDEF) continue;
2560 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2561 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2562 if (Val < 4 || Val > 7)
2563 return false;
2564 }
2565
2566 return true;
2567}
2568
Chris Lattner8a594482007-11-25 00:24:49 +00002569/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002570/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002571static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2572 SDOperand &V2, SDOperand &Mask,
2573 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002574 MVT VT = Op.getValueType();
2575 MVT MaskVT = Mask.getValueType();
2576 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002577 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002578 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002579
2580 for (unsigned i = 0; i != NumElems; ++i) {
2581 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002582 if (Arg.getOpcode() == ISD::UNDEF) {
2583 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2584 continue;
2585 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002586 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2587 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2588 if (Val < NumElems)
2589 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2590 else
2591 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2592 }
2593
Evan Cheng9eca5e82006-10-25 21:49:50 +00002594 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002595 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002596 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002597}
2598
Evan Cheng779ccea2007-12-07 21:30:01 +00002599/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2600/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002601static
2602SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 MVT MaskVT = Mask.getValueType();
2604 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002605 unsigned NumElems = Mask.getNumOperands();
2606 SmallVector<SDOperand, 8> MaskVec;
2607 for (unsigned i = 0; i != NumElems; ++i) {
2608 SDOperand Arg = Mask.getOperand(i);
2609 if (Arg.getOpcode() == ISD::UNDEF) {
2610 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2611 continue;
2612 }
2613 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2614 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2615 if (Val < NumElems)
2616 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2617 else
2618 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2619 }
2620 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2621}
2622
2623
Evan Cheng533a0aa2006-04-19 20:35:22 +00002624/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2625/// match movhlps. The lower half elements should come from upper half of
2626/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002627/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002628static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2629 unsigned NumElems = Mask->getNumOperands();
2630 if (NumElems != 4)
2631 return false;
2632 for (unsigned i = 0, e = 2; i != e; ++i)
2633 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2634 return false;
2635 for (unsigned i = 2; i != 4; ++i)
2636 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2637 return false;
2638 return true;
2639}
2640
Evan Cheng5ced1d82006-04-06 23:23:56 +00002641/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002642/// is promoted to a vector. It also returns the LoadSDNode by reference if
2643/// required.
2644static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng533a0aa2006-04-19 20:35:22 +00002645 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2646 N = N->getOperand(0).Val;
Evan Cheng7e2ff772008-05-08 00:57:18 +00002647 if (ISD::isNON_EXTLoad(N)) {
2648 if (LD)
2649 *LD = cast<LoadSDNode>(N);
2650 return true;
2651 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002652 }
2653 return false;
2654}
2655
Evan Cheng533a0aa2006-04-19 20:35:22 +00002656/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2657/// match movlp{s|d}. The lower half elements should come from lower half of
2658/// V1 (and in order), and the upper half elements should come from the upper
2659/// half of V2 (and in order). And since V1 will become the source of the
2660/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002661static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002662 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002663 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002664 // Is V2 is a vector load, don't do this transformation. We will try to use
2665 // load folding shufps op.
2666 if (ISD::isNON_EXTLoad(V2))
2667 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002668
Evan Cheng533a0aa2006-04-19 20:35:22 +00002669 unsigned NumElems = Mask->getNumOperands();
2670 if (NumElems != 2 && NumElems != 4)
2671 return false;
2672 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2673 if (!isUndefOrEqual(Mask->getOperand(i), i))
2674 return false;
2675 for (unsigned i = NumElems/2; i != NumElems; ++i)
2676 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2677 return false;
2678 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002679}
2680
Evan Cheng39623da2006-04-20 08:58:49 +00002681/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2682/// all the same.
2683static bool isSplatVector(SDNode *N) {
2684 if (N->getOpcode() != ISD::BUILD_VECTOR)
2685 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002686
Evan Cheng39623da2006-04-20 08:58:49 +00002687 SDOperand SplatValue = N->getOperand(0);
2688 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2689 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002690 return false;
2691 return true;
2692}
2693
Evan Cheng8cf723d2006-09-08 01:50:06 +00002694/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2695/// to an undef.
2696static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002697 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002698 return false;
2699
2700 SDOperand V1 = N->getOperand(0);
2701 SDOperand V2 = N->getOperand(1);
2702 SDOperand Mask = N->getOperand(2);
2703 unsigned NumElems = Mask.getNumOperands();
2704 for (unsigned i = 0; i != NumElems; ++i) {
2705 SDOperand Arg = Mask.getOperand(i);
2706 if (Arg.getOpcode() != ISD::UNDEF) {
2707 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2708 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2709 return false;
2710 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2711 return false;
2712 }
2713 }
2714 return true;
2715}
2716
Evan Cheng213d2cf2007-05-17 18:45:50 +00002717/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2718/// constant +0.0.
2719static inline bool isZeroNode(SDOperand Elt) {
2720 return ((isa<ConstantSDNode>(Elt) &&
2721 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2722 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002723 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002724}
2725
2726/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2727/// to an zero vector.
2728static bool isZeroShuffle(SDNode *N) {
2729 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2730 return false;
2731
2732 SDOperand V1 = N->getOperand(0);
2733 SDOperand V2 = N->getOperand(1);
2734 SDOperand Mask = N->getOperand(2);
2735 unsigned NumElems = Mask.getNumOperands();
2736 for (unsigned i = 0; i != NumElems; ++i) {
2737 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002738 if (Arg.getOpcode() == ISD::UNDEF)
2739 continue;
2740
2741 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2742 if (Idx < NumElems) {
2743 unsigned Opc = V1.Val->getOpcode();
2744 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2745 continue;
2746 if (Opc != ISD::BUILD_VECTOR ||
2747 !isZeroNode(V1.Val->getOperand(Idx)))
2748 return false;
2749 } else if (Idx >= NumElems) {
2750 unsigned Opc = V2.Val->getOpcode();
2751 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2752 continue;
2753 if (Opc != ISD::BUILD_VECTOR ||
2754 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2755 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002756 }
2757 }
2758 return true;
2759}
2760
2761/// getZeroVector - Returns a vector of specified type with all zero elements.
2762///
Duncan Sands83ec4b62008-06-06 12:08:01 +00002763static SDOperand getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
2764 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002765
2766 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2767 // type. This ensures they get CSE'd.
Chris Lattner8a594482007-11-25 00:24:49 +00002768 SDOperand Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002769 if (VT.getSizeInBits() == 64) { // MMX
Evan Chengf0df0312008-05-15 08:39:06 +00002770 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002771 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002772 } else if (HasSSE2) { // SSE2
2773 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002774 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002775 } else { // SSE1
2776 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2778 }
Chris Lattner8a594482007-11-25 00:24:49 +00002779 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002780}
2781
Chris Lattner8a594482007-11-25 00:24:49 +00002782/// getOnesVector - Returns a vector of specified type with all bits set.
2783///
Duncan Sands83ec4b62008-06-06 12:08:01 +00002784static SDOperand getOnesVector(MVT VT, SelectionDAG &DAG) {
2785 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002786
2787 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2788 // type. This ensures they get CSE'd.
2789 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2790 SDOperand Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002791 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002792 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2793 else // SSE
2794 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2795 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2796}
2797
2798
Evan Cheng39623da2006-04-20 08:58:49 +00002799/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2800/// that point to V2 points to its first element.
2801static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2802 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2803
2804 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002805 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002806 unsigned NumElems = Mask.getNumOperands();
2807 for (unsigned i = 0; i != NumElems; ++i) {
2808 SDOperand Arg = Mask.getOperand(i);
2809 if (Arg.getOpcode() != ISD::UNDEF) {
2810 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2811 if (Val > NumElems) {
2812 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2813 Changed = true;
2814 }
2815 }
2816 MaskVec.push_back(Arg);
2817 }
2818
2819 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002820 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2821 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002822 return Mask;
2823}
2824
Evan Cheng017dcc62006-04-21 01:05:10 +00002825/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2826/// operation of specified width.
2827static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2829 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002830
Chris Lattner5a88b832007-02-25 07:10:00 +00002831 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002832 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2833 for (unsigned i = 1; i != NumElems; ++i)
2834 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002835 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002836}
2837
Evan Chengc575ca22006-04-17 20:43:08 +00002838/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2839/// of specified width.
2840static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002841 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2842 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00002843 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002844 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2845 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2846 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2847 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002848 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002849}
2850
Evan Cheng39623da2006-04-20 08:58:49 +00002851/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2852/// of specified width.
2853static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002854 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2855 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002856 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002857 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002858 for (unsigned i = 0; i != Half; ++i) {
2859 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2860 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2861 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002862 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002863}
2864
Chris Lattner62098042008-03-09 01:05:04 +00002865/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2866/// element #0 of a vector with the specified index, leaving the rest of the
2867/// elements in place.
2868static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2869 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002870 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2871 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner62098042008-03-09 01:05:04 +00002872 SmallVector<SDOperand, 8> MaskVec;
2873 // Element #0 of the result gets the elt we are replacing.
2874 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2875 for (unsigned i = 1; i != NumElems; ++i)
2876 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2877 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2878}
2879
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002880/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2881static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2883 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002884 if (PVT == VT)
2885 return Op;
Evan Chengc575ca22006-04-17 20:43:08 +00002886 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002887 SDOperand Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002888 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002889 // Special handling of v4f32 -> v4i32.
2890 if (VT != MVT::v4f32) {
2891 Mask = getUnpacklMask(NumElems, DAG);
2892 while (NumElems > 4) {
2893 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2894 NumElems >>= 1;
2895 }
Evan Chengf0df0312008-05-15 08:39:06 +00002896 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002897 }
Evan Chengc575ca22006-04-17 20:43:08 +00002898
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002899 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2900 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2901 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002902 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2903}
2904
Evan Chengba05f722006-04-21 23:03:30 +00002905/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002906/// vector of zero or undef vector. This produces a shuffle where the low
2907/// element of V2 is swizzled into the zero/undef vector, landing at element
2908/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Chris Lattner62098042008-03-09 01:05:04 +00002909static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002910 bool isZero, bool HasSSE2,
2911 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002912 MVT VT = V2.getValueType();
Evan Chengf0df0312008-05-15 08:39:06 +00002913 SDOperand V1 = isZero
2914 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002915 unsigned NumElems = V2.getValueType().getVectorNumElements();
2916 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2917 MVT EVT = MaskVT.getVectorElementType();
Chris Lattner8a594482007-11-25 00:24:49 +00002918 SmallVector<SDOperand, 16> MaskVec;
2919 for (unsigned i = 0; i != NumElems; ++i)
2920 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2921 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2922 else
2923 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002924 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2925 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002927}
2928
Evan Chengf26ffe92008-05-29 08:22:04 +00002929/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2930/// a shuffle that is zero.
2931static
2932unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2933 unsigned NumElems, bool Low,
2934 SelectionDAG &DAG) {
2935 unsigned NumZeros = 0;
2936 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002937 unsigned Index = Low ? i : NumElems-i-1;
2938 SDOperand Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002939 if (Idx.getOpcode() == ISD::UNDEF) {
2940 ++NumZeros;
2941 continue;
2942 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002943 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2944 if (Elt.Val && isZeroNode(Elt))
2945 ++NumZeros;
2946 else
2947 break;
2948 }
2949 return NumZeros;
2950}
2951
2952/// isVectorShift - Returns true if the shuffle can be implemented as a
2953/// logical left or right shift of a vector.
2954static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2955 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2956 unsigned NumElems = Mask.getNumOperands();
2957
2958 isLeft = true;
2959 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2960 if (!NumZeros) {
2961 isLeft = false;
2962 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2963 if (!NumZeros)
2964 return false;
2965 }
2966
2967 bool SeenV1 = false;
2968 bool SeenV2 = false;
2969 for (unsigned i = NumZeros; i < NumElems; ++i) {
2970 unsigned Val = isLeft ? (i - NumZeros) : i;
2971 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2972 if (Idx.getOpcode() == ISD::UNDEF)
2973 continue;
2974 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2975 if (Index < NumElems)
2976 SeenV1 = true;
2977 else {
2978 Index -= NumElems;
2979 SeenV2 = true;
2980 }
2981 if (Index != Val)
2982 return false;
2983 }
2984 if (SeenV1 && SeenV2)
2985 return false;
2986
2987 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2988 ShAmt = NumZeros;
2989 return true;
2990}
2991
2992
Evan Chengc78d3b42006-04-24 18:01:45 +00002993/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2994///
2995static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2996 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002997 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002998 if (NumNonZero > 8)
2999 return SDOperand();
3000
3001 SDOperand V(0, 0);
3002 bool First = true;
3003 for (unsigned i = 0; i < 16; ++i) {
3004 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3005 if (ThisIsNonZero && First) {
3006 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003007 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003008 else
3009 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3010 First = false;
3011 }
3012
3013 if ((i & 1) != 0) {
3014 SDOperand ThisElt(0, 0), LastElt(0, 0);
3015 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3016 if (LastIsNonZero) {
3017 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3018 }
3019 if (ThisIsNonZero) {
3020 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3021 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3022 ThisElt, DAG.getConstant(8, MVT::i8));
3023 if (LastIsNonZero)
3024 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3025 } else
3026 ThisElt = LastElt;
3027
3028 if (ThisElt.Val)
3029 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003030 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003031 }
3032 }
3033
3034 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3035}
3036
Bill Wendlinga348c562007-03-22 18:42:45 +00003037/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003038///
3039static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3040 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003041 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003042 if (NumNonZero > 4)
3043 return SDOperand();
3044
3045 SDOperand V(0, 0);
3046 bool First = true;
3047 for (unsigned i = 0; i < 8; ++i) {
3048 bool isNonZero = (NonZeros & (1 << i)) != 0;
3049 if (isNonZero) {
3050 if (First) {
3051 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003052 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003053 else
3054 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3055 First = false;
3056 }
3057 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003058 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003059 }
3060 }
3061
3062 return V;
3063}
3064
Evan Chengf26ffe92008-05-29 08:22:04 +00003065/// getVShift - Return a vector logical shift node.
3066///
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067static SDOperand getVShift(bool isLeft, MVT VT, SDOperand SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003068 unsigned NumBits, SelectionDAG &DAG,
3069 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003070 bool isMMX = VT.getSizeInBits() == 64;
3071 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003072 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3073 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3074 return DAG.getNode(ISD::BIT_CONVERT, VT,
3075 DAG.getNode(Opc, ShVT, SrcOp,
3076 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3077}
3078
Evan Cheng0db9fe62006-04-25 20:13:52 +00003079SDOperand
3080X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003081 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3082 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3083 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3084 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3085 // eliminated on x86-32 hosts.
3086 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3087 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003088
Chris Lattner8a594482007-11-25 00:24:49 +00003089 if (ISD::isBuildVectorAllOnes(Op.Val))
3090 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003091 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003092 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003093
Duncan Sands83ec4b62008-06-06 12:08:01 +00003094 MVT VT = Op.getValueType();
3095 MVT EVT = VT.getVectorElementType();
3096 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003097
3098 unsigned NumElems = Op.getNumOperands();
3099 unsigned NumZero = 0;
3100 unsigned NumNonZero = 0;
3101 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003102 bool IsAllConstants = true;
Evan Cheng14b32e12007-12-11 01:46:18 +00003103 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003104 for (unsigned i = 0; i < NumElems; ++i) {
3105 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003106 if (Elt.getOpcode() == ISD::UNDEF)
3107 continue;
3108 Values.insert(Elt);
3109 if (Elt.getOpcode() != ISD::Constant &&
3110 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003111 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003112 if (isZeroNode(Elt))
3113 NumZero++;
3114 else {
3115 NonZeros |= (1 << i);
3116 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003117 }
3118 }
3119
Dan Gohman7f321562007-06-25 16:23:39 +00003120 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003121 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3122 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003123 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003124
Chris Lattner67f453a2008-03-09 05:42:06 +00003125 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003126 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003127 unsigned Idx = CountTrailingZeros_32(NonZeros);
3128 SDOperand Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003129
Chris Lattner62098042008-03-09 01:05:04 +00003130 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3131 // the value are obviously zero, truncate the value to i32 and do the
3132 // insertion that way. Only do this if the value is non-constant or if the
3133 // value is a constant being inserted into element 0. It is cheaper to do
3134 // a constant pool load than it is to do a movd + shuffle.
3135 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3136 (!IsAllConstants || Idx == 0)) {
3137 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3138 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003139 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3140 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003141
3142 // Truncate the value (which may itself be a constant) to i32, and
3143 // convert it to a vector with movd (S2V+shuffle to zero extend).
3144 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3145 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003146 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3147 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003148
3149 // Now we have our 32-bit value zero extended in the low element of
3150 // a vector. If Idx != 0, swizzle it into place.
3151 if (Idx != 0) {
3152 SDOperand Ops[] = {
3153 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3154 getSwapEltZeroMask(VecElts, Idx, DAG)
3155 };
3156 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3157 }
3158 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3159 }
3160 }
3161
Chris Lattner19f79692008-03-08 22:59:52 +00003162 // If we have a constant or non-constant insertion into the low element of
3163 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3164 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3165 // depending on what the source datatype is. Because we can only get here
3166 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3167 if (Idx == 0 &&
3168 // Don't do this for i64 values on x86-32.
3169 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003170 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003171 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003172 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3173 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003174 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003175
3176 // Is it a vector logical left shift?
3177 if (NumElems == 2 && Idx == 1 &&
3178 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003179 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003180 return getVShift(true, VT,
3181 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3182 NumBits/2, DAG, *this);
3183 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003184
3185 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00003186 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003187
Chris Lattner19f79692008-03-08 22:59:52 +00003188 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3189 // is a non-constant being inserted into an element other than the low one,
3190 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3191 // movd/movss) to move this into the low element, then shuffle it into
3192 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003193 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003194 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3195
Evan Cheng0db9fe62006-04-25 20:13:52 +00003196 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003197 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3198 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003199 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3200 MVT MaskEVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003201 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003202 for (unsigned i = 0; i < NumElems; i++)
3203 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003204 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3205 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003206 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3207 DAG.getNode(ISD::UNDEF, VT), Mask);
3208 }
3209 }
3210
Chris Lattner67f453a2008-03-09 05:42:06 +00003211 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3212 if (Values.size() == 1)
3213 return SDOperand();
3214
Dan Gohmana3941172007-07-24 22:55:08 +00003215 // A vector full of immediates; various special cases are already
3216 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003217 if (IsAllConstants)
Dan Gohmana3941172007-07-24 22:55:08 +00003218 return SDOperand();
3219
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003220 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003221 if (EVTBits == 64) {
3222 if (NumNonZero == 1) {
3223 // One half is zero or undef.
3224 unsigned Idx = CountTrailingZeros_32(NonZeros);
3225 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3226 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003227 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3228 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003229 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003230 return SDOperand();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003231 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003232
3233 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003234 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003235 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3236 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 if (V.Val) return V;
3238 }
3239
Bill Wendling826f36f2007-03-28 00:57:11 +00003240 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003241 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3242 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003243 if (V.Val) return V;
3244 }
3245
3246 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003247 SmallVector<SDOperand, 8> V;
3248 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003249 if (NumElems == 4 && NumZero > 0) {
3250 for (unsigned i = 0; i < 4; ++i) {
3251 bool isZero = !(NonZeros & (1 << i));
3252 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003253 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003254 else
3255 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3256 }
3257
3258 for (unsigned i = 0; i < 2; ++i) {
3259 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3260 default: break;
3261 case 0:
3262 V[i] = V[i*2]; // Must be a zero vector.
3263 break;
3264 case 1:
3265 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3266 getMOVLMask(NumElems, DAG));
3267 break;
3268 case 2:
3269 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3270 getMOVLMask(NumElems, DAG));
3271 break;
3272 case 3:
3273 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3274 getUnpacklMask(NumElems, DAG));
3275 break;
3276 }
3277 }
3278
Duncan Sands83ec4b62008-06-06 12:08:01 +00003279 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3280 MVT EVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003281 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003282 bool Reverse = (NonZeros & 0x3) == 2;
3283 for (unsigned i = 0; i < 2; ++i)
3284 if (Reverse)
3285 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3286 else
3287 MaskVec.push_back(DAG.getConstant(i, EVT));
3288 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3289 for (unsigned i = 0; i < 2; ++i)
3290 if (Reverse)
3291 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3292 else
3293 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003294 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3295 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003296 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3297 }
3298
3299 if (Values.size() > 2) {
3300 // Expand into a number of unpckl*.
3301 // e.g. for v4f32
3302 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3303 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3304 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3305 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3306 for (unsigned i = 0; i < NumElems; ++i)
3307 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3308 NumElems >>= 1;
3309 while (NumElems != 0) {
3310 for (unsigned i = 0; i < NumElems; ++i)
3311 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3312 UnpckMask);
3313 NumElems >>= 1;
3314 }
3315 return V[0];
3316 }
3317
3318 return SDOperand();
3319}
3320
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003321static
3322SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3323 SDOperand PermMask, SelectionDAG &DAG,
3324 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003325 SDOperand NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003326 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3327 MVT MaskEVT = MaskVT.getVectorElementType();
3328 MVT PtrVT = TLI.getPointerTy();
Evan Cheng14b32e12007-12-11 01:46:18 +00003329 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3330 PermMask.Val->op_end());
3331
3332 // First record which half of which vector the low elements come from.
3333 SmallVector<unsigned, 4> LowQuad(4);
3334 for (unsigned i = 0; i < 4; ++i) {
3335 SDOperand Elt = MaskElts[i];
3336 if (Elt.getOpcode() == ISD::UNDEF)
3337 continue;
3338 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3339 int QuadIdx = EltIdx / 4;
3340 ++LowQuad[QuadIdx];
3341 }
3342 int BestLowQuad = -1;
3343 unsigned MaxQuad = 1;
3344 for (unsigned i = 0; i < 4; ++i) {
3345 if (LowQuad[i] > MaxQuad) {
3346 BestLowQuad = i;
3347 MaxQuad = LowQuad[i];
3348 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003349 }
3350
Evan Cheng14b32e12007-12-11 01:46:18 +00003351 // Record which half of which vector the high elements come from.
3352 SmallVector<unsigned, 4> HighQuad(4);
3353 for (unsigned i = 4; i < 8; ++i) {
3354 SDOperand Elt = MaskElts[i];
3355 if (Elt.getOpcode() == ISD::UNDEF)
3356 continue;
3357 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3358 int QuadIdx = EltIdx / 4;
3359 ++HighQuad[QuadIdx];
3360 }
3361 int BestHighQuad = -1;
3362 MaxQuad = 1;
3363 for (unsigned i = 0; i < 4; ++i) {
3364 if (HighQuad[i] > MaxQuad) {
3365 BestHighQuad = i;
3366 MaxQuad = HighQuad[i];
3367 }
3368 }
3369
3370 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3371 if (BestLowQuad != -1 || BestHighQuad != -1) {
3372 // First sort the 4 chunks in order using shufpd.
3373 SmallVector<SDOperand, 8> MaskVec;
3374 if (BestLowQuad != -1)
3375 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3376 else
3377 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3378 if (BestHighQuad != -1)
3379 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3380 else
3381 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3382 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3383 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3384 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3385 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3386 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3387
3388 // Now sort high and low parts separately.
3389 BitVector InOrder(8);
3390 if (BestLowQuad != -1) {
3391 // Sort lower half in order using PSHUFLW.
3392 MaskVec.clear();
3393 bool AnyOutOrder = false;
3394 for (unsigned i = 0; i != 4; ++i) {
3395 SDOperand Elt = MaskElts[i];
3396 if (Elt.getOpcode() == ISD::UNDEF) {
3397 MaskVec.push_back(Elt);
3398 InOrder.set(i);
3399 } else {
3400 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3401 if (EltIdx != i)
3402 AnyOutOrder = true;
3403 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3404 // If this element is in the right place after this shuffle, then
3405 // remember it.
3406 if ((int)(EltIdx / 4) == BestLowQuad)
3407 InOrder.set(i);
3408 }
3409 }
3410 if (AnyOutOrder) {
3411 for (unsigned i = 4; i != 8; ++i)
3412 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3413 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3414 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3415 }
3416 }
3417
3418 if (BestHighQuad != -1) {
3419 // Sort high half in order using PSHUFHW if possible.
3420 MaskVec.clear();
3421 for (unsigned i = 0; i != 4; ++i)
3422 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3423 bool AnyOutOrder = false;
3424 for (unsigned i = 4; i != 8; ++i) {
3425 SDOperand Elt = MaskElts[i];
3426 if (Elt.getOpcode() == ISD::UNDEF) {
3427 MaskVec.push_back(Elt);
3428 InOrder.set(i);
3429 } else {
3430 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3431 if (EltIdx != i)
3432 AnyOutOrder = true;
3433 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3434 // If this element is in the right place after this shuffle, then
3435 // remember it.
3436 if ((int)(EltIdx / 4) == BestHighQuad)
3437 InOrder.set(i);
3438 }
3439 }
3440 if (AnyOutOrder) {
3441 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3442 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3443 }
3444 }
3445
3446 // The other elements are put in the right place using pextrw and pinsrw.
3447 for (unsigned i = 0; i != 8; ++i) {
3448 if (InOrder[i])
3449 continue;
3450 SDOperand Elt = MaskElts[i];
3451 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003452 SDOperand ExtOp = (EltIdx < 8)
3453 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3454 DAG.getConstant(EltIdx, PtrVT))
3455 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3456 DAG.getConstant(EltIdx - 8, PtrVT));
3457 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3458 DAG.getConstant(i, PtrVT));
3459 }
3460 return NewV;
3461 }
3462
3463 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3464 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003465 // First, let's find out how many elements are already in the right order.
3466 unsigned V1InOrder = 0;
3467 unsigned V1FromV1 = 0;
3468 unsigned V2InOrder = 0;
3469 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003470 SmallVector<SDOperand, 8> V1Elts;
3471 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003472 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003473 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003474 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003475 V1Elts.push_back(Elt);
3476 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003477 ++V1InOrder;
3478 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003479 continue;
3480 }
3481 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3482 if (EltIdx == i) {
3483 V1Elts.push_back(Elt);
3484 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3485 ++V1InOrder;
3486 } else if (EltIdx == i+8) {
3487 V1Elts.push_back(Elt);
3488 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3489 ++V2InOrder;
3490 } else if (EltIdx < 8) {
3491 V1Elts.push_back(Elt);
3492 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003493 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003494 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3495 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003496 }
3497 }
3498
3499 if (V2InOrder > V1InOrder) {
3500 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3501 std::swap(V1, V2);
3502 std::swap(V1Elts, V2Elts);
3503 std::swap(V1FromV1, V2FromV2);
3504 }
3505
Evan Cheng14b32e12007-12-11 01:46:18 +00003506 if ((V1FromV1 + V1InOrder) != 8) {
3507 // Some elements are from V2.
3508 if (V1FromV1) {
3509 // If there are elements that are from V1 but out of place,
3510 // then first sort them in place
3511 SmallVector<SDOperand, 8> MaskVec;
3512 for (unsigned i = 0; i < 8; ++i) {
3513 SDOperand Elt = V1Elts[i];
3514 if (Elt.getOpcode() == ISD::UNDEF) {
3515 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3516 continue;
3517 }
3518 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3519 if (EltIdx >= 8)
3520 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3521 else
3522 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3523 }
3524 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3525 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003526 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003527
3528 NewV = V1;
3529 for (unsigned i = 0; i < 8; ++i) {
3530 SDOperand Elt = V1Elts[i];
3531 if (Elt.getOpcode() == ISD::UNDEF)
3532 continue;
3533 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3534 if (EltIdx < 8)
3535 continue;
3536 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3537 DAG.getConstant(EltIdx - 8, PtrVT));
3538 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3539 DAG.getConstant(i, PtrVT));
3540 }
3541 return NewV;
3542 } else {
3543 // All elements are from V1.
3544 NewV = V1;
3545 for (unsigned i = 0; i < 8; ++i) {
3546 SDOperand Elt = V1Elts[i];
3547 if (Elt.getOpcode() == ISD::UNDEF)
3548 continue;
3549 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3550 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3551 DAG.getConstant(EltIdx, PtrVT));
3552 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3553 DAG.getConstant(i, PtrVT));
3554 }
3555 return NewV;
3556 }
3557}
3558
Evan Cheng7a831ce2007-12-15 03:00:47 +00003559/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3560/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3561/// done when every pair / quad of shuffle mask elements point to elements in
3562/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003563/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3564static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003565SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003566 MVT VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003567 SDOperand PermMask, SelectionDAG &DAG,
3568 TargetLowering &TLI) {
3569 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003570 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003571 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3572 MVT NewVT = MaskVT;
3573 switch (VT.getSimpleVT()) {
3574 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003575 case MVT::v4f32: NewVT = MVT::v2f64; break;
3576 case MVT::v4i32: NewVT = MVT::v2i64; break;
3577 case MVT::v8i16: NewVT = MVT::v4i32; break;
3578 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003579 }
3580
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003581 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003582 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003583 NewVT = MVT::v2i64;
3584 else
3585 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003586 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003587 unsigned Scale = NumElems / NewWidth;
3588 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003589 for (unsigned i = 0; i < NumElems; i += Scale) {
3590 unsigned StartIdx = ~0U;
3591 for (unsigned j = 0; j < Scale; ++j) {
3592 SDOperand Elt = PermMask.getOperand(i+j);
3593 if (Elt.getOpcode() == ISD::UNDEF)
3594 continue;
3595 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3596 if (StartIdx == ~0U)
3597 StartIdx = EltIdx - (EltIdx % Scale);
3598 if (EltIdx != StartIdx + j)
3599 return SDOperand();
3600 }
3601 if (StartIdx == ~0U)
3602 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3603 else
3604 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003605 }
3606
Evan Cheng7a831ce2007-12-15 03:00:47 +00003607 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3608 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3609 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3610 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3611 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003612}
3613
Evan Chengd880b972008-05-09 21:53:03 +00003614/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003615///
Duncan Sands83ec4b62008-06-06 12:08:01 +00003616static SDOperand getVZextMovL(MVT VT, MVT OpVT,
3617 SDOperand SrcOp, SelectionDAG &DAG,
3618 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003619 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3620 LoadSDNode *LD = NULL;
3621 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3622 LD = dyn_cast<LoadSDNode>(SrcOp);
3623 if (!LD) {
3624 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3625 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003626 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003627 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3628 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3629 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3630 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3631 // PR2108
3632 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3633 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003634 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003635 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3636 SrcOp.getOperand(0).getOperand(0))));
3637 }
3638 }
3639 }
3640
3641 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003642 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003643 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3644}
3645
Evan Cheng0db9fe62006-04-25 20:13:52 +00003646SDOperand
3647X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3648 SDOperand V1 = Op.getOperand(0);
3649 SDOperand V2 = Op.getOperand(1);
3650 SDOperand PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003651 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003652 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003654 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3655 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003656 bool V1IsSplat = false;
3657 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003658
Evan Cheng8cf723d2006-09-08 01:50:06 +00003659 if (isUndefShuffle(Op.Val))
3660 return DAG.getNode(ISD::UNDEF, VT);
3661
Evan Cheng213d2cf2007-05-17 18:45:50 +00003662 if (isZeroShuffle(Op.Val))
Evan Chengf0df0312008-05-15 08:39:06 +00003663 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003664
Evan Cheng49892af2007-06-19 00:02:56 +00003665 if (isIdentityMask(PermMask.Val))
3666 return V1;
3667 else if (isIdentityMask(PermMask.Val, true))
3668 return V2;
3669
Evan Cheng0db9fe62006-04-25 20:13:52 +00003670 if (isSplatMask(PermMask.Val)) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003671 if (isMMX || NumElems < 4) return Op;
3672 // Promote it to a v4{if}32 splat.
3673 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 }
3675
Evan Cheng7a831ce2007-12-15 03:00:47 +00003676 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3677 // do it!
3678 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3679 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3680 if (NewOp.Val)
3681 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3682 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3683 // FIXME: Figure out a cleaner way to do this.
3684 // Try to make use of movq to zero out the top part.
3685 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003686 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3687 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003688 if (NewOp.Val) {
3689 SDOperand NewV1 = NewOp.getOperand(0);
3690 SDOperand NewV2 = NewOp.getOperand(1);
3691 SDOperand NewMask = NewOp.getOperand(2);
3692 if (isCommutedMOVL(NewMask.Val, true, false)) {
3693 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00003694 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003695 }
3696 }
3697 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003698 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3699 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003700 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chengd880b972008-05-09 21:53:03 +00003701 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00003702 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003703 }
3704 }
3705
Evan Chengf26ffe92008-05-29 08:22:04 +00003706 // Check if this can be converted into a logical shift.
3707 bool isLeft = false;
3708 unsigned ShAmt = 0;
3709 SDOperand ShVal;
3710 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3711 if (isShift && ShVal.hasOneUse()) {
3712 // If the shifted value has multiple uses, it may be cheaper to use
3713 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714 MVT EVT = VT.getVectorElementType();
3715 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003716 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3717 }
3718
Evan Cheng7e2ff772008-05-08 00:57:18 +00003719 if (X86::isMOVLMask(PermMask.Val)) {
3720 if (V1IsUndef)
3721 return V2;
3722 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chengd880b972008-05-09 21:53:03 +00003723 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003724 return Op;
3725 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003726
Evan Cheng9bbbb982006-10-25 20:48:19 +00003727 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3728 X86::isMOVSLDUPMask(PermMask.Val) ||
3729 X86::isMOVHLPSMask(PermMask.Val) ||
3730 X86::isMOVHPMask(PermMask.Val) ||
3731 X86::isMOVLPMask(PermMask.Val))
3732 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733
Evan Cheng9bbbb982006-10-25 20:48:19 +00003734 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3735 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003736 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003737
Evan Chengf26ffe92008-05-29 08:22:04 +00003738 if (isShift) {
3739 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 MVT EVT = VT.getVectorElementType();
3741 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003742 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3743 }
3744
Evan Cheng9eca5e82006-10-25 21:49:50 +00003745 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003746 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3747 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003748 V1IsSplat = isSplatVector(V1.Val);
3749 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003750
3751 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003752 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003753 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003754 std::swap(V1IsSplat, V2IsSplat);
3755 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003756 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003757 }
3758
Evan Cheng7a831ce2007-12-15 03:00:47 +00003759 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003760 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3761 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003762 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003763 if (V2IsSplat) {
3764 // V2 is a splat, so the mask may be malformed. That is, it may point
3765 // to any V2 element. The instruction selectior won't like this. Get
3766 // a corrected mask and commute to form a proper MOVS{S|D}.
3767 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3768 if (NewMask.Val != PermMask.Val)
3769 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003771 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003772 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003773
Evan Chengd9b8e402006-10-16 06:36:00 +00003774 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003775 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003776 X86::isUNPCKLMask(PermMask.Val) ||
3777 X86::isUNPCKHMask(PermMask.Val))
3778 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003779
Evan Cheng9bbbb982006-10-25 20:48:19 +00003780 if (V2IsSplat) {
3781 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003782 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003783 // new vector_shuffle with the corrected mask.
3784 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3785 if (NewMask.Val != PermMask.Val) {
3786 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3787 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3788 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3789 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3790 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3791 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003792 }
3793 }
3794 }
3795
3796 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003797 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3798 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3799
3800 if (Commuted) {
3801 // Commute is back and try unpck* again.
3802 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3803 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003804 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003805 X86::isUNPCKLMask(PermMask.Val) ||
3806 X86::isUNPCKHMask(PermMask.Val))
3807 return Op;
3808 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003809
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003810 // Try PSHUF* first, then SHUFP*.
3811 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3812 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3813 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3814 if (V2.getOpcode() != ISD::UNDEF)
3815 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3816 DAG.getNode(ISD::UNDEF, VT), PermMask);
3817 return Op;
3818 }
3819
3820 if (!isMMX) {
3821 if (Subtarget->hasSSE2() &&
3822 (X86::isPSHUFDMask(PermMask.Val) ||
3823 X86::isPSHUFHWMask(PermMask.Val) ||
3824 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003825 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003826 if (VT == MVT::v4f32) {
3827 RVT = MVT::v4i32;
3828 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3829 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3830 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3831 } else if (V2.getOpcode() != ISD::UNDEF)
3832 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3833 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3834 if (RVT != VT)
3835 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003836 return Op;
3837 }
3838
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003839 // Binary or unary shufps.
3840 if (X86::isSHUFPMask(PermMask.Val) ||
3841 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Evan Cheng0db9fe62006-04-25 20:13:52 +00003842 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843 }
3844
Evan Cheng14b32e12007-12-11 01:46:18 +00003845 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3846 if (VT == MVT::v8i16) {
3847 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3848 if (NewOp.Val)
3849 return NewOp;
3850 }
3851
3852 // Handle all 4 wide cases with a number of shuffles.
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003853 if (NumElems == 4 && !isMMX) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003854 // Don't do this for MMX.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855 MVT MaskVT = PermMask.getValueType();
3856 MVT MaskEVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003857 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003858 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003859 SmallVector<SDOperand, 8> Mask1(NumElems,
3860 DAG.getNode(ISD::UNDEF, MaskEVT));
3861 SmallVector<SDOperand, 8> Mask2(NumElems,
3862 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003863 unsigned NumHi = 0;
3864 unsigned NumLo = 0;
3865 // If no more than two elements come from either vector. This can be
3866 // implemented with two shuffles. First shuffle gather the elements.
3867 // The second shuffle, which takes the first shuffle as both of its
3868 // vector operands, put the elements into the right order.
3869 for (unsigned i = 0; i != NumElems; ++i) {
3870 SDOperand Elt = PermMask.getOperand(i);
3871 if (Elt.getOpcode() == ISD::UNDEF) {
3872 Locs[i] = std::make_pair(-1, -1);
3873 } else {
3874 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3875 if (Val < NumElems) {
3876 Locs[i] = std::make_pair(0, NumLo);
3877 Mask1[NumLo] = Elt;
3878 NumLo++;
3879 } else {
3880 Locs[i] = std::make_pair(1, NumHi);
3881 if (2+NumHi < NumElems)
3882 Mask1[2+NumHi] = Elt;
3883 NumHi++;
3884 }
3885 }
3886 }
3887 if (NumLo <= 2 && NumHi <= 2) {
3888 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003889 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3890 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003891 for (unsigned i = 0; i != NumElems; ++i) {
3892 if (Locs[i].first == -1)
3893 continue;
3894 else {
3895 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3896 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3897 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3898 }
3899 }
3900
3901 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003902 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3903 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003904 }
3905
3906 // Break it into (shuffle shuffle_hi, shuffle_lo).
3907 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003908 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3909 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3910 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003911 unsigned MaskIdx = 0;
3912 unsigned LoIdx = 0;
3913 unsigned HiIdx = NumElems/2;
3914 for (unsigned i = 0; i != NumElems; ++i) {
3915 if (i == NumElems/2) {
3916 MaskPtr = &HiMask;
3917 MaskIdx = 1;
3918 LoIdx = 0;
3919 HiIdx = NumElems/2;
3920 }
3921 SDOperand Elt = PermMask.getOperand(i);
3922 if (Elt.getOpcode() == ISD::UNDEF) {
3923 Locs[i] = std::make_pair(-1, -1);
3924 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3925 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3926 (*MaskPtr)[LoIdx] = Elt;
3927 LoIdx++;
3928 } else {
3929 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3930 (*MaskPtr)[HiIdx] = Elt;
3931 HiIdx++;
3932 }
3933 }
3934
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003935 SDOperand LoShuffle =
3936 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003937 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3938 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003939 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003940 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003941 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3942 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003943 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 for (unsigned i = 0; i != NumElems; ++i) {
3945 if (Locs[i].first == -1) {
3946 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3947 } else {
3948 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3949 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3950 }
3951 }
3952 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003953 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3954 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003955 }
3956
3957 return SDOperand();
3958}
3959
3960SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00003961X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3962 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003963 MVT VT = Op.getValueType();
3964 if (VT.getSizeInBits() == 8) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00003965 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3966 Op.getOperand(0), Op.getOperand(1));
3967 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3968 DAG.getValueType(VT));
3969 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003970 } else if (VT.getSizeInBits() == 16) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00003971 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3972 Op.getOperand(0), Op.getOperand(1));
3973 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3974 DAG.getValueType(VT));
3975 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00003976 } else if (VT == MVT::f32) {
3977 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3978 // the result back to FR32 register. It's only worth matching if the
Dan Gohman171c11e2008-04-16 02:32:24 +00003979 // result has a single use which is a store or a bitcast to i32.
Evan Cheng62a3f152008-03-24 21:52:23 +00003980 if (!Op.hasOneUse())
3981 return SDOperand();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003982 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman171c11e2008-04-16 02:32:24 +00003983 if (User->getOpcode() != ISD::STORE &&
3984 (User->getOpcode() != ISD::BIT_CONVERT ||
3985 User->getValueType(0) != MVT::i32))
Evan Cheng62a3f152008-03-24 21:52:23 +00003986 return SDOperand();
3987 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3988 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3989 Op.getOperand(1));
3990 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00003991 }
3992 return SDOperand();
3993}
3994
3995
3996SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00003997X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3998 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3999 return SDOperand();
4000
Evan Cheng62a3f152008-03-24 21:52:23 +00004001 if (Subtarget->hasSSE41()) {
4002 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4003 if (Res.Val)
4004 return Res;
4005 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004006
Duncan Sands83ec4b62008-06-06 12:08:01 +00004007 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004008 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004009 if (VT.getSizeInBits() == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00004010 SDOperand Vec = Op.getOperand(0);
4011 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4012 if (Idx == 0)
4013 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4014 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4015 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4016 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004017 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004018 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004019 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4020 Op.getOperand(0), Op.getOperand(1));
4021 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4022 DAG.getValueType(VT));
4023 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004024 } else if (VT.getSizeInBits() == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004025 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4026 if (Idx == 0)
4027 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004028 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004029 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00004030 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004031 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004032 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004033 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004034 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004035 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004036 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004037 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004038 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Chris Lattnere2199452006-08-11 17:38:39 +00004039 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4040 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00004041 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004042 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004043 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004044 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004045 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004047 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4048 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4049 // to match extract_elt for f64.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004050 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4051 if (Idx == 0)
4052 return Op;
4053
4054 // UNPCKHPD the element to the lowest double word, then movsd.
4055 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4056 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004057 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00004058 SmallVector<SDOperand, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004060 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004061 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Chris Lattnere2199452006-08-11 17:38:39 +00004062 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4063 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00004064 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004065 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4066 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4067 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004068 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004069 }
4070
4071 return SDOperand();
4072}
4073
4074SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00004075X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004076 MVT VT = Op.getValueType();
4077 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004078
4079 SDOperand N0 = Op.getOperand(0);
4080 SDOperand N1 = Op.getOperand(1);
4081 SDOperand N2 = Op.getOperand(2);
4082
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4084 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004085 : X86ISD::PINSRW;
4086 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4087 // argument.
4088 if (N1.getValueType() != MVT::i32)
4089 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4090 if (N2.getValueType() != MVT::i32)
4091 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4092 return DAG.getNode(Opc, VT, N0, N1, N2);
4093 } else if (EVT == MVT::f32) {
4094 // Bits [7:6] of the constant are the source select. This will always be
4095 // zero here. The DAG Combiner may combine an extract_elt index into these
4096 // bits. For example (insert (extract, 3), 2) could be matched by putting
4097 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4098 // Bits [5:4] of the constant are the destination select. This is the
4099 // value of the incoming immediate.
4100 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4101 // combine either bitwise AND or insert of float 0.0 to set these bits.
4102 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4103 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4104 }
4105 return SDOperand();
4106}
4107
4108SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00004109X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004110 MVT VT = Op.getValueType();
4111 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004112
4113 if (Subtarget->hasSSE41())
4114 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4115
Evan Cheng794405e2007-12-12 07:55:34 +00004116 if (EVT == MVT::i8)
4117 return SDOperand();
4118
Evan Cheng0db9fe62006-04-25 20:13:52 +00004119 SDOperand N0 = Op.getOperand(0);
4120 SDOperand N1 = Op.getOperand(1);
4121 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004122
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004124 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4125 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004126 if (N1.getValueType() != MVT::i32)
4127 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4128 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00004129 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004130 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131 }
Nate Begeman219f67f2008-01-05 20:51:30 +00004132 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004133}
4134
4135SDOperand
4136X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4137 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004138 MVT VT = MVT::v2i32;
4139 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004140 default: break;
4141 case MVT::v16i8:
4142 case MVT::v8i16:
4143 VT = MVT::v4i32;
4144 break;
4145 }
4146 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4147 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004148}
4149
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004150// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004151// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4152// one of the above mentioned nodes. It has to be wrapped because otherwise
4153// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4154// be used to form addressing mode. These wrapped nodes will be selected
4155// into MOV32ri.
4156SDOperand
4157X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4158 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00004159 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4160 getPointerTy(),
4161 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004162 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004163 // With PIC, the address is actually $g + Offset.
4164 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4165 !Subtarget->isPICStyleRIPRel()) {
4166 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4167 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4168 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004169 }
4170
4171 return Result;
4172}
4173
4174SDOperand
4175X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4176 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004177 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004178 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004179 // With PIC, the address is actually $g + Offset.
4180 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4181 !Subtarget->isPICStyleRIPRel()) {
4182 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4183 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4184 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004185 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004186
4187 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4188 // load the value at address GV, not the value of GV itself. This means that
4189 // the GlobalAddress must be in the base or index register of the address, not
4190 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004191 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004192 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00004193 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004194 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195
4196 return Result;
4197}
4198
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004199// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004200static SDOperand
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004201LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004202 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004203 SDOperand InFlag;
4204 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4205 DAG.getNode(X86ISD::GlobalBaseReg,
4206 PtrVT), InFlag);
4207 InFlag = Chain.getValue(1);
4208
4209 // emit leal symbol@TLSGD(,%ebx,1), %eax
4210 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4211 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4212 GA->getValueType(0),
4213 GA->getOffset());
4214 SDOperand Ops[] = { Chain, TGA, InFlag };
4215 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4216 InFlag = Result.getValue(2);
4217 Chain = Result.getValue(1);
4218
4219 // call ___tls_get_addr. This function receives its argument in
4220 // the register EAX.
4221 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4222 InFlag = Chain.getValue(1);
4223
4224 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4225 SDOperand Ops1[] = { Chain,
4226 DAG.getTargetExternalSymbol("___tls_get_addr",
4227 PtrVT),
4228 DAG.getRegister(X86::EAX, PtrVT),
4229 DAG.getRegister(X86::EBX, PtrVT),
4230 InFlag };
4231 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4232 InFlag = Chain.getValue(1);
4233
4234 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4235}
4236
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004237// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4238static SDOperand
4239LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 const MVT PtrVT) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004241 SDOperand InFlag, Chain;
4242
4243 // emit leaq symbol@TLSGD(%rip), %rdi
4244 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4245 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4246 GA->getValueType(0),
4247 GA->getOffset());
4248 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4249 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4250 Chain = Result.getValue(1);
4251 InFlag = Result.getValue(2);
4252
4253 // call ___tls_get_addr. This function receives its argument in
4254 // the register RDI.
4255 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4256 InFlag = Chain.getValue(1);
4257
4258 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4259 SDOperand Ops1[] = { Chain,
4260 DAG.getTargetExternalSymbol("___tls_get_addr",
4261 PtrVT),
4262 DAG.getRegister(X86::RDI, PtrVT),
4263 InFlag };
4264 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4265 InFlag = Chain.getValue(1);
4266
4267 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4268}
4269
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004270// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4271// "local exec" model.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004272static SDOperand LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4273 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004274 // Get the Thread Pointer
4275 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4276 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4277 // exec)
4278 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4279 GA->getValueType(0),
4280 GA->getOffset());
4281 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004282
4283 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004284 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004285 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004286
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004287 // The address of the thread local variable is the add of the thread
4288 // pointer with the offset of the variable.
4289 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4290}
4291
4292SDOperand
4293X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4294 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004295 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004296 assert(Subtarget->isTargetELF() &&
4297 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004298 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4299 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4300 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004301 if (Subtarget->is64Bit()) {
4302 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4303 } else {
4304 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4305 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4306 else
4307 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4308 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004309}
4310
Evan Cheng0db9fe62006-04-25 20:13:52 +00004311SDOperand
4312X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4313 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004314 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004315 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004316 // With PIC, the address is actually $g + Offset.
4317 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4318 !Subtarget->isPICStyleRIPRel()) {
4319 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4320 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4321 Result);
4322 }
4323
4324 return Result;
4325}
4326
4327SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4328 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4329 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4330 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4331 // With PIC, the address is actually $g + Offset.
4332 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4333 !Subtarget->isPICStyleRIPRel()) {
4334 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4335 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4336 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004337 }
4338
4339 return Result;
4340}
4341
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004342/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4343/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004344SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004345 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004346 MVT VT = Op.getValueType();
4347 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004348 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4349 SDOperand ShOpLo = Op.getOperand(0);
4350 SDOperand ShOpHi = Op.getOperand(1);
4351 SDOperand ShAmt = Op.getOperand(2);
4352 SDOperand Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004353 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4354 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004355
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004356 SDOperand Tmp2, Tmp3;
4357 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004358 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4359 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004360 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004361 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4362 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004363 }
Evan Chenge3413162006-01-09 18:33:28 +00004364
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004365 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004366 DAG.getConstant(VTBits, MVT::i8));
4367 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004368 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004369
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004370 SDOperand Hi, Lo;
4371 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Duncan Sandsf9516202008-06-30 10:19:09 +00004372 SDOperand Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4373 SDOperand Ops1[4] = { Tmp3, Tmp1, CC, Cond };
4374
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004375 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004376 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4377 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004378 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004379 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4380 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004381 }
4382
Duncan Sandsf9516202008-06-30 10:19:09 +00004383 SDOperand Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004384 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385}
Evan Chenga3195e82006-01-12 22:54:21 +00004386
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004388 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004389 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004390 "Unknown SINT_TO_FP to lower!");
4391
4392 // These are really Legal; caller falls through into that case.
4393 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4394 return SDOperand();
4395 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4396 Subtarget->is64Bit())
4397 return SDOperand();
4398
Duncan Sands83ec4b62008-06-06 12:08:01 +00004399 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 MachineFunction &MF = DAG.getMachineFunction();
4401 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4402 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00004403 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004404 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004405 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406
4407 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004408 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004409 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004410 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004411 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4412 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004413 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004414 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004415 Ops.push_back(Chain);
4416 Ops.push_back(StackSlot);
4417 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerb09916b2008-02-27 05:57:41 +00004418 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4419 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004420
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004421 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 Chain = Result.getValue(1);
4423 SDOperand InFlag = Result.getValue(2);
4424
4425 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4426 // shouldn't be necessary except that RFP cannot be live across
4427 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004428 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004429 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004430 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004431 Tys = DAG.getVTList(MVT::Other);
4432 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004433 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004434 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004435 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436 Ops.push_back(DAG.getValueType(Op.getValueType()));
4437 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004438 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004439 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004440 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004441 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004442
Evan Cheng0db9fe62006-04-25 20:13:52 +00004443 return Result;
4444}
4445
Chris Lattner27a6c732007-11-24 07:07:01 +00004446std::pair<SDOperand,SDOperand> X86TargetLowering::
4447FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004448 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4449 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004450 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004451
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004452 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004453 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004454 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattner27a6c732007-11-24 07:07:01 +00004455 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004456 if (Subtarget->is64Bit() &&
4457 Op.getValueType() == MVT::i64 &&
4458 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004459 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004460
Evan Cheng87c89352007-10-15 20:11:21 +00004461 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4462 // stack slot.
4463 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004464 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004465 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4466 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004467 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004468 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004469 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4470 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4471 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4472 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004473 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004474
Evan Cheng0db9fe62006-04-25 20:13:52 +00004475 SDOperand Chain = DAG.getEntryNode();
4476 SDOperand Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004477 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004478 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004479 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004480 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004481 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004482 SDOperand Ops[] = {
4483 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4484 };
4485 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004486 Chain = Value.getValue(1);
4487 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4488 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4489 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004490
Evan Cheng0db9fe62006-04-25 20:13:52 +00004491 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004492 SDOperand Ops[] = { Chain, Value, StackSlot };
4493 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004494
Chris Lattner27a6c732007-11-24 07:07:01 +00004495 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004496}
4497
Chris Lattner27a6c732007-11-24 07:07:01 +00004498SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004499 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4500 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4501 if (FIST.Val == 0) return SDOperand();
4502
4503 // Load the result.
4504 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4505}
4506
4507SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4508 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4509 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4510 if (FIST.Val == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004511
4512 MVT VT = N->getValueType(0);
4513
4514 // Return a load from the stack slot.
4515 SDOperand Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004516
Duncan Sands4bdcb612008-07-02 17:40:58 +00004517 // Use MERGE_VALUES to drop the chain result value and get a node with one
4518 // result. This requires turning off getMergeValues simplification, since
4519 // otherwise it will give us Res back.
4520 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf9516202008-06-30 10:19:09 +00004521}
Chris Lattner27a6c732007-11-24 07:07:01 +00004522
Evan Cheng0db9fe62006-04-25 20:13:52 +00004523SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004524 MVT VT = Op.getValueType();
4525 MVT EltVT = VT;
4526 if (VT.isVector())
4527 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004528 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004529 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004530 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004531 CV.push_back(C);
4532 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004534 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004535 CV.push_back(C);
4536 CV.push_back(C);
4537 CV.push_back(C);
4538 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004539 }
Dan Gohmand3006222007-07-27 17:16:43 +00004540 Constant *C = ConstantVector::get(CV);
4541 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004542 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004543 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004544 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004545 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4546}
4547
4548SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004549 MVT VT = Op.getValueType();
4550 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004551 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004552 if (VT.isVector()) {
4553 EltVT = VT.getVectorElementType();
4554 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004555 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004557 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004558 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004559 CV.push_back(C);
4560 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004562 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004563 CV.push_back(C);
4564 CV.push_back(C);
4565 CV.push_back(C);
4566 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567 }
Dan Gohmand3006222007-07-27 17:16:43 +00004568 Constant *C = ConstantVector::get(CV);
4569 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004570 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004571 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004572 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004574 return DAG.getNode(ISD::BIT_CONVERT, VT,
4575 DAG.getNode(ISD::XOR, MVT::v2i64,
4576 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4577 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4578 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004579 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4580 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581}
4582
Evan Cheng68c47cb2007-01-05 07:55:56 +00004583SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004584 SDOperand Op0 = Op.getOperand(0);
4585 SDOperand Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004586 MVT VT = Op.getValueType();
4587 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004588
4589 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004590 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004591 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4592 SrcVT = VT;
4593 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004594 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004595 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004596 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004597 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004598 }
4599
4600 // At this point the operands and the result should have the same
4601 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004602
Evan Cheng68c47cb2007-01-05 07:55:56 +00004603 // First get the sign bit of second operand.
4604 std::vector<Constant*> CV;
4605 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004606 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4607 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004608 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004609 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4610 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4611 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4612 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004613 }
Dan Gohmand3006222007-07-27 17:16:43 +00004614 Constant *C = ConstantVector::get(CV);
4615 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004616 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004617 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004618 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004619 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004620
4621 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004622 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004623 // Op0 is MVT::f32, Op1 is MVT::f64.
4624 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4625 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4626 DAG.getConstant(32, MVT::i32));
4627 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4628 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004629 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004630 }
4631
Evan Cheng73d6cf12007-01-05 21:37:56 +00004632 // Clear first operand sign bit.
4633 CV.clear();
4634 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004635 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4636 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004637 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004638 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4639 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4640 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4641 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004642 }
Dan Gohmand3006222007-07-27 17:16:43 +00004643 C = ConstantVector::get(CV);
4644 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004645 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004646 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004647 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004648 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4649
4650 // Or the value with the sign bit.
4651 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004652}
4653
Evan Chenge5f62042007-09-29 00:00:36 +00004654SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004655 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004656 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004657 SDOperand Op0 = Op.getOperand(0);
4658 SDOperand Op1 = Op.getOperand(1);
4659 SDOperand CC = Op.getOperand(2);
4660 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004661 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00004662 unsigned X86CC;
4663
Evan Cheng0488db92007-09-25 01:57:46 +00004664 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004665 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004666 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4667 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004668 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004669 }
Evan Cheng0488db92007-09-25 01:57:46 +00004670
4671 assert(isFP && "Illegal integer SetCC!");
4672
Evan Chenge5f62042007-09-29 00:00:36 +00004673 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004674 switch (SetCCOpcode) {
4675 default: assert(false && "Illegal floating point SetCC!");
4676 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004677 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004678 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004679 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004680 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4681 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4682 }
4683 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004684 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004685 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004686 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004687 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4688 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4689 }
4690 }
4691}
4692
Nate Begeman30a0de92008-07-17 16:51:19 +00004693SDOperand X86TargetLowering::LowerVSETCC(SDOperand Op, SelectionDAG &DAG) {
4694 SDOperand Cond;
4695 SDOperand Op0 = Op.getOperand(0);
4696 SDOperand Op1 = Op.getOperand(1);
4697 SDOperand CC = Op.getOperand(2);
4698 MVT VT = Op.getValueType();
4699 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4700 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4701
4702 if (isFP) {
4703 unsigned SSECC = 8;
4704 unsigned Opc = Op0.getValueType() == MVT::v4f32 ? X86ISD::CMPPS :
4705 X86ISD::CMPPD;
4706 bool Swap = false;
4707
4708 switch (SetCCOpcode) {
4709 default: break;
4710 case ISD::SETEQ: SSECC = 0; break;
4711 case ISD::SETOGT:
4712 case ISD::SETGT: Swap = true; // Fallthrough
4713 case ISD::SETLT:
4714 case ISD::SETOLT: SSECC = 1; break;
4715 case ISD::SETOGE:
4716 case ISD::SETGE: Swap = true; // Fallthrough
4717 case ISD::SETLE:
4718 case ISD::SETOLE: SSECC = 2; break;
4719 case ISD::SETUO: SSECC = 3; break;
4720 case ISD::SETONE:
4721 case ISD::SETNE: SSECC = 4; break;
4722 case ISD::SETULE: Swap = true;
4723 case ISD::SETUGE: SSECC = 5; break;
4724 case ISD::SETULT: Swap = true;
4725 case ISD::SETUGT: SSECC = 6; break;
4726 case ISD::SETO: SSECC = 7; break;
4727 }
4728 if (Swap)
4729 std::swap(Op0, Op1);
4730
4731 // In the one special case we can't handle, emit two comparisons.
4732 if (SSECC == 8) {
4733 SDOperand UNORD, EQ;
4734
4735 assert(SetCCOpcode == ISD::SETUEQ && "Illegal FP comparison");
4736
4737 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4738 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4739 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4740 }
4741 // Handle all other FP comparisons here.
4742 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4743 }
4744
4745 // We are handling one of the integer comparisons here. Since SSE only has
4746 // GT and EQ comparisons for integer, swapping operands and multiple
4747 // operations may be required for some comparisons.
4748 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4749 bool Swap = false, Invert = false, FlipSigns = false;
4750
4751 switch (VT.getSimpleVT()) {
4752 default: break;
4753 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4754 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4755 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4756 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4757 }
4758
4759 switch (SetCCOpcode) {
4760 default: break;
4761 case ISD::SETNE: Invert = true;
4762 case ISD::SETEQ: Opc = EQOpc; break;
4763 case ISD::SETLT: Swap = true;
4764 case ISD::SETGT: Opc = GTOpc; break;
4765 case ISD::SETGE: Swap = true;
4766 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4767 case ISD::SETULT: Swap = true;
4768 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4769 case ISD::SETUGE: Swap = true;
4770 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4771 }
4772 if (Swap)
4773 std::swap(Op0, Op1);
4774
4775 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4776 // bits of the inputs before performing those operations.
4777 if (FlipSigns) {
4778 MVT EltVT = VT.getVectorElementType();
4779 SDOperand SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4780 std::vector<SDOperand> SignBits(VT.getVectorNumElements(), SignBit);
4781 SDOperand SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
4782 SignBits.size());
4783 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4784 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4785 }
4786
4787 SDOperand Result = DAG.getNode(Opc, VT, Op0, Op1);
4788
4789 // If the logical-not of the result is required, perform that now.
4790 if (Invert) {
4791 MVT EltVT = VT.getVectorElementType();
4792 SDOperand NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4793 std::vector<SDOperand> NegOnes(VT.getVectorNumElements(), NegOne);
4794 SDOperand NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
4795 NegOnes.size());
4796 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4797 }
4798 return Result;
4799}
Evan Cheng0488db92007-09-25 01:57:46 +00004800
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004802 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004803 SDOperand Cond = Op.getOperand(0);
4804 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004805
Evan Cheng734503b2006-09-11 02:19:56 +00004806 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004807 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004808
Evan Cheng3f41d662007-10-08 22:16:29 +00004809 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4810 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004811 if (Cond.getOpcode() == X86ISD::SETCC) {
4812 CC = Cond.getOperand(0);
4813
Evan Cheng734503b2006-09-11 02:19:56 +00004814 SDOperand Cmp = Cond.getOperand(1);
4815 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004816 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004817
Evan Cheng3f41d662007-10-08 22:16:29 +00004818 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004819 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00004820 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004821 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004822
Evan Chenge5f62042007-09-29 00:00:36 +00004823 if ((Opc == X86ISD::CMP ||
4824 Opc == X86ISD::COMI ||
4825 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004826 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004827 addTest = false;
4828 }
4829 }
4830
4831 if (addTest) {
4832 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004833 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004834 }
4835
Duncan Sands83ec4b62008-06-06 12:08:01 +00004836 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004837 MVT::Flag);
4838 SmallVector<SDOperand, 4> Ops;
4839 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4840 // condition is true.
4841 Ops.push_back(Op.getOperand(2));
4842 Ops.push_back(Op.getOperand(1));
4843 Ops.push_back(CC);
4844 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004845 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004846}
4847
Evan Cheng0db9fe62006-04-25 20:13:52 +00004848SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004849 bool addTest = true;
4850 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004851 SDOperand Cond = Op.getOperand(1);
4852 SDOperand Dest = Op.getOperand(2);
4853 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004854
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004856 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004857
Evan Cheng3f41d662007-10-08 22:16:29 +00004858 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4859 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004860 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004861 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004862
Evan Cheng734503b2006-09-11 02:19:56 +00004863 SDOperand Cmp = Cond.getOperand(1);
4864 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004865 if (Opc == X86ISD::CMP ||
4866 Opc == X86ISD::COMI ||
4867 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004868 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004869 addTest = false;
4870 }
4871 }
4872
4873 if (addTest) {
4874 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004875 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004876 }
Evan Chenge5f62042007-09-29 00:00:36 +00004877 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004878 Chain, Op.getOperand(2), CC, Cond);
4879}
4880
Anton Korobeynikove060b532007-04-17 19:34:00 +00004881
4882// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4883// Calls to _alloca is needed to probe the stack when allocating more than 4k
4884// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4885// that the guard pages used by the OS virtual memory manager are allocated in
4886// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004887SDOperand
4888X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4889 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004890 assert(Subtarget->isTargetCygMing() &&
4891 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004892
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004893 // Get the inputs.
4894 SDOperand Chain = Op.getOperand(0);
4895 SDOperand Size = Op.getOperand(1);
4896 // FIXME: Ensure alignment here
4897
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004898 SDOperand Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004899
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 MVT IntPtr = getPointerTy();
4901 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004902
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004903 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4904
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004905 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4906 Flag = Chain.getValue(1);
4907
4908 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4909 SDOperand Ops[] = { Chain,
4910 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4911 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004912 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004913 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004914 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004915 Flag = Chain.getValue(1);
4916
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004917 Chain = DAG.getCALLSEQ_END(Chain,
4918 DAG.getIntPtrConstant(0),
4919 DAG.getIntPtrConstant(0),
4920 Flag);
4921
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004922 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004923
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004924 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004925 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004926}
4927
Dan Gohman707e0182008-04-12 04:36:06 +00004928SDOperand
4929X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4930 SDOperand Chain,
4931 SDOperand Dst, SDOperand Src,
4932 SDOperand Size, unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00004933 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00004934 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935
Dan Gohman707e0182008-04-12 04:36:06 +00004936 /// If not DWORD aligned or size is more than the threshold, call the library.
4937 /// The libc version is likely to be faster for these cases. It can use the
4938 /// address value and run time information about the CPU.
4939 if ((Align & 3) == 0 ||
4940 !ConstantSize ||
4941 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4942 SDOperand InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00004943
4944 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00004945 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4946 if (const char *bzeroEntry =
4947 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004948 MVT IntPtr = getPointerTy();
Dan Gohman707e0182008-04-12 04:36:06 +00004949 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4950 TargetLowering::ArgListTy Args;
4951 TargetLowering::ArgListEntry Entry;
4952 Entry.Node = Dst;
Dan Gohman68d599d2008-04-01 20:38:36 +00004953 Entry.Ty = IntPtrTy;
4954 Args.push_back(Entry);
Dan Gohman707e0182008-04-12 04:36:06 +00004955 Entry.Node = Size;
4956 Args.push_back(Entry);
4957 std::pair<SDOperand,SDOperand> CallResult =
4958 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4959 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4960 Args, DAG);
4961 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00004962 }
4963
Dan Gohman707e0182008-04-12 04:36:06 +00004964 // Otherwise have the target-independent code call memset.
4965 return SDOperand();
Evan Cheng48090aa2006-03-21 23:01:21 +00004966 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004967
Dan Gohman707e0182008-04-12 04:36:06 +00004968 uint64_t SizeVal = ConstantSize->getValue();
4969 SDOperand InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004970 MVT AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004971 SDOperand Count;
Dan Gohman707e0182008-04-12 04:36:06 +00004972 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004973 unsigned BytesLeft = 0;
4974 bool TwoRepStos = false;
4975 if (ValC) {
4976 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004977 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004978
Evan Cheng0db9fe62006-04-25 20:13:52 +00004979 // If the value is a constant, then we can potentially use larger sets.
4980 switch (Align & 3) {
4981 case 2: // WORD aligned
4982 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004983 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004984 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004985 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004986 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004987 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004988 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004989 Val = (Val << 8) | Val;
4990 Val = (Val << 16) | Val;
Dan Gohman6f836ad2008-04-12 02:35:39 +00004991 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Evan Cheng25ab6902006-09-08 06:48:29 +00004992 AVT = MVT::i64;
4993 ValReg = X86::RAX;
4994 Val = (Val << 32) | Val;
4995 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004996 break;
4997 default: // Byte aligned
4998 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004999 ValReg = X86::AL;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005000 Count = DAG.getIntPtrConstant(SizeVal);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005001 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005002 }
5003
Duncan Sands8e4eb092008-06-08 20:54:56 +00005004 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005005 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005006 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5007 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005008 }
5009
Evan Cheng0db9fe62006-04-25 20:13:52 +00005010 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5011 InFlag);
5012 InFlag = Chain.getValue(1);
5013 } else {
5014 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005015 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005016 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005017 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005018 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005019
Evan Cheng25ab6902006-09-08 06:48:29 +00005020 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5021 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005022 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005023 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005024 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005025 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005026
Chris Lattnerd96d0722007-02-25 06:40:16 +00005027 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005028 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005029 Ops.push_back(Chain);
5030 Ops.push_back(DAG.getValueType(AVT));
5031 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005032 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005033
Evan Cheng0db9fe62006-04-25 20:13:52 +00005034 if (TwoRepStos) {
5035 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005036 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005037 MVT CVT = Count.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005038 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005039 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5040 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5041 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005042 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005043 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005044 Ops.clear();
5045 Ops.push_back(Chain);
5046 Ops.push_back(DAG.getValueType(MVT::i8));
5047 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005048 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005050 // Handle the last 1 - 7 bytes.
5051 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005052 MVT AddrVT = Dst.getValueType();
5053 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005054
5055 Chain = DAG.getMemset(Chain,
5056 DAG.getNode(ISD::ADD, AddrVT, Dst,
5057 DAG.getConstant(Offset, AddrVT)),
5058 Src,
5059 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005060 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005061 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005062
Dan Gohman707e0182008-04-12 04:36:06 +00005063 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005064 return Chain;
5065}
Evan Cheng11e15b32006-04-03 20:53:28 +00005066
Dan Gohman707e0182008-04-12 04:36:06 +00005067SDOperand
5068X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
5069 SDOperand Chain,
5070 SDOperand Dst, SDOperand Src,
5071 SDOperand Size, unsigned Align,
5072 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005073 const Value *DstSV, uint64_t DstSVOff,
5074 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00005075
5076 // This requires the copy size to be a constant, preferrably
5077 // within a subtarget-specific limit.
5078 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5079 if (!ConstantSize)
5080 return SDOperand();
5081 uint64_t SizeVal = ConstantSize->getValue();
5082 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
5083 return SDOperand();
5084
Duncan Sands83ec4b62008-06-06 12:08:01 +00005085 MVT AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005086 unsigned BytesLeft = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00005087 if (Align >= 8 && Subtarget->is64Bit())
5088 AVT = MVT::i64;
5089 else if (Align >= 4)
5090 AVT = MVT::i32;
5091 else if (Align >= 2)
5092 AVT = MVT::i16;
5093 else
5094 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095
Duncan Sands83ec4b62008-06-06 12:08:01 +00005096 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005097 unsigned CountVal = SizeVal / UBytes;
5098 SDOperand Count = DAG.getIntPtrConstant(CountVal);
5099 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005100
Evan Cheng0db9fe62006-04-25 20:13:52 +00005101 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005102 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5103 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005104 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005105 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005106 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005107 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005108 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005109 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005110 InFlag = Chain.getValue(1);
5111
Chris Lattnerd96d0722007-02-25 06:40:16 +00005112 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005113 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005114 Ops.push_back(Chain);
5115 Ops.push_back(DAG.getValueType(AVT));
5116 Ops.push_back(InFlag);
Evan Cheng2749c722008-04-25 00:26:43 +00005117 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005118
Evan Cheng2749c722008-04-25 00:26:43 +00005119 SmallVector<SDOperand, 4> Results;
5120 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005121 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005122 // Handle the last 1 - 7 bytes.
5123 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005124 MVT DstVT = Dst.getValueType();
5125 MVT SrcVT = Src.getValueType();
5126 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005127 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005128 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005129 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005130 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005131 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005132 DAG.getConstant(BytesLeft, SizeVT),
5133 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005134 DstSV, DstSVOff + Offset,
5135 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005136 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005137
Dan Gohman707e0182008-04-12 04:36:06 +00005138 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005139}
5140
Chris Lattner27a6c732007-11-24 07:07:01 +00005141/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5142SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005143 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00005144 SDOperand TheChain = N->getOperand(0);
5145 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005146 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005147 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5148 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5149 MVT::i64, rax.getValue(2));
5150 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005151 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00005152 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005153 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005154 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005155
Duncan Sands4bdcb612008-07-02 17:40:58 +00005156 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005157 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005158
Chris Lattner27a6c732007-11-24 07:07:01 +00005159 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5160 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5161 MVT::i32, eax.getValue(2));
5162 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5163 SDOperand Ops[] = { eax, edx };
5164 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5165
5166 // Use a MERGE_VALUES to return the value and chain.
5167 Ops[1] = edx.getValue(1);
Duncan Sands4bdcb612008-07-02 17:40:58 +00005168 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005169}
5170
5171SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005172 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005173
Evan Cheng25ab6902006-09-08 06:48:29 +00005174 if (!Subtarget->is64Bit()) {
5175 // vastart just stores the address of the VarArgsFrameIndex slot into the
5176 // memory location argument.
5177 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005178 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005179 }
5180
5181 // __va_list_tag:
5182 // gp_offset (0 - 6 * 8)
5183 // fp_offset (48 - 48 + 8 * 16)
5184 // overflow_arg_area (point to parameters coming in memory).
5185 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00005186 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00005187 SDOperand FIN = Op.getOperand(1);
5188 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00005189 SDOperand Store = DAG.getStore(Op.getOperand(0),
5190 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005191 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005192 MemOps.push_back(Store);
5193
5194 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005195 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005196 Store = DAG.getStore(Op.getOperand(0),
5197 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005198 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005199 MemOps.push_back(Store);
5200
5201 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005202 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng25ab6902006-09-08 06:48:29 +00005203 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005204 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005205 MemOps.push_back(Store);
5206
5207 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005208 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Evan Cheng25ab6902006-09-08 06:48:29 +00005209 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005210 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005211 MemOps.push_back(Store);
5212 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005213}
5214
Dan Gohman9018e832008-05-10 01:26:14 +00005215SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5216 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5217 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5218 SDOperand Chain = Op.getOperand(0);
5219 SDOperand SrcPtr = Op.getOperand(1);
5220 SDOperand SrcSV = Op.getOperand(2);
5221
5222 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5223 abort();
Dan Gohman2ce38982008-05-12 16:17:19 +00005224 return SDOperand();
Dan Gohman9018e832008-05-10 01:26:14 +00005225}
5226
Evan Chengae642192007-03-02 23:16:35 +00005227SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5228 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005229 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Evan Chengae642192007-03-02 23:16:35 +00005230 SDOperand Chain = Op.getOperand(0);
5231 SDOperand DstPtr = Op.getOperand(1);
5232 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005233 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5234 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005235
Dan Gohman28269132008-04-18 20:55:41 +00005236 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5237 DAG.getIntPtrConstant(24), 8, false,
5238 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005239}
5240
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241SDOperand
5242X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5243 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5244 switch (IntNo) {
5245 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005246 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247 case Intrinsic::x86_sse_comieq_ss:
5248 case Intrinsic::x86_sse_comilt_ss:
5249 case Intrinsic::x86_sse_comile_ss:
5250 case Intrinsic::x86_sse_comigt_ss:
5251 case Intrinsic::x86_sse_comige_ss:
5252 case Intrinsic::x86_sse_comineq_ss:
5253 case Intrinsic::x86_sse_ucomieq_ss:
5254 case Intrinsic::x86_sse_ucomilt_ss:
5255 case Intrinsic::x86_sse_ucomile_ss:
5256 case Intrinsic::x86_sse_ucomigt_ss:
5257 case Intrinsic::x86_sse_ucomige_ss:
5258 case Intrinsic::x86_sse_ucomineq_ss:
5259 case Intrinsic::x86_sse2_comieq_sd:
5260 case Intrinsic::x86_sse2_comilt_sd:
5261 case Intrinsic::x86_sse2_comile_sd:
5262 case Intrinsic::x86_sse2_comigt_sd:
5263 case Intrinsic::x86_sse2_comige_sd:
5264 case Intrinsic::x86_sse2_comineq_sd:
5265 case Intrinsic::x86_sse2_ucomieq_sd:
5266 case Intrinsic::x86_sse2_ucomilt_sd:
5267 case Intrinsic::x86_sse2_ucomile_sd:
5268 case Intrinsic::x86_sse2_ucomigt_sd:
5269 case Intrinsic::x86_sse2_ucomige_sd:
5270 case Intrinsic::x86_sse2_ucomineq_sd: {
5271 unsigned Opc = 0;
5272 ISD::CondCode CC = ISD::SETCC_INVALID;
5273 switch (IntNo) {
5274 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005275 case Intrinsic::x86_sse_comieq_ss:
5276 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 Opc = X86ISD::COMI;
5278 CC = ISD::SETEQ;
5279 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005280 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005281 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005282 Opc = X86ISD::COMI;
5283 CC = ISD::SETLT;
5284 break;
5285 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005286 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005287 Opc = X86ISD::COMI;
5288 CC = ISD::SETLE;
5289 break;
5290 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005291 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292 Opc = X86ISD::COMI;
5293 CC = ISD::SETGT;
5294 break;
5295 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005296 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297 Opc = X86ISD::COMI;
5298 CC = ISD::SETGE;
5299 break;
5300 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005301 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005302 Opc = X86ISD::COMI;
5303 CC = ISD::SETNE;
5304 break;
5305 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005306 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005307 Opc = X86ISD::UCOMI;
5308 CC = ISD::SETEQ;
5309 break;
5310 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005311 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005312 Opc = X86ISD::UCOMI;
5313 CC = ISD::SETLT;
5314 break;
5315 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005316 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005317 Opc = X86ISD::UCOMI;
5318 CC = ISD::SETLE;
5319 break;
5320 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005321 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005322 Opc = X86ISD::UCOMI;
5323 CC = ISD::SETGT;
5324 break;
5325 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005326 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005327 Opc = X86ISD::UCOMI;
5328 CC = ISD::SETGE;
5329 break;
5330 case Intrinsic::x86_sse_ucomineq_ss:
5331 case Intrinsic::x86_sse2_ucomineq_sd:
5332 Opc = X86ISD::UCOMI;
5333 CC = ISD::SETNE;
5334 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005335 }
Evan Cheng734503b2006-09-11 02:19:56 +00005336
Evan Cheng0db9fe62006-04-25 20:13:52 +00005337 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00005338 SDOperand LHS = Op.getOperand(1);
5339 SDOperand RHS = Op.getOperand(2);
5340 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005341
Evan Chenge5f62042007-09-29 00:00:36 +00005342 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5343 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5344 DAG.getConstant(X86CC, MVT::i8), Cond);
5345 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005346 }
Evan Cheng5759f972008-05-04 09:15:50 +00005347
5348 // Fix vector shift instructions where the last operand is a non-immediate
5349 // i32 value.
5350 case Intrinsic::x86_sse2_pslli_w:
5351 case Intrinsic::x86_sse2_pslli_d:
5352 case Intrinsic::x86_sse2_pslli_q:
5353 case Intrinsic::x86_sse2_psrli_w:
5354 case Intrinsic::x86_sse2_psrli_d:
5355 case Intrinsic::x86_sse2_psrli_q:
5356 case Intrinsic::x86_sse2_psrai_w:
5357 case Intrinsic::x86_sse2_psrai_d:
5358 case Intrinsic::x86_mmx_pslli_w:
5359 case Intrinsic::x86_mmx_pslli_d:
5360 case Intrinsic::x86_mmx_pslli_q:
5361 case Intrinsic::x86_mmx_psrli_w:
5362 case Intrinsic::x86_mmx_psrli_d:
5363 case Intrinsic::x86_mmx_psrli_q:
5364 case Intrinsic::x86_mmx_psrai_w:
5365 case Intrinsic::x86_mmx_psrai_d: {
5366 SDOperand ShAmt = Op.getOperand(2);
5367 if (isa<ConstantSDNode>(ShAmt))
5368 return SDOperand();
5369
5370 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005371 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005372 switch (IntNo) {
5373 case Intrinsic::x86_sse2_pslli_w:
5374 NewIntNo = Intrinsic::x86_sse2_psll_w;
5375 break;
5376 case Intrinsic::x86_sse2_pslli_d:
5377 NewIntNo = Intrinsic::x86_sse2_psll_d;
5378 break;
5379 case Intrinsic::x86_sse2_pslli_q:
5380 NewIntNo = Intrinsic::x86_sse2_psll_q;
5381 break;
5382 case Intrinsic::x86_sse2_psrli_w:
5383 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5384 break;
5385 case Intrinsic::x86_sse2_psrli_d:
5386 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5387 break;
5388 case Intrinsic::x86_sse2_psrli_q:
5389 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5390 break;
5391 case Intrinsic::x86_sse2_psrai_w:
5392 NewIntNo = Intrinsic::x86_sse2_psra_w;
5393 break;
5394 case Intrinsic::x86_sse2_psrai_d:
5395 NewIntNo = Intrinsic::x86_sse2_psra_d;
5396 break;
5397 default: {
5398 ShAmtVT = MVT::v2i32;
5399 switch (IntNo) {
5400 case Intrinsic::x86_mmx_pslli_w:
5401 NewIntNo = Intrinsic::x86_mmx_psll_w;
5402 break;
5403 case Intrinsic::x86_mmx_pslli_d:
5404 NewIntNo = Intrinsic::x86_mmx_psll_d;
5405 break;
5406 case Intrinsic::x86_mmx_pslli_q:
5407 NewIntNo = Intrinsic::x86_mmx_psll_q;
5408 break;
5409 case Intrinsic::x86_mmx_psrli_w:
5410 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5411 break;
5412 case Intrinsic::x86_mmx_psrli_d:
5413 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5414 break;
5415 case Intrinsic::x86_mmx_psrli_q:
5416 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5417 break;
5418 case Intrinsic::x86_mmx_psrai_w:
5419 NewIntNo = Intrinsic::x86_mmx_psra_w;
5420 break;
5421 case Intrinsic::x86_mmx_psrai_d:
5422 NewIntNo = Intrinsic::x86_mmx_psra_d;
5423 break;
5424 default: abort(); // Can't reach here.
5425 }
5426 break;
5427 }
5428 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005429 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005430 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5431 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5432 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5433 DAG.getConstant(NewIntNo, MVT::i32),
5434 Op.getOperand(1), ShAmt);
5435 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005436 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005437}
Evan Cheng72261582005-12-20 06:22:03 +00005438
Nate Begemanbcc5f362007-01-29 22:58:52 +00005439SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5440 // Depths > 0 not supported yet!
5441 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5442 return SDOperand();
5443
5444 // Just load the return address
5445 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5446 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5447}
5448
5449SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5450 // Depths > 0 not supported yet!
5451 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5452 return SDOperand();
5453
5454 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5455 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling71ca3532008-07-11 07:18:52 +00005456 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Nate Begemanbcc5f362007-01-29 22:58:52 +00005457}
5458
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005459SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5460 SelectionDAG &DAG) {
5461 // Is not yet supported on x86-64
5462 if (Subtarget->is64Bit())
5463 return SDOperand();
5464
Chris Lattner0bd48932008-01-17 07:00:52 +00005465 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005466}
5467
5468SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5469{
5470 assert(!Subtarget->is64Bit() &&
5471 "Lowering of eh_return builtin is not supported yet on x86-64");
5472
5473 MachineFunction &MF = DAG.getMachineFunction();
5474 SDOperand Chain = Op.getOperand(0);
5475 SDOperand Offset = Op.getOperand(1);
5476 SDOperand Handler = Op.getOperand(2);
5477
5478 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5479 getPointerTy());
5480
5481 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00005482 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005483 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5484 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5485 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00005486 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005487
5488 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5489 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5490}
5491
Duncan Sandsb116fac2007-07-27 20:02:49 +00005492SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5493 SelectionDAG &DAG) {
5494 SDOperand Root = Op.getOperand(0);
5495 SDOperand Trmp = Op.getOperand(1); // trampoline
5496 SDOperand FPtr = Op.getOperand(2); // nested function
5497 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5498
Dan Gohman69de1932008-02-06 22:27:42 +00005499 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005500
Duncan Sands339e14f2008-01-16 22:55:25 +00005501 const X86InstrInfo *TII =
5502 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5503
Duncan Sandsb116fac2007-07-27 20:02:49 +00005504 if (Subtarget->is64Bit()) {
Duncan Sands339e14f2008-01-16 22:55:25 +00005505 SDOperand OutChains[6];
5506
5507 // Large code-model.
5508
5509 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5510 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5511
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005512 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5513 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005514
5515 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5516
5517 // Load the pointer to the nested function into R11.
5518 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5519 SDOperand Addr = Trmp;
5520 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005521 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005522
5523 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005524 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005525
5526 // Load the 'nest' parameter value into R10.
5527 // R10 is specified in X86CallingConv.td
5528 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5529 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5530 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005531 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005532
5533 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005534 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005535
5536 // Jump to the nested function.
5537 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5538 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5539 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005540 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005541
5542 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5543 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5544 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005545 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005546
5547 SDOperand Ops[] =
5548 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005549 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005550 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005551 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005552 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5553 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005554 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005555
5556 switch (CC) {
5557 default:
5558 assert(0 && "Unsupported calling convention");
5559 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005560 case CallingConv::X86_StdCall: {
5561 // Pass 'nest' parameter in ECX.
5562 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005563 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005564
5565 // Check that ECX wasn't needed by an 'inreg' parameter.
5566 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner58d74912008-03-12 17:45:29 +00005567 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005568
Chris Lattner58d74912008-03-12 17:45:29 +00005569 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005570 unsigned InRegCount = 0;
5571 unsigned Idx = 1;
5572
5573 for (FunctionType::param_iterator I = FTy->param_begin(),
5574 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner58d74912008-03-12 17:45:29 +00005575 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005576 // FIXME: should only count parameters that are lowered to integers.
5577 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5578
5579 if (InRegCount > 2) {
5580 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5581 abort();
5582 }
5583 }
5584 break;
5585 }
5586 case CallingConv::X86_FastCall:
5587 // Pass 'nest' parameter in EAX.
5588 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005589 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005590 break;
5591 }
5592
5593 SDOperand OutChains[4];
5594 SDOperand Addr, Disp;
5595
5596 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5597 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5598
Duncan Sands339e14f2008-01-16 22:55:25 +00005599 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005600 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005601 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005602 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005603
5604 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005605 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005606
Duncan Sands339e14f2008-01-16 22:55:25 +00005607 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005608 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5609 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005610 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005611
5612 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005613 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005614
Duncan Sandsf7331b32007-09-11 14:10:23 +00005615 SDOperand Ops[] =
5616 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005617 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005618 }
5619}
5620
Dan Gohman1a024862008-01-31 00:41:03 +00005621SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005622 /*
5623 The rounding mode is in bits 11:10 of FPSR, and has the following
5624 settings:
5625 00 Round to nearest
5626 01 Round to -inf
5627 10 Round to +inf
5628 11 Round to 0
5629
5630 FLT_ROUNDS, on the other hand, expects the following:
5631 -1 Undefined
5632 0 Round to 0
5633 1 Round to nearest
5634 2 Round to +inf
5635 3 Round to -inf
5636
5637 To perform the conversion, we do:
5638 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5639 */
5640
5641 MachineFunction &MF = DAG.getMachineFunction();
5642 const TargetMachine &TM = MF.getTarget();
5643 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5644 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005645 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005646
5647 // Save FP Control Word to stack slot
5648 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5649 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5650
5651 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5652 DAG.getEntryNode(), StackSlot);
5653
5654 // Load FP Control Word from stack slot
5655 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5656
5657 // Transform as necessary
5658 SDOperand CWD1 =
5659 DAG.getNode(ISD::SRL, MVT::i16,
5660 DAG.getNode(ISD::AND, MVT::i16,
5661 CWD, DAG.getConstant(0x800, MVT::i16)),
5662 DAG.getConstant(11, MVT::i8));
5663 SDOperand CWD2 =
5664 DAG.getNode(ISD::SRL, MVT::i16,
5665 DAG.getNode(ISD::AND, MVT::i16,
5666 CWD, DAG.getConstant(0x400, MVT::i16)),
5667 DAG.getConstant(9, MVT::i8));
5668
5669 SDOperand RetVal =
5670 DAG.getNode(ISD::AND, MVT::i16,
5671 DAG.getNode(ISD::ADD, MVT::i16,
5672 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5673 DAG.getConstant(1, MVT::i16)),
5674 DAG.getConstant(3, MVT::i16));
5675
5676
Duncan Sands83ec4b62008-06-06 12:08:01 +00005677 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005678 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5679}
5680
Evan Cheng18efe262007-12-14 02:13:44 +00005681SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005682 MVT VT = Op.getValueType();
5683 MVT OpVT = VT;
5684 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005685
5686 Op = Op.getOperand(0);
5687 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005688 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005689 OpVT = MVT::i32;
5690 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5691 }
Evan Cheng18efe262007-12-14 02:13:44 +00005692
Evan Cheng152804e2007-12-14 08:30:15 +00005693 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5694 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5695 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5696
5697 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5698 SmallVector<SDOperand, 4> Ops;
5699 Ops.push_back(Op);
5700 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5701 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5702 Ops.push_back(Op.getValue(1));
5703 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5704
5705 // Finally xor with NumBits-1.
5706 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5707
Evan Cheng18efe262007-12-14 02:13:44 +00005708 if (VT == MVT::i8)
5709 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5710 return Op;
5711}
5712
5713SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005714 MVT VT = Op.getValueType();
5715 MVT OpVT = VT;
5716 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005717
5718 Op = Op.getOperand(0);
5719 if (VT == MVT::i8) {
5720 OpVT = MVT::i32;
5721 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5722 }
Evan Cheng152804e2007-12-14 08:30:15 +00005723
5724 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5725 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5726 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5727
5728 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5729 SmallVector<SDOperand, 4> Ops;
5730 Ops.push_back(Op);
5731 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5732 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5733 Ops.push_back(Op.getValue(1));
5734 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5735
Evan Cheng18efe262007-12-14 02:13:44 +00005736 if (VT == MVT::i8)
5737 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5738 return Op;
5739}
5740
Mon P Wang28873102008-06-25 08:15:39 +00005741SDOperand X86TargetLowering::LowerCMP_SWAP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005742 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00005743 unsigned Reg = 0;
5744 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005745 switch(T.getSimpleVT()) {
5746 default:
5747 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005748 case MVT::i8: Reg = X86::AL; size = 1; break;
5749 case MVT::i16: Reg = X86::AX; size = 2; break;
5750 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005751 case MVT::i64:
5752 if (Subtarget->is64Bit()) {
5753 Reg = X86::RAX; size = 8;
5754 } else //Should go away when LowerType stuff lands
Mon P Wang28873102008-06-25 08:15:39 +00005755 return SDOperand(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005756 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005757 };
5758 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharthce1105d2008-03-01 22:27:48 +00005759 Op.getOperand(3), SDOperand());
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005760 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005761 Op.getOperand(1),
5762 Op.getOperand(2),
5763 DAG.getTargetConstant(size, MVT::i8),
5764 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005765 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5766 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5767 SDOperand cpOut =
5768 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5769 return cpOut;
5770}
5771
Mon P Wang28873102008-06-25 08:15:39 +00005772SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005773 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005774 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005775 SDOperand cpInL, cpInH;
5776 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5777 DAG.getConstant(0, MVT::i32));
5778 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5779 DAG.getConstant(1, MVT::i32));
5780 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5781 cpInL, SDOperand());
5782 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5783 cpInH, cpInL.getValue(1));
5784 SDOperand swapInL, swapInH;
5785 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5786 DAG.getConstant(0, MVT::i32));
5787 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5788 DAG.getConstant(1, MVT::i32));
5789 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5790 swapInL, cpInH.getValue(1));
5791 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5792 swapInH, swapInL.getValue(1));
5793 SDOperand Ops[] = { swapInH.getValue(0),
5794 Op->getOperand(1),
5795 swapInH.getValue(1)};
5796 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5797 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5798 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5799 Result.getValue(1));
5800 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5801 cpOutL.getValue(2));
5802 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5803 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
Duncan Sandsf9516202008-06-30 10:19:09 +00005804 SDOperand Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005805 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005806}
5807
Mon P Wang28873102008-06-25 08:15:39 +00005808SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005809 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005810 assert (T == MVT::i32 && "Only know how to expand i32 Atomic Load Sub");
Mon P Wang63307c32008-05-05 19:05:59 +00005811 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5812 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00005813 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005814 Op->getOperand(1), negOp,
Mon P Wang28873102008-06-25 08:15:39 +00005815 cast<AtomicSDNode>(Op)->getSrcValue(),
5816 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang63307c32008-05-05 19:05:59 +00005817}
5818
Evan Cheng0db9fe62006-04-25 20:13:52 +00005819/// LowerOperation - Provide custom lowering hooks for some operations.
5820///
5821SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5822 switch (Op.getOpcode()) {
5823 default: assert(0 && "Should not custom lower this!");
Mon P Wang28873102008-06-25 08:15:39 +00005824 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005825 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5826 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5827 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5828 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5829 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5830 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5831 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005832 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005833 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5834 case ISD::SHL_PARTS:
5835 case ISD::SRA_PARTS:
5836 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5837 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5838 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5839 case ISD::FABS: return LowerFABS(Op, DAG);
5840 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005841 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005842 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00005843 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005844 case ISD::SELECT: return LowerSELECT(Op, DAG);
5845 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005846 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005847 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005849 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005850 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00005851 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005852 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005853 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005854 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5855 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005856 case ISD::FRAME_TO_ARGS_OFFSET:
5857 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005858 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005859 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005860 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005861 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005862 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5863 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005864
5865 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5866 case ISD::READCYCLECOUNTER:
5867 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005868 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005869}
5870
Duncan Sands126d9072008-07-04 11:47:58 +00005871/// ReplaceNodeResults - Replace a node with an illegal result type
5872/// with a new node built out of custom code.
5873SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005874 switch (N->getOpcode()) {
5875 default: assert(0 && "Should not custom lower this!");
5876 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5877 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang28873102008-06-25 08:15:39 +00005878 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5879 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005880 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005881}
5882
Evan Cheng72261582005-12-20 06:22:03 +00005883const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5884 switch (Opcode) {
5885 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005886 case X86ISD::BSF: return "X86ISD::BSF";
5887 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005888 case X86ISD::SHLD: return "X86ISD::SHLD";
5889 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005890 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005891 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005892 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005893 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005894 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005895 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005896 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5897 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5898 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005899 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005900 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00005901 case X86ISD::CALL: return "X86ISD::CALL";
5902 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5903 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5904 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005905 case X86ISD::COMI: return "X86ISD::COMI";
5906 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005907 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005908 case X86ISD::CMOV: return "X86ISD::CMOV";
5909 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005910 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005911 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5912 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005913 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005914 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005915 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00005916 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005917 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5918 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00005919 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005920 case X86ISD::FMAX: return "X86ISD::FMAX";
5921 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005922 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5923 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005924 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5925 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005926 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005927 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005928 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00005929 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5930 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00005931 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5932 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00005933 case X86ISD::VSHL: return "X86ISD::VSHL";
5934 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00005935 case X86ISD::CMPPD: return "X86ISD::CMPPD";
5936 case X86ISD::CMPPS: return "X86ISD::CMPPS";
5937 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
5938 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
5939 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
5940 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
5941 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
5942 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
5943 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
5944 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Evan Cheng72261582005-12-20 06:22:03 +00005945 }
5946}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005947
Chris Lattnerc9addb72007-03-30 23:15:24 +00005948// isLegalAddressingMode - Return true if the addressing mode represented
5949// by AM is legal for this target, for a load/store of the specified type.
5950bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5951 const Type *Ty) const {
5952 // X86 supports extremely general addressing modes.
5953
5954 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5955 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5956 return false;
5957
5958 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005959 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005960 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5961 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005962
5963 // X86-64 only supports addr of globals in small code model.
5964 if (Subtarget->is64Bit()) {
5965 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5966 return false;
5967 // If lower 4G is not available, then we must use rip-relative addressing.
5968 if (AM.BaseOffs || AM.Scale > 1)
5969 return false;
5970 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005971 }
5972
5973 switch (AM.Scale) {
5974 case 0:
5975 case 1:
5976 case 2:
5977 case 4:
5978 case 8:
5979 // These scales always work.
5980 break;
5981 case 3:
5982 case 5:
5983 case 9:
5984 // These scales are formed with basereg+scalereg. Only accept if there is
5985 // no basereg yet.
5986 if (AM.HasBaseReg)
5987 return false;
5988 break;
5989 default: // Other stuff never works.
5990 return false;
5991 }
5992
5993 return true;
5994}
5995
5996
Evan Cheng2bd122c2007-10-26 01:56:11 +00005997bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5998 if (!Ty1->isInteger() || !Ty2->isInteger())
5999 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006000 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6001 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006002 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006003 return false;
6004 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006005}
6006
Duncan Sands83ec4b62008-06-06 12:08:01 +00006007bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6008 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006009 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006010 unsigned NumBits1 = VT1.getSizeInBits();
6011 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006012 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006013 return false;
6014 return Subtarget->is64Bit() || NumBits1 < 64;
6015}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006016
Evan Cheng60c07e12006-07-05 22:17:51 +00006017/// isShuffleMaskLegal - Targets can use this to indicate that they only
6018/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6019/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6020/// are assumed to be legal.
6021bool
Duncan Sands83ec4b62008-06-06 12:08:01 +00006022X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006023 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006024 if (VT.getSizeInBits() == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006025 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00006026 isIdentityMask(Mask.Val) ||
6027 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006028 isSplatMask(Mask.Val) ||
6029 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6030 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006031 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006032 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006033 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00006034}
6035
Dan Gohman7d8143f2008-04-09 20:09:42 +00006036bool
6037X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006038 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006039 unsigned NumElts = BVOps.size();
6040 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006041 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006042 if (NumElts == 2) return true;
6043 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006044 return (isMOVLMask(&BVOps[0], 4) ||
6045 isCommutedMOVL(&BVOps[0], 4, true) ||
6046 isSHUFPMask(&BVOps[0], 4) ||
6047 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006048 }
6049 return false;
6050}
6051
6052//===----------------------------------------------------------------------===//
6053// X86 Scheduler Hooks
6054//===----------------------------------------------------------------------===//
6055
Mon P Wang63307c32008-05-05 19:05:59 +00006056// private utility function
6057MachineBasicBlock *
6058X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6059 MachineBasicBlock *MBB,
6060 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006061 unsigned immOpc,
6062 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006063 // For the atomic bitwise operator, we generate
6064 // thisMBB:
6065 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006066 // ld t1 = [bitinstr.addr]
6067 // op t2 = t1, [bitinstr.val]
6068 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006069 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6070 // bz newMBB
6071 // fallthrough -->nextMBB
6072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6073 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006074 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006075 ++MBBIter;
6076
6077 /// First build the CFG
6078 MachineFunction *F = MBB->getParent();
6079 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006080 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6081 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6082 F->insert(MBBIter, newMBB);
6083 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006084
6085 // Move all successors to thisMBB to nextMBB
6086 nextMBB->transferSuccessors(thisMBB);
6087
6088 // Update thisMBB to fall through to newMBB
6089 thisMBB->addSuccessor(newMBB);
6090
6091 // newMBB jumps to itself and fall through to nextMBB
6092 newMBB->addSuccessor(nextMBB);
6093 newMBB->addSuccessor(newMBB);
6094
6095 // Insert instructions into newMBB based on incoming instruction
6096 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6097 MachineOperand& destOper = bInstr->getOperand(0);
6098 MachineOperand* argOpers[6];
6099 int numArgs = bInstr->getNumOperands() - 1;
6100 for (int i=0; i < numArgs; ++i)
6101 argOpers[i] = &bInstr->getOperand(i+1);
6102
6103 // x86 address has 4 operands: base, index, scale, and displacement
6104 int lastAddrIndx = 3; // [0,3]
6105 int valArgIndx = 4;
6106
Mon P Wangab3e7472008-05-05 22:56:23 +00006107 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6108 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006109 for (int i=0; i <= lastAddrIndx; ++i)
6110 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006111
6112 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6113 if (invSrc) {
6114 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6115 }
6116 else
6117 tt = t1;
6118
Mon P Wang63307c32008-05-05 19:05:59 +00006119 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6120 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6121 && "invalid operand");
6122 if (argOpers[valArgIndx]->isReg())
6123 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6124 else
6125 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006126 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006127 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006128
Mon P Wangab3e7472008-05-05 22:56:23 +00006129 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6130 MIB.addReg(t1);
6131
Mon P Wang63307c32008-05-05 19:05:59 +00006132 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6133 for (int i=0; i <= lastAddrIndx; ++i)
6134 (*MIB).addOperand(*argOpers[i]);
6135 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00006136 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6137 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6138
Mon P Wang63307c32008-05-05 19:05:59 +00006139 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6140 MIB.addReg(X86::EAX);
6141
6142 // insert branch
6143 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6144
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006145 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006146 return nextMBB;
6147}
6148
6149// private utility function
6150MachineBasicBlock *
6151X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6152 MachineBasicBlock *MBB,
6153 unsigned cmovOpc) {
6154 // For the atomic min/max operator, we generate
6155 // thisMBB:
6156 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006157 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006158 // mov t2 = [min/max.val]
6159 // cmp t1, t2
6160 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006161 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006162 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6163 // bz newMBB
6164 // fallthrough -->nextMBB
6165 //
6166 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6167 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006168 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006169 ++MBBIter;
6170
6171 /// First build the CFG
6172 MachineFunction *F = MBB->getParent();
6173 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006174 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6175 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6176 F->insert(MBBIter, newMBB);
6177 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006178
6179 // Move all successors to thisMBB to nextMBB
6180 nextMBB->transferSuccessors(thisMBB);
6181
6182 // Update thisMBB to fall through to newMBB
6183 thisMBB->addSuccessor(newMBB);
6184
6185 // newMBB jumps to newMBB and fall through to nextMBB
6186 newMBB->addSuccessor(nextMBB);
6187 newMBB->addSuccessor(newMBB);
6188
6189 // Insert instructions into newMBB based on incoming instruction
6190 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6191 MachineOperand& destOper = mInstr->getOperand(0);
6192 MachineOperand* argOpers[6];
6193 int numArgs = mInstr->getNumOperands() - 1;
6194 for (int i=0; i < numArgs; ++i)
6195 argOpers[i] = &mInstr->getOperand(i+1);
6196
6197 // x86 address has 4 operands: base, index, scale, and displacement
6198 int lastAddrIndx = 3; // [0,3]
6199 int valArgIndx = 4;
6200
Mon P Wangab3e7472008-05-05 22:56:23 +00006201 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6202 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006203 for (int i=0; i <= lastAddrIndx; ++i)
6204 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006205
Mon P Wang63307c32008-05-05 19:05:59 +00006206 // We only support register and immediate values
6207 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6208 && "invalid operand");
6209
6210 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6211 if (argOpers[valArgIndx]->isReg())
6212 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6213 else
6214 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6215 (*MIB).addOperand(*argOpers[valArgIndx]);
6216
Mon P Wangab3e7472008-05-05 22:56:23 +00006217 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6218 MIB.addReg(t1);
6219
Mon P Wang63307c32008-05-05 19:05:59 +00006220 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6221 MIB.addReg(t1);
6222 MIB.addReg(t2);
6223
6224 // Generate movc
6225 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6226 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6227 MIB.addReg(t2);
6228 MIB.addReg(t1);
6229
6230 // Cmp and exchange if none has modified the memory location
6231 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6232 for (int i=0; i <= lastAddrIndx; ++i)
6233 (*MIB).addOperand(*argOpers[i]);
6234 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00006235 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6236 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00006237
6238 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6239 MIB.addReg(X86::EAX);
6240
6241 // insert branch
6242 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6243
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006244 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006245 return nextMBB;
6246}
6247
6248
Evan Cheng60c07e12006-07-05 22:17:51 +00006249MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006250X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6251 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006253 switch (MI->getOpcode()) {
6254 default: assert(false && "Unexpected instr type to insert");
6255 case X86::CMOV_FR32:
6256 case X86::CMOV_FR64:
6257 case X86::CMOV_V4F32:
6258 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006259 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006260 // To "insert" a SELECT_CC instruction, we actually have to insert the
6261 // diamond control-flow pattern. The incoming instruction knows the
6262 // destination vreg to set, the condition code register to branch on, the
6263 // true/false values to select between, and a branch opcode to use.
6264 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006265 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00006266 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006267
Evan Cheng60c07e12006-07-05 22:17:51 +00006268 // thisMBB:
6269 // ...
6270 // TrueVal = ...
6271 // cmpTY ccX, r1, r2
6272 // bCC copy1MBB
6273 // fallthrough --> copy0MBB
6274 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006275 MachineFunction *F = BB->getParent();
6276 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6277 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006278 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006279 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006280 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006281 F->insert(It, copy0MBB);
6282 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006283 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006284 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006285 sinkMBB->transferSuccessors(BB);
6286
6287 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006288 BB->addSuccessor(copy0MBB);
6289 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006290
Evan Cheng60c07e12006-07-05 22:17:51 +00006291 // copy0MBB:
6292 // %FalseValue = ...
6293 // # fallthrough to sinkMBB
6294 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006295
Evan Cheng60c07e12006-07-05 22:17:51 +00006296 // Update machine-CFG edges
6297 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006298
Evan Cheng60c07e12006-07-05 22:17:51 +00006299 // sinkMBB:
6300 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6301 // ...
6302 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006303 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006304 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6305 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6306
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006307 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006308 return BB;
6309 }
6310
Dale Johannesen849f2142007-07-03 00:53:03 +00006311 case X86::FP32_TO_INT16_IN_MEM:
6312 case X86::FP32_TO_INT32_IN_MEM:
6313 case X86::FP32_TO_INT64_IN_MEM:
6314 case X86::FP64_TO_INT16_IN_MEM:
6315 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006316 case X86::FP64_TO_INT64_IN_MEM:
6317 case X86::FP80_TO_INT16_IN_MEM:
6318 case X86::FP80_TO_INT32_IN_MEM:
6319 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006320 // Change the floating point control register to use "round towards zero"
6321 // mode when truncating to an integer value.
6322 MachineFunction *F = BB->getParent();
6323 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006324 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006325
6326 // Load the old value of the high byte of the control word...
6327 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006328 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006329 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006330
6331 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006332 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6333 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006334
6335 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006336 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006337
6338 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006339 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6340 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006341
6342 // Get the X86 opcode to use.
6343 unsigned Opc;
6344 switch (MI->getOpcode()) {
6345 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006346 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6347 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6348 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6349 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6350 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6351 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006352 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6353 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6354 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006355 }
6356
6357 X86AddressMode AM;
6358 MachineOperand &Op = MI->getOperand(0);
6359 if (Op.isRegister()) {
6360 AM.BaseType = X86AddressMode::RegBase;
6361 AM.Base.Reg = Op.getReg();
6362 } else {
6363 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006364 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006365 }
6366 Op = MI->getOperand(1);
6367 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006368 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006369 Op = MI->getOperand(2);
6370 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006371 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006372 Op = MI->getOperand(3);
6373 if (Op.isGlobalAddress()) {
6374 AM.GV = Op.getGlobal();
6375 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006376 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006377 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006378 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6379 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006380
6381 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00006382 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006383
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006384 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006385 return BB;
6386 }
Mon P Wang63307c32008-05-05 19:05:59 +00006387 case X86::ATOMAND32:
6388 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6389 X86::AND32ri);
6390 case X86::ATOMOR32:
6391 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6392 X86::OR32ri);
6393 case X86::ATOMXOR32:
6394 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6395 X86::XOR32ri);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006396 case X86::ATOMNAND32:
6397 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6398 X86::AND32ri, true);
Mon P Wang63307c32008-05-05 19:05:59 +00006399 case X86::ATOMMIN32:
6400 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6401 case X86::ATOMMAX32:
6402 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6403 case X86::ATOMUMIN32:
6404 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6405 case X86::ATOMUMAX32:
6406 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Evan Cheng60c07e12006-07-05 22:17:51 +00006407 }
6408}
6409
6410//===----------------------------------------------------------------------===//
6411// X86 Optimization Hooks
6412//===----------------------------------------------------------------------===//
6413
Nate Begeman368e18d2006-02-16 21:11:51 +00006414void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006415 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006416 APInt &KnownZero,
6417 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006418 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00006419 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006420 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00006421 assert((Opc >= ISD::BUILTIN_OP_END ||
6422 Opc == ISD::INTRINSIC_WO_CHAIN ||
6423 Opc == ISD::INTRINSIC_W_CHAIN ||
6424 Opc == ISD::INTRINSIC_VOID) &&
6425 "Should use MaskedValueIsZero if you don't know whether Op"
6426 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006427
Dan Gohmanf4f92f52008-02-13 23:07:24 +00006428 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006429 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00006430 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006431 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006432 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6433 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00006434 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006435 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006436}
Chris Lattner259e97c2006-01-31 19:43:35 +00006437
Evan Cheng206ee9d2006-07-07 08:33:52 +00006438/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00006439/// node is a GlobalAddress + offset.
6440bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6441 GlobalValue* &GA, int64_t &Offset) const{
6442 if (N->getOpcode() == X86ISD::Wrapper) {
6443 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006444 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6445 return true;
6446 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006447 }
Evan Chengad4196b2008-05-12 19:56:52 +00006448 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006449}
6450
Evan Chengad4196b2008-05-12 19:56:52 +00006451static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6452 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006453 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00006454 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00006455 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006456 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00006457 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00006458 return false;
6459}
6460
Evan Cheng7e2ff772008-05-08 00:57:18 +00006461static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006462 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00006463 SDNode *&Base,
6464 SelectionDAG &DAG, MachineFrameInfo *MFI,
6465 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006466 Base = NULL;
6467 for (unsigned i = 0; i < NumElems; ++i) {
6468 SDOperand Idx = PermMask.getOperand(i);
6469 if (Idx.getOpcode() == ISD::UNDEF) {
6470 if (!Base)
6471 return false;
6472 continue;
6473 }
6474
Evan Chengab262272008-06-25 20:52:59 +00006475 SDOperand Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006476 if (!Elt.Val ||
6477 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6478 return false;
6479 if (!Base) {
6480 Base = Elt.Val;
Evan Cheng50d9e722008-05-10 06:46:49 +00006481 if (Base->getOpcode() == ISD::UNDEF)
6482 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00006483 continue;
6484 }
6485 if (Elt.getOpcode() == ISD::UNDEF)
6486 continue;
6487
Evan Chengad4196b2008-05-12 19:56:52 +00006488 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006489 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006490 return false;
6491 }
6492 return true;
6493}
Evan Cheng206ee9d2006-07-07 08:33:52 +00006494
6495/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6496/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6497/// if the load addresses are consecutive, non-overlapping, and in the right
6498/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00006499static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006500 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006501 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006502 MVT VT = N->getValueType(0);
6503 MVT EVT = VT.getVectorElementType();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006504 SDOperand PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00006505 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006506 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00006507 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6508 DAG, MFI, TLI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006509 return SDOperand();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006510
Dan Gohmand3006222007-07-27 17:16:43 +00006511 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengad4196b2008-05-12 19:56:52 +00006512 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00006513 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006514 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00006515 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6516 LD->getSrcValueOffset(), LD->isVolatile(),
6517 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00006518}
6519
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006520/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chengd880b972008-05-09 21:53:03 +00006521static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006522 const X86Subtarget *Subtarget,
6523 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00006524 unsigned NumOps = N->getNumOperands();
6525
Evan Chengd880b972008-05-09 21:53:03 +00006526 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00006527 if (NumOps == 1)
Evan Chengd880b972008-05-09 21:53:03 +00006528 return SDOperand();
6529
Duncan Sands83ec4b62008-06-06 12:08:01 +00006530 MVT VT = N->getValueType(0);
6531 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00006532 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6533 // We are looking for load i64 and zero extend. We want to transform
6534 // it before legalizer has a chance to expand it. Also look for i64
6535 // BUILD_PAIR bit casted to f64.
6536 return SDOperand();
6537 // This must be an insertion into a zero vector.
6538 SDOperand HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00006539 if (!isZeroNode(HighElt))
Evan Chengd880b972008-05-09 21:53:03 +00006540 return SDOperand();
6541
6542 // Value must be a load.
Evan Chengd880b972008-05-09 21:53:03 +00006543 SDNode *Base = N->getOperand(0).Val;
6544 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006545 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chengd880b972008-05-09 21:53:03 +00006546 return SDOperand();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006547 Base = Base->getOperand(0).Val;
6548 if (!isa<LoadSDNode>(Base))
Evan Chengd880b972008-05-09 21:53:03 +00006549 return SDOperand();
6550 }
Evan Chengd880b972008-05-09 21:53:03 +00006551
6552 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006553 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00006554
6555 // Load must not be an extload.
6556 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6557 return SDOperand();
6558
Evan Chengd880b972008-05-09 21:53:03 +00006559 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6560}
6561
Chris Lattner83e6c992006-10-04 06:57:07 +00006562/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6563static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6564 const X86Subtarget *Subtarget) {
6565 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006566
Chris Lattner83e6c992006-10-04 06:57:07 +00006567 // If we have SSE[12] support, try to form min/max nodes.
6568 if (Subtarget->hasSSE2() &&
6569 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6570 if (Cond.getOpcode() == ISD::SETCC) {
6571 // Get the LHS/RHS of the select.
6572 SDOperand LHS = N->getOperand(1);
6573 SDOperand RHS = N->getOperand(2);
6574 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006575
Evan Cheng8ca29322006-11-10 21:43:37 +00006576 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006577 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006578 switch (CC) {
6579 default: break;
6580 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6581 case ISD::SETULE:
6582 case ISD::SETLE:
6583 if (!UnsafeFPMath) break;
6584 // FALL THROUGH.
6585 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6586 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006587 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006588 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006589
Chris Lattner1907a7b2006-10-05 04:11:26 +00006590 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6591 case ISD::SETUGT:
6592 case ISD::SETGT:
6593 if (!UnsafeFPMath) break;
6594 // FALL THROUGH.
6595 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6596 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006597 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006598 break;
6599 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006600 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006601 switch (CC) {
6602 default: break;
6603 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6604 case ISD::SETUGT:
6605 case ISD::SETGT:
6606 if (!UnsafeFPMath) break;
6607 // FALL THROUGH.
6608 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6609 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006610 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006611 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006612
Chris Lattner1907a7b2006-10-05 04:11:26 +00006613 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6614 case ISD::SETULE:
6615 case ISD::SETLE:
6616 if (!UnsafeFPMath) break;
6617 // FALL THROUGH.
6618 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6619 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006620 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006621 break;
6622 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006623 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006624
Evan Cheng8ca29322006-11-10 21:43:37 +00006625 if (Opcode)
6626 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006627 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006628
Chris Lattner83e6c992006-10-04 06:57:07 +00006629 }
6630
6631 return SDOperand();
6632}
6633
Chris Lattner149a4e52008-02-22 02:09:43 +00006634/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006635static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00006636 const X86Subtarget *Subtarget) {
6637 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6638 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00006639 // A preferable solution to the general problem is to figure out the right
6640 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006641 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006642 if (St->getValue().getValueType().isVector() &&
6643 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00006644 isa<LoadSDNode>(St->getValue()) &&
6645 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6646 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006647 SDNode* LdVal = St->getValue().Val;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006648 LoadSDNode *Ld = 0;
6649 int TokenFactorIndex = -1;
6650 SmallVector<SDOperand, 8> Ops;
6651 SDNode* ChainVal = St->getChain().Val;
6652 // Must be a store of a load. We currently handle two cases: the load
6653 // is a direct child, and it's under an intervening TokenFactor. It is
6654 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006655 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00006656 Ld = cast<LoadSDNode>(St->getChain());
6657 else if (St->getValue().hasOneUse() &&
6658 ChainVal->getOpcode() == ISD::TokenFactor) {
6659 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006660 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00006661 TokenFactorIndex = i;
6662 Ld = cast<LoadSDNode>(St->getValue());
6663 } else
6664 Ops.push_back(ChainVal->getOperand(i));
6665 }
6666 }
6667 if (Ld) {
6668 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6669 if (Subtarget->is64Bit()) {
6670 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6671 Ld->getBasePtr(), Ld->getSrcValue(),
6672 Ld->getSrcValueOffset(), Ld->isVolatile(),
6673 Ld->getAlignment());
6674 SDOperand NewChain = NewLd.getValue(1);
6675 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00006676 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006677 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6678 Ops.size());
6679 }
6680 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6681 St->getSrcValue(), St->getSrcValueOffset(),
6682 St->isVolatile(), St->getAlignment());
6683 }
6684
6685 // Otherwise, lower to two 32-bit copies.
6686 SDOperand LoAddr = Ld->getBasePtr();
6687 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006688 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006689
6690 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6691 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6692 Ld->isVolatile(), Ld->getAlignment());
6693 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6694 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6695 Ld->isVolatile(),
6696 MinAlign(Ld->getAlignment(), 4));
6697
6698 SDOperand NewChain = LoLd.getValue(1);
6699 if (TokenFactorIndex != -1) {
6700 Ops.push_back(LoLd);
6701 Ops.push_back(HiLd);
6702 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6703 Ops.size());
6704 }
6705
6706 LoAddr = St->getBasePtr();
6707 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006708 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006709
6710 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00006711 St->getSrcValue(), St->getSrcValueOffset(),
6712 St->isVolatile(), St->getAlignment());
Dale Johannesen079f2a62008-02-25 19:20:14 +00006713 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6714 St->getSrcValue(), St->getSrcValueOffset()+4,
6715 St->isVolatile(),
6716 MinAlign(St->getAlignment(), 4));
6717 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00006718 }
Chris Lattner149a4e52008-02-22 02:09:43 +00006719 }
6720 return SDOperand();
6721}
6722
Chris Lattner6cf73262008-01-25 06:14:17 +00006723/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6724/// X86ISD::FXOR nodes.
Chris Lattneraf723b92008-01-25 05:46:26 +00006725static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00006726 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6727 // F[X]OR(0.0, x) -> x
6728 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00006729 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6730 if (C->getValueAPF().isPosZero())
6731 return N->getOperand(1);
6732 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6733 if (C->getValueAPF().isPosZero())
6734 return N->getOperand(0);
6735 return SDOperand();
6736}
6737
6738/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6739static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6740 // FAND(0.0, x) -> 0.0
6741 // FAND(x, 0.0) -> 0.0
6742 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6743 if (C->getValueAPF().isPosZero())
6744 return N->getOperand(0);
6745 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6746 if (C->getValueAPF().isPosZero())
6747 return N->getOperand(1);
6748 return SDOperand();
6749}
6750
Chris Lattner83e6c992006-10-04 06:57:07 +00006751
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006752SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006753 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006754 SelectionDAG &DAG = DCI.DAG;
6755 switch (N->getOpcode()) {
6756 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00006757 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6758 case ISD::BUILD_VECTOR:
6759 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00006760 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006761 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00006762 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00006763 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6764 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006765 }
6766
6767 return SDOperand();
6768}
6769
Evan Cheng60c07e12006-07-05 22:17:51 +00006770//===----------------------------------------------------------------------===//
6771// X86 Inline Assembly Support
6772//===----------------------------------------------------------------------===//
6773
Chris Lattnerf4dff842006-07-11 02:54:03 +00006774/// getConstraintType - Given a constraint letter, return the type of
6775/// constraint it is for this target.
6776X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006777X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6778 if (Constraint.size() == 1) {
6779 switch (Constraint[0]) {
6780 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006781 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00006782 case 'r':
6783 case 'R':
6784 case 'l':
6785 case 'q':
6786 case 'Q':
6787 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00006788 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00006789 case 'Y':
6790 return C_RegisterClass;
6791 default:
6792 break;
6793 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006794 }
Chris Lattner4234f572007-03-25 02:14:49 +00006795 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006796}
6797
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006798/// LowerXConstraint - try to replace an X constraint, which matches anything,
6799/// with another that has more specific requirements based on the type of the
6800/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00006801const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00006802LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00006803 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6804 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006805 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006806 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00006807 return "Y";
6808 if (Subtarget->hasSSE1())
6809 return "x";
6810 }
6811
6812 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006813}
6814
Chris Lattner48884cd2007-08-25 00:47:38 +00006815/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6816/// vector. If it is invalid, don't add anything to Ops.
6817void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6818 char Constraint,
6819 std::vector<SDOperand>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00006820 SelectionDAG &DAG) const {
Chris Lattner48884cd2007-08-25 00:47:38 +00006821 SDOperand Result(0, 0);
6822
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006823 switch (Constraint) {
6824 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006825 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006826 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006827 if (C->getValue() <= 31) {
6828 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6829 break;
6830 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006831 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006832 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006833 case 'N':
6834 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006835 if (C->getValue() <= 255) {
6836 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6837 break;
6838 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006839 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006840 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006841 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006842 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006843 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6844 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6845 break;
6846 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006847
Chris Lattnerdc43a882007-05-03 16:52:29 +00006848 // If we are in non-pic codegen mode, we allow the address of a global (with
6849 // an optional displacement) to be used with 'i'.
6850 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6851 int64_t Offset = 0;
6852
6853 // Match either (GA) or (GA+C)
6854 if (GA) {
6855 Offset = GA->getOffset();
6856 } else if (Op.getOpcode() == ISD::ADD) {
6857 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6858 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6859 if (C && GA) {
6860 Offset = GA->getOffset()+C->getValue();
6861 } else {
6862 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6863 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6864 if (C && GA)
6865 Offset = GA->getOffset()+C->getValue();
6866 else
6867 C = 0, GA = 0;
6868 }
6869 }
6870
6871 if (GA) {
6872 // If addressing this global requires a load (e.g. in PIC mode), we can't
6873 // match.
6874 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6875 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006876 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006877
Chris Lattnerdc43a882007-05-03 16:52:29 +00006878 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6879 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006880 Result = Op;
6881 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006882 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006883
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006884 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006885 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006886 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006887 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006888
6889 if (Result.Val) {
6890 Ops.push_back(Result);
6891 return;
6892 }
6893 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006894}
6895
Chris Lattner259e97c2006-01-31 19:43:35 +00006896std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006897getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006898 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006899 if (Constraint.size() == 1) {
6900 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006901 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006902 default: break; // Unknown constraint letter
6903 case 'A': // EAX/EDX
6904 if (VT == MVT::i32 || VT == MVT::i64)
6905 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6906 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006907 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6908 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006909 if (VT == MVT::i32)
6910 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6911 else if (VT == MVT::i16)
6912 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6913 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006914 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006915 else if (VT == MVT::i64)
6916 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6917 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006918 }
6919 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006920
Chris Lattner1efa40f2006-02-22 00:56:39 +00006921 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006922}
Chris Lattnerf76d1802006-07-31 23:26:50 +00006923
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006924std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00006925X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006926 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00006927 // First, see if this is a constraint that directly corresponds to an LLVM
6928 // register class.
6929 if (Constraint.size() == 1) {
6930 // GCC Constraint Letters
6931 switch (Constraint[0]) {
6932 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006933 case 'r': // GENERAL_REGS
6934 case 'R': // LEGACY_REGS
6935 case 'l': // INDEX_REGS
6936 if (VT == MVT::i64 && Subtarget->is64Bit())
6937 return std::make_pair(0U, X86::GR64RegisterClass);
6938 if (VT == MVT::i32)
6939 return std::make_pair(0U, X86::GR32RegisterClass);
6940 else if (VT == MVT::i16)
6941 return std::make_pair(0U, X86::GR16RegisterClass);
6942 else if (VT == MVT::i8)
6943 return std::make_pair(0U, X86::GR8RegisterClass);
6944 break;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006945 case 'f': // FP Stack registers.
6946 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6947 // value to the correct fpstack register class.
6948 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6949 return std::make_pair(0U, X86::RFP32RegisterClass);
6950 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6951 return std::make_pair(0U, X86::RFP64RegisterClass);
6952 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00006953 case 'y': // MMX_REGS if MMX allowed.
6954 if (!Subtarget->hasMMX()) break;
6955 return std::make_pair(0U, X86::VR64RegisterClass);
6956 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006957 case 'Y': // SSE_REGS if SSE2 allowed
6958 if (!Subtarget->hasSSE2()) break;
6959 // FALL THROUGH.
6960 case 'x': // SSE_REGS if SSE1 allowed
6961 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006962
6963 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00006964 default: break;
6965 // Scalar SSE types.
6966 case MVT::f32:
6967 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00006968 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006969 case MVT::f64:
6970 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00006971 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006972 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00006973 case MVT::v16i8:
6974 case MVT::v8i16:
6975 case MVT::v4i32:
6976 case MVT::v2i64:
6977 case MVT::v4f32:
6978 case MVT::v2f64:
6979 return std::make_pair(0U, X86::VR128RegisterClass);
6980 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006981 break;
6982 }
6983 }
6984
Chris Lattnerf76d1802006-07-31 23:26:50 +00006985 // Use the default implementation in TargetLowering to convert the register
6986 // constraint into a member of a register class.
6987 std::pair<unsigned, const TargetRegisterClass*> Res;
6988 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006989
6990 // Not found as a standard register?
6991 if (Res.second == 0) {
6992 // GCC calls "st(0)" just plain "st".
6993 if (StringsEqualNoCase("{st}", Constraint)) {
6994 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006995 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006996 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006997
Chris Lattner1a60aa72006-10-31 19:42:44 +00006998 return Res;
6999 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007000
Chris Lattnerf76d1802006-07-31 23:26:50 +00007001 // Otherwise, check to see if this is a register class of the wrong value
7002 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7003 // turn into {ax},{dx}.
7004 if (Res.second->hasType(VT))
7005 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007006
Chris Lattnerf76d1802006-07-31 23:26:50 +00007007 // All of the single-register GCC register classes map their values onto
7008 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7009 // really want an 8-bit or 32-bit register, map to the appropriate register
7010 // class and return the appropriate register.
7011 if (Res.second != X86::GR16RegisterClass)
7012 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007013
Chris Lattnerf76d1802006-07-31 23:26:50 +00007014 if (VT == MVT::i8) {
7015 unsigned DestReg = 0;
7016 switch (Res.first) {
7017 default: break;
7018 case X86::AX: DestReg = X86::AL; break;
7019 case X86::DX: DestReg = X86::DL; break;
7020 case X86::CX: DestReg = X86::CL; break;
7021 case X86::BX: DestReg = X86::BL; break;
7022 }
7023 if (DestReg) {
7024 Res.first = DestReg;
7025 Res.second = Res.second = X86::GR8RegisterClass;
7026 }
7027 } else if (VT == MVT::i32) {
7028 unsigned DestReg = 0;
7029 switch (Res.first) {
7030 default: break;
7031 case X86::AX: DestReg = X86::EAX; break;
7032 case X86::DX: DestReg = X86::EDX; break;
7033 case X86::CX: DestReg = X86::ECX; break;
7034 case X86::BX: DestReg = X86::EBX; break;
7035 case X86::SI: DestReg = X86::ESI; break;
7036 case X86::DI: DestReg = X86::EDI; break;
7037 case X86::BP: DestReg = X86::EBP; break;
7038 case X86::SP: DestReg = X86::ESP; break;
7039 }
7040 if (DestReg) {
7041 Res.first = DestReg;
7042 Res.second = Res.second = X86::GR32RegisterClass;
7043 }
Evan Cheng25ab6902006-09-08 06:48:29 +00007044 } else if (VT == MVT::i64) {
7045 unsigned DestReg = 0;
7046 switch (Res.first) {
7047 default: break;
7048 case X86::AX: DestReg = X86::RAX; break;
7049 case X86::DX: DestReg = X86::RDX; break;
7050 case X86::CX: DestReg = X86::RCX; break;
7051 case X86::BX: DestReg = X86::RBX; break;
7052 case X86::SI: DestReg = X86::RSI; break;
7053 case X86::DI: DestReg = X86::RDI; break;
7054 case X86::BP: DestReg = X86::RBP; break;
7055 case X86::SP: DestReg = X86::RSP; break;
7056 }
7057 if (DestReg) {
7058 Res.first = DestReg;
7059 Res.second = Res.second = X86::GR64RegisterClass;
7060 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00007061 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007062
Chris Lattnerf76d1802006-07-31 23:26:50 +00007063 return Res;
7064}