blob: ed1df4dc2c6363f48794758ac444c22254bcff98 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000033#include "llvm/CodeGen/SelectionDAG.h"
34#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000035#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000036#include "llvm/Support/CommandLine.h"
37#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"
Duncan Sandsb116fac2007-07-27 20:02:49 +000041#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042using namespace llvm;
43
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000044X86TargetLowering::X86TargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000046 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000047 X86ScalarSSEf64 = Subtarget->hasSSE2();
48 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000049 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000050
Evan Cheng559806f2006-01-27 08:10:46 +000051
Anton Korobeynikov2365f512007-07-14 14:06:15 +000052 RegInfo = TM.getRegisterInfo();
53
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054 // Set up the TargetLowering object.
55
56 // X86 is weird, it always uses i8 for shift amounts and setcc results.
57 setShiftAmountType(MVT::i8);
58 setSetCCResultType(MVT::i8);
59 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000060 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000061 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000062 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000063
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000064 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000065 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000068 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000077 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000078 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000081 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000083
Evan Chengc5484282006-10-04 00:56:09 +000084 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
85
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
87 // operation.
88 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
90 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000091
Evan Cheng25ab6902006-09-08 06:48:29 +000092 if (Subtarget->is64Bit()) {
93 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000094 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000095 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000096 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +000097 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
99 else
100 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
101 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000102
103 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
104 // this operation.
105 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
106 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000107 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000108 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000109 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000110 // f32 and f64 cases are Legal, f80 case is not
111 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
112 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000113 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
115 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000116
Dale Johannesen73328d12007-09-19 23:55:34 +0000117 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
118 // are Legal, f80 is custom lowered.
119 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
120 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000121
Evan Cheng02568ff2006-01-30 22:13:22 +0000122 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
123 // this operation.
124 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
125 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
126
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000127 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000129 // f32 and f64 cases are Legal, f80 case is not
130 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000131 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000133 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000134 }
135
136 // Handle FP_TO_UINT by promoting the destination to a larger signed
137 // conversion.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
141
Evan Cheng25ab6902006-09-08 06:48:29 +0000142 if (Subtarget->is64Bit()) {
143 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000145 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000146 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000147 // Expand FP_TO_UINT into a select.
148 // FIXME: We would like to use a Custom expander here eventually to do
149 // the optimal thing for SSE vs. the default expansion in the legalizer.
150 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
151 else
152 // With SSE3 we can use fisttpll to convert to a signed i64.
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155
Chris Lattner399610a2006-12-05 18:22:22 +0000156 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000157 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000158 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
159 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 }
Chris Lattner21f66852005-12-23 05:15:23 +0000161
Dan Gohman525178c2007-10-08 18:33:35 +0000162 // Scalar integer multiply, multiply-high, divide, and remainder are
163 // lowered to use operations that produce two results, to match the
164 // available instructions. This exposes the two-result form to trivial
165 // CSE, which is able to combine x/y and x%y into a single instruction,
166 // for example. The single-result multiply instructions are introduced
167 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
168 // is not needed.
169 setOperationAction(ISD::MUL , MVT::i8 , Expand);
170 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
171 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
172 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
173 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
174 setOperationAction(ISD::SREM , MVT::i8 , Expand);
175 setOperationAction(ISD::UREM , MVT::i8 , Expand);
176 setOperationAction(ISD::MUL , MVT::i16 , Expand);
177 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
178 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
179 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
180 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
181 setOperationAction(ISD::SREM , MVT::i16 , Expand);
182 setOperationAction(ISD::UREM , MVT::i16 , Expand);
183 setOperationAction(ISD::MUL , MVT::i32 , Expand);
184 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
185 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
186 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
187 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
188 setOperationAction(ISD::SREM , MVT::i32 , Expand);
189 setOperationAction(ISD::UREM , MVT::i32 , Expand);
190 setOperationAction(ISD::MUL , MVT::i64 , Expand);
191 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
195 setOperationAction(ISD::SREM , MVT::i64 , Expand);
196 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000197
Evan Chengc35497f2006-10-30 08:02:39 +0000198 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000199 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000200 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
201 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
208 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000210 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
211
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000213 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
214 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000216 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
217 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000219 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
220 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 if (Subtarget->is64Bit()) {
222 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 }
226
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000227 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000228 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000229
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230 // These should be promoted to a larger select which is supported.
231 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
232 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000233 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000234 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
235 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
237 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000238 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000239 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
240 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
241 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
243 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000244 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 if (Subtarget->is64Bit()) {
246 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
247 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
248 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000249 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000250 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000251 if (!Subtarget->is64Bit())
252 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
253
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000254 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000255 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000256 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000257 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000258 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000259 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 if (Subtarget->is64Bit()) {
261 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
262 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
263 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
264 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
265 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000266 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
268 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
269 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000270 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000271 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
272 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000273
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000274 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000275 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000276 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000277 if (!Subtarget->isTargetDarwin() &&
278 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000279 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000280 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000281
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000282 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
283 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
284 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
285 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
286 if (Subtarget->is64Bit()) {
287 // FIXME: Verify
288 setExceptionPointerRegister(X86::RAX);
289 setExceptionSelectorRegister(X86::RDX);
290 } else {
291 setExceptionPointerRegister(X86::EAX);
292 setExceptionSelectorRegister(X86::EDX);
293 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000294 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000295
Duncan Sandsf7331b32007-09-11 14:10:23 +0000296 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000297
Nate Begemanacc398c2006-01-25 18:21:52 +0000298 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
299 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000300 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000301 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000302 if (Subtarget->is64Bit())
303 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
304 else
305 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
306
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000307 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000308 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit())
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000311 if (Subtarget->isTargetCygMing())
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
313 else
314 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000315
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000316 if (X86ScalarSSEf64) {
317 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000318 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000319 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
320 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Evan Cheng223547a2006-01-31 22:28:30 +0000322 // Use ANDPD to simulate FABS.
323 setOperationAction(ISD::FABS , MVT::f64, Custom);
324 setOperationAction(ISD::FABS , MVT::f32, Custom);
325
326 // Use XORP to simulate FNEG.
327 setOperationAction(ISD::FNEG , MVT::f64, Custom);
328 setOperationAction(ISD::FNEG , MVT::f32, Custom);
329
Evan Cheng68c47cb2007-01-05 07:55:56 +0000330 // Use ANDPD and ORPD to simulate FCOPYSIGN.
331 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
332 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
333
Evan Chengd25e9e82006-02-02 00:28:23 +0000334 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000335 setOperationAction(ISD::FSIN , MVT::f64, Expand);
336 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000337 setOperationAction(ISD::FREM , MVT::f64, Expand);
338 setOperationAction(ISD::FSIN , MVT::f32, Expand);
339 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000340 setOperationAction(ISD::FREM , MVT::f32, Expand);
341
Chris Lattnera54aa942006-01-29 06:26:08 +0000342 // Expand FP immediates into loads from the stack, except for the special
343 // cases we handle.
344 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
345 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000346 addLegalFPImmediate(APFloat(+0.0)); // xorpd
347 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000348
349 // Conversions to long double (in X87) go through memory.
350 setConvertAction(MVT::f32, MVT::f80, Expand);
351 setConvertAction(MVT::f64, MVT::f80, Expand);
352
353 // Conversions from long double (in X87) go through memory.
354 setConvertAction(MVT::f80, MVT::f32, Expand);
355 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000356 } else if (X86ScalarSSEf32) {
357 // Use SSE for f32, x87 for f64.
358 // Set up the FP register classes.
359 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
360 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
361
362 // Use ANDPS to simulate FABS.
363 setOperationAction(ISD::FABS , MVT::f32, Custom);
364
365 // Use XORP to simulate FNEG.
366 setOperationAction(ISD::FNEG , MVT::f32, Custom);
367
368 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
369
370 // Use ANDPS and ORPS to simulate FCOPYSIGN.
371 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
372 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
373
374 // We don't support sin/cos/fmod
375 setOperationAction(ISD::FSIN , MVT::f32, Expand);
376 setOperationAction(ISD::FCOS , MVT::f32, Expand);
377 setOperationAction(ISD::FREM , MVT::f32, Expand);
378
379 // Expand FP immediates into loads from the stack, except for the special
380 // cases we handle.
381 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
382 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
383 addLegalFPImmediate(APFloat(+0.0f)); // xorps
384 addLegalFPImmediate(APFloat(+0.0)); // FLD0
385 addLegalFPImmediate(APFloat(+1.0)); // FLD1
386 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
387 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
388
389 // SSE->x87 conversions go through memory.
390 setConvertAction(MVT::f32, MVT::f64, Expand);
391 setConvertAction(MVT::f32, MVT::f80, Expand);
392
393 // x87->SSE truncations need to go through memory.
394 setConvertAction(MVT::f80, MVT::f32, Expand);
395 setConvertAction(MVT::f64, MVT::f32, Expand);
396 // And x87->x87 truncations also.
397 setConvertAction(MVT::f80, MVT::f64, Expand);
398
399 if (!UnsafeFPMath) {
400 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
401 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
402 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000404 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000406 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
407 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000408
Evan Cheng68c47cb2007-01-05 07:55:56 +0000409 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000410 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000411 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
412 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000413
414 // Floating truncations need to go through memory.
415 setConvertAction(MVT::f80, MVT::f32, Expand);
416 setConvertAction(MVT::f64, MVT::f32, Expand);
417 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000418
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 if (!UnsafeFPMath) {
420 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
421 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
422 }
423
Chris Lattnera54aa942006-01-29 06:26:08 +0000424 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000425 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000426 addLegalFPImmediate(APFloat(+0.0)); // FLD0
427 addLegalFPImmediate(APFloat(+1.0)); // FLD1
428 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
429 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000430 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
431 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
432 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
433 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000435
Dale Johannesen59a58732007-08-05 18:49:15 +0000436 // Long double always uses X87.
437 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000438 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
440 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000441 if (!UnsafeFPMath) {
442 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
443 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
444 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000445
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000446 // Always use a library call for pow.
447 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
448 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
449 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
450
Evan Chengd30bf012006-03-01 01:11:20 +0000451 // First set operation action for all vector types to expand. Then we
452 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000453 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
454 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000455 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
456 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000457 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000458 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000459 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000460 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000461 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
466 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000467 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000468 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000469 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000470 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000471 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
477 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000478 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
481 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000482 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000483 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman89081322007-12-12 22:21:26 +0000486 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000492 }
493
Evan Chenga88973f2006-03-22 19:22:18 +0000494 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000495 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
496 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
497 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000498 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000499
Evan Chengd30bf012006-03-01 01:11:20 +0000500 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000501
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000502 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
503 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
504 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000505 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000506
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000507 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
508 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
509 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000510 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000511
Bill Wendling74027e92007-03-15 21:24:36 +0000512 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
513 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
514
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000515 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000516 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000517 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000518 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
519 setOperationAction(ISD::AND, MVT::v2i32, Promote);
520 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
521 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000522
523 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000524 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000525 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000526 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
527 setOperationAction(ISD::OR, MVT::v2i32, Promote);
528 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
529 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000530
531 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000532 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000533 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000534 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
535 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
536 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
537 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000538
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000539 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000540 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000541 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000542 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
543 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
544 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
545 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000546
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000547 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
548 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
550 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000551
552 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
553 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
554 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000555 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000556
557 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
558 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000559 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
560 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000561 }
562
Evan Chenga88973f2006-03-22 19:22:18 +0000563 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000564 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
565
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000566 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
567 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
568 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
569 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000570 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
571 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000572 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
573 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
574 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000575 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000576 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000577 }
578
Evan Chenga88973f2006-03-22 19:22:18 +0000579 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000580 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
581 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
582 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
583 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
584 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
585
Evan Chengf7c378e2006-04-10 07:23:14 +0000586 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
587 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
588 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000589 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000590 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
591 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
592 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000593 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000594 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000595 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
596 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
597 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
598 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000599 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
600 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000601
Evan Chengf7c378e2006-04-10 07:23:14 +0000602 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
603 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000604 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000605 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
606 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
607 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000608
Evan Cheng2c3ae372006-04-12 21:21:57 +0000609 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
610 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begeman844e0f92007-12-11 01:41:33 +0000611 // Do not attempt to custom lower non-power-of-2 vectors
612 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
613 continue;
Evan Cheng2c3ae372006-04-12 21:21:57 +0000614 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
616 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
617 }
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
622 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000623 if (Subtarget->is64Bit())
624 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000625
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000626 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000627 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
628 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
629 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
630 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
631 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
632 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
633 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000634 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
635 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000636 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
637 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000638 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000639
640 // Custom lower v2i64 and v2f64 selects.
641 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000642 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000643 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000644 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000645 }
646
Evan Cheng6be2c582006-04-05 23:38:46 +0000647 // We want to custom lower some of our intrinsics.
648 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
649
Evan Cheng206ee9d2006-07-07 08:33:52 +0000650 // We have target-specific dag combine patterns for the following nodes:
651 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000652 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000653
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000654 computeRegisterProperties();
655
Evan Cheng87ed7162006-02-14 08:25:08 +0000656 // FIXME: These should be based on subtarget info. Plus, the values should
657 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000658 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
659 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
660 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661 allowUnalignedMemoryAccesses = true; // x86 supports it!
662}
663
Chris Lattner2b02a442007-02-25 08:29:00 +0000664
Evan Chengcc415862007-11-09 01:32:10 +0000665/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
666/// jumptable.
667SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
668 SelectionDAG &DAG) const {
669 if (usesGlobalOffsetTable())
670 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
671 if (!Subtarget->isPICStyleRIPRel())
672 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
673 return Table;
674}
675
Chris Lattner2b02a442007-02-25 08:29:00 +0000676//===----------------------------------------------------------------------===//
677// Return Value Calling Convention Implementation
678//===----------------------------------------------------------------------===//
679
Chris Lattner59ed56b2007-02-28 04:55:35 +0000680#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000681
682/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
683/// exists skip possible ISD:TokenFactor.
684static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
685 if (Chain.getOpcode()==X86ISD::TAILCALL) {
686 return Chain;
687 } else if (Chain.getOpcode()==ISD::TokenFactor) {
688 if (Chain.getNumOperands() &&
689 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
690 return Chain.getOperand(0);
691 }
692 return Chain;
693}
Chris Lattner9774c912007-02-27 05:28:59 +0000694
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000695/// LowerRET - Lower an ISD::RET node.
696SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
697 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
698
Chris Lattner9774c912007-02-27 05:28:59 +0000699 SmallVector<CCValAssign, 16> RVLocs;
700 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000701 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
702 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000703 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000704
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000705 // If this is the first return lowered for this function, add the regs to the
706 // liveout set for the function.
707 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000708 for (unsigned i = 0; i != RVLocs.size(); ++i)
709 if (RVLocs[i].isRegLoc())
710 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000711 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000712 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000713
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000714 // Handle tail call return.
715 Chain = GetPossiblePreceedingTailCall(Chain);
716 if (Chain.getOpcode() == X86ISD::TAILCALL) {
717 SDOperand TailCall = Chain;
718 SDOperand TargetAddress = TailCall.getOperand(1);
719 SDOperand StackAdjustment = TailCall.getOperand(2);
720 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
721 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
722 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
723 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
724 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
725 "Expecting an global address, external symbol, or register");
726 assert( StackAdjustment.getOpcode() == ISD::Constant &&
727 "Expecting a const value");
728
729 SmallVector<SDOperand,8> Operands;
730 Operands.push_back(Chain.getOperand(0));
731 Operands.push_back(TargetAddress);
732 Operands.push_back(StackAdjustment);
733 // Copy registers used by the call. Last operand is a flag so it is not
734 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000735 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000736 Operands.push_back(Chain.getOperand(i));
737 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000738 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
739 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000740 }
741
742 // Regular return.
743 SDOperand Flag;
744
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000745 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000746 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
747 RVLocs[0].getLocReg() != X86::ST0) {
748 for (unsigned i = 0; i != RVLocs.size(); ++i) {
749 CCValAssign &VA = RVLocs[i];
750 assert(VA.isRegLoc() && "Can only return in registers!");
751 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
752 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000753 Flag = Chain.getValue(1);
754 }
755 } else {
756 // We need to handle a destination of ST0 specially, because it isn't really
757 // a register.
758 SDOperand Value = Op.getOperand(1);
759
760 // If this is an FP return with ScalarSSE, we need to move the value from
761 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000762 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
763 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000764 SDOperand MemLoc;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000765
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000766 // If this is a load into a scalarsse value, don't store the loaded value
767 // back to the stack, only to reload it: just replace the scalar-sse load.
768 if (ISD::isNON_EXTLoad(Value.Val) &&
769 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
770 Chain = Value.getOperand(0);
771 MemLoc = Value.getOperand(1);
772 } else {
773 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000774 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000775 MachineFunction &MF = DAG.getMachineFunction();
776 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
777 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
778 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
779 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000780 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000781 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000782 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
783 Chain = Value.getValue(1);
784 }
785
786 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
787 SDOperand Ops[] = { Chain, Value };
788 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
789 Flag = Chain.getValue(1);
790 }
791
792 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
793 if (Flag.Val)
794 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
795 else
796 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
797}
798
799
Chris Lattner3085e152007-02-25 08:59:22 +0000800/// LowerCallResult - Lower the result values of an ISD::CALL into the
801/// appropriate copies out of appropriate physical registers. This assumes that
802/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
803/// being lowered. The returns a SDNode with the same number of values as the
804/// ISD::CALL.
805SDNode *X86TargetLowering::
806LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
807 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000808
809 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000810 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000811 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
812 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000813 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
814
Chris Lattner3085e152007-02-25 08:59:22 +0000815
Chris Lattnere32bbf62007-02-28 07:09:55 +0000816 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000817
818 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000819 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
820 for (unsigned i = 0; i != RVLocs.size(); ++i) {
821 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
822 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000823 InFlag = Chain.getValue(2);
824 ResultVals.push_back(Chain.getValue(0));
825 }
826 } else {
827 // Copies from the FP stack are special, as ST0 isn't a valid register
828 // before the fp stackifier runs.
829
830 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000831 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000832 SDOperand GROps[] = { Chain, InFlag };
833 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
834 Chain = RetVal.getValue(1);
835 InFlag = RetVal.getValue(2);
836
837 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
838 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000839 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
840 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner3085e152007-02-25 08:59:22 +0000841 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
842 // shouldn't be necessary except that RFP cannot be live across
843 // multiple blocks. When stackifier is fixed, they can be uncoupled.
844 MachineFunction &MF = DAG.getMachineFunction();
845 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
846 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
847 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000848 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000849 };
850 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000851 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000852 Chain = RetVal.getValue(1);
853 }
Chris Lattner3085e152007-02-25 08:59:22 +0000854 ResultVals.push_back(RetVal);
855 }
856
857 // Merge everything together with a MERGE_VALUES node.
858 ResultVals.push_back(Chain);
859 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
860 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000861}
862
863
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000864//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000865// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000866//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000867// StdCall calling convention seems to be standard for many Windows' API
868// routines and around. It differs from C calling convention just a little:
869// callee should clean up the stack, not caller. Symbols should be also
870// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000871// For info on fast calling convention see Fast Calling Convention (tail call)
872// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000873
Evan Cheng85e38002006-04-27 05:35:28 +0000874/// AddLiveIn - This helper function adds the specified physical register to the
875/// MachineFunction as a live in value. It also creates a corresponding virtual
876/// register for it.
877static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000878 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000879 assert(RC->contains(PReg) && "Not the correct regclass!");
880 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
881 MF.addLiveIn(PReg, VReg);
882 return VReg;
883}
884
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000885// align stack arguments according to platform alignment needed for tail calls
886unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
887
Rafael Espindola7effac52007-09-14 15:48:13 +0000888SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
889 const CCValAssign &VA,
890 MachineFrameInfo *MFI,
891 SDOperand Root, unsigned i) {
892 // Create the nodes corresponding to a load from this parameter slot.
893 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
894 VA.getLocMemOffset());
895 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
896
897 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
898
899 if (Flags & ISD::ParamFlags::ByVal)
900 return FIN;
901 else
902 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
903}
904
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000905SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
906 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000907 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000908 MachineFunction &MF = DAG.getMachineFunction();
909 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000910 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000911 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000912 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner638402b2007-02-28 07:00:42 +0000913 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000914 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000915 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +0000916 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000917 // Check for possible tail call calling convention.
918 if (CC == CallingConv::Fast && PerformTailCallOpt)
919 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
920 else
921 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
922
Chris Lattnerf39f7712007-02-28 05:46:49 +0000923 SmallVector<SDOperand, 8> ArgValues;
924 unsigned LastVal = ~0U;
925 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
926 CCValAssign &VA = ArgLocs[i];
927 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
928 // places.
929 assert(VA.getValNo() != LastVal &&
930 "Don't support value assigned to multiple locs yet");
931 LastVal = VA.getValNo();
932
933 if (VA.isRegLoc()) {
934 MVT::ValueType RegVT = VA.getLocVT();
935 TargetRegisterClass *RC;
936 if (RegVT == MVT::i32)
937 RC = X86::GR32RegisterClass;
938 else {
939 assert(MVT::isVector(RegVT));
940 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000941 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000942
Chris Lattner82932a52007-03-02 05:12:29 +0000943 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
944 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000945
946 // If this is an 8 or 16-bit value, it is really passed promoted to 32
947 // bits. Insert an assert[sz]ext to capture this, then truncate to the
948 // right size.
949 if (VA.getLocInfo() == CCValAssign::SExt)
950 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
951 DAG.getValueType(VA.getValVT()));
952 else if (VA.getLocInfo() == CCValAssign::ZExt)
953 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
954 DAG.getValueType(VA.getValVT()));
955
956 if (VA.getLocInfo() != CCValAssign::Full)
957 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
958
959 ArgValues.push_back(ArgValue);
960 } else {
961 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +0000962 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +0000963 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000964 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000965
966 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000967 // align stack specially for tail calls
968 if (CC==CallingConv::Fast)
969 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000970
Evan Cheng25caf632006-05-23 21:06:34 +0000971 ArgValues.push_back(Root);
972
Evan Cheng1bc78042006-04-26 01:20:17 +0000973 // If the function takes variable number of arguments, make a frame index for
974 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000975 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000976 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000977
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000978 // Tail call calling convention (CallingConv::Fast) does not support varargs.
979 assert( !(isVarArg && CC == CallingConv::Fast) &&
980 "CallingConv::Fast does not support varargs.");
981
982 if (isStdCall && !isVarArg &&
983 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000984 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000985 BytesCallerReserves = 0;
986 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000987 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000988
989 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000990 if (NumArgs &&
991 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000992 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000993 BytesToPopOnReturn = 4;
994
995 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000996 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000997
Evan Cheng25ab6902006-09-08 06:48:29 +0000998 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +0000999
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001000 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1001 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001002
Evan Cheng25caf632006-05-23 21:06:34 +00001003 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001004 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001005 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001006}
1007
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001008SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001009 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001010 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001011 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001012 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001013 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001014
Chris Lattner638402b2007-02-28 07:00:42 +00001015 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001016 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001017 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001018 if(CC==CallingConv::Fast && PerformTailCallOpt)
1019 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1020 else
1021 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001022
Chris Lattner423c5f42007-02-28 05:31:48 +00001023 // Get a count of how many bytes are to be pushed on the stack.
1024 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001025 if (CC==CallingConv::Fast)
1026 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001027
Evan Cheng32fe1032006-05-25 00:59:30 +00001028 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001029
Chris Lattner5a88b832007-02-25 07:10:00 +00001030 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1031 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001032
Chris Lattner423c5f42007-02-28 05:31:48 +00001033 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001034
1035 // Walk the register/memloc assignments, inserting copies/loads.
1036 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1037 CCValAssign &VA = ArgLocs[i];
1038 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001039
Chris Lattner423c5f42007-02-28 05:31:48 +00001040 // Promote the value if needed.
1041 switch (VA.getLocInfo()) {
1042 default: assert(0 && "Unknown loc info!");
1043 case CCValAssign::Full: break;
1044 case CCValAssign::SExt:
1045 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1046 break;
1047 case CCValAssign::ZExt:
1048 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1049 break;
1050 case CCValAssign::AExt:
1051 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1052 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001053 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001054
1055 if (VA.isRegLoc()) {
1056 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1057 } else {
1058 assert(VA.isMemLoc());
1059 if (StackPtr.Val == 0)
1060 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001061
1062 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1063 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001064 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001065 }
1066
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001067 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001068 bool isSRet = NumOps &&
1069 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001070 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001071
Evan Cheng32fe1032006-05-25 00:59:30 +00001072 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001073 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1074 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001075
Evan Cheng347d5f72006-04-28 21:29:37 +00001076 // Build a sequence of copy-to-reg nodes chained together with token chain
1077 // and flag operands which copy the outgoing args into registers.
1078 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001079 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1080 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1081 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001082 InFlag = Chain.getValue(1);
1083 }
1084
Evan Chengf4684712007-02-21 21:18:14 +00001085 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1086 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +00001087 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1088 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001089 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1090 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1091 InFlag);
1092 InFlag = Chain.getValue(1);
1093 }
1094
Evan Cheng32fe1032006-05-25 00:59:30 +00001095 // If the callee is a GlobalAddress node (quite common, every direct call is)
1096 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001097 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001098 // We should use extra load for direct calls to dllimported functions in
1099 // non-JIT mode.
1100 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1101 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001102 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1103 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001104 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1105
Chris Lattnerd96d0722007-02-25 06:40:16 +00001106 // Returns a chain & a flag for retval copy to use.
1107 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001108 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001109 Ops.push_back(Chain);
1110 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001111
1112 // Add argument registers to the end of the list so that they are known live
1113 // into the call.
1114 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001115 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001116 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +00001117
1118 // Add an implicit use GOT pointer in EBX.
1119 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1120 Subtarget->isPICStyleGOT())
1121 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001122
Evan Cheng347d5f72006-04-28 21:29:37 +00001123 if (InFlag.Val)
1124 Ops.push_back(InFlag);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001125
1126 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001127 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001128
Chris Lattner2d297092006-05-23 18:50:38 +00001129 // Create the CALLSEQ_END node.
1130 unsigned NumBytesForCalleeToPush = 0;
1131
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001132 if (CC == CallingConv::X86_StdCall ||
1133 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner09c75a42007-02-25 09:06:15 +00001134 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001135 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +00001136 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001137 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001138 assert(!(isVarArg && CC==CallingConv::Fast) &&
1139 "CallingConv::Fast does not support varargs.");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001140 } else {
1141 // If this is is a call to a struct-return function, the callee
1142 // pops the hidden struct pointer, so we have to push it back.
1143 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001144 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001145 }
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001146
1147 Chain = DAG.getCALLSEQ_END(Chain,
1148 DAG.getConstant(NumBytes, getPointerTy()),
1149 DAG.getConstant(NumBytesForCalleeToPush,
1150 getPointerTy()),
1151 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001152 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001153
Chris Lattner3085e152007-02-25 08:59:22 +00001154 // Handle result values, copying them out of physregs into vregs that we
1155 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001156 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001157}
1158
Evan Cheng25ab6902006-09-08 06:48:29 +00001159
1160//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001161// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001162//===----------------------------------------------------------------------===//
1163//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001164// The X86 'fastcall' calling convention passes up to two integer arguments in
1165// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1166// and requires that the callee pop its arguments off the stack (allowing proper
1167// tail calls), and has the same return value conventions as C calling convs.
1168//
1169// This calling convention always arranges for the callee pop value to be 8n+4
1170// bytes, which is needed for tail recursion elimination and stack alignment
1171// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001172SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001173X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001174 MachineFunction &MF = DAG.getMachineFunction();
1175 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001176 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001177 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001178
Chris Lattner638402b2007-02-28 07:00:42 +00001179 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001180 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001181 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1182 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001183 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001184
1185 SmallVector<SDOperand, 8> ArgValues;
1186 unsigned LastVal = ~0U;
1187 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1188 CCValAssign &VA = ArgLocs[i];
1189 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1190 // places.
1191 assert(VA.getValNo() != LastVal &&
1192 "Don't support value assigned to multiple locs yet");
1193 LastVal = VA.getValNo();
1194
1195 if (VA.isRegLoc()) {
1196 MVT::ValueType RegVT = VA.getLocVT();
1197 TargetRegisterClass *RC;
1198 if (RegVT == MVT::i32)
1199 RC = X86::GR32RegisterClass;
1200 else {
1201 assert(MVT::isVector(RegVT));
1202 RC = X86::VR128RegisterClass;
1203 }
1204
Chris Lattner82932a52007-03-02 05:12:29 +00001205 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1206 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001207
1208 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1209 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1210 // right size.
1211 if (VA.getLocInfo() == CCValAssign::SExt)
1212 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1213 DAG.getValueType(VA.getValVT()));
1214 else if (VA.getLocInfo() == CCValAssign::ZExt)
1215 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1216 DAG.getValueType(VA.getValVT()));
1217
1218 if (VA.getLocInfo() != CCValAssign::Full)
1219 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1220
1221 ArgValues.push_back(ArgValue);
1222 } else {
1223 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001224 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001225 }
1226 }
1227
Evan Cheng25caf632006-05-23 21:06:34 +00001228 ArgValues.push_back(Root);
1229
Chris Lattnerfc664c12007-02-28 06:21:19 +00001230 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001231
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001232 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001233 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001234 // arguments and the arguments after the retaddr has been pushed are
1235 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001236 if ((StackSize & 7) == 0)
1237 StackSize += 4;
1238 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001239
1240 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001241 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001242 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001243 BytesCallerReserves = 0;
1244
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1246 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001247
Evan Cheng25caf632006-05-23 21:06:34 +00001248 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001249 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001250 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001251}
1252
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001253SDOperand
1254X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1255 const SDOperand &StackPtr,
1256 const CCValAssign &VA,
1257 SDOperand Chain,
1258 SDOperand Arg) {
1259 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1260 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1261 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1262 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1263 if (Flags & ISD::ParamFlags::ByVal) {
1264 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1265 ISD::ParamFlags::ByValAlignOffs);
1266
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001267 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1268 ISD::ParamFlags::ByValSizeOffs;
1269
1270 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1271 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattner27a6c732007-11-24 07:07:01 +00001272 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001273
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001274 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1275 AlwaysInline);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001276 } else {
1277 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1278 }
1279}
1280
Chris Lattnere87e1152006-09-26 03:57:53 +00001281SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001282 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001283 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001284 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001285 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001286 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001287
Chris Lattner638402b2007-02-28 07:00:42 +00001288 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001289 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001290 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001291 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001292
1293 // Get a count of how many bytes are to be pushed on the stack.
1294 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001295
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001296 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001297 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001298 // arguments and the arguments after the retaddr has been pushed are
1299 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001300 if ((NumBytes & 7) == 0)
1301 NumBytes += 4;
1302 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001303
Chris Lattner94dd2922006-02-13 09:00:43 +00001304 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001305
Chris Lattner5a88b832007-02-25 07:10:00 +00001306 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1307 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001308
1309 SDOperand StackPtr;
1310
1311 // Walk the register/memloc assignments, inserting copies/loads.
1312 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1313 CCValAssign &VA = ArgLocs[i];
1314 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1315
1316 // Promote the value if needed.
1317 switch (VA.getLocInfo()) {
1318 default: assert(0 && "Unknown loc info!");
1319 case CCValAssign::Full: break;
1320 case CCValAssign::SExt:
1321 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001322 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001323 case CCValAssign::ZExt:
1324 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1325 break;
1326 case CCValAssign::AExt:
1327 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1328 break;
1329 }
1330
1331 if (VA.isRegLoc()) {
1332 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1333 } else {
1334 assert(VA.isMemLoc());
1335 if (StackPtr.Val == 0)
1336 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001337
1338 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1339 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001340 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001341 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001342
Evan Cheng32fe1032006-05-25 00:59:30 +00001343 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001344 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1345 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001346
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001347 // Build a sequence of copy-to-reg nodes chained together with token chain
1348 // and flag operands which copy the outgoing args into registers.
1349 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1351 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1352 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001353 InFlag = Chain.getValue(1);
1354 }
1355
Evan Cheng32fe1032006-05-25 00:59:30 +00001356 // If the callee is a GlobalAddress node (quite common, every direct call is)
1357 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001358 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001359 // We should use extra load for direct calls to dllimported functions in
1360 // non-JIT mode.
1361 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1362 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001363 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1364 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001365 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1366
Evan Chengf4684712007-02-21 21:18:14 +00001367 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1368 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001369 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1370 Subtarget->isPICStyleGOT()) {
1371 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1372 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1373 InFlag);
1374 InFlag = Chain.getValue(1);
1375 }
1376
Chris Lattnerd96d0722007-02-25 06:40:16 +00001377 // Returns a chain & a flag for retval copy to use.
1378 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001379 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001380 Ops.push_back(Chain);
1381 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001382
1383 // Add argument registers to the end of the list so that they are known live
1384 // into the call.
1385 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001386 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001387 RegsToPass[i].second.getValueType()));
1388
Evan Chengf4684712007-02-21 21:18:14 +00001389 // Add an implicit use GOT pointer in EBX.
1390 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1391 Subtarget->isPICStyleGOT())
1392 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1393
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001394 if (InFlag.Val)
1395 Ops.push_back(InFlag);
1396
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001397 assert(isTailCall==false && "no tail call here");
1398 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001399 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001400 InFlag = Chain.getValue(1);
1401
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001402 // Returns a flag for retval copy to use.
1403 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001404 Ops.clear();
1405 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001406 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1407 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001408 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001409 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001410 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001411
Chris Lattner339b4392007-02-25 09:10:05 +00001412 // Handle result values, copying them out of physregs into vregs that we
1413 // return.
1414 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001415}
1416
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001417//===----------------------------------------------------------------------===//
1418// Fast Calling Convention (tail call) implementation
1419//===----------------------------------------------------------------------===//
1420
1421// Like std call, callee cleans arguments, convention except that ECX is
1422// reserved for storing the tail called function address. Only 2 registers are
1423// free for argument passing (inreg). Tail call optimization is performed
1424// provided:
1425// * tailcallopt is enabled
1426// * caller/callee are fastcc
1427// * elf/pic is disabled OR
1428// * elf/pic enabled + callee is in module + callee has
1429// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001430// To keep the stack aligned according to platform abi the function
1431// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1432// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001433// If a tail called function callee has more arguments than the caller the
1434// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001435// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001436// original REtADDR, but before the saved framepointer or the spilled registers
1437// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1438// stack layout:
1439// arg1
1440// arg2
1441// RETADDR
1442// [ new RETADDR
1443// move area ]
1444// (possible EBP)
1445// ESI
1446// EDI
1447// local1 ..
1448
1449/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1450/// for a 16 byte align requirement.
1451unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1452 SelectionDAG& DAG) {
1453 if (PerformTailCallOpt) {
1454 MachineFunction &MF = DAG.getMachineFunction();
1455 const TargetMachine &TM = MF.getTarget();
1456 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1457 unsigned StackAlignment = TFI.getStackAlignment();
1458 uint64_t AlignMask = StackAlignment - 1;
1459 int64_t Offset = StackSize;
1460 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1461 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1462 // Number smaller than 12 so just add the difference.
1463 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1464 } else {
1465 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1466 Offset = ((~AlignMask) & Offset) + StackAlignment +
1467 (StackAlignment-SlotSize);
1468 }
1469 StackSize = Offset;
1470 }
1471 return StackSize;
1472}
1473
1474/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001475/// following the call is a return. A function is eligible if caller/callee
1476/// calling conventions match, currently only fastcc supports tail calls, and
1477/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001478bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1479 SDOperand Ret,
1480 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001481 if (!PerformTailCallOpt)
1482 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001483
1484 // Check whether CALL node immediatly preceeds the RET node and whether the
1485 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001486 unsigned NumOps = Ret.getNumOperands();
1487 if ((NumOps == 1 &&
1488 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1489 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001490 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001491 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1492 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 unsigned CallerCC = MF.getFunction()->getCallingConv();
1495 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1496 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1497 SDOperand Callee = Call.getOperand(4);
1498 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001499 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1500 !Subtarget->isPICStyleGOT())
1501 return true;
1502
1503 // Can only do local tail calls with PIC.
1504 GlobalValue * GV = 0;
1505 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1506 if(G != 0 &&
1507 (GV = G->getGlobal()) &&
1508 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1509 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001510 }
1511 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001512
1513 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001514}
1515
1516SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1517 SelectionDAG &DAG,
1518 unsigned CC) {
1519 SDOperand Chain = Op.getOperand(0);
1520 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1521 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1522 SDOperand Callee = Op.getOperand(4);
1523 bool is64Bit = Subtarget->is64Bit();
1524
1525 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1526
1527 // Analyze operands of the call, assigning locations to each operand.
1528 SmallVector<CCValAssign, 16> ArgLocs;
1529 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1530 if (is64Bit)
1531 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1532 else
1533 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1534
1535
1536 // Lower arguments at fp - stackoffset + fpdiff.
1537 MachineFunction &MF = DAG.getMachineFunction();
1538
1539 unsigned NumBytesToBePushed =
1540 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1541
1542 unsigned NumBytesCallerPushed =
1543 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1544 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1545
1546 // Set the delta of movement of the returnaddr stackslot.
1547 // But only set if delta is greater than previous delta.
1548 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1549 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1550
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001551 Chain = DAG.
1552 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1553
1554 // Adjust the Return address stack slot.
1555 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001556 if (FPDiff) {
1557 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001558 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1559 // Load the "old" Return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001560 RetAddrFrIdx =
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001561 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1562 // Calculate the new stack slot for the return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001563 int SlotSize = is64Bit ? 8 : 4;
1564 int NewReturnAddrFI =
1565 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001566 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1567 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001568 }
1569
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001570 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1571 SmallVector<SDOperand, 8> MemOpChains;
1572 SmallVector<SDOperand, 8> MemOpChains2;
1573 SDOperand FramePtr, StackPtr;
1574 SDOperand PtrOff;
1575 SDOperand FIN;
1576 int FI = 0;
1577
1578 // Walk the register/memloc assignments, inserting copies/loads. Lower
1579 // arguments first to the stack slot where they would normally - in case of a
1580 // normal function call - be.
1581 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1582 CCValAssign &VA = ArgLocs[i];
1583 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1584
1585 // Promote the value if needed.
1586 switch (VA.getLocInfo()) {
1587 default: assert(0 && "Unknown loc info!");
1588 case CCValAssign::Full: break;
1589 case CCValAssign::SExt:
1590 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1591 break;
1592 case CCValAssign::ZExt:
1593 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1594 break;
1595 case CCValAssign::AExt:
1596 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1597 break;
1598 }
1599
1600 if (VA.isRegLoc()) {
1601 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1602 } else {
1603 assert(VA.isMemLoc());
1604 if (StackPtr.Val == 0)
1605 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1606
1607 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1608 Arg));
1609 }
1610 }
1611
1612 if (!MemOpChains.empty())
1613 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1614 &MemOpChains[0], MemOpChains.size());
1615
1616 // Build a sequence of copy-to-reg nodes chained together with token chain
1617 // and flag operands which copy the outgoing args into registers.
1618 SDOperand InFlag;
1619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1620 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1621 InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
1624 InFlag = SDOperand();
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001625
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001626 // Copy from stack slots to stack slot of a tail called function. This needs
1627 // to be done because if we would lower the arguments directly to their real
1628 // stack slot we might end up overwriting each other.
1629 // TODO: To make this more efficient (sometimes saving a store/load) we could
1630 // analyse the arguments and emit this store/load/store sequence only for
1631 // arguments which would be overwritten otherwise.
1632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
1634 if (!VA.isRegLoc()) {
1635 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1636 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1637
1638 // Get source stack slot.
1639 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1640 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1641 // Create frame index.
1642 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1643 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1644 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1645 FIN = DAG.getFrameIndex(FI, MVT::i32);
1646 if (Flags & ISD::ParamFlags::ByVal) {
1647 // Copy relative to framepointer.
1648 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1649 ISD::ParamFlags::ByValAlignOffs);
1650
1651 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1652 ISD::ParamFlags::ByValSizeOffs;
1653
1654 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1655 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer38ada862007-11-10 10:48:01 +00001656 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1657
1658 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1659 AlignNode,AlwaysInline));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001660 } else {
1661 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1662 // Store relative to framepointer.
1663 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1664 }
1665 }
1666 }
1667
1668 if (!MemOpChains2.empty())
1669 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1670 &MemOpChains2[0], MemOpChains.size());
1671
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001672 // Store the return address to the appropriate stack slot.
1673 if (FPDiff)
1674 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1675
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001676 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1677 // GOT pointer.
1678 // Does not work with tail call since ebx is not restored correctly by
1679 // tailcaller. TODO: at least for x86 - verify for x86-64
1680
1681 // If the callee is a GlobalAddress node (quite common, every direct call is)
1682 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1683 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1684 // We should use extra load for direct calls to dllimported functions in
1685 // non-JIT mode.
1686 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1687 getTargetMachine(), true))
1688 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1689 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1690 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1691 else {
1692 assert(Callee.getOpcode() == ISD::LOAD &&
1693 "Function destination must be loaded into virtual register");
1694 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1695
1696 Chain = DAG.getCopyToReg(Chain,
1697 DAG.getRegister(Opc, getPointerTy()) ,
1698 Callee,InFlag);
1699 Callee = DAG.getRegister(Opc, getPointerTy());
1700 // Add register as live out.
1701 DAG.getMachineFunction().addLiveOut(Opc);
1702 }
1703
1704 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1705 SmallVector<SDOperand, 8> Ops;
1706
1707 Ops.push_back(Chain);
1708 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1709 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1710 if (InFlag.Val)
1711 Ops.push_back(InFlag);
1712 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1713 InFlag = Chain.getValue(1);
1714
1715 // Returns a chain & a flag for retval copy to use.
1716 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1717 Ops.clear();
1718 Ops.push_back(Chain);
1719 Ops.push_back(Callee);
1720 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1721 // Add argument registers to the end of the list so that they are known live
1722 // into the call.
1723 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1724 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1725 RegsToPass[i].second.getValueType()));
1726 if (InFlag.Val)
1727 Ops.push_back(InFlag);
1728 assert(InFlag.Val &&
1729 "Flag must be set. Depend on flag being set in LowerRET");
1730 Chain = DAG.getNode(X86ISD::TAILCALL,
1731 Op.Val->getVTList(), &Ops[0], Ops.size());
1732
1733 return SDOperand(Chain.Val, Op.ResNo);
1734}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001735
1736//===----------------------------------------------------------------------===//
1737// X86-64 C Calling Convention implementation
1738//===----------------------------------------------------------------------===//
1739
1740SDOperand
1741X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001742 MachineFunction &MF = DAG.getMachineFunction();
1743 MachineFrameInfo *MFI = MF.getFrameInfo();
1744 SDOperand Root = Op.getOperand(0);
1745 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001746 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001747
1748 static const unsigned GPR64ArgRegs[] = {
1749 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1750 };
1751 static const unsigned XMMArgRegs[] = {
1752 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1753 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1754 };
1755
Chris Lattner638402b2007-02-28 07:00:42 +00001756
1757 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001758 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001759 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +00001760 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001761 if (CC == CallingConv::Fast && PerformTailCallOpt)
1762 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1763 else
1764 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001765
1766 SmallVector<SDOperand, 8> ArgValues;
1767 unsigned LastVal = ~0U;
1768 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1769 CCValAssign &VA = ArgLocs[i];
1770 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1771 // places.
1772 assert(VA.getValNo() != LastVal &&
1773 "Don't support value assigned to multiple locs yet");
1774 LastVal = VA.getValNo();
1775
1776 if (VA.isRegLoc()) {
1777 MVT::ValueType RegVT = VA.getLocVT();
1778 TargetRegisterClass *RC;
1779 if (RegVT == MVT::i32)
1780 RC = X86::GR32RegisterClass;
1781 else if (RegVT == MVT::i64)
1782 RC = X86::GR64RegisterClass;
1783 else if (RegVT == MVT::f32)
1784 RC = X86::FR32RegisterClass;
1785 else if (RegVT == MVT::f64)
1786 RC = X86::FR64RegisterClass;
1787 else {
1788 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001789 if (MVT::getSizeInBits(RegVT) == 64) {
1790 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1791 RegVT = MVT::i64;
1792 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001793 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001794 }
Chris Lattner82932a52007-03-02 05:12:29 +00001795
1796 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1797 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001798
1799 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1800 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1801 // right size.
1802 if (VA.getLocInfo() == CCValAssign::SExt)
1803 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1804 DAG.getValueType(VA.getValVT()));
1805 else if (VA.getLocInfo() == CCValAssign::ZExt)
1806 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1807 DAG.getValueType(VA.getValVT()));
1808
1809 if (VA.getLocInfo() != CCValAssign::Full)
1810 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1811
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001812 // Handle MMX values passed in GPRs.
1813 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1814 MVT::getSizeInBits(RegVT) == 64)
1815 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1816
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001817 ArgValues.push_back(ArgValue);
1818 } else {
1819 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001820 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001821 }
1822 }
1823
1824 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001825 if (CC==CallingConv::Fast)
1826 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001827
1828 // If the function takes variable number of arguments, make a frame index for
1829 // the start of the first vararg value... for expansion of llvm.va_start.
1830 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001831 assert(CC!=CallingConv::Fast
1832 && "Var arg not supported with calling convention fastcc");
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001833 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1834 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1835
1836 // For X86-64, if there are vararg parameters that are passed via
1837 // registers, then we must store them to their spots on the stack so they
1838 // may be loaded by deferencing the result of va_next.
1839 VarArgsGPOffset = NumIntRegs * 8;
1840 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1841 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1842 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1843
1844 // Store the integer parameter registers.
1845 SmallVector<SDOperand, 8> MemOps;
1846 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1847 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1848 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1849 for (; NumIntRegs != 6; ++NumIntRegs) {
1850 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1851 X86::GR64RegisterClass);
1852 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1853 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1854 MemOps.push_back(Store);
1855 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1856 DAG.getConstant(8, getPointerTy()));
1857 }
1858
1859 // Now store the XMM (fp + vector) parameter registers.
1860 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1861 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1862 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1863 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1864 X86::VR128RegisterClass);
1865 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1866 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1867 MemOps.push_back(Store);
1868 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1869 DAG.getConstant(16, getPointerTy()));
1870 }
1871 if (!MemOps.empty())
1872 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1873 &MemOps[0], MemOps.size());
1874 }
1875
1876 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001877 // Tail call convention (fastcc) needs callee pop.
Evan Cheng36446012007-10-14 10:09:39 +00001878 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001879 BytesToPopOnReturn = StackSize; // Callee pops everything.
1880 BytesCallerReserves = 0;
1881 } else {
1882 BytesToPopOnReturn = 0; // Callee pops nothing.
1883 BytesCallerReserves = StackSize;
1884 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001885 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1886 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1887
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001888 // Return the new list of results.
1889 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1890 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1891}
1892
1893SDOperand
1894X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1895 unsigned CC) {
1896 SDOperand Chain = Op.getOperand(0);
1897 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001898 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001899
1900 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001901 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001902 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng36446012007-10-14 10:09:39 +00001903 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001904 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1905 else
1906 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001907
1908 // Get a count of how many bytes are to be pushed on the stack.
1909 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001910 if (CC == CallingConv::Fast)
1911 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1912
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001913 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1914
1915 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1916 SmallVector<SDOperand, 8> MemOpChains;
1917
1918 SDOperand StackPtr;
1919
1920 // Walk the register/memloc assignments, inserting copies/loads.
1921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1922 CCValAssign &VA = ArgLocs[i];
1923 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1924
1925 // Promote the value if needed.
1926 switch (VA.getLocInfo()) {
1927 default: assert(0 && "Unknown loc info!");
1928 case CCValAssign::Full: break;
1929 case CCValAssign::SExt:
1930 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1931 break;
1932 case CCValAssign::ZExt:
1933 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1934 break;
1935 case CCValAssign::AExt:
1936 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1937 break;
1938 }
1939
1940 if (VA.isRegLoc()) {
1941 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1942 } else {
1943 assert(VA.isMemLoc());
1944 if (StackPtr.Val == 0)
1945 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00001946
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001947 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1948 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001949 }
1950 }
1951
1952 if (!MemOpChains.empty())
1953 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1954 &MemOpChains[0], MemOpChains.size());
1955
1956 // Build a sequence of copy-to-reg nodes chained together with token chain
1957 // and flag operands which copy the outgoing args into registers.
1958 SDOperand InFlag;
1959 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1960 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1961 InFlag);
1962 InFlag = Chain.getValue(1);
1963 }
1964
1965 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001966 assert ( CallingConv::Fast != CC &&
1967 "Var args not supported with calling convention fastcc");
1968
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001969 // From AMD64 ABI document:
1970 // For calls that may call functions that use varargs or stdargs
1971 // (prototype-less calls or calls to functions containing ellipsis (...) in
1972 // the declaration) %al is used as hidden argument to specify the number
1973 // of SSE registers used. The contents of %al do not need to match exactly
1974 // the number of registers, but must be an ubound on the number of SSE
1975 // registers used and is in the range 0 - 8 inclusive.
1976
1977 // Count the number of XMM registers allocated.
1978 static const unsigned XMMArgRegs[] = {
1979 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1980 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1981 };
1982 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1983
1984 Chain = DAG.getCopyToReg(Chain, X86::AL,
1985 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1986 InFlag = Chain.getValue(1);
1987 }
1988
1989 // If the callee is a GlobalAddress node (quite common, every direct call is)
1990 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1991 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1992 // We should use extra load for direct calls to dllimported functions in
1993 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001994 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001995 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1996 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001997 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1998 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001999 if (getTargetMachine().getCodeModel() != CodeModel::Large)
2000 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002001
2002 // Returns a chain & a flag for retval copy to use.
2003 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2004 SmallVector<SDOperand, 8> Ops;
2005 Ops.push_back(Chain);
2006 Ops.push_back(Callee);
2007
2008 // Add argument registers to the end of the list so that they are known live
2009 // into the call.
2010 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2011 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2012 RegsToPass[i].second.getValueType()));
2013
2014 if (InFlag.Val)
2015 Ops.push_back(InFlag);
2016
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002017 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002018 NodeTys, &Ops[0], Ops.size());
2019 InFlag = Chain.getValue(1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002020 int NumBytesForCalleeToPush = 0;
Evan Cheng36446012007-10-14 10:09:39 +00002021 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002022 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002023 } else {
2024 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2025 }
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002026 // Returns a flag for retval copy to use.
2027 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2028 Ops.clear();
2029 Ops.push_back(Chain);
2030 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002031 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002032 Ops.push_back(InFlag);
2033 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2034 InFlag = Chain.getValue(1);
2035
2036 // Handle result values, copying them out of physregs into vregs that we
2037 // return.
2038 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2039}
2040
2041
2042//===----------------------------------------------------------------------===//
2043// Other Lowering Hooks
2044//===----------------------------------------------------------------------===//
2045
2046
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002047SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002048 MachineFunction &MF = DAG.getMachineFunction();
2049 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2050 int ReturnAddrIndex = FuncInfo->getRAIndex();
2051
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002052 if (ReturnAddrIndex == 0) {
2053 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00002054 if (Subtarget->is64Bit())
2055 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2056 else
2057 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002058
2059 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002060 }
2061
Evan Cheng25ab6902006-09-08 06:48:29 +00002062 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002063}
2064
2065
2066
Evan Cheng6dfa9992006-01-30 23:41:35 +00002067/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2068/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00002069/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2070/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00002071static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00002072 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2073 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00002074 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002075 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002076 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2077 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2078 // X > -1 -> X == 0, jump !sign.
2079 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00002080 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002081 return true;
2082 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2083 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00002084 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002085 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00002086 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2087 // X < 1 -> X <= 0
2088 RHS = DAG.getConstant(0, RHS.getValueType());
2089 X86CC = X86::COND_LE;
2090 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002091 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002092 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002093
Evan Chengd9558e02006-01-06 00:43:03 +00002094 switch (SetCCOpcode) {
2095 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002096 case ISD::SETEQ: X86CC = X86::COND_E; break;
2097 case ISD::SETGT: X86CC = X86::COND_G; break;
2098 case ISD::SETGE: X86CC = X86::COND_GE; break;
2099 case ISD::SETLT: X86CC = X86::COND_L; break;
2100 case ISD::SETLE: X86CC = X86::COND_LE; break;
2101 case ISD::SETNE: X86CC = X86::COND_NE; break;
2102 case ISD::SETULT: X86CC = X86::COND_B; break;
2103 case ISD::SETUGT: X86CC = X86::COND_A; break;
2104 case ISD::SETULE: X86CC = X86::COND_BE; break;
2105 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002106 }
2107 } else {
2108 // On a floating point condition, the flags are set as follows:
2109 // ZF PF CF op
2110 // 0 | 0 | 0 | X > Y
2111 // 0 | 0 | 1 | X < Y
2112 // 1 | 0 | 0 | X == Y
2113 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002114 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002115 switch (SetCCOpcode) {
2116 default: break;
2117 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002118 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002119 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002120 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002121 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002122 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002123 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002124 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002125 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002126 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002127 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002128 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002129 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002130 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002131 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002132 case ISD::SETNE: X86CC = X86::COND_NE; break;
2133 case ISD::SETUO: X86CC = X86::COND_P; break;
2134 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002135 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002136 if (Flip)
2137 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002138 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002139
Chris Lattner7fbe9722006-10-20 17:42:20 +00002140 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002141}
2142
Evan Cheng4a460802006-01-11 00:33:36 +00002143/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2144/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002145/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002146static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002147 switch (X86CC) {
2148 default:
2149 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002150 case X86::COND_B:
2151 case X86::COND_BE:
2152 case X86::COND_E:
2153 case X86::COND_P:
2154 case X86::COND_A:
2155 case X86::COND_AE:
2156 case X86::COND_NE:
2157 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002158 return true;
2159 }
2160}
2161
Evan Cheng5ced1d82006-04-06 23:23:56 +00002162/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002163/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002164static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2165 if (Op.getOpcode() == ISD::UNDEF)
2166 return true;
2167
2168 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002169 return (Val >= Low && Val < Hi);
2170}
2171
2172/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2173/// true if Op is undef or if its value equal to the specified value.
2174static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2175 if (Op.getOpcode() == ISD::UNDEF)
2176 return true;
2177 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002178}
2179
Evan Cheng0188ecb2006-03-22 18:59:22 +00002180/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2181/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2182bool X86::isPSHUFDMask(SDNode *N) {
2183 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2184
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002185 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002186 return false;
2187
2188 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002190 SDOperand Arg = N->getOperand(i);
2191 if (Arg.getOpcode() == ISD::UNDEF) continue;
2192 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002193 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002194 return false;
2195 }
2196
2197 return true;
2198}
2199
2200/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002201/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002202bool X86::isPSHUFHWMask(SDNode *N) {
2203 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2204
2205 if (N->getNumOperands() != 8)
2206 return false;
2207
2208 // Lower quadword copied in order.
2209 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002210 SDOperand Arg = N->getOperand(i);
2211 if (Arg.getOpcode() == ISD::UNDEF) continue;
2212 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2213 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002214 return false;
2215 }
2216
2217 // Upper quadword shuffled.
2218 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002219 SDOperand Arg = N->getOperand(i);
2220 if (Arg.getOpcode() == ISD::UNDEF) continue;
2221 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2222 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002223 if (Val < 4 || Val > 7)
2224 return false;
2225 }
2226
2227 return true;
2228}
2229
2230/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002231/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002232bool X86::isPSHUFLWMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 if (N->getNumOperands() != 8)
2236 return false;
2237
2238 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002239 for (unsigned i = 4; i != 8; ++i)
2240 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002241 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002242
2243 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002244 for (unsigned i = 0; i != 4; ++i)
2245 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002246 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002247
2248 return true;
2249}
2250
Evan Cheng14aed5e2006-03-24 01:18:28 +00002251/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2252/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002253static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002254 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002255
Evan Cheng39623da2006-04-20 08:58:49 +00002256 unsigned Half = NumElems / 2;
2257 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002258 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002259 return false;
2260 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002261 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002262 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002263
2264 return true;
2265}
2266
Evan Cheng39623da2006-04-20 08:58:49 +00002267bool X86::isSHUFPMask(SDNode *N) {
2268 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002269 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002270}
2271
Evan Cheng213d2cf2007-05-17 18:45:50 +00002272/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002273/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2274/// half elements to come from vector 1 (which would equal the dest.) and
2275/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002276static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2277 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002278
Chris Lattner5a88b832007-02-25 07:10:00 +00002279 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002280 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002281 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002282 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002283 for (unsigned i = Half; i < NumOps; ++i)
2284 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002285 return false;
2286 return true;
2287}
2288
2289static bool isCommutedSHUFP(SDNode *N) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002291 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002292}
2293
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002294/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2295/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2296bool X86::isMOVHLPSMask(SDNode *N) {
2297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2298
Evan Cheng2064a2b2006-03-28 06:50:32 +00002299 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002300 return false;
2301
Evan Cheng2064a2b2006-03-28 06:50:32 +00002302 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002303 return isUndefOrEqual(N->getOperand(0), 6) &&
2304 isUndefOrEqual(N->getOperand(1), 7) &&
2305 isUndefOrEqual(N->getOperand(2), 2) &&
2306 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002307}
2308
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002309/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2310/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2311/// <2, 3, 2, 3>
2312bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2313 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2314
2315 if (N->getNumOperands() != 4)
2316 return false;
2317
2318 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2319 return isUndefOrEqual(N->getOperand(0), 2) &&
2320 isUndefOrEqual(N->getOperand(1), 3) &&
2321 isUndefOrEqual(N->getOperand(2), 2) &&
2322 isUndefOrEqual(N->getOperand(3), 3);
2323}
2324
Evan Cheng5ced1d82006-04-06 23:23:56 +00002325/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2326/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2327bool X86::isMOVLPMask(SDNode *N) {
2328 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2329
2330 unsigned NumElems = N->getNumOperands();
2331 if (NumElems != 2 && NumElems != 4)
2332 return false;
2333
Evan Chengc5cdff22006-04-07 21:53:05 +00002334 for (unsigned i = 0; i < NumElems/2; ++i)
2335 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2336 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002337
Evan Chengc5cdff22006-04-07 21:53:05 +00002338 for (unsigned i = NumElems/2; i < NumElems; ++i)
2339 if (!isUndefOrEqual(N->getOperand(i), i))
2340 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002341
2342 return true;
2343}
2344
2345/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002346/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2347/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002348bool X86::isMOVHPMask(SDNode *N) {
2349 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2350
2351 unsigned NumElems = N->getNumOperands();
2352 if (NumElems != 2 && NumElems != 4)
2353 return false;
2354
Evan Chengc5cdff22006-04-07 21:53:05 +00002355 for (unsigned i = 0; i < NumElems/2; ++i)
2356 if (!isUndefOrEqual(N->getOperand(i), i))
2357 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002358
2359 for (unsigned i = 0; i < NumElems/2; ++i) {
2360 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002361 if (!isUndefOrEqual(Arg, i + NumElems))
2362 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002363 }
2364
2365 return true;
2366}
2367
Evan Cheng0038e592006-03-28 00:39:58 +00002368/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2369/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002370bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2371 bool V2IsSplat = false) {
2372 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002373 return false;
2374
Chris Lattner5a88b832007-02-25 07:10:00 +00002375 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2376 SDOperand BitI = Elts[i];
2377 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002378 if (!isUndefOrEqual(BitI, j))
2379 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002380 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002381 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002382 return false;
2383 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002384 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002385 return false;
2386 }
Evan Cheng0038e592006-03-28 00:39:58 +00002387 }
2388
2389 return true;
2390}
2391
Evan Cheng39623da2006-04-20 08:58:49 +00002392bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2393 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002394 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002395}
2396
Evan Cheng4fcb9222006-03-28 02:43:26 +00002397/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2398/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002399bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2400 bool V2IsSplat = false) {
2401 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002402 return false;
2403
Chris Lattner5a88b832007-02-25 07:10:00 +00002404 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2405 SDOperand BitI = Elts[i];
2406 SDOperand BitI1 = Elts[i+1];
2407 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002408 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002409 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002410 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002411 return false;
2412 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002413 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002414 return false;
2415 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002416 }
2417
2418 return true;
2419}
2420
Evan Cheng39623da2006-04-20 08:58:49 +00002421bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2422 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002423 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002424}
2425
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002426/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2427/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2428/// <0, 0, 1, 1>
2429bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2431
2432 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002433 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002434 return false;
2435
2436 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2437 SDOperand BitI = N->getOperand(i);
2438 SDOperand BitI1 = N->getOperand(i+1);
2439
Evan Chengc5cdff22006-04-07 21:53:05 +00002440 if (!isUndefOrEqual(BitI, j))
2441 return false;
2442 if (!isUndefOrEqual(BitI1, j))
2443 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002444 }
2445
2446 return true;
2447}
2448
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002449/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2450/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2451/// <2, 2, 3, 3>
2452bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2453 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2454
2455 unsigned NumElems = N->getNumOperands();
2456 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2457 return false;
2458
2459 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2460 SDOperand BitI = N->getOperand(i);
2461 SDOperand BitI1 = N->getOperand(i + 1);
2462
2463 if (!isUndefOrEqual(BitI, j))
2464 return false;
2465 if (!isUndefOrEqual(BitI1, j))
2466 return false;
2467 }
2468
2469 return true;
2470}
2471
Evan Cheng017dcc62006-04-21 01:05:10 +00002472/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2473/// specifies a shuffle of elements that is suitable for input to MOVSS,
2474/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002475static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002476 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002477 return false;
2478
Chris Lattner5a88b832007-02-25 07:10:00 +00002479 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002480 return false;
2481
Chris Lattner5a88b832007-02-25 07:10:00 +00002482 for (unsigned i = 1; i < NumElts; ++i) {
2483 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002484 return false;
2485 }
2486
2487 return true;
2488}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002489
Evan Cheng017dcc62006-04-21 01:05:10 +00002490bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002491 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002492 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002493}
2494
Evan Cheng017dcc62006-04-21 01:05:10 +00002495/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2496/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002497/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002498static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2499 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002500 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002501 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002502 return false;
2503
2504 if (!isUndefOrEqual(Ops[0], 0))
2505 return false;
2506
Chris Lattner5a88b832007-02-25 07:10:00 +00002507 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002508 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002509 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2510 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2511 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002512 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002513 }
2514
2515 return true;
2516}
2517
Evan Cheng8cf723d2006-09-08 01:50:06 +00002518static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2519 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002520 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002521 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2522 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002523}
2524
Evan Chengd9539472006-04-14 21:59:03 +00002525/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2526/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2527bool X86::isMOVSHDUPMask(SDNode *N) {
2528 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2529
2530 if (N->getNumOperands() != 4)
2531 return false;
2532
2533 // Expect 1, 1, 3, 3
2534 for (unsigned i = 0; i < 2; ++i) {
2535 SDOperand Arg = N->getOperand(i);
2536 if (Arg.getOpcode() == ISD::UNDEF) continue;
2537 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2538 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2539 if (Val != 1) return false;
2540 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002541
2542 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002543 for (unsigned i = 2; i < 4; ++i) {
2544 SDOperand Arg = N->getOperand(i);
2545 if (Arg.getOpcode() == ISD::UNDEF) continue;
2546 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2547 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2548 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002549 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002550 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002551
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002552 // Don't use movshdup if it can be done with a shufps.
2553 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002554}
2555
2556/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2557/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2558bool X86::isMOVSLDUPMask(SDNode *N) {
2559 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2560
2561 if (N->getNumOperands() != 4)
2562 return false;
2563
2564 // Expect 0, 0, 2, 2
2565 for (unsigned i = 0; i < 2; ++i) {
2566 SDOperand Arg = N->getOperand(i);
2567 if (Arg.getOpcode() == ISD::UNDEF) continue;
2568 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2569 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2570 if (Val != 0) return false;
2571 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002572
2573 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002574 for (unsigned i = 2; i < 4; ++i) {
2575 SDOperand Arg = N->getOperand(i);
2576 if (Arg.getOpcode() == ISD::UNDEF) continue;
2577 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2578 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2579 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002580 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002581 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002582
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002583 // Don't use movshdup if it can be done with a shufps.
2584 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002585}
2586
Evan Cheng49892af2007-06-19 00:02:56 +00002587/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2588/// specifies a identity operation on the LHS or RHS.
2589static bool isIdentityMask(SDNode *N, bool RHS = false) {
2590 unsigned NumElems = N->getNumOperands();
2591 for (unsigned i = 0; i < NumElems; ++i)
2592 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2593 return false;
2594 return true;
2595}
2596
Evan Chengb9df0ca2006-03-22 02:53:00 +00002597/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2598/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002599static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002600 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2601
Evan Chengb9df0ca2006-03-22 02:53:00 +00002602 // This is a splat operation if each element of the permute is the same, and
2603 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002604 unsigned NumElems = N->getNumOperands();
2605 SDOperand ElementBase;
2606 unsigned i = 0;
2607 for (; i != NumElems; ++i) {
2608 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002609 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002610 ElementBase = Elt;
2611 break;
2612 }
2613 }
2614
2615 if (!ElementBase.Val)
2616 return false;
2617
2618 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002619 SDOperand Arg = N->getOperand(i);
2620 if (Arg.getOpcode() == ISD::UNDEF) continue;
2621 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002622 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002623 }
2624
2625 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002626 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002627}
2628
Evan Chengc575ca22006-04-17 20:43:08 +00002629/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2630/// a splat of a single element and it's a 2 or 4 element mask.
2631bool X86::isSplatMask(SDNode *N) {
2632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2633
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002634 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002635 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2636 return false;
2637 return ::isSplatMask(N);
2638}
2639
Evan Chengf686d9b2006-10-27 21:08:32 +00002640/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2641/// specifies a splat of zero element.
2642bool X86::isSplatLoMask(SDNode *N) {
2643 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2644
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002645 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002646 if (!isUndefOrEqual(N->getOperand(i), 0))
2647 return false;
2648 return true;
2649}
2650
Evan Cheng63d33002006-03-22 08:01:21 +00002651/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2652/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2653/// instructions.
2654unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002655 unsigned NumOperands = N->getNumOperands();
2656 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2657 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002658 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002659 unsigned Val = 0;
2660 SDOperand Arg = N->getOperand(NumOperands-i-1);
2661 if (Arg.getOpcode() != ISD::UNDEF)
2662 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002663 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002664 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002665 if (i != NumOperands - 1)
2666 Mask <<= Shift;
2667 }
Evan Cheng63d33002006-03-22 08:01:21 +00002668
2669 return Mask;
2670}
2671
Evan Cheng506d3df2006-03-29 23:07:14 +00002672/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2673/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2674/// instructions.
2675unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2676 unsigned Mask = 0;
2677 // 8 nodes, but we only care about the last 4.
2678 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002679 unsigned Val = 0;
2680 SDOperand Arg = N->getOperand(i);
2681 if (Arg.getOpcode() != ISD::UNDEF)
2682 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002683 Mask |= (Val - 4);
2684 if (i != 4)
2685 Mask <<= 2;
2686 }
2687
2688 return Mask;
2689}
2690
2691/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2692/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2693/// instructions.
2694unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2695 unsigned Mask = 0;
2696 // 8 nodes, but we only care about the first 4.
2697 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002698 unsigned Val = 0;
2699 SDOperand Arg = N->getOperand(i);
2700 if (Arg.getOpcode() != ISD::UNDEF)
2701 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002702 Mask |= Val;
2703 if (i != 0)
2704 Mask <<= 2;
2705 }
2706
2707 return Mask;
2708}
2709
Evan Chengc21a0532006-04-05 01:47:37 +00002710/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2711/// specifies a 8 element shuffle that can be broken into a pair of
2712/// PSHUFHW and PSHUFLW.
2713static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2714 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2715
2716 if (N->getNumOperands() != 8)
2717 return false;
2718
2719 // Lower quadword shuffled.
2720 for (unsigned i = 0; i != 4; ++i) {
2721 SDOperand Arg = N->getOperand(i);
2722 if (Arg.getOpcode() == ISD::UNDEF) continue;
2723 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2724 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002725 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002726 return false;
2727 }
2728
2729 // Upper quadword shuffled.
2730 for (unsigned i = 4; i != 8; ++i) {
2731 SDOperand Arg = N->getOperand(i);
2732 if (Arg.getOpcode() == ISD::UNDEF) continue;
2733 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2734 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2735 if (Val < 4 || Val > 7)
2736 return false;
2737 }
2738
2739 return true;
2740}
2741
Chris Lattner8a594482007-11-25 00:24:49 +00002742/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002743/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002744static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2745 SDOperand &V2, SDOperand &Mask,
2746 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002747 MVT::ValueType VT = Op.getValueType();
2748 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002749 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002750 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002751 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002752
2753 for (unsigned i = 0; i != NumElems; ++i) {
2754 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002755 if (Arg.getOpcode() == ISD::UNDEF) {
2756 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2757 continue;
2758 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002759 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2760 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2761 if (Val < NumElems)
2762 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2763 else
2764 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2765 }
2766
Evan Cheng9eca5e82006-10-25 21:49:50 +00002767 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002768 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002769 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002770}
2771
Evan Cheng779ccea2007-12-07 21:30:01 +00002772/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2773/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002774static
2775SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2776 MVT::ValueType MaskVT = Mask.getValueType();
2777 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2778 unsigned NumElems = Mask.getNumOperands();
2779 SmallVector<SDOperand, 8> MaskVec;
2780 for (unsigned i = 0; i != NumElems; ++i) {
2781 SDOperand Arg = Mask.getOperand(i);
2782 if (Arg.getOpcode() == ISD::UNDEF) {
2783 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2784 continue;
2785 }
2786 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2787 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2788 if (Val < NumElems)
2789 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2790 else
2791 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2792 }
2793 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2794}
2795
2796
Evan Cheng533a0aa2006-04-19 20:35:22 +00002797/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2798/// match movhlps. The lower half elements should come from upper half of
2799/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002800/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002801static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2802 unsigned NumElems = Mask->getNumOperands();
2803 if (NumElems != 4)
2804 return false;
2805 for (unsigned i = 0, e = 2; i != e; ++i)
2806 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2807 return false;
2808 for (unsigned i = 2; i != 4; ++i)
2809 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2810 return false;
2811 return true;
2812}
2813
Evan Cheng5ced1d82006-04-06 23:23:56 +00002814/// isScalarLoadToVector - Returns true if the node is a scalar load that
2815/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002816static inline bool isScalarLoadToVector(SDNode *N) {
2817 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2818 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002819 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002820 }
2821 return false;
2822}
2823
Evan Cheng533a0aa2006-04-19 20:35:22 +00002824/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2825/// match movlp{s|d}. The lower half elements should come from lower half of
2826/// V1 (and in order), and the upper half elements should come from the upper
2827/// half of V2 (and in order). And since V1 will become the source of the
2828/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002829static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002830 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002831 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002832 // Is V2 is a vector load, don't do this transformation. We will try to use
2833 // load folding shufps op.
2834 if (ISD::isNON_EXTLoad(V2))
2835 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002836
Evan Cheng533a0aa2006-04-19 20:35:22 +00002837 unsigned NumElems = Mask->getNumOperands();
2838 if (NumElems != 2 && NumElems != 4)
2839 return false;
2840 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2841 if (!isUndefOrEqual(Mask->getOperand(i), i))
2842 return false;
2843 for (unsigned i = NumElems/2; i != NumElems; ++i)
2844 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2845 return false;
2846 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002847}
2848
Evan Cheng39623da2006-04-20 08:58:49 +00002849/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2850/// all the same.
2851static bool isSplatVector(SDNode *N) {
2852 if (N->getOpcode() != ISD::BUILD_VECTOR)
2853 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002854
Evan Cheng39623da2006-04-20 08:58:49 +00002855 SDOperand SplatValue = N->getOperand(0);
2856 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2857 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002858 return false;
2859 return true;
2860}
2861
Evan Cheng8cf723d2006-09-08 01:50:06 +00002862/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2863/// to an undef.
2864static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002865 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002866 return false;
2867
2868 SDOperand V1 = N->getOperand(0);
2869 SDOperand V2 = N->getOperand(1);
2870 SDOperand Mask = N->getOperand(2);
2871 unsigned NumElems = Mask.getNumOperands();
2872 for (unsigned i = 0; i != NumElems; ++i) {
2873 SDOperand Arg = Mask.getOperand(i);
2874 if (Arg.getOpcode() != ISD::UNDEF) {
2875 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2876 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2877 return false;
2878 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2879 return false;
2880 }
2881 }
2882 return true;
2883}
2884
Evan Cheng213d2cf2007-05-17 18:45:50 +00002885/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2886/// constant +0.0.
2887static inline bool isZeroNode(SDOperand Elt) {
2888 return ((isa<ConstantSDNode>(Elt) &&
2889 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2890 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002891 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002892}
2893
2894/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2895/// to an zero vector.
2896static bool isZeroShuffle(SDNode *N) {
2897 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2898 return false;
2899
2900 SDOperand V1 = N->getOperand(0);
2901 SDOperand V2 = N->getOperand(1);
2902 SDOperand Mask = N->getOperand(2);
2903 unsigned NumElems = Mask.getNumOperands();
2904 for (unsigned i = 0; i != NumElems; ++i) {
2905 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002906 if (Arg.getOpcode() == ISD::UNDEF)
2907 continue;
2908
2909 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2910 if (Idx < NumElems) {
2911 unsigned Opc = V1.Val->getOpcode();
2912 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2913 continue;
2914 if (Opc != ISD::BUILD_VECTOR ||
2915 !isZeroNode(V1.Val->getOperand(Idx)))
2916 return false;
2917 } else if (Idx >= NumElems) {
2918 unsigned Opc = V2.Val->getOpcode();
2919 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2920 continue;
2921 if (Opc != ISD::BUILD_VECTOR ||
2922 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2923 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002924 }
2925 }
2926 return true;
2927}
2928
2929/// getZeroVector - Returns a vector of specified type with all zero elements.
2930///
2931static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2932 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002933
2934 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2935 // type. This ensures they get CSE'd.
2936 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2937 SDOperand Vec;
2938 if (MVT::getSizeInBits(VT) == 64) // MMX
2939 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2940 else // SSE
2941 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2942 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002943}
2944
Chris Lattner8a594482007-11-25 00:24:49 +00002945/// getOnesVector - Returns a vector of specified type with all bits set.
2946///
2947static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2948 assert(MVT::isVector(VT) && "Expected a vector type");
2949
2950 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2951 // type. This ensures they get CSE'd.
2952 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2953 SDOperand Vec;
2954 if (MVT::getSizeInBits(VT) == 64) // MMX
2955 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2956 else // SSE
2957 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2958 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2959}
2960
2961
Evan Cheng39623da2006-04-20 08:58:49 +00002962/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2963/// that point to V2 points to its first element.
2964static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2965 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2966
2967 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002968 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002969 unsigned NumElems = Mask.getNumOperands();
2970 for (unsigned i = 0; i != NumElems; ++i) {
2971 SDOperand Arg = Mask.getOperand(i);
2972 if (Arg.getOpcode() != ISD::UNDEF) {
2973 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2974 if (Val > NumElems) {
2975 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2976 Changed = true;
2977 }
2978 }
2979 MaskVec.push_back(Arg);
2980 }
2981
2982 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002983 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2984 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002985 return Mask;
2986}
2987
Evan Cheng017dcc62006-04-21 01:05:10 +00002988/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2989/// operation of specified width.
2990static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002991 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002992 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002993
Chris Lattner5a88b832007-02-25 07:10:00 +00002994 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002995 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2996 for (unsigned i = 1; i != NumElems; ++i)
2997 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002998 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002999}
3000
Evan Chengc575ca22006-04-17 20:43:08 +00003001/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3002/// of specified width.
3003static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3004 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003005 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003006 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00003007 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3008 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3009 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3010 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003011 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00003012}
3013
Evan Cheng39623da2006-04-20 08:58:49 +00003014/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3015/// of specified width.
3016static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3017 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003018 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00003019 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00003020 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003021 for (unsigned i = 0; i != Half; ++i) {
3022 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3023 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3024 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003025 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003026}
3027
Evan Chengc575ca22006-04-17 20:43:08 +00003028/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3029///
3030static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3031 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00003032 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00003033 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00003034 unsigned NumElems = Mask.getNumOperands();
3035 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003036 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00003037 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003038 NumElems >>= 1;
3039 }
3040 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3041
Chris Lattner8a594482007-11-25 00:24:49 +00003042 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003043 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00003044 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003045 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3046}
3047
Evan Chengba05f722006-04-21 23:03:30 +00003048/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003049/// vector of zero or undef vector. This produces a shuffle where the low
3050/// element of V2 is swizzled into the zero/undef vector, landing at element
3051/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Evan Chengba05f722006-04-21 23:03:30 +00003052static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00003053 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00003054 bool isZero, SelectionDAG &DAG) {
3055 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00003056 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003057 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner8a594482007-11-25 00:24:49 +00003058 SmallVector<SDOperand, 16> MaskVec;
3059 for (unsigned i = 0; i != NumElems; ++i)
3060 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3061 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3062 else
3063 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003064 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3065 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00003066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003067}
3068
Evan Chengc78d3b42006-04-24 18:01:45 +00003069/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3070///
3071static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3072 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003073 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003074 if (NumNonZero > 8)
3075 return SDOperand();
3076
3077 SDOperand V(0, 0);
3078 bool First = true;
3079 for (unsigned i = 0; i < 16; ++i) {
3080 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3081 if (ThisIsNonZero && First) {
3082 if (NumZero)
3083 V = getZeroVector(MVT::v8i16, DAG);
3084 else
3085 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3086 First = false;
3087 }
3088
3089 if ((i & 1) != 0) {
3090 SDOperand ThisElt(0, 0), LastElt(0, 0);
3091 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3092 if (LastIsNonZero) {
3093 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3094 }
3095 if (ThisIsNonZero) {
3096 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3097 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3098 ThisElt, DAG.getConstant(8, MVT::i8));
3099 if (LastIsNonZero)
3100 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3101 } else
3102 ThisElt = LastElt;
3103
3104 if (ThisElt.Val)
3105 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00003106 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003107 }
3108 }
3109
3110 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3111}
3112
Bill Wendlinga348c562007-03-22 18:42:45 +00003113/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003114///
3115static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3116 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003117 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003118 if (NumNonZero > 4)
3119 return SDOperand();
3120
3121 SDOperand V(0, 0);
3122 bool First = true;
3123 for (unsigned i = 0; i < 8; ++i) {
3124 bool isNonZero = (NonZeros & (1 << i)) != 0;
3125 if (isNonZero) {
3126 if (First) {
3127 if (NumZero)
3128 V = getZeroVector(MVT::v8i16, DAG);
3129 else
3130 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3131 First = false;
3132 }
3133 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00003134 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003135 }
3136 }
3137
3138 return V;
3139}
3140
Evan Cheng0db9fe62006-04-25 20:13:52 +00003141SDOperand
3142X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003143 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3144 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3145 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3146 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3147 // eliminated on x86-32 hosts.
3148 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3149 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003150
Chris Lattner8a594482007-11-25 00:24:49 +00003151 if (ISD::isBuildVectorAllOnes(Op.Val))
3152 return getOnesVector(Op.getValueType(), DAG);
3153 return getZeroVector(Op.getValueType(), DAG);
3154 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003155
3156 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003157 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003158 unsigned EVTBits = MVT::getSizeInBits(EVT);
3159
3160 unsigned NumElems = Op.getNumOperands();
3161 unsigned NumZero = 0;
3162 unsigned NumNonZero = 0;
3163 unsigned NonZeros = 0;
Evan Chengdb2d5242007-12-12 06:45:40 +00003164 bool HasNonImms = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003165 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003166 for (unsigned i = 0; i < NumElems; ++i) {
3167 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003168 if (Elt.getOpcode() == ISD::UNDEF)
3169 continue;
3170 Values.insert(Elt);
3171 if (Elt.getOpcode() != ISD::Constant &&
3172 Elt.getOpcode() != ISD::ConstantFP)
3173 HasNonImms = true;
3174 if (isZeroNode(Elt))
3175 NumZero++;
3176 else {
3177 NonZeros |= (1 << i);
3178 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003179 }
3180 }
3181
Dan Gohman7f321562007-06-25 16:23:39 +00003182 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003183 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3184 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003185 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003186
3187 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3188 if (Values.size() == 1)
3189 return SDOperand();
3190
3191 // Special case for single non-zero element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003192 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003193 unsigned Idx = CountTrailingZeros_32(NonZeros);
3194 SDOperand Item = Op.getOperand(Idx);
3195 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3196 if (Idx == 0)
3197 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3198 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3199 NumZero > 0, DAG);
Evan Chengdb2d5242007-12-12 06:45:40 +00003200 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3201 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003202
3203 if (EVTBits == 32) {
3204 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3205 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3206 DAG);
3207 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003208 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003209 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003210 for (unsigned i = 0; i < NumElems; i++)
3211 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003212 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3213 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003214 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3215 DAG.getNode(ISD::UNDEF, VT), Mask);
3216 }
3217 }
3218
Dan Gohmana3941172007-07-24 22:55:08 +00003219 // A vector full of immediates; various special cases are already
3220 // handled, so this is best done with a single constant-pool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00003221 if (!HasNonImms)
Dan Gohmana3941172007-07-24 22:55:08 +00003222 return SDOperand();
3223
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003224 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003225 if (EVTBits == 64)
3226 return SDOperand();
3227
3228 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003229 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003230 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3231 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003232 if (V.Val) return V;
3233 }
3234
Bill Wendling826f36f2007-03-28 00:57:11 +00003235 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003236 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3237 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003238 if (V.Val) return V;
3239 }
3240
3241 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003242 SmallVector<SDOperand, 8> V;
3243 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003244 if (NumElems == 4 && NumZero > 0) {
3245 for (unsigned i = 0; i < 4; ++i) {
3246 bool isZero = !(NonZeros & (1 << i));
3247 if (isZero)
3248 V[i] = getZeroVector(VT, DAG);
3249 else
3250 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3251 }
3252
3253 for (unsigned i = 0; i < 2; ++i) {
3254 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3255 default: break;
3256 case 0:
3257 V[i] = V[i*2]; // Must be a zero vector.
3258 break;
3259 case 1:
3260 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3261 getMOVLMask(NumElems, DAG));
3262 break;
3263 case 2:
3264 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3265 getMOVLMask(NumElems, DAG));
3266 break;
3267 case 3:
3268 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3269 getUnpacklMask(NumElems, DAG));
3270 break;
3271 }
3272 }
3273
Evan Cheng069287d2006-05-16 07:21:53 +00003274 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003275 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003276 // FIXME: we can do the same for v4f32 case when we know both parts of
3277 // the lower half come from scalar_to_vector (loadf32). We should do
3278 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003279 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003280 return V[0];
3281 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003282 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003283 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003284 bool Reverse = (NonZeros & 0x3) == 2;
3285 for (unsigned i = 0; i < 2; ++i)
3286 if (Reverse)
3287 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3288 else
3289 MaskVec.push_back(DAG.getConstant(i, EVT));
3290 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3291 for (unsigned i = 0; i < 2; ++i)
3292 if (Reverse)
3293 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3294 else
3295 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003296 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3297 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003298 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3299 }
3300
3301 if (Values.size() > 2) {
3302 // Expand into a number of unpckl*.
3303 // e.g. for v4f32
3304 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3305 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3306 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3307 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3308 for (unsigned i = 0; i < NumElems; ++i)
3309 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3310 NumElems >>= 1;
3311 while (NumElems != 0) {
3312 for (unsigned i = 0; i < NumElems; ++i)
3313 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3314 UnpckMask);
3315 NumElems >>= 1;
3316 }
3317 return V[0];
3318 }
3319
3320 return SDOperand();
3321}
3322
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003323static
3324SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3325 SDOperand PermMask, SelectionDAG &DAG,
3326 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003327 SDOperand NewV;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003328 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3329 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng14b32e12007-12-11 01:46:18 +00003330 MVT::ValueType PtrVT = TLI.getPointerTy();
3331 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3332 PermMask.Val->op_end());
3333
3334 // First record which half of which vector the low elements come from.
3335 SmallVector<unsigned, 4> LowQuad(4);
3336 for (unsigned i = 0; i < 4; ++i) {
3337 SDOperand Elt = MaskElts[i];
3338 if (Elt.getOpcode() == ISD::UNDEF)
3339 continue;
3340 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3341 int QuadIdx = EltIdx / 4;
3342 ++LowQuad[QuadIdx];
3343 }
3344 int BestLowQuad = -1;
3345 unsigned MaxQuad = 1;
3346 for (unsigned i = 0; i < 4; ++i) {
3347 if (LowQuad[i] > MaxQuad) {
3348 BestLowQuad = i;
3349 MaxQuad = LowQuad[i];
3350 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003351 }
3352
Evan Cheng14b32e12007-12-11 01:46:18 +00003353 // Record which half of which vector the high elements come from.
3354 SmallVector<unsigned, 4> HighQuad(4);
3355 for (unsigned i = 4; i < 8; ++i) {
3356 SDOperand Elt = MaskElts[i];
3357 if (Elt.getOpcode() == ISD::UNDEF)
3358 continue;
3359 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3360 int QuadIdx = EltIdx / 4;
3361 ++HighQuad[QuadIdx];
3362 }
3363 int BestHighQuad = -1;
3364 MaxQuad = 1;
3365 for (unsigned i = 0; i < 4; ++i) {
3366 if (HighQuad[i] > MaxQuad) {
3367 BestHighQuad = i;
3368 MaxQuad = HighQuad[i];
3369 }
3370 }
3371
3372 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3373 if (BestLowQuad != -1 || BestHighQuad != -1) {
3374 // First sort the 4 chunks in order using shufpd.
3375 SmallVector<SDOperand, 8> MaskVec;
3376 if (BestLowQuad != -1)
3377 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3378 else
3379 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3380 if (BestHighQuad != -1)
3381 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3382 else
3383 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3384 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3385 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3386 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3387 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3388 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3389
3390 // Now sort high and low parts separately.
3391 BitVector InOrder(8);
3392 if (BestLowQuad != -1) {
3393 // Sort lower half in order using PSHUFLW.
3394 MaskVec.clear();
3395 bool AnyOutOrder = false;
3396 for (unsigned i = 0; i != 4; ++i) {
3397 SDOperand Elt = MaskElts[i];
3398 if (Elt.getOpcode() == ISD::UNDEF) {
3399 MaskVec.push_back(Elt);
3400 InOrder.set(i);
3401 } else {
3402 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3403 if (EltIdx != i)
3404 AnyOutOrder = true;
3405 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3406 // If this element is in the right place after this shuffle, then
3407 // remember it.
3408 if ((int)(EltIdx / 4) == BestLowQuad)
3409 InOrder.set(i);
3410 }
3411 }
3412 if (AnyOutOrder) {
3413 for (unsigned i = 4; i != 8; ++i)
3414 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3415 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3416 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3417 }
3418 }
3419
3420 if (BestHighQuad != -1) {
3421 // Sort high half in order using PSHUFHW if possible.
3422 MaskVec.clear();
3423 for (unsigned i = 0; i != 4; ++i)
3424 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3425 bool AnyOutOrder = false;
3426 for (unsigned i = 4; i != 8; ++i) {
3427 SDOperand Elt = MaskElts[i];
3428 if (Elt.getOpcode() == ISD::UNDEF) {
3429 MaskVec.push_back(Elt);
3430 InOrder.set(i);
3431 } else {
3432 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3433 if (EltIdx != i)
3434 AnyOutOrder = true;
3435 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3436 // If this element is in the right place after this shuffle, then
3437 // remember it.
3438 if ((int)(EltIdx / 4) == BestHighQuad)
3439 InOrder.set(i);
3440 }
3441 }
3442 if (AnyOutOrder) {
3443 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3444 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3445 }
3446 }
3447
3448 // The other elements are put in the right place using pextrw and pinsrw.
3449 for (unsigned i = 0; i != 8; ++i) {
3450 if (InOrder[i])
3451 continue;
3452 SDOperand Elt = MaskElts[i];
3453 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3454 if (EltIdx == i)
3455 continue;
3456 SDOperand ExtOp = (EltIdx < 8)
3457 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3458 DAG.getConstant(EltIdx, PtrVT))
3459 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3460 DAG.getConstant(EltIdx - 8, PtrVT));
3461 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3462 DAG.getConstant(i, PtrVT));
3463 }
3464 return NewV;
3465 }
3466
3467 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3468 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003469 // First, let's find out how many elements are already in the right order.
3470 unsigned V1InOrder = 0;
3471 unsigned V1FromV1 = 0;
3472 unsigned V2InOrder = 0;
3473 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003474 SmallVector<SDOperand, 8> V1Elts;
3475 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003476 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003477 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003478 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003479 V1Elts.push_back(Elt);
3480 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003481 ++V1InOrder;
3482 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003483 continue;
3484 }
3485 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3486 if (EltIdx == i) {
3487 V1Elts.push_back(Elt);
3488 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3489 ++V1InOrder;
3490 } else if (EltIdx == i+8) {
3491 V1Elts.push_back(Elt);
3492 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3493 ++V2InOrder;
3494 } else if (EltIdx < 8) {
3495 V1Elts.push_back(Elt);
3496 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003497 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003498 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3499 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003500 }
3501 }
3502
3503 if (V2InOrder > V1InOrder) {
3504 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3505 std::swap(V1, V2);
3506 std::swap(V1Elts, V2Elts);
3507 std::swap(V1FromV1, V2FromV2);
3508 }
3509
Evan Cheng14b32e12007-12-11 01:46:18 +00003510 if ((V1FromV1 + V1InOrder) != 8) {
3511 // Some elements are from V2.
3512 if (V1FromV1) {
3513 // If there are elements that are from V1 but out of place,
3514 // then first sort them in place
3515 SmallVector<SDOperand, 8> MaskVec;
3516 for (unsigned i = 0; i < 8; ++i) {
3517 SDOperand Elt = V1Elts[i];
3518 if (Elt.getOpcode() == ISD::UNDEF) {
3519 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3520 continue;
3521 }
3522 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3523 if (EltIdx >= 8)
3524 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3525 else
3526 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3527 }
3528 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3529 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003530 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003531
3532 NewV = V1;
3533 for (unsigned i = 0; i < 8; ++i) {
3534 SDOperand Elt = V1Elts[i];
3535 if (Elt.getOpcode() == ISD::UNDEF)
3536 continue;
3537 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3538 if (EltIdx < 8)
3539 continue;
3540 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3541 DAG.getConstant(EltIdx - 8, PtrVT));
3542 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3543 DAG.getConstant(i, PtrVT));
3544 }
3545 return NewV;
3546 } else {
3547 // All elements are from V1.
3548 NewV = V1;
3549 for (unsigned i = 0; i < 8; ++i) {
3550 SDOperand Elt = V1Elts[i];
3551 if (Elt.getOpcode() == ISD::UNDEF)
3552 continue;
3553 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3554 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3555 DAG.getConstant(EltIdx, PtrVT));
3556 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3557 DAG.getConstant(i, PtrVT));
3558 }
3559 return NewV;
3560 }
3561}
3562
Evan Cheng7a831ce2007-12-15 03:00:47 +00003563/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3564/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3565/// done when every pair / quad of shuffle mask elements point to elements in
3566/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003567/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3568static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003569SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3570 MVT::ValueType VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003571 SDOperand PermMask, SelectionDAG &DAG,
3572 TargetLowering &TLI) {
3573 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003574 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3575 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3576 MVT::ValueType NewVT = MaskVT;
3577 switch (VT) {
3578 case MVT::v4f32: NewVT = MVT::v2f64; break;
3579 case MVT::v4i32: NewVT = MVT::v2i64; break;
3580 case MVT::v8i16: NewVT = MVT::v4i32; break;
3581 case MVT::v16i8: NewVT = MVT::v4i32; break;
3582 default: assert(false && "Unexpected!");
3583 }
3584
3585 if (NewWidth == 2)
3586 if (MVT::isInteger(VT))
3587 NewVT = MVT::v2i64;
3588 else
3589 NewVT = MVT::v2f64;
3590 unsigned Scale = NumElems / NewWidth;
3591 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003592 for (unsigned i = 0; i < NumElems; i += Scale) {
3593 unsigned StartIdx = ~0U;
3594 for (unsigned j = 0; j < Scale; ++j) {
3595 SDOperand Elt = PermMask.getOperand(i+j);
3596 if (Elt.getOpcode() == ISD::UNDEF)
3597 continue;
3598 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3599 if (StartIdx == ~0U)
3600 StartIdx = EltIdx - (EltIdx % Scale);
3601 if (EltIdx != StartIdx + j)
3602 return SDOperand();
3603 }
3604 if (StartIdx == ~0U)
3605 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3606 else
3607 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003608 }
3609
Evan Cheng7a831ce2007-12-15 03:00:47 +00003610 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3611 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3612 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3613 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3614 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003615}
3616
Evan Cheng0db9fe62006-04-25 20:13:52 +00003617SDOperand
3618X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3619 SDOperand V1 = Op.getOperand(0);
3620 SDOperand V2 = Op.getOperand(1);
3621 SDOperand PermMask = Op.getOperand(2);
3622 MVT::ValueType VT = Op.getValueType();
3623 unsigned NumElems = PermMask.getNumOperands();
3624 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3625 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003626 bool V1IsSplat = false;
3627 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003628
Evan Cheng8cf723d2006-09-08 01:50:06 +00003629 if (isUndefShuffle(Op.Val))
3630 return DAG.getNode(ISD::UNDEF, VT);
3631
Evan Cheng213d2cf2007-05-17 18:45:50 +00003632 if (isZeroShuffle(Op.Val))
3633 return getZeroVector(VT, DAG);
3634
Evan Cheng49892af2007-06-19 00:02:56 +00003635 if (isIdentityMask(PermMask.Val))
3636 return V1;
3637 else if (isIdentityMask(PermMask.Val, true))
3638 return V2;
3639
Evan Cheng0db9fe62006-04-25 20:13:52 +00003640 if (isSplatMask(PermMask.Val)) {
3641 if (NumElems <= 4) return Op;
3642 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003643 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003644 }
3645
Evan Cheng7a831ce2007-12-15 03:00:47 +00003646 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3647 // do it!
3648 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3649 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3650 if (NewOp.Val)
3651 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3652 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3653 // FIXME: Figure out a cleaner way to do this.
3654 // Try to make use of movq to zero out the top part.
3655 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3656 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3657 if (NewOp.Val) {
3658 SDOperand NewV1 = NewOp.getOperand(0);
3659 SDOperand NewV2 = NewOp.getOperand(1);
3660 SDOperand NewMask = NewOp.getOperand(2);
3661 if (isCommutedMOVL(NewMask.Val, true, false)) {
3662 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3663 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3664 NewV1, NewV2, getMOVLMask(2, DAG));
3665 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3666 }
3667 }
3668 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3669 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3670 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3671 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3672 }
3673 }
3674
Evan Cheng9bbbb982006-10-25 20:48:19 +00003675 if (X86::isMOVLMask(PermMask.Val))
3676 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003677
Evan Cheng9bbbb982006-10-25 20:48:19 +00003678 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3679 X86::isMOVSLDUPMask(PermMask.Val) ||
3680 X86::isMOVHLPSMask(PermMask.Val) ||
3681 X86::isMOVHPMask(PermMask.Val) ||
3682 X86::isMOVLPMask(PermMask.Val))
3683 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684
Evan Cheng9bbbb982006-10-25 20:48:19 +00003685 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3686 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003687 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003688
Evan Cheng9eca5e82006-10-25 21:49:50 +00003689 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003690 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3691 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003692 V1IsSplat = isSplatVector(V1.Val);
3693 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003694
3695 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003696 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003697 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003698 std::swap(V1IsSplat, V2IsSplat);
3699 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003700 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003701 }
3702
Evan Cheng7a831ce2007-12-15 03:00:47 +00003703 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003704 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3705 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003706 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003707 if (V2IsSplat) {
3708 // V2 is a splat, so the mask may be malformed. That is, it may point
3709 // to any V2 element. The instruction selectior won't like this. Get
3710 // a corrected mask and commute to form a proper MOVS{S|D}.
3711 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3712 if (NewMask.Val != PermMask.Val)
3713 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003714 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003715 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003716 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003717
Evan Chengd9b8e402006-10-16 06:36:00 +00003718 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003719 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003720 X86::isUNPCKLMask(PermMask.Val) ||
3721 X86::isUNPCKHMask(PermMask.Val))
3722 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003723
Evan Cheng9bbbb982006-10-25 20:48:19 +00003724 if (V2IsSplat) {
3725 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003726 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003727 // new vector_shuffle with the corrected mask.
3728 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3729 if (NewMask.Val != PermMask.Val) {
3730 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3731 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3732 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3733 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3734 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3735 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736 }
3737 }
3738 }
3739
3740 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003741 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3742 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3743
3744 if (Commuted) {
3745 // Commute is back and try unpck* again.
3746 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3747 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003748 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003749 X86::isUNPCKLMask(PermMask.Val) ||
3750 X86::isUNPCKHMask(PermMask.Val))
3751 return Op;
3752 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003753
3754 // If VT is integer, try PSHUF* first, then SHUFP*.
3755 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003756 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3757 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3758 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3759 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 X86::isPSHUFHWMask(PermMask.Val) ||
3761 X86::isPSHUFLWMask(PermMask.Val)) {
3762 if (V2.getOpcode() != ISD::UNDEF)
3763 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3764 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3765 return Op;
3766 }
3767
Chris Lattner07c70cd2007-05-17 17:13:13 +00003768 if (X86::isSHUFPMask(PermMask.Val) &&
3769 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771 } else {
3772 // Floating point cases in the other order.
3773 if (X86::isSHUFPMask(PermMask.Val))
3774 return Op;
3775 if (X86::isPSHUFDMask(PermMask.Val) ||
3776 X86::isPSHUFHWMask(PermMask.Val) ||
3777 X86::isPSHUFLWMask(PermMask.Val)) {
3778 if (V2.getOpcode() != ISD::UNDEF)
3779 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3780 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3781 return Op;
3782 }
3783 }
3784
Evan Cheng14b32e12007-12-11 01:46:18 +00003785 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3786 if (VT == MVT::v8i16) {
3787 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3788 if (NewOp.Val)
3789 return NewOp;
3790 }
3791
3792 // Handle all 4 wide cases with a number of shuffles.
3793 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003794 // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003795 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003796 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003797 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003798 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003799 SmallVector<SDOperand, 8> Mask1(NumElems,
3800 DAG.getNode(ISD::UNDEF, MaskEVT));
3801 SmallVector<SDOperand, 8> Mask2(NumElems,
3802 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003803 unsigned NumHi = 0;
3804 unsigned NumLo = 0;
3805 // If no more than two elements come from either vector. This can be
3806 // implemented with two shuffles. First shuffle gather the elements.
3807 // The second shuffle, which takes the first shuffle as both of its
3808 // vector operands, put the elements into the right order.
3809 for (unsigned i = 0; i != NumElems; ++i) {
3810 SDOperand Elt = PermMask.getOperand(i);
3811 if (Elt.getOpcode() == ISD::UNDEF) {
3812 Locs[i] = std::make_pair(-1, -1);
3813 } else {
3814 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3815 if (Val < NumElems) {
3816 Locs[i] = std::make_pair(0, NumLo);
3817 Mask1[NumLo] = Elt;
3818 NumLo++;
3819 } else {
3820 Locs[i] = std::make_pair(1, NumHi);
3821 if (2+NumHi < NumElems)
3822 Mask1[2+NumHi] = Elt;
3823 NumHi++;
3824 }
3825 }
3826 }
3827 if (NumLo <= 2 && NumHi <= 2) {
3828 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003829 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3830 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003831 for (unsigned i = 0; i != NumElems; ++i) {
3832 if (Locs[i].first == -1)
3833 continue;
3834 else {
3835 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3836 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3837 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3838 }
3839 }
3840
3841 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003842 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3843 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003844 }
3845
3846 // Break it into (shuffle shuffle_hi, shuffle_lo).
3847 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003848 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3849 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3850 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003851 unsigned MaskIdx = 0;
3852 unsigned LoIdx = 0;
3853 unsigned HiIdx = NumElems/2;
3854 for (unsigned i = 0; i != NumElems; ++i) {
3855 if (i == NumElems/2) {
3856 MaskPtr = &HiMask;
3857 MaskIdx = 1;
3858 LoIdx = 0;
3859 HiIdx = NumElems/2;
3860 }
3861 SDOperand Elt = PermMask.getOperand(i);
3862 if (Elt.getOpcode() == ISD::UNDEF) {
3863 Locs[i] = std::make_pair(-1, -1);
3864 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3865 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3866 (*MaskPtr)[LoIdx] = Elt;
3867 LoIdx++;
3868 } else {
3869 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3870 (*MaskPtr)[HiIdx] = Elt;
3871 HiIdx++;
3872 }
3873 }
3874
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003875 SDOperand LoShuffle =
3876 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003877 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3878 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003879 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003880 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003881 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3882 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003883 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003884 for (unsigned i = 0; i != NumElems; ++i) {
3885 if (Locs[i].first == -1) {
3886 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3887 } else {
3888 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3889 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3890 }
3891 }
3892 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003893 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3894 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003895 }
3896
3897 return SDOperand();
3898}
3899
3900SDOperand
3901X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3902 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3903 return SDOperand();
3904
3905 MVT::ValueType VT = Op.getValueType();
3906 // TODO: handle v16i8.
3907 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003908 SDOperand Vec = Op.getOperand(0);
3909 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3910 if (Idx == 0)
3911 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3912 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3913 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3914 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003915 // Transform it so it match pextrw which produces a 32-bit result.
3916 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3917 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3918 Op.getOperand(0), Op.getOperand(1));
3919 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3920 DAG.getValueType(VT));
3921 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3922 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003923 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3924 if (Idx == 0)
3925 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003926 // SHUFPS the element to the lowest double word, then movss.
3927 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003928 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003929 IdxVec.
3930 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3931 IdxVec.
3932 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3933 IdxVec.
3934 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3935 IdxVec.
3936 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003937 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3938 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003939 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003940 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003941 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003942 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003943 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 } else if (MVT::getSizeInBits(VT) == 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003945 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3946 if (Idx == 0)
3947 return Op;
3948
3949 // UNPCKHPD the element to the lowest double word, then movsd.
3950 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3951 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3952 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003953 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003954 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003955 IdxVec.
3956 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003957 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3958 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003959 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003960 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3961 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3962 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003963 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 }
3965
3966 return SDOperand();
3967}
3968
3969SDOperand
3970X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003971 MVT::ValueType VT = Op.getValueType();
Evan Cheng794405e2007-12-12 07:55:34 +00003972 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3973 if (EVT == MVT::i8)
3974 return SDOperand();
3975
Evan Cheng0db9fe62006-04-25 20:13:52 +00003976 SDOperand N0 = Op.getOperand(0);
3977 SDOperand N1 = Op.getOperand(1);
3978 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00003979
3980 if (MVT::getSizeInBits(EVT) == 16) {
3981 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3982 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003983 if (N1.getValueType() != MVT::i32)
3984 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3985 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00003986 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003987 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003988 }
3989
Evan Cheng794405e2007-12-12 07:55:34 +00003990 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3991 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3992 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3993 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3994 SmallVector<SDOperand, 4> MaskVec;
3995 for (unsigned i = 0; i < 4; ++i)
3996 MaskVec.push_back(DAG.getConstant((i == Idx) ? i+4 : i, MaskEVT));
3997 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
3998 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3999 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004000}
4001
4002SDOperand
4003X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4004 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
4005 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
4006}
4007
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004008// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4010// one of the above mentioned nodes. It has to be wrapped because otherwise
4011// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4012// be used to form addressing mode. These wrapped nodes will be selected
4013// into MOV32ri.
4014SDOperand
4015X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4016 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00004017 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4018 getPointerTy(),
4019 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004020 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004021 // With PIC, the address is actually $g + Offset.
4022 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4023 !Subtarget->isPICStyleRIPRel()) {
4024 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4025 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4026 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004027 }
4028
4029 return Result;
4030}
4031
4032SDOperand
4033X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4034 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004035 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004036 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004037 // With PIC, the address is actually $g + Offset.
4038 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4039 !Subtarget->isPICStyleRIPRel()) {
4040 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4041 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4042 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004043 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004044
4045 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4046 // load the value at address GV, not the value of GV itself. This means that
4047 // the GlobalAddress must be in the base or index register of the address, not
4048 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004049 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004050 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
4051 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004052
4053 return Result;
4054}
4055
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004056// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4057static SDOperand
4058LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4059 const MVT::ValueType PtrVT) {
4060 SDOperand InFlag;
4061 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4062 DAG.getNode(X86ISD::GlobalBaseReg,
4063 PtrVT), InFlag);
4064 InFlag = Chain.getValue(1);
4065
4066 // emit leal symbol@TLSGD(,%ebx,1), %eax
4067 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4068 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4069 GA->getValueType(0),
4070 GA->getOffset());
4071 SDOperand Ops[] = { Chain, TGA, InFlag };
4072 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4073 InFlag = Result.getValue(2);
4074 Chain = Result.getValue(1);
4075
4076 // call ___tls_get_addr. This function receives its argument in
4077 // the register EAX.
4078 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4079 InFlag = Chain.getValue(1);
4080
4081 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4082 SDOperand Ops1[] = { Chain,
4083 DAG.getTargetExternalSymbol("___tls_get_addr",
4084 PtrVT),
4085 DAG.getRegister(X86::EAX, PtrVT),
4086 DAG.getRegister(X86::EBX, PtrVT),
4087 InFlag };
4088 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4089 InFlag = Chain.getValue(1);
4090
4091 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4092}
4093
4094// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4095// "local exec" model.
4096static SDOperand
4097LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4098 const MVT::ValueType PtrVT) {
4099 // Get the Thread Pointer
4100 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4101 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4102 // exec)
4103 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4104 GA->getValueType(0),
4105 GA->getOffset());
4106 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004107
4108 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4109 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
4110
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004111 // The address of the thread local variable is the add of the thread
4112 // pointer with the offset of the variable.
4113 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4114}
4115
4116SDOperand
4117X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4118 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004119 // TODO: implement the "initial exec"model for pic executables
4120 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4121 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004122 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4123 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4124 // otherwise use the "Local Exec"TLS Model
4125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4126 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4127 else
4128 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4129}
4130
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131SDOperand
4132X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4133 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004134 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004135 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004136 // With PIC, the address is actually $g + Offset.
4137 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4138 !Subtarget->isPICStyleRIPRel()) {
4139 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4140 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4141 Result);
4142 }
4143
4144 return Result;
4145}
4146
4147SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4148 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4149 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4150 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4151 // With PIC, the address is actually $g + Offset.
4152 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4153 !Subtarget->isPICStyleRIPRel()) {
4154 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4155 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4156 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157 }
4158
4159 return Result;
4160}
4161
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004162/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4163/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004165 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4166 "Not an i64 shift!");
4167 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4168 SDOperand ShOpLo = Op.getOperand(0);
4169 SDOperand ShOpHi = Op.getOperand(1);
4170 SDOperand ShAmt = Op.getOperand(2);
4171 SDOperand Tmp1 = isSRA ?
4172 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4173 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00004174
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004175 SDOperand Tmp2, Tmp3;
4176 if (Op.getOpcode() == ISD::SHL_PARTS) {
4177 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4178 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4179 } else {
4180 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4181 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4182 }
Evan Chenge3413162006-01-09 18:33:28 +00004183
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004184 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4185 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4186 DAG.getConstant(32, MVT::i8));
4187 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4188 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004189
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004190 SDOperand Hi, Lo;
4191 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4192 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4193 SmallVector<SDOperand, 4> Ops;
4194 if (Op.getOpcode() == ISD::SHL_PARTS) {
4195 Ops.push_back(Tmp2);
4196 Ops.push_back(Tmp3);
4197 Ops.push_back(CC);
4198 Ops.push_back(Cond);
4199 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00004200
Evan Chenge3413162006-01-09 18:33:28 +00004201 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004202 Ops.push_back(Tmp3);
4203 Ops.push_back(Tmp1);
4204 Ops.push_back(CC);
4205 Ops.push_back(Cond);
4206 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4207 } else {
4208 Ops.push_back(Tmp2);
4209 Ops.push_back(Tmp3);
4210 Ops.push_back(CC);
4211 Ops.push_back(Cond);
4212 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4213
4214 Ops.clear();
4215 Ops.push_back(Tmp3);
4216 Ops.push_back(Tmp1);
4217 Ops.push_back(CC);
4218 Ops.push_back(Cond);
4219 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4220 }
4221
4222 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4223 Ops.clear();
4224 Ops.push_back(Lo);
4225 Ops.push_back(Hi);
4226 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004227}
Evan Chenga3195e82006-01-12 22:54:21 +00004228
Evan Cheng0db9fe62006-04-25 20:13:52 +00004229SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4230 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4231 Op.getOperand(0).getValueType() >= MVT::i16 &&
4232 "Unknown SINT_TO_FP to lower!");
4233
4234 SDOperand Result;
4235 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4236 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4237 MachineFunction &MF = DAG.getMachineFunction();
4238 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4239 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00004240 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004241 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004242
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004243 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004244 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
4245 return Result;
4246 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004247 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00004248 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
4249 Subtarget->is64Bit())
4250 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004251
Evan Cheng0db9fe62006-04-25 20:13:52 +00004252 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004253 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004254 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
4255 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004256 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004257 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4258 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004259 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004260 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261 Ops.push_back(Chain);
4262 Ops.push_back(StackSlot);
4263 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004264 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004265 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004266
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004267 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004268 Chain = Result.getValue(1);
4269 SDOperand InFlag = Result.getValue(2);
4270
4271 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4272 // shouldn't be necessary except that RFP cannot be live across
4273 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004274 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004275 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004276 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004277 Tys = DAG.getVTList(MVT::Other);
4278 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004279 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004280 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004281 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004282 Ops.push_back(DAG.getValueType(Op.getValueType()));
4283 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004284 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00004285 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004287
Evan Cheng0db9fe62006-04-25 20:13:52 +00004288 return Result;
4289}
4290
Chris Lattner27a6c732007-11-24 07:07:01 +00004291std::pair<SDOperand,SDOperand> X86TargetLowering::
4292FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004293 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4294 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004295
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004296 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004297 if (Op.getValueType() == MVT::i32 &&
4298 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattner27a6c732007-11-24 07:07:01 +00004299 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004300 if (Op.getValueType() == MVT::i32 &&
4301 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattner27a6c732007-11-24 07:07:01 +00004302 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004303 if (Subtarget->is64Bit() &&
4304 Op.getValueType() == MVT::i64 &&
4305 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004306 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004307
Evan Cheng87c89352007-10-15 20:11:21 +00004308 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4309 // stack slot.
4310 MachineFunction &MF = DAG.getMachineFunction();
4311 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4312 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4313 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314 unsigned Opc;
4315 switch (Op.getValueType()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004316 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4317 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4318 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4319 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004321
Evan Cheng0db9fe62006-04-25 20:13:52 +00004322 SDOperand Chain = DAG.getEntryNode();
4323 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004324 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
4325 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004326 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00004327 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004328 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004329 SDOperand Ops[] = {
4330 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4331 };
4332 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 Chain = Value.getValue(1);
4334 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4335 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4336 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004337
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004339 SDOperand Ops[] = { Chain, Value, StackSlot };
4340 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004341
Chris Lattner27a6c732007-11-24 07:07:01 +00004342 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004343}
4344
Chris Lattner27a6c732007-11-24 07:07:01 +00004345SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004346 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4347 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4348 if (FIST.Val == 0) return SDOperand();
4349
4350 // Load the result.
4351 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4352}
4353
4354SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4355 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4356 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4357 if (FIST.Val == 0) return 0;
4358
4359 // Return an i64 load from the stack slot.
4360 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4361
4362 // Use a MERGE_VALUES node to drop the chain result value.
4363 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4364}
4365
Evan Cheng0db9fe62006-04-25 20:13:52 +00004366SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4367 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004368 MVT::ValueType EltVT = VT;
4369 if (MVT::isVector(VT))
4370 EltVT = MVT::getVectorElementType(VT);
4371 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004372 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004373 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004374 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004375 CV.push_back(C);
4376 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004377 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004378 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004379 CV.push_back(C);
4380 CV.push_back(C);
4381 CV.push_back(C);
4382 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004383 }
Dan Gohmand3006222007-07-27 17:16:43 +00004384 Constant *C = ConstantVector::get(CV);
4385 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4386 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4387 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4389}
4390
4391SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4392 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004393 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004394 unsigned EltNum = 1;
4395 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004396 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004397 EltNum = MVT::getVectorNumElements(VT);
4398 }
Dan Gohman20382522007-07-10 00:05:58 +00004399 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004401 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004402 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004403 CV.push_back(C);
4404 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004406 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004407 CV.push_back(C);
4408 CV.push_back(C);
4409 CV.push_back(C);
4410 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004411 }
Dan Gohmand3006222007-07-27 17:16:43 +00004412 Constant *C = ConstantVector::get(CV);
4413 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4414 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4415 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004416 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004417 return DAG.getNode(ISD::BIT_CONVERT, VT,
4418 DAG.getNode(ISD::XOR, MVT::v2i64,
4419 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4420 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4421 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004422 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4423 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004424}
4425
Evan Cheng68c47cb2007-01-05 07:55:56 +00004426SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004427 SDOperand Op0 = Op.getOperand(0);
4428 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004429 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004430 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004431 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004432
4433 // If second operand is smaller, extend it first.
4434 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4435 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4436 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004437 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004438 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004439 // And if it is bigger, shrink it first.
4440 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4441 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4442 SrcVT = VT;
4443 SrcTy = MVT::getTypeForValueType(SrcVT);
4444 }
4445
4446 // At this point the operands and the result should have the same
4447 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004448
Evan Cheng68c47cb2007-01-05 07:55:56 +00004449 // First get the sign bit of second operand.
4450 std::vector<Constant*> CV;
4451 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004452 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4453 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004454 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004455 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4456 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4457 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4458 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004459 }
Dan Gohmand3006222007-07-27 17:16:43 +00004460 Constant *C = ConstantVector::get(CV);
4461 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4462 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4463 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004464 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004465
4466 // Shift sign bit right or left if the two operands have different types.
4467 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4468 // Op0 is MVT::f32, Op1 is MVT::f64.
4469 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4470 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4471 DAG.getConstant(32, MVT::i32));
4472 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4473 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4474 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004475 }
4476
Evan Cheng73d6cf12007-01-05 21:37:56 +00004477 // Clear first operand sign bit.
4478 CV.clear();
4479 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004480 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4481 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004482 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004483 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4484 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4485 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4486 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004487 }
Dan Gohmand3006222007-07-27 17:16:43 +00004488 C = ConstantVector::get(CV);
4489 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4490 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4491 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004492 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4493
4494 // Or the value with the sign bit.
4495 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004496}
4497
Evan Chenge5f62042007-09-29 00:00:36 +00004498SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004499 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004500 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004501 SDOperand Op0 = Op.getOperand(0);
4502 SDOperand Op1 = Op.getOperand(1);
4503 SDOperand CC = Op.getOperand(2);
4504 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4505 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4506 unsigned X86CC;
4507
Evan Cheng0488db92007-09-25 01:57:46 +00004508 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004509 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004510 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4511 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004512 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004513 }
Evan Cheng0488db92007-09-25 01:57:46 +00004514
4515 assert(isFP && "Illegal integer SetCC!");
4516
Evan Chenge5f62042007-09-29 00:00:36 +00004517 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004518 switch (SetCCOpcode) {
4519 default: assert(false && "Illegal floating point SetCC!");
4520 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004521 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004522 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004523 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004524 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4525 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4526 }
4527 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004528 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004529 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004530 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004531 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4532 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4533 }
4534 }
4535}
4536
4537
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004539 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004540 SDOperand Cond = Op.getOperand(0);
4541 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004542
Evan Cheng734503b2006-09-11 02:19:56 +00004543 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004544 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004545
Evan Cheng3f41d662007-10-08 22:16:29 +00004546 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4547 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004548 if (Cond.getOpcode() == X86ISD::SETCC) {
4549 CC = Cond.getOperand(0);
4550
Evan Cheng734503b2006-09-11 02:19:56 +00004551 SDOperand Cmp = Cond.getOperand(1);
4552 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004553 MVT::ValueType VT = Op.getValueType();
4554 bool IllegalFPCMov = false;
4555 if (VT == MVT::f32 && !X86ScalarSSEf32)
4556 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4557 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4558 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesenc274f542007-10-16 18:09:08 +00004559 else if (VT == MVT::f80)
4560 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chenge5f62042007-09-29 00:00:36 +00004561 if ((Opc == X86ISD::CMP ||
4562 Opc == X86ISD::COMI ||
4563 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004564 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004565 addTest = false;
4566 }
4567 }
4568
4569 if (addTest) {
4570 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004571 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004572 }
4573
4574 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4575 MVT::Flag);
4576 SmallVector<SDOperand, 4> Ops;
4577 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4578 // condition is true.
4579 Ops.push_back(Op.getOperand(2));
4580 Ops.push_back(Op.getOperand(1));
4581 Ops.push_back(CC);
4582 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004583 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004584}
4585
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004587 bool addTest = true;
4588 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004589 SDOperand Cond = Op.getOperand(1);
4590 SDOperand Dest = Op.getOperand(2);
4591 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004592
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004594 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595
Evan Cheng3f41d662007-10-08 22:16:29 +00004596 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4597 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004599 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600
Evan Cheng734503b2006-09-11 02:19:56 +00004601 SDOperand Cmp = Cond.getOperand(1);
4602 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004603 if (Opc == X86ISD::CMP ||
4604 Opc == X86ISD::COMI ||
4605 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004606 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004607 addTest = false;
4608 }
4609 }
4610
4611 if (addTest) {
4612 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004613 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004614 }
Evan Chenge5f62042007-09-29 00:00:36 +00004615 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004616 Chain, Op.getOperand(2), CC, Cond);
4617}
4618
Evan Cheng32fe1032006-05-25 00:59:30 +00004619SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004620 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4621 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004622
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004623 if (Subtarget->is64Bit())
4624 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4625 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4626 else
4627 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00004628 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004629 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004630 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004631 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00004632 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004633 if (isTailCall && PerformTailCallOpt)
4634 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4635 else
4636 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004637 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004638 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004639 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004640 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004641 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004642 }
Evan Cheng32fe1032006-05-25 00:59:30 +00004643}
4644
Anton Korobeynikove060b532007-04-17 19:34:00 +00004645
4646// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4647// Calls to _alloca is needed to probe the stack when allocating more than 4k
4648// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4649// that the guard pages used by the OS virtual memory manager are allocated in
4650// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004651SDOperand
4652X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4653 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004654 assert(Subtarget->isTargetCygMing() &&
4655 "This should be used only on Cygwin/Mingw targets");
4656
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004657 // Get the inputs.
4658 SDOperand Chain = Op.getOperand(0);
4659 SDOperand Size = Op.getOperand(1);
4660 // FIXME: Ensure alignment here
4661
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004662 SDOperand Flag;
4663
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004664 MVT::ValueType IntPtr = getPointerTy();
4665 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004666
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004667 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4668 Flag = Chain.getValue(1);
4669
4670 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4671 SDOperand Ops[] = { Chain,
4672 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4673 DAG.getRegister(X86::EAX, IntPtr),
4674 Flag };
4675 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4676 Flag = Chain.getValue(1);
4677
4678 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004679
4680 std::vector<MVT::ValueType> Tys;
4681 Tys.push_back(SPTy);
4682 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004683 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4684 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004685}
4686
Evan Cheng1bc78042006-04-26 01:20:17 +00004687SDOperand
4688X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004689 MachineFunction &MF = DAG.getMachineFunction();
4690 const Function* Fn = MF.getFunction();
4691 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004692 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004693 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00004694 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00004695
Evan Cheng25caf632006-05-23 21:06:34 +00004696 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004697 if (Subtarget->is64Bit())
4698 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004699 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004700 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004701 default:
4702 assert(0 && "Unsupported calling convention");
4703 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004704 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004705 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004706 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004707 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004708 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004709 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004710 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004711 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004712 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00004713 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004714 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004715}
4716
Evan Cheng0db9fe62006-04-25 20:13:52 +00004717SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4718 SDOperand InFlag(0, 0);
4719 SDOperand Chain = Op.getOperand(0);
4720 unsigned Align =
4721 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4722 if (Align == 0) Align = 1;
4723
4724 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004725 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004726 // The libc version is likely to be faster for these cases. It can use the
4727 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004728 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004729 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004730 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004731 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004732 TargetLowering::ArgListTy Args;
4733 TargetLowering::ArgListEntry Entry;
4734 Entry.Node = Op.getOperand(1);
4735 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004736 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004737 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004738 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4739 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004740 Args.push_back(Entry);
4741 Entry.Node = Op.getOperand(3);
4742 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004743 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004744 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004745 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4746 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004747 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004748
Evan Cheng0db9fe62006-04-25 20:13:52 +00004749 MVT::ValueType AVT;
4750 SDOperand Count;
4751 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4752 unsigned BytesLeft = 0;
4753 bool TwoRepStos = false;
4754 if (ValC) {
4755 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004756 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004757
Evan Cheng0db9fe62006-04-25 20:13:52 +00004758 // If the value is a constant, then we can potentially use larger sets.
4759 switch (Align & 3) {
4760 case 2: // WORD aligned
4761 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004762 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004763 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004764 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004765 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004766 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004767 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004768 Val = (Val << 8) | Val;
4769 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004770 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4771 AVT = MVT::i64;
4772 ValReg = X86::RAX;
4773 Val = (Val << 32) | Val;
4774 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004775 break;
4776 default: // Byte aligned
4777 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004778 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004779 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004780 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004781 }
4782
Evan Cheng25ab6902006-09-08 06:48:29 +00004783 if (AVT > MVT::i8) {
4784 if (I) {
4785 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4786 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4787 BytesLeft = I->getValue() % UBytes;
4788 } else {
4789 assert(AVT >= MVT::i32 &&
4790 "Do not use rep;stos if not at least DWORD aligned");
4791 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4792 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4793 TwoRepStos = true;
4794 }
4795 }
4796
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4798 InFlag);
4799 InFlag = Chain.getValue(1);
4800 } else {
4801 AVT = MVT::i8;
4802 Count = Op.getOperand(3);
4803 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4804 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004805 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004806
Evan Cheng25ab6902006-09-08 06:48:29 +00004807 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4808 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004810 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4811 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004812 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004813
Chris Lattnerd96d0722007-02-25 06:40:16 +00004814 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004815 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 Ops.push_back(Chain);
4817 Ops.push_back(DAG.getValueType(AVT));
4818 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004819 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004820
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 if (TwoRepStos) {
4822 InFlag = Chain.getValue(1);
4823 Count = Op.getOperand(3);
4824 MVT::ValueType CVT = Count.getValueType();
4825 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004826 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4827 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4828 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004830 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 Ops.clear();
4832 Ops.push_back(Chain);
4833 Ops.push_back(DAG.getValueType(MVT::i8));
4834 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004835 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004837 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838 SDOperand Value;
4839 unsigned Val = ValC->getValue() & 255;
4840 unsigned Offset = I->getValue() - BytesLeft;
4841 SDOperand DstAddr = Op.getOperand(1);
4842 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004843 if (BytesLeft >= 4) {
4844 Val = (Val << 8) | Val;
4845 Val = (Val << 16) | Val;
4846 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004847 Chain = DAG.getStore(Chain, Value,
4848 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4849 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004850 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004851 BytesLeft -= 4;
4852 Offset += 4;
4853 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004854 if (BytesLeft >= 2) {
4855 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004856 Chain = DAG.getStore(Chain, Value,
4857 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4858 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004859 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004860 BytesLeft -= 2;
4861 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004862 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004863 if (BytesLeft == 1) {
4864 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004865 Chain = DAG.getStore(Chain, Value,
4866 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4867 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004868 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004869 }
Evan Cheng386031a2006-03-24 07:29:27 +00004870 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004871
Evan Cheng0db9fe62006-04-25 20:13:52 +00004872 return Chain;
4873}
Evan Cheng11e15b32006-04-03 20:53:28 +00004874
Rafael Espindola068317b2007-09-28 12:53:01 +00004875SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4876 SDOperand Dest,
4877 SDOperand Source,
4878 unsigned Size,
4879 unsigned Align,
4880 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004881 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004882 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004883 switch (Align & 3) {
4884 case 2: // WORD aligned
4885 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004886 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004887 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004888 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004889 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4890 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004891 break;
4892 default: // Byte aligned
4893 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004894 break;
4895 }
4896
Rafael Espindola068317b2007-09-28 12:53:01 +00004897 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4898 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4899 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004900
Evan Cheng0db9fe62006-04-25 20:13:52 +00004901 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004902 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4903 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004904 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004905 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004906 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004907 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004908 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004909 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004910 InFlag = Chain.getValue(1);
4911
Chris Lattnerd96d0722007-02-25 06:40:16 +00004912 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004913 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004914 Ops.push_back(Chain);
4915 Ops.push_back(DAG.getValueType(AVT));
4916 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004917 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004918
Rafael Espindola068317b2007-09-28 12:53:01 +00004919 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004920 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004921 unsigned Offset = Size - BytesLeft;
4922 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004924 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004925 MVT::ValueType SrcVT = SrcAddr.getValueType();
4926 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004927 if (BytesLeft >= 4) {
4928 Value = DAG.getLoad(MVT::i32, Chain,
4929 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4930 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004931 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004932 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004933 Chain = DAG.getStore(Chain, Value,
4934 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4935 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004936 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004937 BytesLeft -= 4;
4938 Offset += 4;
4939 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004940 if (BytesLeft >= 2) {
4941 Value = DAG.getLoad(MVT::i16, Chain,
4942 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4943 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004944 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004946 Chain = DAG.getStore(Chain, Value,
4947 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4948 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004949 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950 BytesLeft -= 2;
4951 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004952 }
4953
Evan Cheng0db9fe62006-04-25 20:13:52 +00004954 if (BytesLeft == 1) {
4955 Value = DAG.getLoad(MVT::i8, Chain,
4956 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4957 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004958 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004959 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004960 Chain = DAG.getStore(Chain, Value,
4961 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4962 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004963 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004964 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004965 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004966
4967 return Chain;
4968}
4969
Chris Lattner27a6c732007-11-24 07:07:01 +00004970/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4971SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00004972 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00004973 SDOperand TheChain = N->getOperand(0);
4974 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004975 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004976 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4977 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4978 MVT::i64, rax.getValue(2));
4979 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004980 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004981 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00004982 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00004983 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004984
4985 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner27a6c732007-11-24 07:07:01 +00004986 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004987 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004988
Chris Lattner27a6c732007-11-24 07:07:01 +00004989 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4990 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4991 MVT::i32, eax.getValue(2));
4992 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4993 SDOperand Ops[] = { eax, edx };
4994 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4995
4996 // Use a MERGE_VALUES to return the value and chain.
4997 Ops[1] = edx.getValue(1);
4998 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4999 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005000}
5001
5002SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005003 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
5004
Evan Cheng25ab6902006-09-08 06:48:29 +00005005 if (!Subtarget->is64Bit()) {
5006 // vastart just stores the address of the VarArgsFrameIndex slot into the
5007 // memory location argument.
5008 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005009 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
5010 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005011 }
5012
5013 // __va_list_tag:
5014 // gp_offset (0 - 6 * 8)
5015 // fp_offset (48 - 48 + 8 * 16)
5016 // overflow_arg_area (point to parameters coming in memory).
5017 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00005018 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00005019 SDOperand FIN = Op.getOperand(1);
5020 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00005021 SDOperand Store = DAG.getStore(Op.getOperand(0),
5022 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005023 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005024 MemOps.push_back(Store);
5025
5026 // Store fp_offset
5027 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5028 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00005029 Store = DAG.getStore(Op.getOperand(0),
5030 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005031 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005032 MemOps.push_back(Store);
5033
5034 // Store ptr to overflow_arg_area
5035 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5036 DAG.getConstant(4, getPointerTy()));
5037 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005038 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
5039 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005040 MemOps.push_back(Store);
5041
5042 // Store ptr to reg_save_area.
5043 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5044 DAG.getConstant(8, getPointerTy()));
5045 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005046 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
5047 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005048 MemOps.push_back(Store);
5049 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050}
5051
Evan Chengae642192007-03-02 23:16:35 +00005052SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5053 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5054 SDOperand Chain = Op.getOperand(0);
5055 SDOperand DstPtr = Op.getOperand(1);
5056 SDOperand SrcPtr = Op.getOperand(2);
5057 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
5058 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
5059
5060 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
5061 SrcSV->getValue(), SrcSV->getOffset());
5062 Chain = SrcPtr.getValue(1);
5063 for (unsigned i = 0; i < 3; ++i) {
5064 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
5065 SrcSV->getValue(), SrcSV->getOffset());
5066 Chain = Val.getValue(1);
5067 Chain = DAG.getStore(Chain, Val, DstPtr,
5068 DstSV->getValue(), DstSV->getOffset());
5069 if (i == 2)
5070 break;
5071 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
5072 DAG.getConstant(8, getPointerTy()));
5073 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
5074 DAG.getConstant(8, getPointerTy()));
5075 }
5076 return Chain;
5077}
5078
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079SDOperand
5080X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5081 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5082 switch (IntNo) {
5083 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00005084 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085 case Intrinsic::x86_sse_comieq_ss:
5086 case Intrinsic::x86_sse_comilt_ss:
5087 case Intrinsic::x86_sse_comile_ss:
5088 case Intrinsic::x86_sse_comigt_ss:
5089 case Intrinsic::x86_sse_comige_ss:
5090 case Intrinsic::x86_sse_comineq_ss:
5091 case Intrinsic::x86_sse_ucomieq_ss:
5092 case Intrinsic::x86_sse_ucomilt_ss:
5093 case Intrinsic::x86_sse_ucomile_ss:
5094 case Intrinsic::x86_sse_ucomigt_ss:
5095 case Intrinsic::x86_sse_ucomige_ss:
5096 case Intrinsic::x86_sse_ucomineq_ss:
5097 case Intrinsic::x86_sse2_comieq_sd:
5098 case Intrinsic::x86_sse2_comilt_sd:
5099 case Intrinsic::x86_sse2_comile_sd:
5100 case Intrinsic::x86_sse2_comigt_sd:
5101 case Intrinsic::x86_sse2_comige_sd:
5102 case Intrinsic::x86_sse2_comineq_sd:
5103 case Intrinsic::x86_sse2_ucomieq_sd:
5104 case Intrinsic::x86_sse2_ucomilt_sd:
5105 case Intrinsic::x86_sse2_ucomile_sd:
5106 case Intrinsic::x86_sse2_ucomigt_sd:
5107 case Intrinsic::x86_sse2_ucomige_sd:
5108 case Intrinsic::x86_sse2_ucomineq_sd: {
5109 unsigned Opc = 0;
5110 ISD::CondCode CC = ISD::SETCC_INVALID;
5111 switch (IntNo) {
5112 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005113 case Intrinsic::x86_sse_comieq_ss:
5114 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005115 Opc = X86ISD::COMI;
5116 CC = ISD::SETEQ;
5117 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005118 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005119 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005120 Opc = X86ISD::COMI;
5121 CC = ISD::SETLT;
5122 break;
5123 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005124 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125 Opc = X86ISD::COMI;
5126 CC = ISD::SETLE;
5127 break;
5128 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005129 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005130 Opc = X86ISD::COMI;
5131 CC = ISD::SETGT;
5132 break;
5133 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005134 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005135 Opc = X86ISD::COMI;
5136 CC = ISD::SETGE;
5137 break;
5138 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005139 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005140 Opc = X86ISD::COMI;
5141 CC = ISD::SETNE;
5142 break;
5143 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005144 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145 Opc = X86ISD::UCOMI;
5146 CC = ISD::SETEQ;
5147 break;
5148 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005149 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150 Opc = X86ISD::UCOMI;
5151 CC = ISD::SETLT;
5152 break;
5153 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005154 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005155 Opc = X86ISD::UCOMI;
5156 CC = ISD::SETLE;
5157 break;
5158 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005159 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005160 Opc = X86ISD::UCOMI;
5161 CC = ISD::SETGT;
5162 break;
5163 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005164 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005165 Opc = X86ISD::UCOMI;
5166 CC = ISD::SETGE;
5167 break;
5168 case Intrinsic::x86_sse_ucomineq_ss:
5169 case Intrinsic::x86_sse2_ucomineq_sd:
5170 Opc = X86ISD::UCOMI;
5171 CC = ISD::SETNE;
5172 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005173 }
Evan Cheng734503b2006-09-11 02:19:56 +00005174
Evan Cheng0db9fe62006-04-25 20:13:52 +00005175 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00005176 SDOperand LHS = Op.getOperand(1);
5177 SDOperand RHS = Op.getOperand(2);
5178 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005179
Evan Chenge5f62042007-09-29 00:00:36 +00005180 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5181 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5182 DAG.getConstant(X86CC, MVT::i8), Cond);
5183 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005184 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005185 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005186}
Evan Cheng72261582005-12-20 06:22:03 +00005187
Nate Begemanbcc5f362007-01-29 22:58:52 +00005188SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5189 // Depths > 0 not supported yet!
5190 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5191 return SDOperand();
5192
5193 // Just load the return address
5194 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5195 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5196}
5197
5198SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5199 // Depths > 0 not supported yet!
5200 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5201 return SDOperand();
5202
5203 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5204 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5205 DAG.getConstant(4, getPointerTy()));
5206}
5207
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005208SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5209 SelectionDAG &DAG) {
5210 // Is not yet supported on x86-64
5211 if (Subtarget->is64Bit())
5212 return SDOperand();
5213
5214 return DAG.getConstant(8, getPointerTy());
5215}
5216
5217SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5218{
5219 assert(!Subtarget->is64Bit() &&
5220 "Lowering of eh_return builtin is not supported yet on x86-64");
5221
5222 MachineFunction &MF = DAG.getMachineFunction();
5223 SDOperand Chain = Op.getOperand(0);
5224 SDOperand Offset = Op.getOperand(1);
5225 SDOperand Handler = Op.getOperand(2);
5226
5227 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5228 getPointerTy());
5229
5230 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5231 DAG.getConstant(-4UL, getPointerTy()));
5232 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5233 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5234 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5235 MF.addLiveOut(X86::ECX);
5236
5237 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5238 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5239}
5240
Duncan Sandsb116fac2007-07-27 20:02:49 +00005241SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5242 SelectionDAG &DAG) {
5243 SDOperand Root = Op.getOperand(0);
5244 SDOperand Trmp = Op.getOperand(1); // trampoline
5245 SDOperand FPtr = Op.getOperand(2); // nested function
5246 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5247
5248 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
5249
5250 if (Subtarget->is64Bit()) {
5251 return SDOperand(); // not yet supported
5252 } else {
5253 Function *Func = (Function *)
5254 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5255 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005256 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005257
5258 switch (CC) {
5259 default:
5260 assert(0 && "Unsupported calling convention");
5261 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005262 case CallingConv::X86_StdCall: {
5263 // Pass 'nest' parameter in ECX.
5264 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005265 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005266
5267 // Check that ECX wasn't needed by an 'inreg' parameter.
5268 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00005269 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005270
5271 if (Attrs && !Func->isVarArg()) {
5272 unsigned InRegCount = 0;
5273 unsigned Idx = 1;
5274
5275 for (FunctionType::param_iterator I = FTy->param_begin(),
5276 E = FTy->param_end(); I != E; ++I, ++Idx)
5277 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5278 // FIXME: should only count parameters that are lowered to integers.
5279 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5280
5281 if (InRegCount > 2) {
5282 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5283 abort();
5284 }
5285 }
5286 break;
5287 }
5288 case CallingConv::X86_FastCall:
5289 // Pass 'nest' parameter in EAX.
5290 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005291 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005292 break;
5293 }
5294
Duncan Sandsee465742007-08-29 19:01:20 +00005295 const X86InstrInfo *TII =
5296 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5297
Duncan Sandsb116fac2007-07-27 20:02:49 +00005298 SDOperand OutChains[4];
5299 SDOperand Addr, Disp;
5300
5301 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5302 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5303
Duncan Sandsee465742007-08-29 19:01:20 +00005304 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5305 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
5306 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00005307 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5308
5309 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5310 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5311 TrmpSV->getOffset() + 1, false, 1);
5312
Duncan Sandsee465742007-08-29 19:01:20 +00005313 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005314 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5315 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5316 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5317
5318 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5319 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5320 TrmpSV->getOffset() + 6, false, 1);
5321
Duncan Sandsf7331b32007-09-11 14:10:23 +00005322 SDOperand Ops[] =
5323 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5324 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005325 }
5326}
5327
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005328SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5329 /*
5330 The rounding mode is in bits 11:10 of FPSR, and has the following
5331 settings:
5332 00 Round to nearest
5333 01 Round to -inf
5334 10 Round to +inf
5335 11 Round to 0
5336
5337 FLT_ROUNDS, on the other hand, expects the following:
5338 -1 Undefined
5339 0 Round to 0
5340 1 Round to nearest
5341 2 Round to +inf
5342 3 Round to -inf
5343
5344 To perform the conversion, we do:
5345 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5346 */
5347
5348 MachineFunction &MF = DAG.getMachineFunction();
5349 const TargetMachine &TM = MF.getTarget();
5350 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5351 unsigned StackAlignment = TFI.getStackAlignment();
5352 MVT::ValueType VT = Op.getValueType();
5353
5354 // Save FP Control Word to stack slot
5355 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5356 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5357
5358 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5359 DAG.getEntryNode(), StackSlot);
5360
5361 // Load FP Control Word from stack slot
5362 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5363
5364 // Transform as necessary
5365 SDOperand CWD1 =
5366 DAG.getNode(ISD::SRL, MVT::i16,
5367 DAG.getNode(ISD::AND, MVT::i16,
5368 CWD, DAG.getConstant(0x800, MVT::i16)),
5369 DAG.getConstant(11, MVT::i8));
5370 SDOperand CWD2 =
5371 DAG.getNode(ISD::SRL, MVT::i16,
5372 DAG.getNode(ISD::AND, MVT::i16,
5373 CWD, DAG.getConstant(0x400, MVT::i16)),
5374 DAG.getConstant(9, MVT::i8));
5375
5376 SDOperand RetVal =
5377 DAG.getNode(ISD::AND, MVT::i16,
5378 DAG.getNode(ISD::ADD, MVT::i16,
5379 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5380 DAG.getConstant(1, MVT::i16)),
5381 DAG.getConstant(3, MVT::i16));
5382
5383
5384 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5385 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5386}
5387
Evan Cheng18efe262007-12-14 02:13:44 +00005388SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5389 MVT::ValueType VT = Op.getValueType();
5390 MVT::ValueType OpVT = VT;
5391 unsigned NumBits = MVT::getSizeInBits(VT);
5392
5393 Op = Op.getOperand(0);
5394 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005395 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005396 OpVT = MVT::i32;
5397 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5398 }
Evan Cheng18efe262007-12-14 02:13:44 +00005399
Evan Cheng152804e2007-12-14 08:30:15 +00005400 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5401 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5402 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5403
5404 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5405 SmallVector<SDOperand, 4> Ops;
5406 Ops.push_back(Op);
5407 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5408 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5409 Ops.push_back(Op.getValue(1));
5410 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5411
5412 // Finally xor with NumBits-1.
5413 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5414
Evan Cheng18efe262007-12-14 02:13:44 +00005415 if (VT == MVT::i8)
5416 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5417 return Op;
5418}
5419
5420SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5421 MVT::ValueType VT = Op.getValueType();
5422 MVT::ValueType OpVT = VT;
Evan Cheng152804e2007-12-14 08:30:15 +00005423 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng18efe262007-12-14 02:13:44 +00005424
5425 Op = Op.getOperand(0);
5426 if (VT == MVT::i8) {
5427 OpVT = MVT::i32;
5428 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5429 }
Evan Cheng152804e2007-12-14 08:30:15 +00005430
5431 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5432 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5433 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5434
5435 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5436 SmallVector<SDOperand, 4> Ops;
5437 Ops.push_back(Op);
5438 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5439 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5440 Ops.push_back(Op.getValue(1));
5441 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5442
Evan Cheng18efe262007-12-14 02:13:44 +00005443 if (VT == MVT::i8)
5444 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5445 return Op;
5446}
5447
Evan Cheng0db9fe62006-04-25 20:13:52 +00005448/// LowerOperation - Provide custom lowering hooks for some operations.
5449///
5450SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5451 switch (Op.getOpcode()) {
5452 default: assert(0 && "Should not custom lower this!");
5453 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5454 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5455 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5456 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5457 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5458 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5459 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005460 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005461 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5462 case ISD::SHL_PARTS:
5463 case ISD::SRA_PARTS:
5464 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5465 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5466 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5467 case ISD::FABS: return LowerFABS(Op, DAG);
5468 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005469 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005470 case ISD::SETCC: return LowerSETCC(Op, DAG);
5471 case ISD::SELECT: return LowerSELECT(Op, DAG);
5472 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005473 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005474 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005475 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005476 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5478 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005480 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005482 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5483 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005484 case ISD::FRAME_TO_ARGS_OFFSET:
5485 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005486 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005487 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005488 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005489 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005490 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5491 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005492
5493 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5494 case ISD::READCYCLECOUNTER:
5495 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005496 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005497}
5498
5499/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5500SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5501 switch (N->getOpcode()) {
5502 default: assert(0 && "Should not custom lower this!");
5503 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5504 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5505 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506}
5507
Evan Cheng72261582005-12-20 06:22:03 +00005508const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5509 switch (Opcode) {
5510 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005511 case X86ISD::BSF: return "X86ISD::BSF";
5512 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005513 case X86ISD::SHLD: return "X86ISD::SHLD";
5514 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005515 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005516 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005517 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005518 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005519 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005520 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005521 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5522 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5523 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005524 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005525 case X86ISD::FST: return "X86ISD::FST";
5526 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00005527 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005528 case X86ISD::CALL: return "X86ISD::CALL";
5529 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5530 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5531 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005532 case X86ISD::COMI: return "X86ISD::COMI";
5533 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005534 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005535 case X86ISD::CMOV: return "X86ISD::CMOV";
5536 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005537 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005538 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5539 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005540 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005541 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005542 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005543 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005544 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005545 case X86ISD::FMAX: return "X86ISD::FMAX";
5546 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005547 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5548 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005549 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5550 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005551 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005552 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005553 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng72261582005-12-20 06:22:03 +00005554 }
5555}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005556
Chris Lattnerc9addb72007-03-30 23:15:24 +00005557// isLegalAddressingMode - Return true if the addressing mode represented
5558// by AM is legal for this target, for a load/store of the specified type.
5559bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5560 const Type *Ty) const {
5561 // X86 supports extremely general addressing modes.
5562
5563 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5564 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5565 return false;
5566
5567 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005568 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005569 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5570 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005571
5572 // X86-64 only supports addr of globals in small code model.
5573 if (Subtarget->is64Bit()) {
5574 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5575 return false;
5576 // If lower 4G is not available, then we must use rip-relative addressing.
5577 if (AM.BaseOffs || AM.Scale > 1)
5578 return false;
5579 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005580 }
5581
5582 switch (AM.Scale) {
5583 case 0:
5584 case 1:
5585 case 2:
5586 case 4:
5587 case 8:
5588 // These scales always work.
5589 break;
5590 case 3:
5591 case 5:
5592 case 9:
5593 // These scales are formed with basereg+scalereg. Only accept if there is
5594 // no basereg yet.
5595 if (AM.HasBaseReg)
5596 return false;
5597 break;
5598 default: // Other stuff never works.
5599 return false;
5600 }
5601
5602 return true;
5603}
5604
5605
Evan Cheng2bd122c2007-10-26 01:56:11 +00005606bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5607 if (!Ty1->isInteger() || !Ty2->isInteger())
5608 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005609 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5610 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5611 if (NumBits1 <= NumBits2)
5612 return false;
5613 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005614}
5615
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005616bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5617 MVT::ValueType VT2) const {
5618 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5619 return false;
5620 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5621 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5622 if (NumBits1 <= NumBits2)
5623 return false;
5624 return Subtarget->is64Bit() || NumBits1 < 64;
5625}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005626
Evan Cheng60c07e12006-07-05 22:17:51 +00005627/// isShuffleMaskLegal - Targets can use this to indicate that they only
5628/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5629/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5630/// are assumed to be legal.
5631bool
5632X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5633 // Only do shuffles on 128-bit vector types for now.
5634 if (MVT::getSizeInBits(VT) == 64) return false;
5635 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005636 isIdentityMask(Mask.Val) ||
5637 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005638 isSplatMask(Mask.Val) ||
5639 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5640 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005641 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005642 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005643 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005644}
5645
5646bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5647 MVT::ValueType EVT,
5648 SelectionDAG &DAG) const {
5649 unsigned NumElts = BVOps.size();
5650 // Only do shuffles on 128-bit vector types for now.
5651 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5652 if (NumElts == 2) return true;
5653 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005654 return (isMOVLMask(&BVOps[0], 4) ||
5655 isCommutedMOVL(&BVOps[0], 4, true) ||
5656 isSHUFPMask(&BVOps[0], 4) ||
5657 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005658 }
5659 return false;
5660}
5661
5662//===----------------------------------------------------------------------===//
5663// X86 Scheduler Hooks
5664//===----------------------------------------------------------------------===//
5665
5666MachineBasicBlock *
5667X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5668 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005669 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005670 switch (MI->getOpcode()) {
5671 default: assert(false && "Unexpected instr type to insert");
5672 case X86::CMOV_FR32:
5673 case X86::CMOV_FR64:
5674 case X86::CMOV_V4F32:
5675 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005676 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005677 // To "insert" a SELECT_CC instruction, we actually have to insert the
5678 // diamond control-flow pattern. The incoming instruction knows the
5679 // destination vreg to set, the condition code register to branch on, the
5680 // true/false values to select between, and a branch opcode to use.
5681 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5682 ilist<MachineBasicBlock>::iterator It = BB;
5683 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005684
Evan Cheng60c07e12006-07-05 22:17:51 +00005685 // thisMBB:
5686 // ...
5687 // TrueVal = ...
5688 // cmpTY ccX, r1, r2
5689 // bCC copy1MBB
5690 // fallthrough --> copy0MBB
5691 MachineBasicBlock *thisMBB = BB;
5692 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5693 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005694 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005695 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005696 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005697 MachineFunction *F = BB->getParent();
5698 F->getBasicBlockList().insert(It, copy0MBB);
5699 F->getBasicBlockList().insert(It, sinkMBB);
5700 // Update machine-CFG edges by first adding all successors of the current
5701 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005702 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005703 e = BB->succ_end(); i != e; ++i)
5704 sinkMBB->addSuccessor(*i);
5705 // Next, remove all successors of the current block, and add the true
5706 // and fallthrough blocks as its successors.
5707 while(!BB->succ_empty())
5708 BB->removeSuccessor(BB->succ_begin());
5709 BB->addSuccessor(copy0MBB);
5710 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005711
Evan Cheng60c07e12006-07-05 22:17:51 +00005712 // copy0MBB:
5713 // %FalseValue = ...
5714 // # fallthrough to sinkMBB
5715 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005716
Evan Cheng60c07e12006-07-05 22:17:51 +00005717 // Update machine-CFG edges
5718 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005719
Evan Cheng60c07e12006-07-05 22:17:51 +00005720 // sinkMBB:
5721 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5722 // ...
5723 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005724 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005725 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5726 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5727
5728 delete MI; // The pseudo instruction is gone now.
5729 return BB;
5730 }
5731
Dale Johannesen849f2142007-07-03 00:53:03 +00005732 case X86::FP32_TO_INT16_IN_MEM:
5733 case X86::FP32_TO_INT32_IN_MEM:
5734 case X86::FP32_TO_INT64_IN_MEM:
5735 case X86::FP64_TO_INT16_IN_MEM:
5736 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005737 case X86::FP64_TO_INT64_IN_MEM:
5738 case X86::FP80_TO_INT16_IN_MEM:
5739 case X86::FP80_TO_INT32_IN_MEM:
5740 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005741 // Change the floating point control register to use "round towards zero"
5742 // mode when truncating to an integer value.
5743 MachineFunction *F = BB->getParent();
5744 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005745 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005746
5747 // Load the old value of the high byte of the control word...
5748 unsigned OldCW =
5749 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005750 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005751
5752 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005753 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5754 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005755
5756 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005757 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005758
5759 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005760 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5761 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005762
5763 // Get the X86 opcode to use.
5764 unsigned Opc;
5765 switch (MI->getOpcode()) {
5766 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005767 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5768 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5769 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5770 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5771 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5772 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005773 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5774 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5775 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005776 }
5777
5778 X86AddressMode AM;
5779 MachineOperand &Op = MI->getOperand(0);
5780 if (Op.isRegister()) {
5781 AM.BaseType = X86AddressMode::RegBase;
5782 AM.Base.Reg = Op.getReg();
5783 } else {
5784 AM.BaseType = X86AddressMode::FrameIndexBase;
5785 AM.Base.FrameIndex = Op.getFrameIndex();
5786 }
5787 Op = MI->getOperand(1);
5788 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005789 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005790 Op = MI->getOperand(2);
5791 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005792 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005793 Op = MI->getOperand(3);
5794 if (Op.isGlobalAddress()) {
5795 AM.GV = Op.getGlobal();
5796 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005797 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005798 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005799 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5800 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005801
5802 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005803 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005804
5805 delete MI; // The pseudo instruction is gone now.
5806 return BB;
5807 }
5808 }
5809}
5810
5811//===----------------------------------------------------------------------===//
5812// X86 Optimization Hooks
5813//===----------------------------------------------------------------------===//
5814
Nate Begeman368e18d2006-02-16 21:11:51 +00005815void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5816 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005817 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005818 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005819 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005820 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005821 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005822 assert((Opc >= ISD::BUILTIN_OP_END ||
5823 Opc == ISD::INTRINSIC_WO_CHAIN ||
5824 Opc == ISD::INTRINSIC_W_CHAIN ||
5825 Opc == ISD::INTRINSIC_VOID) &&
5826 "Should use MaskedValueIsZero if you don't know whether Op"
5827 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005828
Evan Cheng865f0602006-04-05 06:11:20 +00005829 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005830 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005831 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005832 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005833 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5834 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005835 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005836}
Chris Lattner259e97c2006-01-31 19:43:35 +00005837
Evan Cheng206ee9d2006-07-07 08:33:52 +00005838/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5839/// element of the result of the vector shuffle.
5840static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5841 MVT::ValueType VT = N->getValueType(0);
5842 SDOperand PermMask = N->getOperand(2);
5843 unsigned NumElems = PermMask.getNumOperands();
5844 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5845 i %= NumElems;
5846 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5847 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005848 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005849 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5850 SDOperand Idx = PermMask.getOperand(i);
5851 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005852 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005853 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5854 }
5855 return SDOperand();
5856}
5857
5858/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5859/// node is a GlobalAddress + an offset.
5860static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005861 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005862 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005863 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5864 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5865 return true;
5866 }
Evan Cheng0085a282006-11-30 21:55:46 +00005867 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005868 SDOperand N1 = N->getOperand(0);
5869 SDOperand N2 = N->getOperand(1);
5870 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5871 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5872 if (V) {
5873 Offset += V->getSignExtended();
5874 return true;
5875 }
5876 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5877 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5878 if (V) {
5879 Offset += V->getSignExtended();
5880 return true;
5881 }
5882 }
5883 }
5884 return false;
5885}
5886
5887/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5888/// + Dist * Size.
5889static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5890 MachineFrameInfo *MFI) {
5891 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5892 return false;
5893
5894 SDOperand Loc = N->getOperand(1);
5895 SDOperand BaseLoc = Base->getOperand(1);
5896 if (Loc.getOpcode() == ISD::FrameIndex) {
5897 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5898 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005899 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5900 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005901 int FS = MFI->getObjectSize(FI);
5902 int BFS = MFI->getObjectSize(BFI);
5903 if (FS != BFS || FS != Size) return false;
5904 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5905 } else {
5906 GlobalValue *GV1 = NULL;
5907 GlobalValue *GV2 = NULL;
5908 int64_t Offset1 = 0;
5909 int64_t Offset2 = 0;
5910 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5911 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5912 if (isGA1 && isGA2 && GV1 == GV2)
5913 return Offset1 == (Offset2 + Dist*Size);
5914 }
5915
5916 return false;
5917}
5918
Evan Cheng1e60c092006-07-10 21:37:44 +00005919static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5920 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005921 GlobalValue *GV;
5922 int64_t Offset;
5923 if (isGAPlusOffset(Base, GV, Offset))
5924 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5925 else {
5926 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005927 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005928 if (BFI < 0)
5929 // Fixed objects do not specify alignment, however the offsets are known.
5930 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5931 (MFI->getObjectOffset(BFI) % 16) == 0);
5932 else
5933 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005934 }
5935 return false;
5936}
5937
5938
5939/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5940/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5941/// if the load addresses are consecutive, non-overlapping, and in the right
5942/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005943static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5944 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005945 MachineFunction &MF = DAG.getMachineFunction();
5946 MachineFrameInfo *MFI = MF.getFrameInfo();
5947 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005948 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005949 SDOperand PermMask = N->getOperand(2);
5950 int NumElems = (int)PermMask.getNumOperands();
5951 SDNode *Base = NULL;
5952 for (int i = 0; i < NumElems; ++i) {
5953 SDOperand Idx = PermMask.getOperand(i);
5954 if (Idx.getOpcode() == ISD::UNDEF) {
5955 if (!Base) return SDOperand();
5956 } else {
5957 SDOperand Arg =
5958 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005959 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005960 return SDOperand();
5961 if (!Base)
5962 Base = Arg.Val;
5963 else if (!isConsecutiveLoad(Arg.Val, Base,
5964 i, MVT::getSizeInBits(EVT)/8,MFI))
5965 return SDOperand();
5966 }
5967 }
5968
Evan Cheng1e60c092006-07-10 21:37:44 +00005969 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005970 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005971 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005972 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005973 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005974 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005975 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5976 LD->getSrcValueOffset(), LD->isVolatile(),
5977 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005978 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005979}
5980
Chris Lattner83e6c992006-10-04 06:57:07 +00005981/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5982static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5983 const X86Subtarget *Subtarget) {
5984 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005985
Chris Lattner83e6c992006-10-04 06:57:07 +00005986 // If we have SSE[12] support, try to form min/max nodes.
5987 if (Subtarget->hasSSE2() &&
5988 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5989 if (Cond.getOpcode() == ISD::SETCC) {
5990 // Get the LHS/RHS of the select.
5991 SDOperand LHS = N->getOperand(1);
5992 SDOperand RHS = N->getOperand(2);
5993 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005994
Evan Cheng8ca29322006-11-10 21:43:37 +00005995 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005996 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005997 switch (CC) {
5998 default: break;
5999 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6000 case ISD::SETULE:
6001 case ISD::SETLE:
6002 if (!UnsafeFPMath) break;
6003 // FALL THROUGH.
6004 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6005 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006006 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006007 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006008
Chris Lattner1907a7b2006-10-05 04:11:26 +00006009 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6010 case ISD::SETUGT:
6011 case ISD::SETGT:
6012 if (!UnsafeFPMath) break;
6013 // FALL THROUGH.
6014 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6015 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006016 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006017 break;
6018 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006019 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006020 switch (CC) {
6021 default: break;
6022 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6023 case ISD::SETUGT:
6024 case ISD::SETGT:
6025 if (!UnsafeFPMath) break;
6026 // FALL THROUGH.
6027 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6028 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006029 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006030 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006031
Chris Lattner1907a7b2006-10-05 04:11:26 +00006032 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6033 case ISD::SETULE:
6034 case ISD::SETLE:
6035 if (!UnsafeFPMath) break;
6036 // FALL THROUGH.
6037 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6038 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006039 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006040 break;
6041 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006042 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006043
Evan Cheng8ca29322006-11-10 21:43:37 +00006044 if (Opcode)
6045 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006046 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006047
Chris Lattner83e6c992006-10-04 06:57:07 +00006048 }
6049
6050 return SDOperand();
6051}
6052
6053
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006054SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006055 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006056 SelectionDAG &DAG = DCI.DAG;
6057 switch (N->getOpcode()) {
6058 default: break;
6059 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00006060 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00006061 case ISD::SELECT:
6062 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006063 }
6064
6065 return SDOperand();
6066}
6067
Evan Cheng60c07e12006-07-05 22:17:51 +00006068//===----------------------------------------------------------------------===//
6069// X86 Inline Assembly Support
6070//===----------------------------------------------------------------------===//
6071
Chris Lattnerf4dff842006-07-11 02:54:03 +00006072/// getConstraintType - Given a constraint letter, return the type of
6073/// constraint it is for this target.
6074X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006075X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6076 if (Constraint.size() == 1) {
6077 switch (Constraint[0]) {
6078 case 'A':
6079 case 'r':
6080 case 'R':
6081 case 'l':
6082 case 'q':
6083 case 'Q':
6084 case 'x':
6085 case 'Y':
6086 return C_RegisterClass;
6087 default:
6088 break;
6089 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006090 }
Chris Lattner4234f572007-03-25 02:14:49 +00006091 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006092}
6093
Chris Lattner48884cd2007-08-25 00:47:38 +00006094/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6095/// vector. If it is invalid, don't add anything to Ops.
6096void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6097 char Constraint,
6098 std::vector<SDOperand>&Ops,
6099 SelectionDAG &DAG) {
6100 SDOperand Result(0, 0);
6101
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006102 switch (Constraint) {
6103 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006104 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006106 if (C->getValue() <= 31) {
6107 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6108 break;
6109 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006110 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006111 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006112 case 'N':
6113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006114 if (C->getValue() <= 255) {
6115 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6116 break;
6117 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006118 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006119 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006120 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006121 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006122 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6123 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6124 break;
6125 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006126
Chris Lattnerdc43a882007-05-03 16:52:29 +00006127 // If we are in non-pic codegen mode, we allow the address of a global (with
6128 // an optional displacement) to be used with 'i'.
6129 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6130 int64_t Offset = 0;
6131
6132 // Match either (GA) or (GA+C)
6133 if (GA) {
6134 Offset = GA->getOffset();
6135 } else if (Op.getOpcode() == ISD::ADD) {
6136 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6137 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6138 if (C && GA) {
6139 Offset = GA->getOffset()+C->getValue();
6140 } else {
6141 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6142 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6143 if (C && GA)
6144 Offset = GA->getOffset()+C->getValue();
6145 else
6146 C = 0, GA = 0;
6147 }
6148 }
6149
6150 if (GA) {
6151 // If addressing this global requires a load (e.g. in PIC mode), we can't
6152 // match.
6153 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6154 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006155 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006156
Chris Lattnerdc43a882007-05-03 16:52:29 +00006157 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6158 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006159 Result = Op;
6160 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006161 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006162
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006163 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006164 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006165 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006166 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006167
6168 if (Result.Val) {
6169 Ops.push_back(Result);
6170 return;
6171 }
6172 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006173}
6174
Chris Lattner259e97c2006-01-31 19:43:35 +00006175std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006176getRegClassForInlineAsmConstraint(const std::string &Constraint,
6177 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006178 if (Constraint.size() == 1) {
6179 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006180 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006181 default: break; // Unknown constraint letter
6182 case 'A': // EAX/EDX
6183 if (VT == MVT::i32 || VT == MVT::i64)
6184 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6185 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006186 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6187 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006188 if (VT == MVT::i32)
6189 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6190 else if (VT == MVT::i16)
6191 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6192 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006193 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006194 else if (VT == MVT::i64)
6195 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6196 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006197 }
6198 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006199
Chris Lattner1efa40f2006-02-22 00:56:39 +00006200 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006201}
Chris Lattnerf76d1802006-07-31 23:26:50 +00006202
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006203std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00006204X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6205 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00006206 // First, see if this is a constraint that directly corresponds to an LLVM
6207 // register class.
6208 if (Constraint.size() == 1) {
6209 // GCC Constraint Letters
6210 switch (Constraint[0]) {
6211 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006212 case 'r': // GENERAL_REGS
6213 case 'R': // LEGACY_REGS
6214 case 'l': // INDEX_REGS
6215 if (VT == MVT::i64 && Subtarget->is64Bit())
6216 return std::make_pair(0U, X86::GR64RegisterClass);
6217 if (VT == MVT::i32)
6218 return std::make_pair(0U, X86::GR32RegisterClass);
6219 else if (VT == MVT::i16)
6220 return std::make_pair(0U, X86::GR16RegisterClass);
6221 else if (VT == MVT::i8)
6222 return std::make_pair(0U, X86::GR8RegisterClass);
6223 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00006224 case 'y': // MMX_REGS if MMX allowed.
6225 if (!Subtarget->hasMMX()) break;
6226 return std::make_pair(0U, X86::VR64RegisterClass);
6227 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006228 case 'Y': // SSE_REGS if SSE2 allowed
6229 if (!Subtarget->hasSSE2()) break;
6230 // FALL THROUGH.
6231 case 'x': // SSE_REGS if SSE1 allowed
6232 if (!Subtarget->hasSSE1()) break;
6233
6234 switch (VT) {
6235 default: break;
6236 // Scalar SSE types.
6237 case MVT::f32:
6238 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00006239 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006240 case MVT::f64:
6241 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00006242 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006243 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00006244 case MVT::v16i8:
6245 case MVT::v8i16:
6246 case MVT::v4i32:
6247 case MVT::v2i64:
6248 case MVT::v4f32:
6249 case MVT::v2f64:
6250 return std::make_pair(0U, X86::VR128RegisterClass);
6251 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006252 break;
6253 }
6254 }
6255
Chris Lattnerf76d1802006-07-31 23:26:50 +00006256 // Use the default implementation in TargetLowering to convert the register
6257 // constraint into a member of a register class.
6258 std::pair<unsigned, const TargetRegisterClass*> Res;
6259 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006260
6261 // Not found as a standard register?
6262 if (Res.second == 0) {
6263 // GCC calls "st(0)" just plain "st".
6264 if (StringsEqualNoCase("{st}", Constraint)) {
6265 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006266 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006267 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006268
Chris Lattner1a60aa72006-10-31 19:42:44 +00006269 return Res;
6270 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006271
Chris Lattnerf76d1802006-07-31 23:26:50 +00006272 // Otherwise, check to see if this is a register class of the wrong value
6273 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6274 // turn into {ax},{dx}.
6275 if (Res.second->hasType(VT))
6276 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006277
Chris Lattnerf76d1802006-07-31 23:26:50 +00006278 // All of the single-register GCC register classes map their values onto
6279 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6280 // really want an 8-bit or 32-bit register, map to the appropriate register
6281 // class and return the appropriate register.
6282 if (Res.second != X86::GR16RegisterClass)
6283 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006284
Chris Lattnerf76d1802006-07-31 23:26:50 +00006285 if (VT == MVT::i8) {
6286 unsigned DestReg = 0;
6287 switch (Res.first) {
6288 default: break;
6289 case X86::AX: DestReg = X86::AL; break;
6290 case X86::DX: DestReg = X86::DL; break;
6291 case X86::CX: DestReg = X86::CL; break;
6292 case X86::BX: DestReg = X86::BL; break;
6293 }
6294 if (DestReg) {
6295 Res.first = DestReg;
6296 Res.second = Res.second = X86::GR8RegisterClass;
6297 }
6298 } else if (VT == MVT::i32) {
6299 unsigned DestReg = 0;
6300 switch (Res.first) {
6301 default: break;
6302 case X86::AX: DestReg = X86::EAX; break;
6303 case X86::DX: DestReg = X86::EDX; break;
6304 case X86::CX: DestReg = X86::ECX; break;
6305 case X86::BX: DestReg = X86::EBX; break;
6306 case X86::SI: DestReg = X86::ESI; break;
6307 case X86::DI: DestReg = X86::EDI; break;
6308 case X86::BP: DestReg = X86::EBP; break;
6309 case X86::SP: DestReg = X86::ESP; break;
6310 }
6311 if (DestReg) {
6312 Res.first = DestReg;
6313 Res.second = Res.second = X86::GR32RegisterClass;
6314 }
Evan Cheng25ab6902006-09-08 06:48:29 +00006315 } else if (VT == MVT::i64) {
6316 unsigned DestReg = 0;
6317 switch (Res.first) {
6318 default: break;
6319 case X86::AX: DestReg = X86::RAX; break;
6320 case X86::DX: DestReg = X86::RDX; break;
6321 case X86::CX: DestReg = X86::RCX; break;
6322 case X86::BX: DestReg = X86::RBX; break;
6323 case X86::SI: DestReg = X86::RSI; break;
6324 case X86::DI: DestReg = X86::RDI; break;
6325 case X86::BP: DestReg = X86::RBP; break;
6326 case X86::SP: DestReg = X86::RSP; break;
6327 }
6328 if (DestReg) {
6329 Res.first = DestReg;
6330 Res.second = Res.second = X86::GR64RegisterClass;
6331 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00006332 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006333
Chris Lattnerf76d1802006-07-31 23:26:50 +00006334 return Res;
6335}