blob: efb73072787e40714a7b2f59f2ac4eea80b264fa [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
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/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000039#include "llvm/ADT/StringExtras.h"
Duncan Sandsb116fac2007-07-27 20:02:49 +000040#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043X86TargetLowering::X86TargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000045 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000046 X86ScalarSSEf64 = Subtarget->hasSSE2();
47 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000048 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000049
Evan Cheng559806f2006-01-27 08:10:46 +000050
Anton Korobeynikov2365f512007-07-14 14:06:15 +000051 RegInfo = TM.getRegisterInfo();
52
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 // Set up the TargetLowering object.
54
55 // X86 is weird, it always uses i8 for shift amounts and setcc results.
56 setShiftAmountType(MVT::i8);
57 setSetCCResultType(MVT::i8);
58 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000059 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000060 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000061 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000062
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000063 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000064 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000065 setUseUnderscoreSetJmp(false);
66 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000067 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000068 // MS runtime is weird: it exports _setjmp, but longjmp!
69 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(false);
71 } else {
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(true);
74 }
75
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000076 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000077 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
78 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
79 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000080 if (Subtarget->is64Bit())
81 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000082
Evan Chengc5484282006-10-04 00:56:09 +000083 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
84
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000085 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
86 // operation.
87 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
89 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000090
Evan Cheng25ab6902006-09-08 06:48:29 +000091 if (Subtarget->is64Bit()) {
92 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000093 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000094 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000095 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +000096 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
97 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
98 else
99 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
100 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000101
102 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
105 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000106 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000107 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000108 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000109 // f32 and f64 cases are Legal, f80 case is not
110 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
111 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000112 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
113 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
114 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000115
Dale Johannesen73328d12007-09-19 23:55:34 +0000116 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
117 // are Legal, f80 is custom lowered.
118 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
119 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000120
Evan Cheng02568ff2006-01-30 22:13:22 +0000121 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
122 // this operation.
123 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
124 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
125
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000126 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000127 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000128 // f32 and f64 cases are Legal, f80 case is not
129 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000130 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000131 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000132 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133 }
134
135 // Handle FP_TO_UINT by promoting the destination to a larger signed
136 // conversion.
137 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
138 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
140
Evan Cheng25ab6902006-09-08 06:48:29 +0000141 if (Subtarget->is64Bit()) {
142 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000144 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000145 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000146 // Expand FP_TO_UINT into a select.
147 // FIXME: We would like to use a Custom expander here eventually to do
148 // the optimal thing for SSE vs. the default expansion in the legalizer.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
150 else
151 // With SSE3 we can use fisttpll to convert to a signed i64.
152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
153 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154
Chris Lattner399610a2006-12-05 18:22:22 +0000155 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000156 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000157 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
158 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
159 }
Chris Lattner21f66852005-12-23 05:15:23 +0000160
Dan Gohman525178c2007-10-08 18:33:35 +0000161 // Scalar integer multiply, multiply-high, divide, and remainder are
162 // lowered to use operations that produce two results, to match the
163 // available instructions. This exposes the two-result form to trivial
164 // CSE, which is able to combine x/y and x%y into a single instruction,
165 // for example. The single-result multiply instructions are introduced
166 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
167 // is not needed.
168 setOperationAction(ISD::MUL , MVT::i8 , Expand);
169 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
170 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
171 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
172 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
173 setOperationAction(ISD::SREM , MVT::i8 , Expand);
174 setOperationAction(ISD::UREM , MVT::i8 , Expand);
175 setOperationAction(ISD::MUL , MVT::i16 , Expand);
176 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
177 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
178 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
179 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
180 setOperationAction(ISD::SREM , MVT::i16 , Expand);
181 setOperationAction(ISD::UREM , MVT::i16 , Expand);
182 setOperationAction(ISD::MUL , MVT::i32 , Expand);
183 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
187 setOperationAction(ISD::SREM , MVT::i32 , Expand);
188 setOperationAction(ISD::UREM , MVT::i32 , Expand);
189 setOperationAction(ISD::MUL , MVT::i64 , Expand);
190 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
191 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
192 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
193 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
194 setOperationAction(ISD::SREM , MVT::i64 , Expand);
195 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000196
Evan Chengc35497f2006-10-30 08:02:39 +0000197 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000198 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000199 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
200 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000201 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
207 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000208 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000209 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
210
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000211 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000212 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
213 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000215 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
216 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000217 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000218 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
219 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 if (Subtarget->is64Bit()) {
221 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 }
225
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000226 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000227 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000228
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229 // These should be promoted to a larger select which is supported.
230 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
231 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000232 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000233 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
234 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
235 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
236 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000237 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000238 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
239 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
240 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
241 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
242 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000243 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 if (Subtarget->is64Bit()) {
245 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
246 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
247 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000248 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000249 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000250 if (!Subtarget->is64Bit())
251 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
252
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000253 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000254 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000255 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000256 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000257 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000258 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000259 if (Subtarget->is64Bit()) {
260 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
261 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
262 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
263 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
264 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000265 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000266 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
267 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
268 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000269 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000270 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
271 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000273 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000274 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000275 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000276 if (!Subtarget->isTargetDarwin() &&
277 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000278 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000279 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000280
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000281 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
282 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
283 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
284 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
285 if (Subtarget->is64Bit()) {
286 // FIXME: Verify
287 setExceptionPointerRegister(X86::RAX);
288 setExceptionSelectorRegister(X86::RDX);
289 } else {
290 setExceptionPointerRegister(X86::EAX);
291 setExceptionSelectorRegister(X86::EDX);
292 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000293 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000294
Duncan Sandsf7331b32007-09-11 14:10:23 +0000295 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000296
Nate Begemanacc398c2006-01-25 18:21:52 +0000297 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
298 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000299 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000300 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000301 if (Subtarget->is64Bit())
302 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
303 else
304 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
305
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000306 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000307 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000308 if (Subtarget->is64Bit())
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000310 if (Subtarget->isTargetCygMing())
311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
312 else
313 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000314
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000315 if (X86ScalarSSEf64) {
316 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000317 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000318 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
319 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320
Evan Cheng223547a2006-01-31 22:28:30 +0000321 // Use ANDPD to simulate FABS.
322 setOperationAction(ISD::FABS , MVT::f64, Custom);
323 setOperationAction(ISD::FABS , MVT::f32, Custom);
324
325 // Use XORP to simulate FNEG.
326 setOperationAction(ISD::FNEG , MVT::f64, Custom);
327 setOperationAction(ISD::FNEG , MVT::f32, Custom);
328
Evan Cheng68c47cb2007-01-05 07:55:56 +0000329 // Use ANDPD and ORPD to simulate FCOPYSIGN.
330 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
331 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
332
Evan Chengd25e9e82006-02-02 00:28:23 +0000333 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 setOperationAction(ISD::FSIN , MVT::f64, Expand);
335 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336 setOperationAction(ISD::FREM , MVT::f64, Expand);
337 setOperationAction(ISD::FSIN , MVT::f32, Expand);
338 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 setOperationAction(ISD::FREM , MVT::f32, Expand);
340
Chris Lattnera54aa942006-01-29 06:26:08 +0000341 // Expand FP immediates into loads from the stack, except for the special
342 // cases we handle.
343 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
344 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000345 addLegalFPImmediate(APFloat(+0.0)); // xorpd
346 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000347
348 // Conversions to long double (in X87) go through memory.
349 setConvertAction(MVT::f32, MVT::f80, Expand);
350 setConvertAction(MVT::f64, MVT::f80, Expand);
351
352 // Conversions from long double (in X87) go through memory.
353 setConvertAction(MVT::f80, MVT::f32, Expand);
354 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000355 } else if (X86ScalarSSEf32) {
356 // Use SSE for f32, x87 for f64.
357 // Set up the FP register classes.
358 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
359 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
360
361 // Use ANDPS to simulate FABS.
362 setOperationAction(ISD::FABS , MVT::f32, Custom);
363
364 // Use XORP to simulate FNEG.
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
367 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
368
369 // Use ANDPS and ORPS to simulate FCOPYSIGN.
370 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
371 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
372
373 // We don't support sin/cos/fmod
374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
376 setOperationAction(ISD::FREM , MVT::f32, Expand);
377
378 // Expand FP immediates into loads from the stack, except for the special
379 // cases we handle.
380 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
381 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
382 addLegalFPImmediate(APFloat(+0.0f)); // xorps
383 addLegalFPImmediate(APFloat(+0.0)); // FLD0
384 addLegalFPImmediate(APFloat(+1.0)); // FLD1
385 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
386 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
387
388 // SSE->x87 conversions go through memory.
389 setConvertAction(MVT::f32, MVT::f64, Expand);
390 setConvertAction(MVT::f32, MVT::f80, Expand);
391
392 // x87->SSE truncations need to go through memory.
393 setConvertAction(MVT::f80, MVT::f32, Expand);
394 setConvertAction(MVT::f64, MVT::f32, Expand);
395 // And x87->x87 truncations also.
396 setConvertAction(MVT::f80, MVT::f64, Expand);
397
398 if (!UnsafeFPMath) {
399 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
400 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
401 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000402 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000403 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000404 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000405 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
406 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000407
Evan Cheng68c47cb2007-01-05 07:55:56 +0000408 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000409 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000410 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
411 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000412
413 // Floating truncations need to go through memory.
414 setConvertAction(MVT::f80, MVT::f32, Expand);
415 setConvertAction(MVT::f64, MVT::f32, Expand);
416 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000417
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000418 if (!UnsafeFPMath) {
419 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
420 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
421 }
422
Chris Lattnera54aa942006-01-29 06:26:08 +0000423 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000424 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000425 addLegalFPImmediate(APFloat(+0.0)); // FLD0
426 addLegalFPImmediate(APFloat(+1.0)); // FLD1
427 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
428 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
430 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
431 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
432 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000434
Dale Johannesen59a58732007-08-05 18:49:15 +0000435 // Long double always uses X87.
436 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000437 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
438 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
439 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000440 if (!UnsafeFPMath) {
441 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
442 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
443 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000444
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000445 // Always use a library call for pow.
446 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
447 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
448 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
449
Evan Chengd30bf012006-03-01 01:11:20 +0000450 // First set operation action for all vector types to expand. Then we
451 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000452 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
453 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000454 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
455 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000456 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000457 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000458 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000459 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000460 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
461 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
462 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
463 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
464 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
465 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000466 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000467 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000468 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000469 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000470 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
471 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
472 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
473 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
474 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
475 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
476 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000477 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
478 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
479 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
480 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000481 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000482 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
483 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
484 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman89081322007-12-12 22:21:26 +0000485 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000491 }
492
Evan Chenga88973f2006-03-22 19:22:18 +0000493 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000494 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
495 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
496 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000497 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000498
Evan Chengd30bf012006-03-01 01:11:20 +0000499 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000500
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000501 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
502 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
503 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000504 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000505
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000506 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
507 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
508 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000509 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000510
Bill Wendling74027e92007-03-15 21:24:36 +0000511 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
512 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
513
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000514 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000515 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000516 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000517 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
518 setOperationAction(ISD::AND, MVT::v2i32, Promote);
519 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
520 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000521
522 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000523 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000524 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000525 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
526 setOperationAction(ISD::OR, MVT::v2i32, Promote);
527 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
528 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000529
530 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000531 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000532 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000533 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
534 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
535 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
536 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000537
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000538 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000539 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000540 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000541 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
542 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
543 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
544 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000545
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000546 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
547 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
548 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
549 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000550
551 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
552 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
553 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000554 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000555
556 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
557 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000558 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
559 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000560 }
561
Evan Chenga88973f2006-03-22 19:22:18 +0000562 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000563 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
564
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000565 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
566 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
567 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
568 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000569 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
570 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000571 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
572 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
573 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000574 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000575 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000576 }
577
Evan Chenga88973f2006-03-22 19:22:18 +0000578 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000579 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
580 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
581 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
582 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
583 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
584
Evan Chengf7c378e2006-04-10 07:23:14 +0000585 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
586 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
587 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000588 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000589 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
590 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
591 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000592 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000593 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000594 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
595 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
596 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
597 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000598 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
599 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000600
Evan Chengf7c378e2006-04-10 07:23:14 +0000601 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
602 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000603 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000604 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
605 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
606 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000607
Evan Cheng2c3ae372006-04-12 21:21:57 +0000608 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
609 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begeman844e0f92007-12-11 01:41:33 +0000610 // Do not attempt to custom lower non-power-of-2 vectors
611 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
612 continue;
Evan Cheng2c3ae372006-04-12 21:21:57 +0000613 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
614 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
615 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
616 }
617 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
621 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000622 if (Subtarget->is64Bit())
623 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000624
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000626 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
627 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
628 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
629 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
630 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
631 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
632 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000633 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
634 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000635 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
636 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000637 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000638
639 // Custom lower v2i64 and v2f64 selects.
640 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000641 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000642 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000643 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644 }
645
Evan Cheng6be2c582006-04-05 23:38:46 +0000646 // We want to custom lower some of our intrinsics.
647 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
648
Evan Cheng206ee9d2006-07-07 08:33:52 +0000649 // We have target-specific dag combine patterns for the following nodes:
650 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000651 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000652
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000653 computeRegisterProperties();
654
Evan Cheng87ed7162006-02-14 08:25:08 +0000655 // FIXME: These should be based on subtarget info. Plus, the values should
656 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000657 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
658 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
659 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000660 allowUnalignedMemoryAccesses = true; // x86 supports it!
661}
662
Chris Lattner2b02a442007-02-25 08:29:00 +0000663
Evan Chengcc415862007-11-09 01:32:10 +0000664/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
665/// jumptable.
666SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
667 SelectionDAG &DAG) const {
668 if (usesGlobalOffsetTable())
669 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
670 if (!Subtarget->isPICStyleRIPRel())
671 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
672 return Table;
673}
674
Chris Lattner2b02a442007-02-25 08:29:00 +0000675//===----------------------------------------------------------------------===//
676// Return Value Calling Convention Implementation
677//===----------------------------------------------------------------------===//
678
Chris Lattner59ed56b2007-02-28 04:55:35 +0000679#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000680
681/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
682/// exists skip possible ISD:TokenFactor.
683static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
684 if (Chain.getOpcode()==X86ISD::TAILCALL) {
685 return Chain;
686 } else if (Chain.getOpcode()==ISD::TokenFactor) {
687 if (Chain.getNumOperands() &&
688 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
689 return Chain.getOperand(0);
690 }
691 return Chain;
692}
Chris Lattner9774c912007-02-27 05:28:59 +0000693
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000694/// LowerRET - Lower an ISD::RET node.
695SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
696 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
697
Chris Lattner9774c912007-02-27 05:28:59 +0000698 SmallVector<CCValAssign, 16> RVLocs;
699 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000700 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
701 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000702 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000703
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000704 // If this is the first return lowered for this function, add the regs to the
705 // liveout set for the function.
706 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000707 for (unsigned i = 0; i != RVLocs.size(); ++i)
708 if (RVLocs[i].isRegLoc())
709 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000710 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000711 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000712
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000713 // Handle tail call return.
714 Chain = GetPossiblePreceedingTailCall(Chain);
715 if (Chain.getOpcode() == X86ISD::TAILCALL) {
716 SDOperand TailCall = Chain;
717 SDOperand TargetAddress = TailCall.getOperand(1);
718 SDOperand StackAdjustment = TailCall.getOperand(2);
719 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
720 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
721 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
722 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
723 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
724 "Expecting an global address, external symbol, or register");
725 assert( StackAdjustment.getOpcode() == ISD::Constant &&
726 "Expecting a const value");
727
728 SmallVector<SDOperand,8> Operands;
729 Operands.push_back(Chain.getOperand(0));
730 Operands.push_back(TargetAddress);
731 Operands.push_back(StackAdjustment);
732 // Copy registers used by the call. Last operand is a flag so it is not
733 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000734 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000735 Operands.push_back(Chain.getOperand(i));
736 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000737 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
738 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000739 }
740
741 // Regular return.
742 SDOperand Flag;
743
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000744 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000745 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
746 RVLocs[0].getLocReg() != X86::ST0) {
747 for (unsigned i = 0; i != RVLocs.size(); ++i) {
748 CCValAssign &VA = RVLocs[i];
749 assert(VA.isRegLoc() && "Can only return in registers!");
750 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
751 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000752 Flag = Chain.getValue(1);
753 }
754 } else {
755 // We need to handle a destination of ST0 specially, because it isn't really
756 // a register.
757 SDOperand Value = Op.getOperand(1);
758
759 // If this is an FP return with ScalarSSE, we need to move the value from
760 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000761 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
762 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000763 SDOperand MemLoc;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000764
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000765 // If this is a load into a scalarsse value, don't store the loaded value
766 // back to the stack, only to reload it: just replace the scalar-sse load.
767 if (ISD::isNON_EXTLoad(Value.Val) &&
768 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
769 Chain = Value.getOperand(0);
770 MemLoc = Value.getOperand(1);
771 } else {
772 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000773 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000774 MachineFunction &MF = DAG.getMachineFunction();
775 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
776 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
777 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
778 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000779 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000780 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000781 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
782 Chain = Value.getValue(1);
783 }
784
785 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
786 SDOperand Ops[] = { Chain, Value };
787 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
788 Flag = Chain.getValue(1);
789 }
790
791 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
792 if (Flag.Val)
793 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
794 else
795 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
796}
797
798
Chris Lattner3085e152007-02-25 08:59:22 +0000799/// LowerCallResult - Lower the result values of an ISD::CALL into the
800/// appropriate copies out of appropriate physical registers. This assumes that
801/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
802/// being lowered. The returns a SDNode with the same number of values as the
803/// ISD::CALL.
804SDNode *X86TargetLowering::
805LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
806 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000807
808 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000809 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000810 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
811 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000812 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
813
Chris Lattnere32bbf62007-02-28 07:09:55 +0000814 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000815
816 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000817 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
818 for (unsigned i = 0; i != RVLocs.size(); ++i) {
819 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
820 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000821 InFlag = Chain.getValue(2);
822 ResultVals.push_back(Chain.getValue(0));
823 }
824 } else {
825 // Copies from the FP stack are special, as ST0 isn't a valid register
826 // before the fp stackifier runs.
827
828 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000829 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000830 SDOperand GROps[] = { Chain, InFlag };
831 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
832 Chain = RetVal.getValue(1);
833 InFlag = RetVal.getValue(2);
834
835 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
836 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000837 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
838 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner112dedc2007-12-29 06:41:28 +0000839 SDOperand StoreLoc;
840 const Value *SrcVal = 0;
841 int SrcValOffset = 0;
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000842 MVT::ValueType RetStoreVT = RVLocs[0].getValVT();
Chris Lattner112dedc2007-12-29 06:41:28 +0000843
844 // Determine where to store the value. If the call result is directly
845 // used by a store, see if we can store directly into the location. In
846 // this case, we'll end up producing a fst + movss[load] + movss[store] to
847 // the same location, and the two movss's will be nuked as dead. This
848 // optimizes common things like "*D = atof(..)" to not need an
849 // intermediate stack slot.
850 if (SDOperand(TheCall, 0).hasOneUse() &&
851 SDOperand(TheCall, 1).hasOneUse()) {
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000852 // In addition to direct uses, we also support a FP_ROUND that uses the
853 // value, if it is directly stored somewhere.
854 SDNode *User = *TheCall->use_begin();
855 if (User->getOpcode() == ISD::FP_ROUND && User->hasOneUse())
856 User = *User->use_begin();
857
Chris Lattner112dedc2007-12-29 06:41:28 +0000858 // Ok, we have one use of the value and one use of the chain. See if
859 // they are the same node: a store.
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000860 if (StoreSDNode *N = dyn_cast<StoreSDNode>(User)) {
861 // Verify that the value being stored is either the call or a
862 // truncation of the call.
863 SDNode *StoreVal = N->getValue().Val;
864 if (StoreVal == TheCall)
865 ; // ok.
866 else if (StoreVal->getOpcode() == ISD::FP_ROUND &&
867 StoreVal->hasOneUse() &&
868 StoreVal->getOperand(0).Val == TheCall)
869 ; // ok.
870 else
871 N = 0; // not ok.
872
873 if (N && N->getChain().Val == TheCall &&
Chris Lattner112dedc2007-12-29 06:41:28 +0000874 !N->isVolatile() && !N->isTruncatingStore() &&
875 N->getAddressingMode() == ISD::UNINDEXED) {
876 StoreLoc = N->getBasePtr();
877 SrcVal = N->getSrcValue();
878 SrcValOffset = N->getSrcValueOffset();
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000879 RetStoreVT = N->getValue().getValueType();
Chris Lattner112dedc2007-12-29 06:41:28 +0000880 }
881 }
882 }
883
884 // If we weren't able to optimize the result, just create a temporary
885 // stack slot.
886 if (StoreLoc.Val == 0) {
887 MachineFunction &MF = DAG.getMachineFunction();
888 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
889 StoreLoc = DAG.getFrameIndex(SSFI, getPointerTy());
890 }
891
Chris Lattner3085e152007-02-25 08:59:22 +0000892 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
893 // shouldn't be necessary except that RFP cannot be live across
Chris Lattner112dedc2007-12-29 06:41:28 +0000894 // multiple blocks (which could happen if a select gets lowered into
895 // multiple blocks and scheduled in between them). When stackifier is
896 // fixed, they can be uncoupled.
Chris Lattner3085e152007-02-25 08:59:22 +0000897 SDOperand Ops[] = {
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000898 Chain, RetVal, StoreLoc, DAG.getValueType(RetStoreVT), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000899 };
900 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000901 RetVal = DAG.getLoad(RetStoreVT, Chain,
Chris Lattner112dedc2007-12-29 06:41:28 +0000902 StoreLoc, SrcVal, SrcValOffset);
Chris Lattner3085e152007-02-25 08:59:22 +0000903 Chain = RetVal.getValue(1);
Chris Lattnerd60eedc2007-12-29 06:57:38 +0000904
905 // If we optimized a truncate, then extend the result back to its desired
906 // type.
907 if (RVLocs[0].getValVT() != RetStoreVT)
908 RetVal = DAG.getNode(ISD::FP_EXTEND, RVLocs[0].getValVT(), RetVal);
Chris Lattner3085e152007-02-25 08:59:22 +0000909 }
Chris Lattner3085e152007-02-25 08:59:22 +0000910 ResultVals.push_back(RetVal);
911 }
912
913 // Merge everything together with a MERGE_VALUES node.
914 ResultVals.push_back(Chain);
915 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
916 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000917}
918
919
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000920//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000921// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000922//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000923// StdCall calling convention seems to be standard for many Windows' API
924// routines and around. It differs from C calling convention just a little:
925// callee should clean up the stack, not caller. Symbols should be also
926// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000927// For info on fast calling convention see Fast Calling Convention (tail call)
928// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000929
Evan Cheng85e38002006-04-27 05:35:28 +0000930/// AddLiveIn - This helper function adds the specified physical register to the
931/// MachineFunction as a live in value. It also creates a corresponding virtual
932/// register for it.
933static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000934 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000935 assert(RC->contains(PReg) && "Not the correct regclass!");
936 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
937 MF.addLiveIn(PReg, VReg);
938 return VReg;
939}
940
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000941// align stack arguments according to platform alignment needed for tail calls
942unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
943
Rafael Espindola7effac52007-09-14 15:48:13 +0000944SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
945 const CCValAssign &VA,
946 MachineFrameInfo *MFI,
947 SDOperand Root, unsigned i) {
948 // Create the nodes corresponding to a load from this parameter slot.
949 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
950 VA.getLocMemOffset());
951 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
952
953 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
954
955 if (Flags & ISD::ParamFlags::ByVal)
956 return FIN;
957 else
958 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
959}
960
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000961SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
962 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000963 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000964 MachineFunction &MF = DAG.getMachineFunction();
965 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000966 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000967 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000968 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner638402b2007-02-28 07:00:42 +0000969 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000970 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000971 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +0000972 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000973 // Check for possible tail call calling convention.
974 if (CC == CallingConv::Fast && PerformTailCallOpt)
975 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
976 else
977 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
978
Chris Lattnerf39f7712007-02-28 05:46:49 +0000979 SmallVector<SDOperand, 8> ArgValues;
980 unsigned LastVal = ~0U;
981 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
982 CCValAssign &VA = ArgLocs[i];
983 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
984 // places.
985 assert(VA.getValNo() != LastVal &&
986 "Don't support value assigned to multiple locs yet");
987 LastVal = VA.getValNo();
988
989 if (VA.isRegLoc()) {
990 MVT::ValueType RegVT = VA.getLocVT();
991 TargetRegisterClass *RC;
992 if (RegVT == MVT::i32)
993 RC = X86::GR32RegisterClass;
994 else {
995 assert(MVT::isVector(RegVT));
996 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000997 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000998
Chris Lattner82932a52007-03-02 05:12:29 +0000999 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1000 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001001
1002 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1003 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1004 // right size.
1005 if (VA.getLocInfo() == CCValAssign::SExt)
1006 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1007 DAG.getValueType(VA.getValVT()));
1008 else if (VA.getLocInfo() == CCValAssign::ZExt)
1009 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1010 DAG.getValueType(VA.getValVT()));
1011
1012 if (VA.getLocInfo() != CCValAssign::Full)
1013 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1014
1015 ArgValues.push_back(ArgValue);
1016 } else {
1017 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001018 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001019 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001020 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001021
1022 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001023 // align stack specially for tail calls
1024 if (CC==CallingConv::Fast)
1025 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00001026
Evan Cheng25caf632006-05-23 21:06:34 +00001027 ArgValues.push_back(Root);
1028
Evan Cheng1bc78042006-04-26 01:20:17 +00001029 // If the function takes variable number of arguments, make a frame index for
1030 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +00001031 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001032 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001033
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001034 // Tail call calling convention (CallingConv::Fast) does not support varargs.
1035 assert( !(isVarArg && CC == CallingConv::Fast) &&
1036 "CallingConv::Fast does not support varargs.");
1037
1038 if (isStdCall && !isVarArg &&
1039 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerf39f7712007-02-28 05:46:49 +00001040 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001041 BytesCallerReserves = 0;
1042 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001043 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001044
1045 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001046 if (NumArgs &&
1047 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001048 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001049 BytesToPopOnReturn = 4;
1050
1051 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001052 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001053
Evan Cheng25ab6902006-09-08 06:48:29 +00001054 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +00001055
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001056 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1057 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001058
Evan Cheng25caf632006-05-23 21:06:34 +00001059 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001060 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001061 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001062}
1063
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001064SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001065 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001066 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001067 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001068 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001069 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001070
Chris Lattner638402b2007-02-28 07:00:42 +00001071 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001072 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001073 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001074 if(CC==CallingConv::Fast && PerformTailCallOpt)
1075 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1076 else
1077 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001078
Chris Lattner423c5f42007-02-28 05:31:48 +00001079 // Get a count of how many bytes are to be pushed on the stack.
1080 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001081 if (CC==CallingConv::Fast)
1082 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001083
Evan Cheng32fe1032006-05-25 00:59:30 +00001084 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001085
Chris Lattner5a88b832007-02-25 07:10:00 +00001086 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1087 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001088
Chris Lattner423c5f42007-02-28 05:31:48 +00001089 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001090
1091 // Walk the register/memloc assignments, inserting copies/loads.
1092 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1093 CCValAssign &VA = ArgLocs[i];
1094 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001095
Chris Lattner423c5f42007-02-28 05:31:48 +00001096 // Promote the value if needed.
1097 switch (VA.getLocInfo()) {
1098 default: assert(0 && "Unknown loc info!");
1099 case CCValAssign::Full: break;
1100 case CCValAssign::SExt:
1101 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1102 break;
1103 case CCValAssign::ZExt:
1104 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1105 break;
1106 case CCValAssign::AExt:
1107 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1108 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001109 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001110
1111 if (VA.isRegLoc()) {
1112 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1113 } else {
1114 assert(VA.isMemLoc());
1115 if (StackPtr.Val == 0)
1116 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001117
1118 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1119 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001120 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121 }
1122
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001123 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001124 bool isSRet = NumOps &&
1125 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001126 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001127
Evan Cheng32fe1032006-05-25 00:59:30 +00001128 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001129 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1130 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001131
Evan Cheng347d5f72006-04-28 21:29:37 +00001132 // Build a sequence of copy-to-reg nodes chained together with token chain
1133 // and flag operands which copy the outgoing args into registers.
1134 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001135 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1136 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1137 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001138 InFlag = Chain.getValue(1);
1139 }
1140
Evan Chengf4684712007-02-21 21:18:14 +00001141 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1142 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +00001143 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1144 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001145 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1146 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1147 InFlag);
1148 InFlag = Chain.getValue(1);
1149 }
1150
Evan Cheng32fe1032006-05-25 00:59:30 +00001151 // If the callee is a GlobalAddress node (quite common, every direct call is)
1152 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001153 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001154 // We should use extra load for direct calls to dllimported functions in
1155 // non-JIT mode.
1156 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1157 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001158 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1159 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001160 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1161
Chris Lattnerd96d0722007-02-25 06:40:16 +00001162 // Returns a chain & a flag for retval copy to use.
1163 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001164 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001165 Ops.push_back(Chain);
1166 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001167
1168 // Add argument registers to the end of the list so that they are known live
1169 // into the call.
1170 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001171 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001172 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +00001173
1174 // Add an implicit use GOT pointer in EBX.
1175 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1176 Subtarget->isPICStyleGOT())
1177 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001178
Evan Cheng347d5f72006-04-28 21:29:37 +00001179 if (InFlag.Val)
1180 Ops.push_back(InFlag);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001181
1182 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001183 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001184
Chris Lattner2d297092006-05-23 18:50:38 +00001185 // Create the CALLSEQ_END node.
1186 unsigned NumBytesForCalleeToPush = 0;
1187
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001188 if (CC == CallingConv::X86_StdCall ||
1189 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner09c75a42007-02-25 09:06:15 +00001190 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001191 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +00001192 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001193 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001194 assert(!(isVarArg && CC==CallingConv::Fast) &&
1195 "CallingConv::Fast does not support varargs.");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001196 } else {
1197 // If this is is a call to a struct-return function, the callee
1198 // pops the hidden struct pointer, so we have to push it back.
1199 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001200 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001201 }
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001202
1203 Chain = DAG.getCALLSEQ_END(Chain,
1204 DAG.getConstant(NumBytes, getPointerTy()),
1205 DAG.getConstant(NumBytesForCalleeToPush,
1206 getPointerTy()),
1207 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001208 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001209
Chris Lattner3085e152007-02-25 08:59:22 +00001210 // Handle result values, copying them out of physregs into vregs that we
1211 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001212 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001213}
1214
Evan Cheng25ab6902006-09-08 06:48:29 +00001215
1216//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001217// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001218//===----------------------------------------------------------------------===//
1219//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001220// The X86 'fastcall' calling convention passes up to two integer arguments in
1221// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1222// and requires that the callee pop its arguments off the stack (allowing proper
1223// tail calls), and has the same return value conventions as C calling convs.
1224//
1225// This calling convention always arranges for the callee pop value to be 8n+4
1226// bytes, which is needed for tail recursion elimination and stack alignment
1227// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001228SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001229X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001230 MachineFunction &MF = DAG.getMachineFunction();
1231 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001232 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001233 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001234
Chris Lattner638402b2007-02-28 07:00:42 +00001235 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001236 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001237 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1238 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001239 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001240
1241 SmallVector<SDOperand, 8> ArgValues;
1242 unsigned LastVal = ~0U;
1243 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1244 CCValAssign &VA = ArgLocs[i];
1245 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1246 // places.
1247 assert(VA.getValNo() != LastVal &&
1248 "Don't support value assigned to multiple locs yet");
1249 LastVal = VA.getValNo();
1250
1251 if (VA.isRegLoc()) {
1252 MVT::ValueType RegVT = VA.getLocVT();
1253 TargetRegisterClass *RC;
1254 if (RegVT == MVT::i32)
1255 RC = X86::GR32RegisterClass;
1256 else {
1257 assert(MVT::isVector(RegVT));
1258 RC = X86::VR128RegisterClass;
1259 }
1260
Chris Lattner82932a52007-03-02 05:12:29 +00001261 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1262 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001263
1264 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1265 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1266 // right size.
1267 if (VA.getLocInfo() == CCValAssign::SExt)
1268 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1269 DAG.getValueType(VA.getValVT()));
1270 else if (VA.getLocInfo() == CCValAssign::ZExt)
1271 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1272 DAG.getValueType(VA.getValVT()));
1273
1274 if (VA.getLocInfo() != CCValAssign::Full)
1275 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1276
1277 ArgValues.push_back(ArgValue);
1278 } else {
1279 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001280 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001281 }
1282 }
1283
Evan Cheng25caf632006-05-23 21:06:34 +00001284 ArgValues.push_back(Root);
1285
Chris Lattnerfc664c12007-02-28 06:21:19 +00001286 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001287
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001288 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001289 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001290 // arguments and the arguments after the retaddr has been pushed are
1291 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001292 if ((StackSize & 7) == 0)
1293 StackSize += 4;
1294 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001295
1296 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001297 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001298 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001299 BytesCallerReserves = 0;
1300
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001301 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1302 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001303
Evan Cheng25caf632006-05-23 21:06:34 +00001304 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001305 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001306 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001307}
1308
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001309SDOperand
1310X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1311 const SDOperand &StackPtr,
1312 const CCValAssign &VA,
1313 SDOperand Chain,
1314 SDOperand Arg) {
1315 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1316 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1317 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1318 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1319 if (Flags & ISD::ParamFlags::ByVal) {
1320 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1321 ISD::ParamFlags::ByValAlignOffs);
1322
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001323 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1324 ISD::ParamFlags::ByValSizeOffs;
1325
1326 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1327 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Chris Lattner27a6c732007-11-24 07:07:01 +00001328 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001329
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001330 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1331 AlwaysInline);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001332 } else {
1333 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1334 }
1335}
1336
Chris Lattnere87e1152006-09-26 03:57:53 +00001337SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001338 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001339 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001340 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001341 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001342 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001343
Chris Lattner638402b2007-02-28 07:00:42 +00001344 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001345 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001346 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001347 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001348
1349 // Get a count of how many bytes are to be pushed on the stack.
1350 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001351
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001352 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001353 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001354 // arguments and the arguments after the retaddr has been pushed are
1355 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001356 if ((NumBytes & 7) == 0)
1357 NumBytes += 4;
1358 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001359
Chris Lattner94dd2922006-02-13 09:00:43 +00001360 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001361
Chris Lattner5a88b832007-02-25 07:10:00 +00001362 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1363 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001364
1365 SDOperand StackPtr;
1366
1367 // Walk the register/memloc assignments, inserting copies/loads.
1368 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1369 CCValAssign &VA = ArgLocs[i];
1370 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1371
1372 // Promote the value if needed.
1373 switch (VA.getLocInfo()) {
1374 default: assert(0 && "Unknown loc info!");
1375 case CCValAssign::Full: break;
1376 case CCValAssign::SExt:
1377 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001378 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001379 case CCValAssign::ZExt:
1380 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1381 break;
1382 case CCValAssign::AExt:
1383 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1384 break;
1385 }
1386
1387 if (VA.isRegLoc()) {
1388 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1389 } else {
1390 assert(VA.isMemLoc());
1391 if (StackPtr.Val == 0)
1392 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001393
1394 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1395 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001396 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001397 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001398
Evan Cheng32fe1032006-05-25 00:59:30 +00001399 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001400 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1401 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001402
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001403 // Build a sequence of copy-to-reg nodes chained together with token chain
1404 // and flag operands which copy the outgoing args into registers.
1405 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1407 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1408 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001409 InFlag = Chain.getValue(1);
1410 }
1411
Evan Cheng32fe1032006-05-25 00:59:30 +00001412 // If the callee is a GlobalAddress node (quite common, every direct call is)
1413 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001414 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001415 // We should use extra load for direct calls to dllimported functions in
1416 // non-JIT mode.
1417 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1418 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001419 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1420 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001421 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1422
Evan Chengf4684712007-02-21 21:18:14 +00001423 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1424 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001425 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1426 Subtarget->isPICStyleGOT()) {
1427 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1428 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1429 InFlag);
1430 InFlag = Chain.getValue(1);
1431 }
1432
Chris Lattnerd96d0722007-02-25 06:40:16 +00001433 // Returns a chain & a flag for retval copy to use.
1434 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001435 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001436 Ops.push_back(Chain);
1437 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001438
1439 // Add argument registers to the end of the list so that they are known live
1440 // into the call.
1441 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001442 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001443 RegsToPass[i].second.getValueType()));
1444
Evan Chengf4684712007-02-21 21:18:14 +00001445 // Add an implicit use GOT pointer in EBX.
1446 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1447 Subtarget->isPICStyleGOT())
1448 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1449
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001450 if (InFlag.Val)
1451 Ops.push_back(InFlag);
1452
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001453 assert(isTailCall==false && "no tail call here");
1454 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001455 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001456 InFlag = Chain.getValue(1);
1457
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001458 // Returns a flag for retval copy to use.
1459 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001460 Ops.clear();
1461 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001462 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1463 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001464 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001465 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001466 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001467
Chris Lattner339b4392007-02-25 09:10:05 +00001468 // Handle result values, copying them out of physregs into vregs that we
1469 // return.
1470 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001471}
1472
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001473//===----------------------------------------------------------------------===//
1474// Fast Calling Convention (tail call) implementation
1475//===----------------------------------------------------------------------===//
1476
1477// Like std call, callee cleans arguments, convention except that ECX is
1478// reserved for storing the tail called function address. Only 2 registers are
1479// free for argument passing (inreg). Tail call optimization is performed
1480// provided:
1481// * tailcallopt is enabled
1482// * caller/callee are fastcc
1483// * elf/pic is disabled OR
1484// * elf/pic enabled + callee is in module + callee has
1485// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001486// To keep the stack aligned according to platform abi the function
1487// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1488// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001489// If a tail called function callee has more arguments than the caller the
1490// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001491// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001492// original REtADDR, but before the saved framepointer or the spilled registers
1493// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1494// stack layout:
1495// arg1
1496// arg2
1497// RETADDR
1498// [ new RETADDR
1499// move area ]
1500// (possible EBP)
1501// ESI
1502// EDI
1503// local1 ..
1504
1505/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1506/// for a 16 byte align requirement.
1507unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1508 SelectionDAG& DAG) {
1509 if (PerformTailCallOpt) {
1510 MachineFunction &MF = DAG.getMachineFunction();
1511 const TargetMachine &TM = MF.getTarget();
1512 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1513 unsigned StackAlignment = TFI.getStackAlignment();
1514 uint64_t AlignMask = StackAlignment - 1;
1515 int64_t Offset = StackSize;
1516 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1517 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1518 // Number smaller than 12 so just add the difference.
1519 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1520 } else {
1521 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1522 Offset = ((~AlignMask) & Offset) + StackAlignment +
1523 (StackAlignment-SlotSize);
1524 }
1525 StackSize = Offset;
1526 }
1527 return StackSize;
1528}
1529
1530/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001531/// following the call is a return. A function is eligible if caller/callee
1532/// calling conventions match, currently only fastcc supports tail calls, and
1533/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001534bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1535 SDOperand Ret,
1536 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001537 if (!PerformTailCallOpt)
1538 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001539
1540 // Check whether CALL node immediatly preceeds the RET node and whether the
1541 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001542 unsigned NumOps = Ret.getNumOperands();
1543 if ((NumOps == 1 &&
1544 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1545 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001546 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001547 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1548 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001549 MachineFunction &MF = DAG.getMachineFunction();
1550 unsigned CallerCC = MF.getFunction()->getCallingConv();
1551 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1552 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1553 SDOperand Callee = Call.getOperand(4);
1554 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001555 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1556 !Subtarget->isPICStyleGOT())
1557 return true;
1558
1559 // Can only do local tail calls with PIC.
1560 GlobalValue * GV = 0;
1561 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1562 if(G != 0 &&
1563 (GV = G->getGlobal()) &&
1564 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1565 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001566 }
1567 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001568
1569 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001570}
1571
1572SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1573 SelectionDAG &DAG,
1574 unsigned CC) {
1575 SDOperand Chain = Op.getOperand(0);
1576 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1577 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1578 SDOperand Callee = Op.getOperand(4);
1579 bool is64Bit = Subtarget->is64Bit();
1580
1581 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1582
1583 // Analyze operands of the call, assigning locations to each operand.
1584 SmallVector<CCValAssign, 16> ArgLocs;
1585 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1586 if (is64Bit)
1587 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1588 else
1589 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1590
1591
1592 // Lower arguments at fp - stackoffset + fpdiff.
1593 MachineFunction &MF = DAG.getMachineFunction();
1594
1595 unsigned NumBytesToBePushed =
1596 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1597
1598 unsigned NumBytesCallerPushed =
1599 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1600 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1601
1602 // Set the delta of movement of the returnaddr stackslot.
1603 // But only set if delta is greater than previous delta.
1604 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1605 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1606
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001607 Chain = DAG.
1608 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1609
1610 // Adjust the Return address stack slot.
1611 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001612 if (FPDiff) {
1613 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001614 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1615 // Load the "old" Return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001616 RetAddrFrIdx =
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001617 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1618 // Calculate the new stack slot for the return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001619 int SlotSize = is64Bit ? 8 : 4;
1620 int NewReturnAddrFI =
1621 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001622 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1623 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001624 }
1625
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001626 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1627 SmallVector<SDOperand, 8> MemOpChains;
1628 SmallVector<SDOperand, 8> MemOpChains2;
1629 SDOperand FramePtr, StackPtr;
1630 SDOperand PtrOff;
1631 SDOperand FIN;
1632 int FI = 0;
1633
1634 // Walk the register/memloc assignments, inserting copies/loads. Lower
1635 // arguments first to the stack slot where they would normally - in case of a
1636 // normal function call - be.
1637 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1638 CCValAssign &VA = ArgLocs[i];
1639 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1640
1641 // Promote the value if needed.
1642 switch (VA.getLocInfo()) {
1643 default: assert(0 && "Unknown loc info!");
1644 case CCValAssign::Full: break;
1645 case CCValAssign::SExt:
1646 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1647 break;
1648 case CCValAssign::ZExt:
1649 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1650 break;
1651 case CCValAssign::AExt:
1652 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1653 break;
1654 }
1655
1656 if (VA.isRegLoc()) {
1657 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1658 } else {
1659 assert(VA.isMemLoc());
1660 if (StackPtr.Val == 0)
1661 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1662
1663 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1664 Arg));
1665 }
1666 }
1667
1668 if (!MemOpChains.empty())
1669 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1670 &MemOpChains[0], MemOpChains.size());
1671
1672 // Build a sequence of copy-to-reg nodes chained together with token chain
1673 // and flag operands which copy the outgoing args into registers.
1674 SDOperand InFlag;
1675 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1676 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1677 InFlag);
1678 InFlag = Chain.getValue(1);
1679 }
1680 InFlag = SDOperand();
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001681
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001682 // Copy from stack slots to stack slot of a tail called function. This needs
1683 // to be done because if we would lower the arguments directly to their real
1684 // stack slot we might end up overwriting each other.
1685 // TODO: To make this more efficient (sometimes saving a store/load) we could
1686 // analyse the arguments and emit this store/load/store sequence only for
1687 // arguments which would be overwritten otherwise.
1688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1689 CCValAssign &VA = ArgLocs[i];
1690 if (!VA.isRegLoc()) {
1691 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1692 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1693
1694 // Get source stack slot.
1695 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1696 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1697 // Create frame index.
1698 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1699 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1700 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1701 FIN = DAG.getFrameIndex(FI, MVT::i32);
1702 if (Flags & ISD::ParamFlags::ByVal) {
1703 // Copy relative to framepointer.
1704 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1705 ISD::ParamFlags::ByValAlignOffs);
1706
1707 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1708 ISD::ParamFlags::ByValSizeOffs;
1709
1710 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1711 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer38ada862007-11-10 10:48:01 +00001712 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
1713
1714 MemOpChains2.push_back(DAG.getMemcpy(Chain, FIN, PtrOff, SizeNode,
1715 AlignNode,AlwaysInline));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001716 } else {
1717 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1718 // Store relative to framepointer.
1719 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1720 }
1721 }
1722 }
1723
1724 if (!MemOpChains2.empty())
1725 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1726 &MemOpChains2[0], MemOpChains.size());
1727
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001728 // Store the return address to the appropriate stack slot.
1729 if (FPDiff)
1730 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1731
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001732 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1733 // GOT pointer.
1734 // Does not work with tail call since ebx is not restored correctly by
1735 // tailcaller. TODO: at least for x86 - verify for x86-64
1736
1737 // If the callee is a GlobalAddress node (quite common, every direct call is)
1738 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1739 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1740 // We should use extra load for direct calls to dllimported functions in
1741 // non-JIT mode.
1742 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1743 getTargetMachine(), true))
1744 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1745 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1746 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1747 else {
1748 assert(Callee.getOpcode() == ISD::LOAD &&
1749 "Function destination must be loaded into virtual register");
1750 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1751
1752 Chain = DAG.getCopyToReg(Chain,
1753 DAG.getRegister(Opc, getPointerTy()) ,
1754 Callee,InFlag);
1755 Callee = DAG.getRegister(Opc, getPointerTy());
1756 // Add register as live out.
1757 DAG.getMachineFunction().addLiveOut(Opc);
1758 }
1759
1760 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1761 SmallVector<SDOperand, 8> Ops;
1762
1763 Ops.push_back(Chain);
1764 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1765 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1766 if (InFlag.Val)
1767 Ops.push_back(InFlag);
1768 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1769 InFlag = Chain.getValue(1);
1770
1771 // Returns a chain & a flag for retval copy to use.
1772 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1773 Ops.clear();
1774 Ops.push_back(Chain);
1775 Ops.push_back(Callee);
1776 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1777 // Add argument registers to the end of the list so that they are known live
1778 // into the call.
1779 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1780 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1781 RegsToPass[i].second.getValueType()));
1782 if (InFlag.Val)
1783 Ops.push_back(InFlag);
1784 assert(InFlag.Val &&
1785 "Flag must be set. Depend on flag being set in LowerRET");
1786 Chain = DAG.getNode(X86ISD::TAILCALL,
1787 Op.Val->getVTList(), &Ops[0], Ops.size());
1788
1789 return SDOperand(Chain.Val, Op.ResNo);
1790}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001791
1792//===----------------------------------------------------------------------===//
1793// X86-64 C Calling Convention implementation
1794//===----------------------------------------------------------------------===//
1795
1796SDOperand
1797X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001798 MachineFunction &MF = DAG.getMachineFunction();
1799 MachineFrameInfo *MFI = MF.getFrameInfo();
1800 SDOperand Root = Op.getOperand(0);
1801 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001802 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001803
1804 static const unsigned GPR64ArgRegs[] = {
1805 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1806 };
1807 static const unsigned XMMArgRegs[] = {
1808 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1809 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1810 };
1811
Chris Lattner638402b2007-02-28 07:00:42 +00001812
1813 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001814 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001815 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +00001816 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001817 if (CC == CallingConv::Fast && PerformTailCallOpt)
1818 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1819 else
1820 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001821
1822 SmallVector<SDOperand, 8> ArgValues;
1823 unsigned LastVal = ~0U;
1824 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1825 CCValAssign &VA = ArgLocs[i];
1826 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1827 // places.
1828 assert(VA.getValNo() != LastVal &&
1829 "Don't support value assigned to multiple locs yet");
1830 LastVal = VA.getValNo();
1831
1832 if (VA.isRegLoc()) {
1833 MVT::ValueType RegVT = VA.getLocVT();
1834 TargetRegisterClass *RC;
1835 if (RegVT == MVT::i32)
1836 RC = X86::GR32RegisterClass;
1837 else if (RegVT == MVT::i64)
1838 RC = X86::GR64RegisterClass;
1839 else if (RegVT == MVT::f32)
1840 RC = X86::FR32RegisterClass;
1841 else if (RegVT == MVT::f64)
1842 RC = X86::FR64RegisterClass;
1843 else {
1844 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001845 if (MVT::getSizeInBits(RegVT) == 64) {
1846 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1847 RegVT = MVT::i64;
1848 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001849 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001850 }
Chris Lattner82932a52007-03-02 05:12:29 +00001851
1852 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1853 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001854
1855 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1856 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1857 // right size.
1858 if (VA.getLocInfo() == CCValAssign::SExt)
1859 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1860 DAG.getValueType(VA.getValVT()));
1861 else if (VA.getLocInfo() == CCValAssign::ZExt)
1862 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1863 DAG.getValueType(VA.getValVT()));
1864
1865 if (VA.getLocInfo() != CCValAssign::Full)
1866 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1867
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001868 // Handle MMX values passed in GPRs.
1869 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1870 MVT::getSizeInBits(RegVT) == 64)
1871 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1872
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001873 ArgValues.push_back(ArgValue);
1874 } else {
1875 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001876 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001877 }
1878 }
1879
1880 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001881 if (CC==CallingConv::Fast)
1882 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001883
1884 // If the function takes variable number of arguments, make a frame index for
1885 // the start of the first vararg value... for expansion of llvm.va_start.
1886 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001887 assert(CC!=CallingConv::Fast
1888 && "Var arg not supported with calling convention fastcc");
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001889 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1890 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1891
1892 // For X86-64, if there are vararg parameters that are passed via
1893 // registers, then we must store them to their spots on the stack so they
1894 // may be loaded by deferencing the result of va_next.
1895 VarArgsGPOffset = NumIntRegs * 8;
1896 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1897 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1898 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1899
1900 // Store the integer parameter registers.
1901 SmallVector<SDOperand, 8> MemOps;
1902 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1903 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1904 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1905 for (; NumIntRegs != 6; ++NumIntRegs) {
1906 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1907 X86::GR64RegisterClass);
1908 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1909 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1910 MemOps.push_back(Store);
1911 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1912 DAG.getConstant(8, getPointerTy()));
1913 }
1914
1915 // Now store the XMM (fp + vector) parameter registers.
1916 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1917 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1918 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1919 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1920 X86::VR128RegisterClass);
1921 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1922 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1923 MemOps.push_back(Store);
1924 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1925 DAG.getConstant(16, getPointerTy()));
1926 }
1927 if (!MemOps.empty())
1928 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1929 &MemOps[0], MemOps.size());
1930 }
1931
1932 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001933 // Tail call convention (fastcc) needs callee pop.
Evan Cheng36446012007-10-14 10:09:39 +00001934 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001935 BytesToPopOnReturn = StackSize; // Callee pops everything.
1936 BytesCallerReserves = 0;
1937 } else {
1938 BytesToPopOnReturn = 0; // Callee pops nothing.
1939 BytesCallerReserves = StackSize;
1940 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001941 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1942 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1943
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001944 // Return the new list of results.
1945 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1946 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1947}
1948
1949SDOperand
1950X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1951 unsigned CC) {
1952 SDOperand Chain = Op.getOperand(0);
1953 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001954 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001955
1956 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001957 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001958 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng36446012007-10-14 10:09:39 +00001959 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001960 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1961 else
1962 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001963
1964 // Get a count of how many bytes are to be pushed on the stack.
1965 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001966 if (CC == CallingConv::Fast)
1967 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1968
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001969 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1970
1971 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1972 SmallVector<SDOperand, 8> MemOpChains;
1973
1974 SDOperand StackPtr;
1975
1976 // Walk the register/memloc assignments, inserting copies/loads.
1977 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1978 CCValAssign &VA = ArgLocs[i];
1979 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1980
1981 // Promote the value if needed.
1982 switch (VA.getLocInfo()) {
1983 default: assert(0 && "Unknown loc info!");
1984 case CCValAssign::Full: break;
1985 case CCValAssign::SExt:
1986 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1987 break;
1988 case CCValAssign::ZExt:
1989 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1990 break;
1991 case CCValAssign::AExt:
1992 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1993 break;
1994 }
1995
1996 if (VA.isRegLoc()) {
1997 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1998 } else {
1999 assert(VA.isMemLoc());
2000 if (StackPtr.Val == 0)
2001 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00002002
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00002003 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
2004 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002005 }
2006 }
2007
2008 if (!MemOpChains.empty())
2009 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2010 &MemOpChains[0], MemOpChains.size());
2011
2012 // Build a sequence of copy-to-reg nodes chained together with token chain
2013 // and flag operands which copy the outgoing args into registers.
2014 SDOperand InFlag;
2015 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2016 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
2017 InFlag);
2018 InFlag = Chain.getValue(1);
2019 }
2020
2021 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002022 assert ( CallingConv::Fast != CC &&
2023 "Var args not supported with calling convention fastcc");
2024
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002025 // From AMD64 ABI document:
2026 // For calls that may call functions that use varargs or stdargs
2027 // (prototype-less calls or calls to functions containing ellipsis (...) in
2028 // the declaration) %al is used as hidden argument to specify the number
2029 // of SSE registers used. The contents of %al do not need to match exactly
2030 // the number of registers, but must be an ubound on the number of SSE
2031 // registers used and is in the range 0 - 8 inclusive.
2032
2033 // Count the number of XMM registers allocated.
2034 static const unsigned XMMArgRegs[] = {
2035 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2036 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2037 };
2038 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2039
2040 Chain = DAG.getCopyToReg(Chain, X86::AL,
2041 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
2042 InFlag = Chain.getValue(1);
2043 }
2044
2045 // If the callee is a GlobalAddress node (quite common, every direct call is)
2046 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
2047 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2048 // We should use extra load for direct calls to dllimported functions in
2049 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00002050 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00002051 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
2052 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002053 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
2054 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00002055 if (getTargetMachine().getCodeModel() != CodeModel::Large)
2056 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002057
2058 // Returns a chain & a flag for retval copy to use.
2059 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2060 SmallVector<SDOperand, 8> Ops;
2061 Ops.push_back(Chain);
2062 Ops.push_back(Callee);
2063
2064 // Add argument registers to the end of the list so that they are known live
2065 // into the call.
2066 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2067 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2068 RegsToPass[i].second.getValueType()));
2069
2070 if (InFlag.Val)
2071 Ops.push_back(InFlag);
2072
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002073 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002074 NodeTys, &Ops[0], Ops.size());
2075 InFlag = Chain.getValue(1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002076 int NumBytesForCalleeToPush = 0;
Evan Cheng36446012007-10-14 10:09:39 +00002077 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002078 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002079 } else {
2080 NumBytesForCalleeToPush = 0; // Callee pops nothing.
2081 }
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002082 // Returns a flag for retval copy to use.
2083 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2084 Ops.clear();
2085 Ops.push_back(Chain);
2086 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002087 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002088 Ops.push_back(InFlag);
2089 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2090 InFlag = Chain.getValue(1);
2091
2092 // Handle result values, copying them out of physregs into vregs that we
2093 // return.
2094 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2095}
2096
2097
2098//===----------------------------------------------------------------------===//
2099// Other Lowering Hooks
2100//===----------------------------------------------------------------------===//
2101
2102
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002103SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002104 MachineFunction &MF = DAG.getMachineFunction();
2105 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2106 int ReturnAddrIndex = FuncInfo->getRAIndex();
2107
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002108 if (ReturnAddrIndex == 0) {
2109 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00002110 if (Subtarget->is64Bit())
2111 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2112 else
2113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002114
2115 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002116 }
2117
Evan Cheng25ab6902006-09-08 06:48:29 +00002118 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002119}
2120
2121
2122
Evan Cheng6dfa9992006-01-30 23:41:35 +00002123/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2124/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00002125/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2126/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00002127static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00002128 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2129 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00002130 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002131 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002132 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2133 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2134 // X > -1 -> X == 0, jump !sign.
2135 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00002136 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002137 return true;
2138 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2139 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00002140 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002141 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00002142 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2143 // X < 1 -> X <= 0
2144 RHS = DAG.getConstant(0, RHS.getValueType());
2145 X86CC = X86::COND_LE;
2146 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002147 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002148 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002149
Evan Chengd9558e02006-01-06 00:43:03 +00002150 switch (SetCCOpcode) {
2151 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002152 case ISD::SETEQ: X86CC = X86::COND_E; break;
2153 case ISD::SETGT: X86CC = X86::COND_G; break;
2154 case ISD::SETGE: X86CC = X86::COND_GE; break;
2155 case ISD::SETLT: X86CC = X86::COND_L; break;
2156 case ISD::SETLE: X86CC = X86::COND_LE; break;
2157 case ISD::SETNE: X86CC = X86::COND_NE; break;
2158 case ISD::SETULT: X86CC = X86::COND_B; break;
2159 case ISD::SETUGT: X86CC = X86::COND_A; break;
2160 case ISD::SETULE: X86CC = X86::COND_BE; break;
2161 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002162 }
2163 } else {
2164 // On a floating point condition, the flags are set as follows:
2165 // ZF PF CF op
2166 // 0 | 0 | 0 | X > Y
2167 // 0 | 0 | 1 | X < Y
2168 // 1 | 0 | 0 | X == Y
2169 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002170 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002171 switch (SetCCOpcode) {
2172 default: break;
2173 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002174 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002175 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002176 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002177 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002178 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002179 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002180 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002181 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002182 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002183 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002184 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002185 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002186 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002187 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002188 case ISD::SETNE: X86CC = X86::COND_NE; break;
2189 case ISD::SETUO: X86CC = X86::COND_P; break;
2190 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002191 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002192 if (Flip)
2193 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002194 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002195
Chris Lattner7fbe9722006-10-20 17:42:20 +00002196 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002197}
2198
Evan Cheng4a460802006-01-11 00:33:36 +00002199/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2200/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002201/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002202static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002203 switch (X86CC) {
2204 default:
2205 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002206 case X86::COND_B:
2207 case X86::COND_BE:
2208 case X86::COND_E:
2209 case X86::COND_P:
2210 case X86::COND_A:
2211 case X86::COND_AE:
2212 case X86::COND_NE:
2213 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002214 return true;
2215 }
2216}
2217
Evan Cheng5ced1d82006-04-06 23:23:56 +00002218/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002219/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002220static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2221 if (Op.getOpcode() == ISD::UNDEF)
2222 return true;
2223
2224 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002225 return (Val >= Low && Val < Hi);
2226}
2227
2228/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2229/// true if Op is undef or if its value equal to the specified value.
2230static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2231 if (Op.getOpcode() == ISD::UNDEF)
2232 return true;
2233 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002234}
2235
Evan Cheng0188ecb2006-03-22 18:59:22 +00002236/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2238bool X86::isPSHUFDMask(SDNode *N) {
2239 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2240
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002241 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002242 return false;
2243
2244 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002246 SDOperand Arg = N->getOperand(i);
2247 if (Arg.getOpcode() == ISD::UNDEF) continue;
2248 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002249 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002250 return false;
2251 }
2252
2253 return true;
2254}
2255
2256/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002257/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002258bool X86::isPSHUFHWMask(SDNode *N) {
2259 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2260
2261 if (N->getNumOperands() != 8)
2262 return false;
2263
2264 // Lower quadword copied in order.
2265 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002266 SDOperand Arg = N->getOperand(i);
2267 if (Arg.getOpcode() == ISD::UNDEF) continue;
2268 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2269 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002270 return false;
2271 }
2272
2273 // Upper quadword shuffled.
2274 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002275 SDOperand Arg = N->getOperand(i);
2276 if (Arg.getOpcode() == ISD::UNDEF) continue;
2277 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2278 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002279 if (Val < 4 || Val > 7)
2280 return false;
2281 }
2282
2283 return true;
2284}
2285
2286/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002287/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002288bool X86::isPSHUFLWMask(SDNode *N) {
2289 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2290
2291 if (N->getNumOperands() != 8)
2292 return false;
2293
2294 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002295 for (unsigned i = 4; i != 8; ++i)
2296 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002297 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002298
2299 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002300 for (unsigned i = 0; i != 4; ++i)
2301 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002302 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002303
2304 return true;
2305}
2306
Evan Cheng14aed5e2006-03-24 01:18:28 +00002307/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2308/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002309static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002310 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002311
Evan Cheng39623da2006-04-20 08:58:49 +00002312 unsigned Half = NumElems / 2;
2313 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002314 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002315 return false;
2316 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002317 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002318 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002319
2320 return true;
2321}
2322
Evan Cheng39623da2006-04-20 08:58:49 +00002323bool X86::isSHUFPMask(SDNode *N) {
2324 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002325 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002326}
2327
Evan Cheng213d2cf2007-05-17 18:45:50 +00002328/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002329/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2330/// half elements to come from vector 1 (which would equal the dest.) and
2331/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002332static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2333 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002334
Chris Lattner5a88b832007-02-25 07:10:00 +00002335 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002336 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002337 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002338 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002339 for (unsigned i = Half; i < NumOps; ++i)
2340 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002341 return false;
2342 return true;
2343}
2344
2345static bool isCommutedSHUFP(SDNode *N) {
2346 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002347 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002348}
2349
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002350/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2351/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2352bool X86::isMOVHLPSMask(SDNode *N) {
2353 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2354
Evan Cheng2064a2b2006-03-28 06:50:32 +00002355 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002356 return false;
2357
Evan Cheng2064a2b2006-03-28 06:50:32 +00002358 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002359 return isUndefOrEqual(N->getOperand(0), 6) &&
2360 isUndefOrEqual(N->getOperand(1), 7) &&
2361 isUndefOrEqual(N->getOperand(2), 2) &&
2362 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002363}
2364
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002365/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2366/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2367/// <2, 3, 2, 3>
2368bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2369 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2370
2371 if (N->getNumOperands() != 4)
2372 return false;
2373
2374 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2375 return isUndefOrEqual(N->getOperand(0), 2) &&
2376 isUndefOrEqual(N->getOperand(1), 3) &&
2377 isUndefOrEqual(N->getOperand(2), 2) &&
2378 isUndefOrEqual(N->getOperand(3), 3);
2379}
2380
Evan Cheng5ced1d82006-04-06 23:23:56 +00002381/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2382/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2383bool X86::isMOVLPMask(SDNode *N) {
2384 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2385
2386 unsigned NumElems = N->getNumOperands();
2387 if (NumElems != 2 && NumElems != 4)
2388 return false;
2389
Evan Chengc5cdff22006-04-07 21:53:05 +00002390 for (unsigned i = 0; i < NumElems/2; ++i)
2391 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2392 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002393
Evan Chengc5cdff22006-04-07 21:53:05 +00002394 for (unsigned i = NumElems/2; i < NumElems; ++i)
2395 if (!isUndefOrEqual(N->getOperand(i), i))
2396 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002397
2398 return true;
2399}
2400
2401/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002402/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2403/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002404bool X86::isMOVHPMask(SDNode *N) {
2405 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2406
2407 unsigned NumElems = N->getNumOperands();
2408 if (NumElems != 2 && NumElems != 4)
2409 return false;
2410
Evan Chengc5cdff22006-04-07 21:53:05 +00002411 for (unsigned i = 0; i < NumElems/2; ++i)
2412 if (!isUndefOrEqual(N->getOperand(i), i))
2413 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002414
2415 for (unsigned i = 0; i < NumElems/2; ++i) {
2416 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002417 if (!isUndefOrEqual(Arg, i + NumElems))
2418 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002419 }
2420
2421 return true;
2422}
2423
Evan Cheng0038e592006-03-28 00:39:58 +00002424/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2425/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002426bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2427 bool V2IsSplat = false) {
2428 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002429 return false;
2430
Chris Lattner5a88b832007-02-25 07:10:00 +00002431 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2432 SDOperand BitI = Elts[i];
2433 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002434 if (!isUndefOrEqual(BitI, j))
2435 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002436 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002437 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002438 return false;
2439 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002440 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002441 return false;
2442 }
Evan Cheng0038e592006-03-28 00:39:58 +00002443 }
2444
2445 return true;
2446}
2447
Evan Cheng39623da2006-04-20 08:58:49 +00002448bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2449 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002450 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002451}
2452
Evan Cheng4fcb9222006-03-28 02:43:26 +00002453/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2454/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002455bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2456 bool V2IsSplat = false) {
2457 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002458 return false;
2459
Chris Lattner5a88b832007-02-25 07:10:00 +00002460 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2461 SDOperand BitI = Elts[i];
2462 SDOperand BitI1 = Elts[i+1];
2463 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002464 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002465 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002466 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002467 return false;
2468 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002469 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002470 return false;
2471 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002472 }
2473
2474 return true;
2475}
2476
Evan Cheng39623da2006-04-20 08:58:49 +00002477bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2478 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002479 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002480}
2481
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002482/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2483/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2484/// <0, 0, 1, 1>
2485bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2486 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2487
2488 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002489 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002490 return false;
2491
2492 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2493 SDOperand BitI = N->getOperand(i);
2494 SDOperand BitI1 = N->getOperand(i+1);
2495
Evan Chengc5cdff22006-04-07 21:53:05 +00002496 if (!isUndefOrEqual(BitI, j))
2497 return false;
2498 if (!isUndefOrEqual(BitI1, j))
2499 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002500 }
2501
2502 return true;
2503}
2504
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002505/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2506/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2507/// <2, 2, 3, 3>
2508bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2509 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2510
2511 unsigned NumElems = N->getNumOperands();
2512 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2513 return false;
2514
2515 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2516 SDOperand BitI = N->getOperand(i);
2517 SDOperand BitI1 = N->getOperand(i + 1);
2518
2519 if (!isUndefOrEqual(BitI, j))
2520 return false;
2521 if (!isUndefOrEqual(BitI1, j))
2522 return false;
2523 }
2524
2525 return true;
2526}
2527
Evan Cheng017dcc62006-04-21 01:05:10 +00002528/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2529/// specifies a shuffle of elements that is suitable for input to MOVSS,
2530/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002531static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002532 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002533 return false;
2534
Chris Lattner5a88b832007-02-25 07:10:00 +00002535 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002536 return false;
2537
Chris Lattner5a88b832007-02-25 07:10:00 +00002538 for (unsigned i = 1; i < NumElts; ++i) {
2539 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002540 return false;
2541 }
2542
2543 return true;
2544}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002545
Evan Cheng017dcc62006-04-21 01:05:10 +00002546bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002547 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002548 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002549}
2550
Evan Cheng017dcc62006-04-21 01:05:10 +00002551/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2552/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002553/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002554static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2555 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002556 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002557 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002558 return false;
2559
2560 if (!isUndefOrEqual(Ops[0], 0))
2561 return false;
2562
Chris Lattner5a88b832007-02-25 07:10:00 +00002563 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002564 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002565 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2566 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2567 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002568 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002569 }
2570
2571 return true;
2572}
2573
Evan Cheng8cf723d2006-09-08 01:50:06 +00002574static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2575 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002576 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002577 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2578 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002579}
2580
Evan Chengd9539472006-04-14 21:59:03 +00002581/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2582/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2583bool X86::isMOVSHDUPMask(SDNode *N) {
2584 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2585
2586 if (N->getNumOperands() != 4)
2587 return false;
2588
2589 // Expect 1, 1, 3, 3
2590 for (unsigned i = 0; i < 2; ++i) {
2591 SDOperand Arg = N->getOperand(i);
2592 if (Arg.getOpcode() == ISD::UNDEF) continue;
2593 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2594 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2595 if (Val != 1) return false;
2596 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002597
2598 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002599 for (unsigned i = 2; i < 4; ++i) {
2600 SDOperand Arg = N->getOperand(i);
2601 if (Arg.getOpcode() == ISD::UNDEF) continue;
2602 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2603 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2604 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002605 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002606 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002607
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002608 // Don't use movshdup if it can be done with a shufps.
2609 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002610}
2611
2612/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2613/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2614bool X86::isMOVSLDUPMask(SDNode *N) {
2615 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2616
2617 if (N->getNumOperands() != 4)
2618 return false;
2619
2620 // Expect 0, 0, 2, 2
2621 for (unsigned i = 0; i < 2; ++i) {
2622 SDOperand Arg = N->getOperand(i);
2623 if (Arg.getOpcode() == ISD::UNDEF) continue;
2624 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2625 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2626 if (Val != 0) return false;
2627 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002628
2629 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002630 for (unsigned i = 2; i < 4; ++i) {
2631 SDOperand Arg = N->getOperand(i);
2632 if (Arg.getOpcode() == ISD::UNDEF) continue;
2633 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2634 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2635 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002636 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002637 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002638
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002639 // Don't use movshdup if it can be done with a shufps.
2640 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002641}
2642
Evan Cheng49892af2007-06-19 00:02:56 +00002643/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2644/// specifies a identity operation on the LHS or RHS.
2645static bool isIdentityMask(SDNode *N, bool RHS = false) {
2646 unsigned NumElems = N->getNumOperands();
2647 for (unsigned i = 0; i < NumElems; ++i)
2648 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2649 return false;
2650 return true;
2651}
2652
Evan Chengb9df0ca2006-03-22 02:53:00 +00002653/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2654/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002655static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002656 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2657
Evan Chengb9df0ca2006-03-22 02:53:00 +00002658 // This is a splat operation if each element of the permute is the same, and
2659 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002660 unsigned NumElems = N->getNumOperands();
2661 SDOperand ElementBase;
2662 unsigned i = 0;
2663 for (; i != NumElems; ++i) {
2664 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002665 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002666 ElementBase = Elt;
2667 break;
2668 }
2669 }
2670
2671 if (!ElementBase.Val)
2672 return false;
2673
2674 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002675 SDOperand Arg = N->getOperand(i);
2676 if (Arg.getOpcode() == ISD::UNDEF) continue;
2677 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002678 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002679 }
2680
2681 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002682 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002683}
2684
Evan Chengc575ca22006-04-17 20:43:08 +00002685/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2686/// a splat of a single element and it's a 2 or 4 element mask.
2687bool X86::isSplatMask(SDNode *N) {
2688 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2689
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002690 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002691 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2692 return false;
2693 return ::isSplatMask(N);
2694}
2695
Evan Chengf686d9b2006-10-27 21:08:32 +00002696/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2697/// specifies a splat of zero element.
2698bool X86::isSplatLoMask(SDNode *N) {
2699 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2700
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002701 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002702 if (!isUndefOrEqual(N->getOperand(i), 0))
2703 return false;
2704 return true;
2705}
2706
Evan Cheng63d33002006-03-22 08:01:21 +00002707/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2708/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2709/// instructions.
2710unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002711 unsigned NumOperands = N->getNumOperands();
2712 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2713 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002714 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002715 unsigned Val = 0;
2716 SDOperand Arg = N->getOperand(NumOperands-i-1);
2717 if (Arg.getOpcode() != ISD::UNDEF)
2718 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002719 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002720 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002721 if (i != NumOperands - 1)
2722 Mask <<= Shift;
2723 }
Evan Cheng63d33002006-03-22 08:01:21 +00002724
2725 return Mask;
2726}
2727
Evan Cheng506d3df2006-03-29 23:07:14 +00002728/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2729/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2730/// instructions.
2731unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2732 unsigned Mask = 0;
2733 // 8 nodes, but we only care about the last 4.
2734 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002735 unsigned Val = 0;
2736 SDOperand Arg = N->getOperand(i);
2737 if (Arg.getOpcode() != ISD::UNDEF)
2738 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002739 Mask |= (Val - 4);
2740 if (i != 4)
2741 Mask <<= 2;
2742 }
2743
2744 return Mask;
2745}
2746
2747/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2748/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2749/// instructions.
2750unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2751 unsigned Mask = 0;
2752 // 8 nodes, but we only care about the first 4.
2753 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002754 unsigned Val = 0;
2755 SDOperand Arg = N->getOperand(i);
2756 if (Arg.getOpcode() != ISD::UNDEF)
2757 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002758 Mask |= Val;
2759 if (i != 0)
2760 Mask <<= 2;
2761 }
2762
2763 return Mask;
2764}
2765
Evan Chengc21a0532006-04-05 01:47:37 +00002766/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2767/// specifies a 8 element shuffle that can be broken into a pair of
2768/// PSHUFHW and PSHUFLW.
2769static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2770 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2771
2772 if (N->getNumOperands() != 8)
2773 return false;
2774
2775 // Lower quadword shuffled.
2776 for (unsigned i = 0; i != 4; ++i) {
2777 SDOperand Arg = N->getOperand(i);
2778 if (Arg.getOpcode() == ISD::UNDEF) continue;
2779 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2780 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002781 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002782 return false;
2783 }
2784
2785 // Upper quadword shuffled.
2786 for (unsigned i = 4; i != 8; ++i) {
2787 SDOperand Arg = N->getOperand(i);
2788 if (Arg.getOpcode() == ISD::UNDEF) continue;
2789 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2790 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2791 if (Val < 4 || Val > 7)
2792 return false;
2793 }
2794
2795 return true;
2796}
2797
Chris Lattner8a594482007-11-25 00:24:49 +00002798/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002799/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002800static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2801 SDOperand &V2, SDOperand &Mask,
2802 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002803 MVT::ValueType VT = Op.getValueType();
2804 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002805 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002806 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002807 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002808
2809 for (unsigned i = 0; i != NumElems; ++i) {
2810 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002811 if (Arg.getOpcode() == ISD::UNDEF) {
2812 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2813 continue;
2814 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002815 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2816 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2817 if (Val < NumElems)
2818 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2819 else
2820 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2821 }
2822
Evan Cheng9eca5e82006-10-25 21:49:50 +00002823 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002824 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002825 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002826}
2827
Evan Cheng779ccea2007-12-07 21:30:01 +00002828/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2829/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002830static
2831SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2832 MVT::ValueType MaskVT = Mask.getValueType();
2833 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2834 unsigned NumElems = Mask.getNumOperands();
2835 SmallVector<SDOperand, 8> MaskVec;
2836 for (unsigned i = 0; i != NumElems; ++i) {
2837 SDOperand Arg = Mask.getOperand(i);
2838 if (Arg.getOpcode() == ISD::UNDEF) {
2839 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2840 continue;
2841 }
2842 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2843 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2844 if (Val < NumElems)
2845 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2846 else
2847 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2848 }
2849 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2850}
2851
2852
Evan Cheng533a0aa2006-04-19 20:35:22 +00002853/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2854/// match movhlps. The lower half elements should come from upper half of
2855/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002856/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002857static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2858 unsigned NumElems = Mask->getNumOperands();
2859 if (NumElems != 4)
2860 return false;
2861 for (unsigned i = 0, e = 2; i != e; ++i)
2862 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2863 return false;
2864 for (unsigned i = 2; i != 4; ++i)
2865 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2866 return false;
2867 return true;
2868}
2869
Evan Cheng5ced1d82006-04-06 23:23:56 +00002870/// isScalarLoadToVector - Returns true if the node is a scalar load that
2871/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002872static inline bool isScalarLoadToVector(SDNode *N) {
2873 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2874 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002875 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002876 }
2877 return false;
2878}
2879
Evan Cheng533a0aa2006-04-19 20:35:22 +00002880/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2881/// match movlp{s|d}. The lower half elements should come from lower half of
2882/// V1 (and in order), and the upper half elements should come from the upper
2883/// half of V2 (and in order). And since V1 will become the source of the
2884/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002885static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002886 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002887 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002888 // Is V2 is a vector load, don't do this transformation. We will try to use
2889 // load folding shufps op.
2890 if (ISD::isNON_EXTLoad(V2))
2891 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002892
Evan Cheng533a0aa2006-04-19 20:35:22 +00002893 unsigned NumElems = Mask->getNumOperands();
2894 if (NumElems != 2 && NumElems != 4)
2895 return false;
2896 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2897 if (!isUndefOrEqual(Mask->getOperand(i), i))
2898 return false;
2899 for (unsigned i = NumElems/2; i != NumElems; ++i)
2900 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2901 return false;
2902 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002903}
2904
Evan Cheng39623da2006-04-20 08:58:49 +00002905/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2906/// all the same.
2907static bool isSplatVector(SDNode *N) {
2908 if (N->getOpcode() != ISD::BUILD_VECTOR)
2909 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002910
Evan Cheng39623da2006-04-20 08:58:49 +00002911 SDOperand SplatValue = N->getOperand(0);
2912 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2913 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002914 return false;
2915 return true;
2916}
2917
Evan Cheng8cf723d2006-09-08 01:50:06 +00002918/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2919/// to an undef.
2920static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002921 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002922 return false;
2923
2924 SDOperand V1 = N->getOperand(0);
2925 SDOperand V2 = N->getOperand(1);
2926 SDOperand Mask = N->getOperand(2);
2927 unsigned NumElems = Mask.getNumOperands();
2928 for (unsigned i = 0; i != NumElems; ++i) {
2929 SDOperand Arg = Mask.getOperand(i);
2930 if (Arg.getOpcode() != ISD::UNDEF) {
2931 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2932 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2933 return false;
2934 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2935 return false;
2936 }
2937 }
2938 return true;
2939}
2940
Evan Cheng213d2cf2007-05-17 18:45:50 +00002941/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2942/// constant +0.0.
2943static inline bool isZeroNode(SDOperand Elt) {
2944 return ((isa<ConstantSDNode>(Elt) &&
2945 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2946 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002947 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002948}
2949
2950/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2951/// to an zero vector.
2952static bool isZeroShuffle(SDNode *N) {
2953 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2954 return false;
2955
2956 SDOperand V1 = N->getOperand(0);
2957 SDOperand V2 = N->getOperand(1);
2958 SDOperand Mask = N->getOperand(2);
2959 unsigned NumElems = Mask.getNumOperands();
2960 for (unsigned i = 0; i != NumElems; ++i) {
2961 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002962 if (Arg.getOpcode() == ISD::UNDEF)
2963 continue;
2964
2965 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2966 if (Idx < NumElems) {
2967 unsigned Opc = V1.Val->getOpcode();
2968 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2969 continue;
2970 if (Opc != ISD::BUILD_VECTOR ||
2971 !isZeroNode(V1.Val->getOperand(Idx)))
2972 return false;
2973 } else if (Idx >= NumElems) {
2974 unsigned Opc = V2.Val->getOpcode();
2975 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2976 continue;
2977 if (Opc != ISD::BUILD_VECTOR ||
2978 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2979 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002980 }
2981 }
2982 return true;
2983}
2984
2985/// getZeroVector - Returns a vector of specified type with all zero elements.
2986///
2987static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2988 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002989
2990 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2991 // type. This ensures they get CSE'd.
2992 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2993 SDOperand Vec;
2994 if (MVT::getSizeInBits(VT) == 64) // MMX
2995 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2996 else // SSE
2997 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2998 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002999}
3000
Chris Lattner8a594482007-11-25 00:24:49 +00003001/// getOnesVector - Returns a vector of specified type with all bits set.
3002///
3003static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
3004 assert(MVT::isVector(VT) && "Expected a vector type");
3005
3006 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3007 // type. This ensures they get CSE'd.
3008 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
3009 SDOperand Vec;
3010 if (MVT::getSizeInBits(VT) == 64) // MMX
3011 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3012 else // SSE
3013 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
3014 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
3015}
3016
3017
Evan Cheng39623da2006-04-20 08:58:49 +00003018/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3019/// that point to V2 points to its first element.
3020static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
3021 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
3022
3023 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00003024 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003025 unsigned NumElems = Mask.getNumOperands();
3026 for (unsigned i = 0; i != NumElems; ++i) {
3027 SDOperand Arg = Mask.getOperand(i);
3028 if (Arg.getOpcode() != ISD::UNDEF) {
3029 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
3030 if (Val > NumElems) {
3031 Arg = DAG.getConstant(NumElems, Arg.getValueType());
3032 Changed = true;
3033 }
3034 }
3035 MaskVec.push_back(Arg);
3036 }
3037
3038 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003039 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
3040 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003041 return Mask;
3042}
3043
Evan Cheng017dcc62006-04-21 01:05:10 +00003044/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3045/// operation of specified width.
3046static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00003047 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003048 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00003049
Chris Lattner5a88b832007-02-25 07:10:00 +00003050 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003051 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
3052 for (unsigned i = 1; i != NumElems; ++i)
3053 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003054 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003055}
3056
Evan Chengc575ca22006-04-17 20:43:08 +00003057/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
3058/// of specified width.
3059static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
3060 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003061 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003062 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00003063 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
3064 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3065 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3066 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003067 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00003068}
3069
Evan Cheng39623da2006-04-20 08:58:49 +00003070/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3071/// of specified width.
3072static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
3073 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003074 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00003075 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00003076 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003077 for (unsigned i = 0; i != Half; ++i) {
3078 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3079 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3080 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003081 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003082}
3083
Evan Chengc575ca22006-04-17 20:43:08 +00003084/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
3085///
3086static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
3087 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00003088 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00003089 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00003090 unsigned NumElems = Mask.getNumOperands();
3091 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003092 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00003093 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003094 NumElems >>= 1;
3095 }
3096 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
3097
Chris Lattner8a594482007-11-25 00:24:49 +00003098 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00003099 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00003100 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00003101 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3102}
3103
Evan Chengba05f722006-04-21 23:03:30 +00003104/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003105/// vector of zero or undef vector. This produces a shuffle where the low
3106/// element of V2 is swizzled into the zero/undef vector, landing at element
3107/// 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 +00003108static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00003109 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00003110 bool isZero, SelectionDAG &DAG) {
3111 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00003112 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003113 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner8a594482007-11-25 00:24:49 +00003114 SmallVector<SDOperand, 16> MaskVec;
3115 for (unsigned i = 0; i != NumElems; ++i)
3116 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3117 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3118 else
3119 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003120 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3121 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00003122 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003123}
3124
Evan Chengc78d3b42006-04-24 18:01:45 +00003125/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3126///
3127static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3128 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003129 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003130 if (NumNonZero > 8)
3131 return SDOperand();
3132
3133 SDOperand V(0, 0);
3134 bool First = true;
3135 for (unsigned i = 0; i < 16; ++i) {
3136 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3137 if (ThisIsNonZero && First) {
3138 if (NumZero)
3139 V = getZeroVector(MVT::v8i16, DAG);
3140 else
3141 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3142 First = false;
3143 }
3144
3145 if ((i & 1) != 0) {
3146 SDOperand ThisElt(0, 0), LastElt(0, 0);
3147 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3148 if (LastIsNonZero) {
3149 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3150 }
3151 if (ThisIsNonZero) {
3152 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3153 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3154 ThisElt, DAG.getConstant(8, MVT::i8));
3155 if (LastIsNonZero)
3156 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3157 } else
3158 ThisElt = LastElt;
3159
3160 if (ThisElt.Val)
3161 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00003162 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003163 }
3164 }
3165
3166 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3167}
3168
Bill Wendlinga348c562007-03-22 18:42:45 +00003169/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003170///
3171static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3172 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003173 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003174 if (NumNonZero > 4)
3175 return SDOperand();
3176
3177 SDOperand V(0, 0);
3178 bool First = true;
3179 for (unsigned i = 0; i < 8; ++i) {
3180 bool isNonZero = (NonZeros & (1 << i)) != 0;
3181 if (isNonZero) {
3182 if (First) {
3183 if (NumZero)
3184 V = getZeroVector(MVT::v8i16, DAG);
3185 else
3186 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3187 First = false;
3188 }
3189 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00003190 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003191 }
3192 }
3193
3194 return V;
3195}
3196
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197SDOperand
3198X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003199 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3200 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3201 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3202 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3203 // eliminated on x86-32 hosts.
3204 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3205 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003206
Chris Lattner8a594482007-11-25 00:24:49 +00003207 if (ISD::isBuildVectorAllOnes(Op.Val))
3208 return getOnesVector(Op.getValueType(), DAG);
3209 return getZeroVector(Op.getValueType(), DAG);
3210 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003211
3212 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003213 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003214 unsigned EVTBits = MVT::getSizeInBits(EVT);
3215
3216 unsigned NumElems = Op.getNumOperands();
3217 unsigned NumZero = 0;
3218 unsigned NumNonZero = 0;
3219 unsigned NonZeros = 0;
Evan Chengdb2d5242007-12-12 06:45:40 +00003220 bool HasNonImms = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003221 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003222 for (unsigned i = 0; i < NumElems; ++i) {
3223 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003224 if (Elt.getOpcode() == ISD::UNDEF)
3225 continue;
3226 Values.insert(Elt);
3227 if (Elt.getOpcode() != ISD::Constant &&
3228 Elt.getOpcode() != ISD::ConstantFP)
3229 HasNonImms = true;
3230 if (isZeroNode(Elt))
3231 NumZero++;
3232 else {
3233 NonZeros |= (1 << i);
3234 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003235 }
3236 }
3237
Dan Gohman7f321562007-06-25 16:23:39 +00003238 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003239 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3240 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003241 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003242
3243 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3244 if (Values.size() == 1)
3245 return SDOperand();
3246
3247 // Special case for single non-zero element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003248 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003249 unsigned Idx = CountTrailingZeros_32(NonZeros);
3250 SDOperand Item = Op.getOperand(Idx);
3251 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3252 if (Idx == 0)
3253 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3254 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3255 NumZero > 0, DAG);
Evan Chengdb2d5242007-12-12 06:45:40 +00003256 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
3257 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003258
3259 if (EVTBits == 32) {
3260 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3261 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3262 DAG);
3263 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003264 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003265 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003266 for (unsigned i = 0; i < NumElems; i++)
3267 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003268 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3269 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003270 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3271 DAG.getNode(ISD::UNDEF, VT), Mask);
3272 }
3273 }
3274
Dan Gohmana3941172007-07-24 22:55:08 +00003275 // A vector full of immediates; various special cases are already
3276 // handled, so this is best done with a single constant-pool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00003277 if (!HasNonImms)
Dan Gohmana3941172007-07-24 22:55:08 +00003278 return SDOperand();
3279
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003280 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003281 if (EVTBits == 64)
3282 return SDOperand();
3283
3284 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003285 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003286 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3287 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003288 if (V.Val) return V;
3289 }
3290
Bill Wendling826f36f2007-03-28 00:57:11 +00003291 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003292 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3293 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003294 if (V.Val) return V;
3295 }
3296
3297 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003298 SmallVector<SDOperand, 8> V;
3299 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003300 if (NumElems == 4 && NumZero > 0) {
3301 for (unsigned i = 0; i < 4; ++i) {
3302 bool isZero = !(NonZeros & (1 << i));
3303 if (isZero)
3304 V[i] = getZeroVector(VT, DAG);
3305 else
3306 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3307 }
3308
3309 for (unsigned i = 0; i < 2; ++i) {
3310 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3311 default: break;
3312 case 0:
3313 V[i] = V[i*2]; // Must be a zero vector.
3314 break;
3315 case 1:
3316 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3317 getMOVLMask(NumElems, DAG));
3318 break;
3319 case 2:
3320 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3321 getMOVLMask(NumElems, DAG));
3322 break;
3323 case 3:
3324 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3325 getUnpacklMask(NumElems, DAG));
3326 break;
3327 }
3328 }
3329
Evan Cheng069287d2006-05-16 07:21:53 +00003330 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003331 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003332 // FIXME: we can do the same for v4f32 case when we know both parts of
3333 // the lower half come from scalar_to_vector (loadf32). We should do
3334 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003335 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003336 return V[0];
3337 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003338 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003339 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003340 bool Reverse = (NonZeros & 0x3) == 2;
3341 for (unsigned i = 0; i < 2; ++i)
3342 if (Reverse)
3343 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3344 else
3345 MaskVec.push_back(DAG.getConstant(i, EVT));
3346 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3347 for (unsigned i = 0; i < 2; ++i)
3348 if (Reverse)
3349 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3350 else
3351 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003352 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3353 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003354 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3355 }
3356
3357 if (Values.size() > 2) {
3358 // Expand into a number of unpckl*.
3359 // e.g. for v4f32
3360 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3361 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3362 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3363 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3364 for (unsigned i = 0; i < NumElems; ++i)
3365 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3366 NumElems >>= 1;
3367 while (NumElems != 0) {
3368 for (unsigned i = 0; i < NumElems; ++i)
3369 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3370 UnpckMask);
3371 NumElems >>= 1;
3372 }
3373 return V[0];
3374 }
3375
3376 return SDOperand();
3377}
3378
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003379static
3380SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3381 SDOperand PermMask, SelectionDAG &DAG,
3382 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003383 SDOperand NewV;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003384 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3385 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng14b32e12007-12-11 01:46:18 +00003386 MVT::ValueType PtrVT = TLI.getPointerTy();
3387 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3388 PermMask.Val->op_end());
3389
3390 // First record which half of which vector the low elements come from.
3391 SmallVector<unsigned, 4> LowQuad(4);
3392 for (unsigned i = 0; i < 4; ++i) {
3393 SDOperand Elt = MaskElts[i];
3394 if (Elt.getOpcode() == ISD::UNDEF)
3395 continue;
3396 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3397 int QuadIdx = EltIdx / 4;
3398 ++LowQuad[QuadIdx];
3399 }
3400 int BestLowQuad = -1;
3401 unsigned MaxQuad = 1;
3402 for (unsigned i = 0; i < 4; ++i) {
3403 if (LowQuad[i] > MaxQuad) {
3404 BestLowQuad = i;
3405 MaxQuad = LowQuad[i];
3406 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003407 }
3408
Evan Cheng14b32e12007-12-11 01:46:18 +00003409 // Record which half of which vector the high elements come from.
3410 SmallVector<unsigned, 4> HighQuad(4);
3411 for (unsigned i = 4; i < 8; ++i) {
3412 SDOperand Elt = MaskElts[i];
3413 if (Elt.getOpcode() == ISD::UNDEF)
3414 continue;
3415 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3416 int QuadIdx = EltIdx / 4;
3417 ++HighQuad[QuadIdx];
3418 }
3419 int BestHighQuad = -1;
3420 MaxQuad = 1;
3421 for (unsigned i = 0; i < 4; ++i) {
3422 if (HighQuad[i] > MaxQuad) {
3423 BestHighQuad = i;
3424 MaxQuad = HighQuad[i];
3425 }
3426 }
3427
3428 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3429 if (BestLowQuad != -1 || BestHighQuad != -1) {
3430 // First sort the 4 chunks in order using shufpd.
3431 SmallVector<SDOperand, 8> MaskVec;
3432 if (BestLowQuad != -1)
3433 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3434 else
3435 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3436 if (BestHighQuad != -1)
3437 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3438 else
3439 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3440 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3441 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3442 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3443 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3444 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3445
3446 // Now sort high and low parts separately.
3447 BitVector InOrder(8);
3448 if (BestLowQuad != -1) {
3449 // Sort lower half in order using PSHUFLW.
3450 MaskVec.clear();
3451 bool AnyOutOrder = false;
3452 for (unsigned i = 0; i != 4; ++i) {
3453 SDOperand Elt = MaskElts[i];
3454 if (Elt.getOpcode() == ISD::UNDEF) {
3455 MaskVec.push_back(Elt);
3456 InOrder.set(i);
3457 } else {
3458 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3459 if (EltIdx != i)
3460 AnyOutOrder = true;
3461 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3462 // If this element is in the right place after this shuffle, then
3463 // remember it.
3464 if ((int)(EltIdx / 4) == BestLowQuad)
3465 InOrder.set(i);
3466 }
3467 }
3468 if (AnyOutOrder) {
3469 for (unsigned i = 4; i != 8; ++i)
3470 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3471 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3472 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3473 }
3474 }
3475
3476 if (BestHighQuad != -1) {
3477 // Sort high half in order using PSHUFHW if possible.
3478 MaskVec.clear();
3479 for (unsigned i = 0; i != 4; ++i)
3480 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3481 bool AnyOutOrder = false;
3482 for (unsigned i = 4; i != 8; ++i) {
3483 SDOperand Elt = MaskElts[i];
3484 if (Elt.getOpcode() == ISD::UNDEF) {
3485 MaskVec.push_back(Elt);
3486 InOrder.set(i);
3487 } else {
3488 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3489 if (EltIdx != i)
3490 AnyOutOrder = true;
3491 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3492 // If this element is in the right place after this shuffle, then
3493 // remember it.
3494 if ((int)(EltIdx / 4) == BestHighQuad)
3495 InOrder.set(i);
3496 }
3497 }
3498 if (AnyOutOrder) {
3499 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3500 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3501 }
3502 }
3503
3504 // The other elements are put in the right place using pextrw and pinsrw.
3505 for (unsigned i = 0; i != 8; ++i) {
3506 if (InOrder[i])
3507 continue;
3508 SDOperand Elt = MaskElts[i];
3509 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3510 if (EltIdx == i)
3511 continue;
3512 SDOperand ExtOp = (EltIdx < 8)
3513 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3514 DAG.getConstant(EltIdx, PtrVT))
3515 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3516 DAG.getConstant(EltIdx - 8, PtrVT));
3517 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3518 DAG.getConstant(i, PtrVT));
3519 }
3520 return NewV;
3521 }
3522
3523 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3524 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003525 // First, let's find out how many elements are already in the right order.
3526 unsigned V1InOrder = 0;
3527 unsigned V1FromV1 = 0;
3528 unsigned V2InOrder = 0;
3529 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003530 SmallVector<SDOperand, 8> V1Elts;
3531 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003532 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003533 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003534 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003535 V1Elts.push_back(Elt);
3536 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003537 ++V1InOrder;
3538 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003539 continue;
3540 }
3541 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3542 if (EltIdx == i) {
3543 V1Elts.push_back(Elt);
3544 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3545 ++V1InOrder;
3546 } else if (EltIdx == i+8) {
3547 V1Elts.push_back(Elt);
3548 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3549 ++V2InOrder;
3550 } else if (EltIdx < 8) {
3551 V1Elts.push_back(Elt);
3552 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003553 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003554 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3555 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003556 }
3557 }
3558
3559 if (V2InOrder > V1InOrder) {
3560 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3561 std::swap(V1, V2);
3562 std::swap(V1Elts, V2Elts);
3563 std::swap(V1FromV1, V2FromV2);
3564 }
3565
Evan Cheng14b32e12007-12-11 01:46:18 +00003566 if ((V1FromV1 + V1InOrder) != 8) {
3567 // Some elements are from V2.
3568 if (V1FromV1) {
3569 // If there are elements that are from V1 but out of place,
3570 // then first sort them in place
3571 SmallVector<SDOperand, 8> MaskVec;
3572 for (unsigned i = 0; i < 8; ++i) {
3573 SDOperand Elt = V1Elts[i];
3574 if (Elt.getOpcode() == ISD::UNDEF) {
3575 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3576 continue;
3577 }
3578 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3579 if (EltIdx >= 8)
3580 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3581 else
3582 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3583 }
3584 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3585 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003586 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003587
3588 NewV = V1;
3589 for (unsigned i = 0; i < 8; ++i) {
3590 SDOperand Elt = V1Elts[i];
3591 if (Elt.getOpcode() == ISD::UNDEF)
3592 continue;
3593 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3594 if (EltIdx < 8)
3595 continue;
3596 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3597 DAG.getConstant(EltIdx - 8, PtrVT));
3598 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3599 DAG.getConstant(i, PtrVT));
3600 }
3601 return NewV;
3602 } else {
3603 // All elements are from V1.
3604 NewV = V1;
3605 for (unsigned i = 0; i < 8; ++i) {
3606 SDOperand Elt = V1Elts[i];
3607 if (Elt.getOpcode() == ISD::UNDEF)
3608 continue;
3609 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3610 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3611 DAG.getConstant(EltIdx, PtrVT));
3612 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3613 DAG.getConstant(i, PtrVT));
3614 }
3615 return NewV;
3616 }
3617}
3618
Evan Cheng7a831ce2007-12-15 03:00:47 +00003619/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3620/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3621/// done when every pair / quad of shuffle mask elements point to elements in
3622/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003623/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3624static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003625SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3626 MVT::ValueType VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003627 SDOperand PermMask, SelectionDAG &DAG,
3628 TargetLowering &TLI) {
3629 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003630 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3631 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3632 MVT::ValueType NewVT = MaskVT;
3633 switch (VT) {
3634 case MVT::v4f32: NewVT = MVT::v2f64; break;
3635 case MVT::v4i32: NewVT = MVT::v2i64; break;
3636 case MVT::v8i16: NewVT = MVT::v4i32; break;
3637 case MVT::v16i8: NewVT = MVT::v4i32; break;
3638 default: assert(false && "Unexpected!");
3639 }
3640
3641 if (NewWidth == 2)
3642 if (MVT::isInteger(VT))
3643 NewVT = MVT::v2i64;
3644 else
3645 NewVT = MVT::v2f64;
3646 unsigned Scale = NumElems / NewWidth;
3647 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003648 for (unsigned i = 0; i < NumElems; i += Scale) {
3649 unsigned StartIdx = ~0U;
3650 for (unsigned j = 0; j < Scale; ++j) {
3651 SDOperand Elt = PermMask.getOperand(i+j);
3652 if (Elt.getOpcode() == ISD::UNDEF)
3653 continue;
3654 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3655 if (StartIdx == ~0U)
3656 StartIdx = EltIdx - (EltIdx % Scale);
3657 if (EltIdx != StartIdx + j)
3658 return SDOperand();
3659 }
3660 if (StartIdx == ~0U)
3661 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3662 else
3663 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003664 }
3665
Evan Cheng7a831ce2007-12-15 03:00:47 +00003666 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3667 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3668 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3669 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3670 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003671}
3672
Evan Cheng0db9fe62006-04-25 20:13:52 +00003673SDOperand
3674X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3675 SDOperand V1 = Op.getOperand(0);
3676 SDOperand V2 = Op.getOperand(1);
3677 SDOperand PermMask = Op.getOperand(2);
3678 MVT::ValueType VT = Op.getValueType();
3679 unsigned NumElems = PermMask.getNumOperands();
3680 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3681 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003682 bool V1IsSplat = false;
3683 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684
Evan Cheng8cf723d2006-09-08 01:50:06 +00003685 if (isUndefShuffle(Op.Val))
3686 return DAG.getNode(ISD::UNDEF, VT);
3687
Evan Cheng213d2cf2007-05-17 18:45:50 +00003688 if (isZeroShuffle(Op.Val))
3689 return getZeroVector(VT, DAG);
3690
Evan Cheng49892af2007-06-19 00:02:56 +00003691 if (isIdentityMask(PermMask.Val))
3692 return V1;
3693 else if (isIdentityMask(PermMask.Val, true))
3694 return V2;
3695
Evan Cheng0db9fe62006-04-25 20:13:52 +00003696 if (isSplatMask(PermMask.Val)) {
3697 if (NumElems <= 4) return Op;
3698 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003699 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003700 }
3701
Evan Cheng7a831ce2007-12-15 03:00:47 +00003702 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3703 // do it!
3704 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3705 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3706 if (NewOp.Val)
3707 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3708 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3709 // FIXME: Figure out a cleaner way to do this.
3710 // Try to make use of movq to zero out the top part.
3711 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3712 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3713 if (NewOp.Val) {
3714 SDOperand NewV1 = NewOp.getOperand(0);
3715 SDOperand NewV2 = NewOp.getOperand(1);
3716 SDOperand NewMask = NewOp.getOperand(2);
3717 if (isCommutedMOVL(NewMask.Val, true, false)) {
3718 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3719 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3720 NewV1, NewV2, getMOVLMask(2, DAG));
3721 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3722 }
3723 }
3724 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3725 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3726 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3727 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3728 }
3729 }
3730
Evan Cheng9bbbb982006-10-25 20:48:19 +00003731 if (X86::isMOVLMask(PermMask.Val))
3732 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003733
Evan Cheng9bbbb982006-10-25 20:48:19 +00003734 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3735 X86::isMOVSLDUPMask(PermMask.Val) ||
3736 X86::isMOVHLPSMask(PermMask.Val) ||
3737 X86::isMOVHPMask(PermMask.Val) ||
3738 X86::isMOVLPMask(PermMask.Val))
3739 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003740
Evan Cheng9bbbb982006-10-25 20:48:19 +00003741 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3742 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003743 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003744
Evan Cheng9eca5e82006-10-25 21:49:50 +00003745 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003746 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3747 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003748 V1IsSplat = isSplatVector(V1.Val);
3749 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003750
3751 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003752 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003753 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003754 std::swap(V1IsSplat, V2IsSplat);
3755 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003756 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003757 }
3758
Evan Cheng7a831ce2007-12-15 03:00:47 +00003759 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003760 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3761 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003762 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003763 if (V2IsSplat) {
3764 // V2 is a splat, so the mask may be malformed. That is, it may point
3765 // to any V2 element. The instruction selectior won't like this. Get
3766 // a corrected mask and commute to form a proper MOVS{S|D}.
3767 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3768 if (NewMask.Val != PermMask.Val)
3769 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003771 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003772 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003773
Evan Chengd9b8e402006-10-16 06:36:00 +00003774 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003775 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003776 X86::isUNPCKLMask(PermMask.Val) ||
3777 X86::isUNPCKHMask(PermMask.Val))
3778 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003779
Evan Cheng9bbbb982006-10-25 20:48:19 +00003780 if (V2IsSplat) {
3781 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003782 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003783 // new vector_shuffle with the corrected mask.
3784 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3785 if (NewMask.Val != PermMask.Val) {
3786 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3787 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3788 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3789 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3790 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3791 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003792 }
3793 }
3794 }
3795
3796 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003797 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3798 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3799
3800 if (Commuted) {
3801 // Commute is back and try unpck* again.
3802 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3803 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003804 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003805 X86::isUNPCKLMask(PermMask.Val) ||
3806 X86::isUNPCKHMask(PermMask.Val))
3807 return Op;
3808 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003809
3810 // If VT is integer, try PSHUF* first, then SHUFP*.
3811 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003812 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3813 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3814 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3815 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003816 X86::isPSHUFHWMask(PermMask.Val) ||
3817 X86::isPSHUFLWMask(PermMask.Val)) {
3818 if (V2.getOpcode() != ISD::UNDEF)
3819 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3820 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3821 return Op;
3822 }
3823
Chris Lattner07c70cd2007-05-17 17:13:13 +00003824 if (X86::isSHUFPMask(PermMask.Val) &&
3825 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003826 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003827 } else {
3828 // Floating point cases in the other order.
3829 if (X86::isSHUFPMask(PermMask.Val))
3830 return Op;
3831 if (X86::isPSHUFDMask(PermMask.Val) ||
3832 X86::isPSHUFHWMask(PermMask.Val) ||
3833 X86::isPSHUFLWMask(PermMask.Val)) {
3834 if (V2.getOpcode() != ISD::UNDEF)
3835 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3836 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3837 return Op;
3838 }
3839 }
3840
Evan Cheng14b32e12007-12-11 01:46:18 +00003841 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3842 if (VT == MVT::v8i16) {
3843 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3844 if (NewOp.Val)
3845 return NewOp;
3846 }
3847
3848 // Handle all 4 wide cases with a number of shuffles.
3849 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003850 // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003851 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003852 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003853 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003854 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003855 SmallVector<SDOperand, 8> Mask1(NumElems,
3856 DAG.getNode(ISD::UNDEF, MaskEVT));
3857 SmallVector<SDOperand, 8> Mask2(NumElems,
3858 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003859 unsigned NumHi = 0;
3860 unsigned NumLo = 0;
3861 // If no more than two elements come from either vector. This can be
3862 // implemented with two shuffles. First shuffle gather the elements.
3863 // The second shuffle, which takes the first shuffle as both of its
3864 // vector operands, put the elements into the right order.
3865 for (unsigned i = 0; i != NumElems; ++i) {
3866 SDOperand Elt = PermMask.getOperand(i);
3867 if (Elt.getOpcode() == ISD::UNDEF) {
3868 Locs[i] = std::make_pair(-1, -1);
3869 } else {
3870 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3871 if (Val < NumElems) {
3872 Locs[i] = std::make_pair(0, NumLo);
3873 Mask1[NumLo] = Elt;
3874 NumLo++;
3875 } else {
3876 Locs[i] = std::make_pair(1, NumHi);
3877 if (2+NumHi < NumElems)
3878 Mask1[2+NumHi] = Elt;
3879 NumHi++;
3880 }
3881 }
3882 }
3883 if (NumLo <= 2 && NumHi <= 2) {
3884 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003885 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3886 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003887 for (unsigned i = 0; i != NumElems; ++i) {
3888 if (Locs[i].first == -1)
3889 continue;
3890 else {
3891 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3892 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3893 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3894 }
3895 }
3896
3897 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003898 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3899 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003900 }
3901
3902 // Break it into (shuffle shuffle_hi, shuffle_lo).
3903 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003904 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3905 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3906 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003907 unsigned MaskIdx = 0;
3908 unsigned LoIdx = 0;
3909 unsigned HiIdx = NumElems/2;
3910 for (unsigned i = 0; i != NumElems; ++i) {
3911 if (i == NumElems/2) {
3912 MaskPtr = &HiMask;
3913 MaskIdx = 1;
3914 LoIdx = 0;
3915 HiIdx = NumElems/2;
3916 }
3917 SDOperand Elt = PermMask.getOperand(i);
3918 if (Elt.getOpcode() == ISD::UNDEF) {
3919 Locs[i] = std::make_pair(-1, -1);
3920 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3921 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3922 (*MaskPtr)[LoIdx] = Elt;
3923 LoIdx++;
3924 } else {
3925 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3926 (*MaskPtr)[HiIdx] = Elt;
3927 HiIdx++;
3928 }
3929 }
3930
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003931 SDOperand LoShuffle =
3932 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003933 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3934 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003935 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003936 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003937 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3938 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003939 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003940 for (unsigned i = 0; i != NumElems; ++i) {
3941 if (Locs[i].first == -1) {
3942 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3943 } else {
3944 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3945 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3946 }
3947 }
3948 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003949 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3950 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003951 }
3952
3953 return SDOperand();
3954}
3955
3956SDOperand
3957X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3958 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3959 return SDOperand();
3960
3961 MVT::ValueType VT = Op.getValueType();
3962 // TODO: handle v16i8.
3963 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003964 SDOperand Vec = Op.getOperand(0);
3965 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3966 if (Idx == 0)
3967 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3968 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3969 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3970 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003971 // Transform it so it match pextrw which produces a 32-bit result.
3972 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3973 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3974 Op.getOperand(0), Op.getOperand(1));
3975 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3976 DAG.getValueType(VT));
3977 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3978 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3980 if (Idx == 0)
3981 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003982 // SHUFPS the element to the lowest double word, then movss.
3983 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003984 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003985 IdxVec.
3986 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3987 IdxVec.
3988 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3989 IdxVec.
3990 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3991 IdxVec.
3992 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003993 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3994 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003995 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003996 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003997 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003998 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003999 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004000 } else if (MVT::getSizeInBits(VT) == 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004001 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4002 if (Idx == 0)
4003 return Op;
4004
4005 // UNPCKHPD the element to the lowest double word, then movsd.
4006 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4007 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
4008 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00004009 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00004010 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004011 IdxVec.
4012 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00004013 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4014 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00004015 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004016 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4017 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4018 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00004019 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004020 }
4021
4022 return SDOperand();
4023}
4024
4025SDOperand
4026X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004027 MVT::ValueType VT = Op.getValueType();
Evan Cheng794405e2007-12-12 07:55:34 +00004028 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4029 if (EVT == MVT::i8)
4030 return SDOperand();
4031
Evan Cheng0db9fe62006-04-25 20:13:52 +00004032 SDOperand N0 = Op.getOperand(0);
4033 SDOperand N1 = Op.getOperand(1);
4034 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004035
4036 if (MVT::getSizeInBits(EVT) == 16) {
4037 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4038 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004039 if (N1.getValueType() != MVT::i32)
4040 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4041 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00004042 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004043 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004044 }
4045
Evan Cheng794405e2007-12-12 07:55:34 +00004046 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
4047 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
4048 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
4049 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
4050 SmallVector<SDOperand, 4> MaskVec;
4051 for (unsigned i = 0; i < 4; ++i)
4052 MaskVec.push_back(DAG.getConstant((i == Idx) ? i+4 : i, MaskEVT));
4053 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
4054 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4055 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004056}
4057
4058SDOperand
4059X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4060 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
4061 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
4062}
4063
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004064// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004065// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4066// one of the above mentioned nodes. It has to be wrapped because otherwise
4067// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4068// be used to form addressing mode. These wrapped nodes will be selected
4069// into MOV32ri.
4070SDOperand
4071X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4072 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00004073 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4074 getPointerTy(),
4075 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004076 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004077 // With PIC, the address is actually $g + Offset.
4078 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4079 !Subtarget->isPICStyleRIPRel()) {
4080 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4081 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4082 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004083 }
4084
4085 return Result;
4086}
4087
4088SDOperand
4089X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4090 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004091 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004092 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004093 // With PIC, the address is actually $g + Offset.
4094 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4095 !Subtarget->isPICStyleRIPRel()) {
4096 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4097 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4098 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004099 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004100
4101 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4102 // load the value at address GV, not the value of GV itself. This means that
4103 // the GlobalAddress must be in the base or index register of the address, not
4104 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004105 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004106 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
4107 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004108
4109 return Result;
4110}
4111
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004112// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4113static SDOperand
4114LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4115 const MVT::ValueType PtrVT) {
4116 SDOperand InFlag;
4117 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4118 DAG.getNode(X86ISD::GlobalBaseReg,
4119 PtrVT), InFlag);
4120 InFlag = Chain.getValue(1);
4121
4122 // emit leal symbol@TLSGD(,%ebx,1), %eax
4123 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4124 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4125 GA->getValueType(0),
4126 GA->getOffset());
4127 SDOperand Ops[] = { Chain, TGA, InFlag };
4128 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4129 InFlag = Result.getValue(2);
4130 Chain = Result.getValue(1);
4131
4132 // call ___tls_get_addr. This function receives its argument in
4133 // the register EAX.
4134 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4135 InFlag = Chain.getValue(1);
4136
4137 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4138 SDOperand Ops1[] = { Chain,
4139 DAG.getTargetExternalSymbol("___tls_get_addr",
4140 PtrVT),
4141 DAG.getRegister(X86::EAX, PtrVT),
4142 DAG.getRegister(X86::EBX, PtrVT),
4143 InFlag };
4144 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4145 InFlag = Chain.getValue(1);
4146
4147 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4148}
4149
4150// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4151// "local exec" model.
4152static SDOperand
4153LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4154 const MVT::ValueType PtrVT) {
4155 // Get the Thread Pointer
4156 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4157 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4158 // exec)
4159 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4160 GA->getValueType(0),
4161 GA->getOffset());
4162 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004163
4164 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4165 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
4166
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004167 // The address of the thread local variable is the add of the thread
4168 // pointer with the offset of the variable.
4169 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4170}
4171
4172SDOperand
4173X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4174 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004175 // TODO: implement the "initial exec"model for pic executables
4176 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4177 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004178 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4179 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4180 // otherwise use the "Local Exec"TLS Model
4181 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4182 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4183 else
4184 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4185}
4186
Evan Cheng0db9fe62006-04-25 20:13:52 +00004187SDOperand
4188X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4189 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004190 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004191 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004192 // With PIC, the address is actually $g + Offset.
4193 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4194 !Subtarget->isPICStyleRIPRel()) {
4195 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4196 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4197 Result);
4198 }
4199
4200 return Result;
4201}
4202
4203SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4204 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4205 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4206 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4207 // With PIC, the address is actually $g + Offset.
4208 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4209 !Subtarget->isPICStyleRIPRel()) {
4210 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4211 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4212 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004213 }
4214
4215 return Result;
4216}
4217
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004218/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4219/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004220SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004221 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4222 "Not an i64 shift!");
4223 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4224 SDOperand ShOpLo = Op.getOperand(0);
4225 SDOperand ShOpHi = Op.getOperand(1);
4226 SDOperand ShAmt = Op.getOperand(2);
4227 SDOperand Tmp1 = isSRA ?
4228 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4229 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00004230
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004231 SDOperand Tmp2, Tmp3;
4232 if (Op.getOpcode() == ISD::SHL_PARTS) {
4233 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4234 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4235 } else {
4236 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4237 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4238 }
Evan Chenge3413162006-01-09 18:33:28 +00004239
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004240 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4241 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4242 DAG.getConstant(32, MVT::i8));
4243 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4244 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004245
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004246 SDOperand Hi, Lo;
4247 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4248 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4249 SmallVector<SDOperand, 4> Ops;
4250 if (Op.getOpcode() == ISD::SHL_PARTS) {
4251 Ops.push_back(Tmp2);
4252 Ops.push_back(Tmp3);
4253 Ops.push_back(CC);
4254 Ops.push_back(Cond);
4255 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00004256
Evan Chenge3413162006-01-09 18:33:28 +00004257 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004258 Ops.push_back(Tmp3);
4259 Ops.push_back(Tmp1);
4260 Ops.push_back(CC);
4261 Ops.push_back(Cond);
4262 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4263 } else {
4264 Ops.push_back(Tmp2);
4265 Ops.push_back(Tmp3);
4266 Ops.push_back(CC);
4267 Ops.push_back(Cond);
4268 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4269
4270 Ops.clear();
4271 Ops.push_back(Tmp3);
4272 Ops.push_back(Tmp1);
4273 Ops.push_back(CC);
4274 Ops.push_back(Cond);
4275 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4276 }
4277
4278 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4279 Ops.clear();
4280 Ops.push_back(Lo);
4281 Ops.push_back(Hi);
4282 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004283}
Evan Chenga3195e82006-01-12 22:54:21 +00004284
Evan Cheng0db9fe62006-04-25 20:13:52 +00004285SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4286 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4287 Op.getOperand(0).getValueType() >= MVT::i16 &&
4288 "Unknown SINT_TO_FP to lower!");
4289
4290 SDOperand Result;
4291 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4292 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4293 MachineFunction &MF = DAG.getMachineFunction();
4294 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4295 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00004296 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004297 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004298
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004299 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004300 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
4301 return Result;
4302 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004303 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00004304 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
4305 Subtarget->is64Bit())
4306 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004307
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004309 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004310 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
4311 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004312 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004313 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4314 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004315 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004316 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004317 Ops.push_back(Chain);
4318 Ops.push_back(StackSlot);
4319 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004320 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004321 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004322
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004323 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004324 Chain = Result.getValue(1);
4325 SDOperand InFlag = Result.getValue(2);
4326
4327 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4328 // shouldn't be necessary except that RFP cannot be live across
4329 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004330 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004331 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004332 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004333 Tys = DAG.getVTList(MVT::Other);
4334 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004335 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004336 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004337 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 Ops.push_back(DAG.getValueType(Op.getValueType()));
4339 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004340 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00004341 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004342 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004343
Evan Cheng0db9fe62006-04-25 20:13:52 +00004344 return Result;
4345}
4346
Chris Lattner27a6c732007-11-24 07:07:01 +00004347std::pair<SDOperand,SDOperand> X86TargetLowering::
4348FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4350 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004351
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004352 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004353 if (Op.getValueType() == MVT::i32 &&
4354 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
Chris Lattner27a6c732007-11-24 07:07:01 +00004355 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004356 if (Op.getValueType() == MVT::i32 &&
4357 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Chris Lattner27a6c732007-11-24 07:07:01 +00004358 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004359 if (Subtarget->is64Bit() &&
4360 Op.getValueType() == MVT::i64 &&
4361 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004362 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004363
Evan Cheng87c89352007-10-15 20:11:21 +00004364 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4365 // stack slot.
4366 MachineFunction &MF = DAG.getMachineFunction();
4367 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4368 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4369 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 unsigned Opc;
4371 switch (Op.getValueType()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004372 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4373 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4374 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4375 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004376 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004377
Evan Cheng0db9fe62006-04-25 20:13:52 +00004378 SDOperand Chain = DAG.getEntryNode();
4379 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004380 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
4381 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004382 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00004383 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004384 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004385 SDOperand Ops[] = {
4386 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4387 };
4388 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389 Chain = Value.getValue(1);
4390 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4391 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4392 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004393
Evan Cheng0db9fe62006-04-25 20:13:52 +00004394 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004395 SDOperand Ops[] = { Chain, Value, StackSlot };
4396 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004397
Chris Lattner27a6c732007-11-24 07:07:01 +00004398 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399}
4400
Chris Lattner27a6c732007-11-24 07:07:01 +00004401SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004402 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4403 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4404 if (FIST.Val == 0) return SDOperand();
4405
4406 // Load the result.
4407 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4408}
4409
4410SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4411 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4412 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4413 if (FIST.Val == 0) return 0;
4414
4415 // Return an i64 load from the stack slot.
4416 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4417
4418 // Use a MERGE_VALUES node to drop the chain result value.
4419 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4420}
4421
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4423 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004424 MVT::ValueType EltVT = VT;
4425 if (MVT::isVector(VT))
4426 EltVT = MVT::getVectorElementType(VT);
4427 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004428 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004429 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004430 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004431 CV.push_back(C);
4432 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004434 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004435 CV.push_back(C);
4436 CV.push_back(C);
4437 CV.push_back(C);
4438 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004439 }
Dan Gohmand3006222007-07-27 17:16:43 +00004440 Constant *C = ConstantVector::get(CV);
4441 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4442 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4443 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004444 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4445}
4446
4447SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4448 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004449 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004450 unsigned EltNum = 1;
4451 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004452 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004453 EltNum = MVT::getVectorNumElements(VT);
4454 }
Dan Gohman20382522007-07-10 00:05:58 +00004455 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004456 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004457 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004458 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004459 CV.push_back(C);
4460 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004461 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004462 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004463 CV.push_back(C);
4464 CV.push_back(C);
4465 CV.push_back(C);
4466 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004467 }
Dan Gohmand3006222007-07-27 17:16:43 +00004468 Constant *C = ConstantVector::get(CV);
4469 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4470 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4471 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004472 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004473 return DAG.getNode(ISD::BIT_CONVERT, VT,
4474 DAG.getNode(ISD::XOR, MVT::v2i64,
4475 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4476 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4477 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004478 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4479 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004480}
4481
Evan Cheng68c47cb2007-01-05 07:55:56 +00004482SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004483 SDOperand Op0 = Op.getOperand(0);
4484 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004485 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004486 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004487 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004488
4489 // If second operand is smaller, extend it first.
4490 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4491 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4492 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004493 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004494 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004495 // And if it is bigger, shrink it first.
4496 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4497 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1);
4498 SrcVT = VT;
4499 SrcTy = MVT::getTypeForValueType(SrcVT);
4500 }
4501
4502 // At this point the operands and the result should have the same
4503 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004504
Evan Cheng68c47cb2007-01-05 07:55:56 +00004505 // First get the sign bit of second operand.
4506 std::vector<Constant*> CV;
4507 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004508 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4509 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004510 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004511 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4512 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4513 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4514 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004515 }
Dan Gohmand3006222007-07-27 17:16:43 +00004516 Constant *C = ConstantVector::get(CV);
4517 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4518 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4519 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004520 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004521
4522 // Shift sign bit right or left if the two operands have different types.
4523 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4524 // Op0 is MVT::f32, Op1 is MVT::f64.
4525 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4526 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4527 DAG.getConstant(32, MVT::i32));
4528 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4529 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4530 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004531 }
4532
Evan Cheng73d6cf12007-01-05 21:37:56 +00004533 // Clear first operand sign bit.
4534 CV.clear();
4535 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004536 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4537 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004538 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004539 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4540 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4541 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4542 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004543 }
Dan Gohmand3006222007-07-27 17:16:43 +00004544 C = ConstantVector::get(CV);
4545 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4546 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4547 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004548 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4549
4550 // Or the value with the sign bit.
4551 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004552}
4553
Evan Chenge5f62042007-09-29 00:00:36 +00004554SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004555 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004556 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004557 SDOperand Op0 = Op.getOperand(0);
4558 SDOperand Op1 = Op.getOperand(1);
4559 SDOperand CC = Op.getOperand(2);
4560 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4561 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4562 unsigned X86CC;
4563
Evan Cheng0488db92007-09-25 01:57:46 +00004564 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004565 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004566 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4567 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004568 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004569 }
Evan Cheng0488db92007-09-25 01:57:46 +00004570
4571 assert(isFP && "Illegal integer SetCC!");
4572
Evan Chenge5f62042007-09-29 00:00:36 +00004573 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004574 switch (SetCCOpcode) {
4575 default: assert(false && "Illegal floating point SetCC!");
4576 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004577 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004578 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004579 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004580 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4581 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4582 }
4583 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004584 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004585 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004586 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004587 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4588 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4589 }
4590 }
4591}
4592
4593
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004595 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004596 SDOperand Cond = Op.getOperand(0);
4597 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004598
Evan Cheng734503b2006-09-11 02:19:56 +00004599 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004600 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004601
Evan Cheng3f41d662007-10-08 22:16:29 +00004602 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4603 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004604 if (Cond.getOpcode() == X86ISD::SETCC) {
4605 CC = Cond.getOperand(0);
4606
Evan Cheng734503b2006-09-11 02:19:56 +00004607 SDOperand Cmp = Cond.getOperand(1);
4608 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004609 MVT::ValueType VT = Op.getValueType();
4610 bool IllegalFPCMov = false;
4611 if (VT == MVT::f32 && !X86ScalarSSEf32)
4612 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4613 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4614 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesenc274f542007-10-16 18:09:08 +00004615 else if (VT == MVT::f80)
4616 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chenge5f62042007-09-29 00:00:36 +00004617 if ((Opc == X86ISD::CMP ||
4618 Opc == X86ISD::COMI ||
4619 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004620 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004621 addTest = false;
4622 }
4623 }
4624
4625 if (addTest) {
4626 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004627 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004628 }
4629
4630 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4631 MVT::Flag);
4632 SmallVector<SDOperand, 4> Ops;
4633 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4634 // condition is true.
4635 Ops.push_back(Op.getOperand(2));
4636 Ops.push_back(Op.getOperand(1));
4637 Ops.push_back(CC);
4638 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004639 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004640}
4641
Evan Cheng0db9fe62006-04-25 20:13:52 +00004642SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004643 bool addTest = true;
4644 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004645 SDOperand Cond = Op.getOperand(1);
4646 SDOperand Dest = Op.getOperand(2);
4647 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004648
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004650 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004651
Evan Cheng3f41d662007-10-08 22:16:29 +00004652 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4653 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004654 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004655 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004656
Evan Cheng734503b2006-09-11 02:19:56 +00004657 SDOperand Cmp = Cond.getOperand(1);
4658 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004659 if (Opc == X86ISD::CMP ||
4660 Opc == X86ISD::COMI ||
4661 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004662 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004663 addTest = false;
4664 }
4665 }
4666
4667 if (addTest) {
4668 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004669 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004670 }
Evan Chenge5f62042007-09-29 00:00:36 +00004671 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004672 Chain, Op.getOperand(2), CC, Cond);
4673}
4674
Evan Cheng32fe1032006-05-25 00:59:30 +00004675SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004676 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4677 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004678
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004679 if (Subtarget->is64Bit())
4680 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4681 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4682 else
4683 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00004684 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004685 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004686 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004687 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00004688 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004689 if (isTailCall && PerformTailCallOpt)
4690 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4691 else
4692 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004693 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004694 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004695 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004696 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004697 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004698 }
Evan Cheng32fe1032006-05-25 00:59:30 +00004699}
4700
Anton Korobeynikove060b532007-04-17 19:34:00 +00004701
4702// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4703// Calls to _alloca is needed to probe the stack when allocating more than 4k
4704// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4705// that the guard pages used by the OS virtual memory manager are allocated in
4706// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004707SDOperand
4708X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4709 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004710 assert(Subtarget->isTargetCygMing() &&
4711 "This should be used only on Cygwin/Mingw targets");
4712
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004713 // Get the inputs.
4714 SDOperand Chain = Op.getOperand(0);
4715 SDOperand Size = Op.getOperand(1);
4716 // FIXME: Ensure alignment here
4717
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004718 SDOperand Flag;
4719
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004720 MVT::ValueType IntPtr = getPointerTy();
4721 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004722
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004723 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4724 Flag = Chain.getValue(1);
4725
4726 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4727 SDOperand Ops[] = { Chain,
4728 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4729 DAG.getRegister(X86::EAX, IntPtr),
4730 Flag };
4731 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4732 Flag = Chain.getValue(1);
4733
4734 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004735
4736 std::vector<MVT::ValueType> Tys;
4737 Tys.push_back(SPTy);
4738 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004739 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4740 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004741}
4742
Evan Cheng1bc78042006-04-26 01:20:17 +00004743SDOperand
4744X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004745 MachineFunction &MF = DAG.getMachineFunction();
4746 const Function* Fn = MF.getFunction();
4747 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004748 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004749 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00004750 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00004751
Evan Cheng25caf632006-05-23 21:06:34 +00004752 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004753 if (Subtarget->is64Bit())
4754 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004755 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004756 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004757 default:
4758 assert(0 && "Unsupported calling convention");
4759 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004760 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004761 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004762 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004763 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004764 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004765 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004766 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004767 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004768 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00004769 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004770 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004771}
4772
Evan Cheng0db9fe62006-04-25 20:13:52 +00004773SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4774 SDOperand InFlag(0, 0);
4775 SDOperand Chain = Op.getOperand(0);
4776 unsigned Align =
4777 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4778 if (Align == 0) Align = 1;
4779
4780 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004781 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004782 // The libc version is likely to be faster for these cases. It can use the
4783 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004784 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004785 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004787 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004788 TargetLowering::ArgListTy Args;
4789 TargetLowering::ArgListEntry Entry;
4790 Entry.Node = Op.getOperand(1);
4791 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004792 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004793 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004794 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4795 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004796 Args.push_back(Entry);
4797 Entry.Node = Op.getOperand(3);
4798 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004799 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004800 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4802 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004803 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004804
Evan Cheng0db9fe62006-04-25 20:13:52 +00004805 MVT::ValueType AVT;
4806 SDOperand Count;
4807 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4808 unsigned BytesLeft = 0;
4809 bool TwoRepStos = false;
4810 if (ValC) {
4811 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004812 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004813
Evan Cheng0db9fe62006-04-25 20:13:52 +00004814 // If the value is a constant, then we can potentially use larger sets.
4815 switch (Align & 3) {
4816 case 2: // WORD aligned
4817 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004818 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004819 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004820 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004821 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004822 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004823 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824 Val = (Val << 8) | Val;
4825 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004826 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4827 AVT = MVT::i64;
4828 ValReg = X86::RAX;
4829 Val = (Val << 32) | Val;
4830 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 break;
4832 default: // Byte aligned
4833 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004834 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004835 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004837 }
4838
Evan Cheng25ab6902006-09-08 06:48:29 +00004839 if (AVT > MVT::i8) {
4840 if (I) {
4841 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4842 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4843 BytesLeft = I->getValue() % UBytes;
4844 } else {
4845 assert(AVT >= MVT::i32 &&
4846 "Do not use rep;stos if not at least DWORD aligned");
4847 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4848 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4849 TwoRepStos = true;
4850 }
4851 }
4852
Evan Cheng0db9fe62006-04-25 20:13:52 +00004853 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4854 InFlag);
4855 InFlag = Chain.getValue(1);
4856 } else {
4857 AVT = MVT::i8;
4858 Count = Op.getOperand(3);
4859 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4860 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004861 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004862
Evan Cheng25ab6902006-09-08 06:48:29 +00004863 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4864 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004865 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004866 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4867 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004868 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004869
Chris Lattnerd96d0722007-02-25 06:40:16 +00004870 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004871 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004872 Ops.push_back(Chain);
4873 Ops.push_back(DAG.getValueType(AVT));
4874 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004875 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004876
Evan Cheng0db9fe62006-04-25 20:13:52 +00004877 if (TwoRepStos) {
4878 InFlag = Chain.getValue(1);
4879 Count = Op.getOperand(3);
4880 MVT::ValueType CVT = Count.getValueType();
4881 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004882 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4883 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4884 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004885 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004886 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004887 Ops.clear();
4888 Ops.push_back(Chain);
4889 Ops.push_back(DAG.getValueType(MVT::i8));
4890 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004891 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004892 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004893 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004894 SDOperand Value;
4895 unsigned Val = ValC->getValue() & 255;
4896 unsigned Offset = I->getValue() - BytesLeft;
4897 SDOperand DstAddr = Op.getOperand(1);
4898 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004899 if (BytesLeft >= 4) {
4900 Val = (Val << 8) | Val;
4901 Val = (Val << 16) | Val;
4902 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004903 Chain = DAG.getStore(Chain, Value,
4904 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4905 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004906 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004907 BytesLeft -= 4;
4908 Offset += 4;
4909 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004910 if (BytesLeft >= 2) {
4911 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004912 Chain = DAG.getStore(Chain, Value,
4913 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4914 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004915 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004916 BytesLeft -= 2;
4917 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004918 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004919 if (BytesLeft == 1) {
4920 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004921 Chain = DAG.getStore(Chain, Value,
4922 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4923 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004924 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004925 }
Evan Cheng386031a2006-03-24 07:29:27 +00004926 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004927
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 return Chain;
4929}
Evan Cheng11e15b32006-04-03 20:53:28 +00004930
Rafael Espindola068317b2007-09-28 12:53:01 +00004931SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4932 SDOperand Dest,
4933 SDOperand Source,
4934 unsigned Size,
4935 unsigned Align,
4936 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004937 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004939 switch (Align & 3) {
4940 case 2: // WORD aligned
4941 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004942 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004943 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004944 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004945 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4946 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004947 break;
4948 default: // Byte aligned
4949 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950 break;
4951 }
4952
Rafael Espindola068317b2007-09-28 12:53:01 +00004953 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4954 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4955 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004956
Evan Cheng0db9fe62006-04-25 20:13:52 +00004957 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004958 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4959 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004960 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004961 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004962 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004963 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004964 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004965 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004966 InFlag = Chain.getValue(1);
4967
Chris Lattnerd96d0722007-02-25 06:40:16 +00004968 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004969 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004970 Ops.push_back(Chain);
4971 Ops.push_back(DAG.getValueType(AVT));
4972 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004973 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004974
Rafael Espindola068317b2007-09-28 12:53:01 +00004975 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004976 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004977 unsigned Offset = Size - BytesLeft;
4978 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004979 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004980 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004981 MVT::ValueType SrcVT = SrcAddr.getValueType();
4982 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004983 if (BytesLeft >= 4) {
4984 Value = DAG.getLoad(MVT::i32, Chain,
4985 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4986 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004987 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004988 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004989 Chain = DAG.getStore(Chain, Value,
4990 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4991 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004992 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004993 BytesLeft -= 4;
4994 Offset += 4;
4995 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004996 if (BytesLeft >= 2) {
4997 Value = DAG.getLoad(MVT::i16, Chain,
4998 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4999 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00005000 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005001 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00005002 Chain = DAG.getStore(Chain, Value,
5003 DAG.getNode(ISD::ADD, DstVT, DstAddr,
5004 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005005 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005006 BytesLeft -= 2;
5007 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00005008 }
5009
Evan Cheng0db9fe62006-04-25 20:13:52 +00005010 if (BytesLeft == 1) {
5011 Value = DAG.getLoad(MVT::i8, Chain,
5012 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
5013 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00005014 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005015 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00005016 Chain = DAG.getStore(Chain, Value,
5017 DAG.getNode(ISD::ADD, DstVT, DstAddr,
5018 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005019 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005020 }
Evan Chengb067a1e2006-03-31 19:22:53 +00005021 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005022
5023 return Chain;
5024}
5025
Chris Lattner27a6c732007-11-24 07:07:01 +00005026/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5027SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005028 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00005029 SDOperand TheChain = N->getOperand(0);
5030 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005031 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005032 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5033 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5034 MVT::i64, rax.getValue(2));
5035 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005036 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00005037 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005038 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005039 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005040
5041 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner27a6c732007-11-24 07:07:01 +00005042 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005043 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005044
Chris Lattner27a6c732007-11-24 07:07:01 +00005045 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5046 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5047 MVT::i32, eax.getValue(2));
5048 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5049 SDOperand Ops[] = { eax, edx };
5050 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5051
5052 // Use a MERGE_VALUES to return the value and chain.
5053 Ops[1] = edx.getValue(1);
5054 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5055 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056}
5057
5058SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005059 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
5060
Evan Cheng25ab6902006-09-08 06:48:29 +00005061 if (!Subtarget->is64Bit()) {
5062 // vastart just stores the address of the VarArgsFrameIndex slot into the
5063 // memory location argument.
5064 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005065 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
5066 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005067 }
5068
5069 // __va_list_tag:
5070 // gp_offset (0 - 6 * 8)
5071 // fp_offset (48 - 48 + 8 * 16)
5072 // overflow_arg_area (point to parameters coming in memory).
5073 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00005074 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00005075 SDOperand FIN = Op.getOperand(1);
5076 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00005077 SDOperand Store = DAG.getStore(Op.getOperand(0),
5078 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005079 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005080 MemOps.push_back(Store);
5081
5082 // Store fp_offset
5083 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5084 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00005085 Store = DAG.getStore(Op.getOperand(0),
5086 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005087 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005088 MemOps.push_back(Store);
5089
5090 // Store ptr to overflow_arg_area
5091 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5092 DAG.getConstant(4, getPointerTy()));
5093 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005094 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
5095 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005096 MemOps.push_back(Store);
5097
5098 // Store ptr to reg_save_area.
5099 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
5100 DAG.getConstant(8, getPointerTy()));
5101 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00005102 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
5103 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00005104 MemOps.push_back(Store);
5105 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005106}
5107
Evan Chengae642192007-03-02 23:16:35 +00005108SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5109 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5110 SDOperand Chain = Op.getOperand(0);
5111 SDOperand DstPtr = Op.getOperand(1);
5112 SDOperand SrcPtr = Op.getOperand(2);
5113 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
5114 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
5115
5116 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
5117 SrcSV->getValue(), SrcSV->getOffset());
5118 Chain = SrcPtr.getValue(1);
5119 for (unsigned i = 0; i < 3; ++i) {
5120 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
5121 SrcSV->getValue(), SrcSV->getOffset());
5122 Chain = Val.getValue(1);
5123 Chain = DAG.getStore(Chain, Val, DstPtr,
5124 DstSV->getValue(), DstSV->getOffset());
5125 if (i == 2)
5126 break;
5127 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
5128 DAG.getConstant(8, getPointerTy()));
5129 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
5130 DAG.getConstant(8, getPointerTy()));
5131 }
5132 return Chain;
5133}
5134
Evan Cheng0db9fe62006-04-25 20:13:52 +00005135SDOperand
5136X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5137 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5138 switch (IntNo) {
5139 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00005140 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005141 case Intrinsic::x86_sse_comieq_ss:
5142 case Intrinsic::x86_sse_comilt_ss:
5143 case Intrinsic::x86_sse_comile_ss:
5144 case Intrinsic::x86_sse_comigt_ss:
5145 case Intrinsic::x86_sse_comige_ss:
5146 case Intrinsic::x86_sse_comineq_ss:
5147 case Intrinsic::x86_sse_ucomieq_ss:
5148 case Intrinsic::x86_sse_ucomilt_ss:
5149 case Intrinsic::x86_sse_ucomile_ss:
5150 case Intrinsic::x86_sse_ucomigt_ss:
5151 case Intrinsic::x86_sse_ucomige_ss:
5152 case Intrinsic::x86_sse_ucomineq_ss:
5153 case Intrinsic::x86_sse2_comieq_sd:
5154 case Intrinsic::x86_sse2_comilt_sd:
5155 case Intrinsic::x86_sse2_comile_sd:
5156 case Intrinsic::x86_sse2_comigt_sd:
5157 case Intrinsic::x86_sse2_comige_sd:
5158 case Intrinsic::x86_sse2_comineq_sd:
5159 case Intrinsic::x86_sse2_ucomieq_sd:
5160 case Intrinsic::x86_sse2_ucomilt_sd:
5161 case Intrinsic::x86_sse2_ucomile_sd:
5162 case Intrinsic::x86_sse2_ucomigt_sd:
5163 case Intrinsic::x86_sse2_ucomige_sd:
5164 case Intrinsic::x86_sse2_ucomineq_sd: {
5165 unsigned Opc = 0;
5166 ISD::CondCode CC = ISD::SETCC_INVALID;
5167 switch (IntNo) {
5168 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005169 case Intrinsic::x86_sse_comieq_ss:
5170 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005171 Opc = X86ISD::COMI;
5172 CC = ISD::SETEQ;
5173 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005174 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005175 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176 Opc = X86ISD::COMI;
5177 CC = ISD::SETLT;
5178 break;
5179 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005180 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005181 Opc = X86ISD::COMI;
5182 CC = ISD::SETLE;
5183 break;
5184 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005185 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186 Opc = X86ISD::COMI;
5187 CC = ISD::SETGT;
5188 break;
5189 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005190 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 Opc = X86ISD::COMI;
5192 CC = ISD::SETGE;
5193 break;
5194 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005195 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005196 Opc = X86ISD::COMI;
5197 CC = ISD::SETNE;
5198 break;
5199 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005200 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005201 Opc = X86ISD::UCOMI;
5202 CC = ISD::SETEQ;
5203 break;
5204 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005205 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005206 Opc = X86ISD::UCOMI;
5207 CC = ISD::SETLT;
5208 break;
5209 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005210 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211 Opc = X86ISD::UCOMI;
5212 CC = ISD::SETLE;
5213 break;
5214 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005215 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005216 Opc = X86ISD::UCOMI;
5217 CC = ISD::SETGT;
5218 break;
5219 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005220 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005221 Opc = X86ISD::UCOMI;
5222 CC = ISD::SETGE;
5223 break;
5224 case Intrinsic::x86_sse_ucomineq_ss:
5225 case Intrinsic::x86_sse2_ucomineq_sd:
5226 Opc = X86ISD::UCOMI;
5227 CC = ISD::SETNE;
5228 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005229 }
Evan Cheng734503b2006-09-11 02:19:56 +00005230
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00005232 SDOperand LHS = Op.getOperand(1);
5233 SDOperand RHS = Op.getOperand(2);
5234 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005235
Evan Chenge5f62042007-09-29 00:00:36 +00005236 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5237 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5238 DAG.getConstant(X86CC, MVT::i8), Cond);
5239 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005240 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005241 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005242}
Evan Cheng72261582005-12-20 06:22:03 +00005243
Nate Begemanbcc5f362007-01-29 22:58:52 +00005244SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5245 // Depths > 0 not supported yet!
5246 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5247 return SDOperand();
5248
5249 // Just load the return address
5250 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5251 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5252}
5253
5254SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5255 // Depths > 0 not supported yet!
5256 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5257 return SDOperand();
5258
5259 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5260 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5261 DAG.getConstant(4, getPointerTy()));
5262}
5263
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005264SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5265 SelectionDAG &DAG) {
5266 // Is not yet supported on x86-64
5267 if (Subtarget->is64Bit())
5268 return SDOperand();
5269
5270 return DAG.getConstant(8, getPointerTy());
5271}
5272
5273SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5274{
5275 assert(!Subtarget->is64Bit() &&
5276 "Lowering of eh_return builtin is not supported yet on x86-64");
5277
5278 MachineFunction &MF = DAG.getMachineFunction();
5279 SDOperand Chain = Op.getOperand(0);
5280 SDOperand Offset = Op.getOperand(1);
5281 SDOperand Handler = Op.getOperand(2);
5282
5283 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5284 getPointerTy());
5285
5286 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5287 DAG.getConstant(-4UL, getPointerTy()));
5288 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5289 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5290 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5291 MF.addLiveOut(X86::ECX);
5292
5293 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5294 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5295}
5296
Duncan Sandsb116fac2007-07-27 20:02:49 +00005297SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5298 SelectionDAG &DAG) {
5299 SDOperand Root = Op.getOperand(0);
5300 SDOperand Trmp = Op.getOperand(1); // trampoline
5301 SDOperand FPtr = Op.getOperand(2); // nested function
5302 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5303
5304 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
5305
5306 if (Subtarget->is64Bit()) {
5307 return SDOperand(); // not yet supported
5308 } else {
5309 Function *Func = (Function *)
5310 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5311 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005312 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005313
5314 switch (CC) {
5315 default:
5316 assert(0 && "Unsupported calling convention");
5317 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005318 case CallingConv::X86_StdCall: {
5319 // Pass 'nest' parameter in ECX.
5320 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005321 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005322
5323 // Check that ECX wasn't needed by an 'inreg' parameter.
5324 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00005325 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005326
5327 if (Attrs && !Func->isVarArg()) {
5328 unsigned InRegCount = 0;
5329 unsigned Idx = 1;
5330
5331 for (FunctionType::param_iterator I = FTy->param_begin(),
5332 E = FTy->param_end(); I != E; ++I, ++Idx)
5333 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5334 // FIXME: should only count parameters that are lowered to integers.
5335 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5336
5337 if (InRegCount > 2) {
5338 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5339 abort();
5340 }
5341 }
5342 break;
5343 }
5344 case CallingConv::X86_FastCall:
5345 // Pass 'nest' parameter in EAX.
5346 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005347 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005348 break;
5349 }
5350
Duncan Sandsee465742007-08-29 19:01:20 +00005351 const X86InstrInfo *TII =
5352 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5353
Duncan Sandsb116fac2007-07-27 20:02:49 +00005354 SDOperand OutChains[4];
5355 SDOperand Addr, Disp;
5356
5357 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5358 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5359
Duncan Sandsee465742007-08-29 19:01:20 +00005360 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Chris Lattner48b01332007-12-16 20:26:54 +00005361 unsigned char N86Reg = ((X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005362 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00005363 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5364
5365 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5366 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5367 TrmpSV->getOffset() + 1, false, 1);
5368
Duncan Sandsee465742007-08-29 19:01:20 +00005369 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005370 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5371 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5372 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5373
5374 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5375 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5376 TrmpSV->getOffset() + 6, false, 1);
5377
Duncan Sandsf7331b32007-09-11 14:10:23 +00005378 SDOperand Ops[] =
5379 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5380 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005381 }
5382}
5383
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005384SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5385 /*
5386 The rounding mode is in bits 11:10 of FPSR, and has the following
5387 settings:
5388 00 Round to nearest
5389 01 Round to -inf
5390 10 Round to +inf
5391 11 Round to 0
5392
5393 FLT_ROUNDS, on the other hand, expects the following:
5394 -1 Undefined
5395 0 Round to 0
5396 1 Round to nearest
5397 2 Round to +inf
5398 3 Round to -inf
5399
5400 To perform the conversion, we do:
5401 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5402 */
5403
5404 MachineFunction &MF = DAG.getMachineFunction();
5405 const TargetMachine &TM = MF.getTarget();
5406 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5407 unsigned StackAlignment = TFI.getStackAlignment();
5408 MVT::ValueType VT = Op.getValueType();
5409
5410 // Save FP Control Word to stack slot
5411 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5412 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5413
5414 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5415 DAG.getEntryNode(), StackSlot);
5416
5417 // Load FP Control Word from stack slot
5418 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5419
5420 // Transform as necessary
5421 SDOperand CWD1 =
5422 DAG.getNode(ISD::SRL, MVT::i16,
5423 DAG.getNode(ISD::AND, MVT::i16,
5424 CWD, DAG.getConstant(0x800, MVT::i16)),
5425 DAG.getConstant(11, MVT::i8));
5426 SDOperand CWD2 =
5427 DAG.getNode(ISD::SRL, MVT::i16,
5428 DAG.getNode(ISD::AND, MVT::i16,
5429 CWD, DAG.getConstant(0x400, MVT::i16)),
5430 DAG.getConstant(9, MVT::i8));
5431
5432 SDOperand RetVal =
5433 DAG.getNode(ISD::AND, MVT::i16,
5434 DAG.getNode(ISD::ADD, MVT::i16,
5435 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5436 DAG.getConstant(1, MVT::i16)),
5437 DAG.getConstant(3, MVT::i16));
5438
5439
5440 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5441 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5442}
5443
Evan Cheng18efe262007-12-14 02:13:44 +00005444SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5445 MVT::ValueType VT = Op.getValueType();
5446 MVT::ValueType OpVT = VT;
5447 unsigned NumBits = MVT::getSizeInBits(VT);
5448
5449 Op = Op.getOperand(0);
5450 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005451 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005452 OpVT = MVT::i32;
5453 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5454 }
Evan Cheng18efe262007-12-14 02:13:44 +00005455
Evan Cheng152804e2007-12-14 08:30:15 +00005456 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5457 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5458 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5459
5460 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5461 SmallVector<SDOperand, 4> Ops;
5462 Ops.push_back(Op);
5463 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5464 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5465 Ops.push_back(Op.getValue(1));
5466 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5467
5468 // Finally xor with NumBits-1.
5469 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5470
Evan Cheng18efe262007-12-14 02:13:44 +00005471 if (VT == MVT::i8)
5472 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5473 return Op;
5474}
5475
5476SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5477 MVT::ValueType VT = Op.getValueType();
5478 MVT::ValueType OpVT = VT;
Evan Cheng152804e2007-12-14 08:30:15 +00005479 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng18efe262007-12-14 02:13:44 +00005480
5481 Op = Op.getOperand(0);
5482 if (VT == MVT::i8) {
5483 OpVT = MVT::i32;
5484 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5485 }
Evan Cheng152804e2007-12-14 08:30:15 +00005486
5487 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5488 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5489 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5490
5491 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5492 SmallVector<SDOperand, 4> Ops;
5493 Ops.push_back(Op);
5494 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5495 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5496 Ops.push_back(Op.getValue(1));
5497 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5498
Evan Cheng18efe262007-12-14 02:13:44 +00005499 if (VT == MVT::i8)
5500 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5501 return Op;
5502}
5503
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504/// LowerOperation - Provide custom lowering hooks for some operations.
5505///
5506SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5507 switch (Op.getOpcode()) {
5508 default: assert(0 && "Should not custom lower this!");
5509 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5510 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5511 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5512 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5513 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5514 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5515 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005516 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005517 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5518 case ISD::SHL_PARTS:
5519 case ISD::SRA_PARTS:
5520 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5521 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5522 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5523 case ISD::FABS: return LowerFABS(Op, DAG);
5524 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005525 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005526 case ISD::SETCC: return LowerSETCC(Op, DAG);
5527 case ISD::SELECT: return LowerSELECT(Op, DAG);
5528 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005529 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005530 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005532 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5534 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005535 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005536 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005537 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005538 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5539 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005540 case ISD::FRAME_TO_ARGS_OFFSET:
5541 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005542 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005543 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005544 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005545 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005546 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5547 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005548
5549 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5550 case ISD::READCYCLECOUNTER:
5551 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005552 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005553}
5554
5555/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5556SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5557 switch (N->getOpcode()) {
5558 default: assert(0 && "Should not custom lower this!");
5559 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5560 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5561 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005562}
5563
Evan Cheng72261582005-12-20 06:22:03 +00005564const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5565 switch (Opcode) {
5566 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005567 case X86ISD::BSF: return "X86ISD::BSF";
5568 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005569 case X86ISD::SHLD: return "X86ISD::SHLD";
5570 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005571 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005572 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005573 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005574 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005575 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005576 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005577 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5578 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5579 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005580 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005581 case X86ISD::FST: return "X86ISD::FST";
5582 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00005583 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005584 case X86ISD::CALL: return "X86ISD::CALL";
5585 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5586 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5587 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005588 case X86ISD::COMI: return "X86ISD::COMI";
5589 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005590 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005591 case X86ISD::CMOV: return "X86ISD::CMOV";
5592 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005593 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005594 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5595 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005596 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005597 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005598 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005599 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005600 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005601 case X86ISD::FMAX: return "X86ISD::FMAX";
5602 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005603 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5604 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005605 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5606 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005607 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005608 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005609 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng72261582005-12-20 06:22:03 +00005610 }
5611}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005612
Chris Lattnerc9addb72007-03-30 23:15:24 +00005613// isLegalAddressingMode - Return true if the addressing mode represented
5614// by AM is legal for this target, for a load/store of the specified type.
5615bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5616 const Type *Ty) const {
5617 // X86 supports extremely general addressing modes.
5618
5619 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5620 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5621 return false;
5622
5623 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005624 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005625 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5626 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005627
5628 // X86-64 only supports addr of globals in small code model.
5629 if (Subtarget->is64Bit()) {
5630 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5631 return false;
5632 // If lower 4G is not available, then we must use rip-relative addressing.
5633 if (AM.BaseOffs || AM.Scale > 1)
5634 return false;
5635 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005636 }
5637
5638 switch (AM.Scale) {
5639 case 0:
5640 case 1:
5641 case 2:
5642 case 4:
5643 case 8:
5644 // These scales always work.
5645 break;
5646 case 3:
5647 case 5:
5648 case 9:
5649 // These scales are formed with basereg+scalereg. Only accept if there is
5650 // no basereg yet.
5651 if (AM.HasBaseReg)
5652 return false;
5653 break;
5654 default: // Other stuff never works.
5655 return false;
5656 }
5657
5658 return true;
5659}
5660
5661
Evan Cheng2bd122c2007-10-26 01:56:11 +00005662bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5663 if (!Ty1->isInteger() || !Ty2->isInteger())
5664 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005665 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5666 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5667 if (NumBits1 <= NumBits2)
5668 return false;
5669 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005670}
5671
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005672bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5673 MVT::ValueType VT2) const {
5674 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5675 return false;
5676 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5677 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5678 if (NumBits1 <= NumBits2)
5679 return false;
5680 return Subtarget->is64Bit() || NumBits1 < 64;
5681}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005682
Evan Cheng60c07e12006-07-05 22:17:51 +00005683/// isShuffleMaskLegal - Targets can use this to indicate that they only
5684/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5685/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5686/// are assumed to be legal.
5687bool
5688X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5689 // Only do shuffles on 128-bit vector types for now.
5690 if (MVT::getSizeInBits(VT) == 64) return false;
5691 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005692 isIdentityMask(Mask.Val) ||
5693 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005694 isSplatMask(Mask.Val) ||
5695 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5696 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005697 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005698 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005699 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005700}
5701
5702bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5703 MVT::ValueType EVT,
5704 SelectionDAG &DAG) const {
5705 unsigned NumElts = BVOps.size();
5706 // Only do shuffles on 128-bit vector types for now.
5707 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5708 if (NumElts == 2) return true;
5709 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005710 return (isMOVLMask(&BVOps[0], 4) ||
5711 isCommutedMOVL(&BVOps[0], 4, true) ||
5712 isSHUFPMask(&BVOps[0], 4) ||
5713 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005714 }
5715 return false;
5716}
5717
5718//===----------------------------------------------------------------------===//
5719// X86 Scheduler Hooks
5720//===----------------------------------------------------------------------===//
5721
5722MachineBasicBlock *
5723X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5724 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005725 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005726 switch (MI->getOpcode()) {
5727 default: assert(false && "Unexpected instr type to insert");
5728 case X86::CMOV_FR32:
5729 case X86::CMOV_FR64:
5730 case X86::CMOV_V4F32:
5731 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005732 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005733 // To "insert" a SELECT_CC instruction, we actually have to insert the
5734 // diamond control-flow pattern. The incoming instruction knows the
5735 // destination vreg to set, the condition code register to branch on, the
5736 // true/false values to select between, and a branch opcode to use.
5737 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5738 ilist<MachineBasicBlock>::iterator It = BB;
5739 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005740
Evan Cheng60c07e12006-07-05 22:17:51 +00005741 // thisMBB:
5742 // ...
5743 // TrueVal = ...
5744 // cmpTY ccX, r1, r2
5745 // bCC copy1MBB
5746 // fallthrough --> copy0MBB
5747 MachineBasicBlock *thisMBB = BB;
5748 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5749 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005750 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005751 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005752 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005753 MachineFunction *F = BB->getParent();
5754 F->getBasicBlockList().insert(It, copy0MBB);
5755 F->getBasicBlockList().insert(It, sinkMBB);
5756 // Update machine-CFG edges by first adding all successors of the current
5757 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005758 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005759 e = BB->succ_end(); i != e; ++i)
5760 sinkMBB->addSuccessor(*i);
5761 // Next, remove all successors of the current block, and add the true
5762 // and fallthrough blocks as its successors.
5763 while(!BB->succ_empty())
5764 BB->removeSuccessor(BB->succ_begin());
5765 BB->addSuccessor(copy0MBB);
5766 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005767
Evan Cheng60c07e12006-07-05 22:17:51 +00005768 // copy0MBB:
5769 // %FalseValue = ...
5770 // # fallthrough to sinkMBB
5771 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005772
Evan Cheng60c07e12006-07-05 22:17:51 +00005773 // Update machine-CFG edges
5774 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005775
Evan Cheng60c07e12006-07-05 22:17:51 +00005776 // sinkMBB:
5777 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5778 // ...
5779 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005780 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005781 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5782 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5783
5784 delete MI; // The pseudo instruction is gone now.
5785 return BB;
5786 }
5787
Dale Johannesen849f2142007-07-03 00:53:03 +00005788 case X86::FP32_TO_INT16_IN_MEM:
5789 case X86::FP32_TO_INT32_IN_MEM:
5790 case X86::FP32_TO_INT64_IN_MEM:
5791 case X86::FP64_TO_INT16_IN_MEM:
5792 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005793 case X86::FP64_TO_INT64_IN_MEM:
5794 case X86::FP80_TO_INT16_IN_MEM:
5795 case X86::FP80_TO_INT32_IN_MEM:
5796 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005797 // Change the floating point control register to use "round towards zero"
5798 // mode when truncating to an integer value.
5799 MachineFunction *F = BB->getParent();
5800 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005801 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005802
5803 // Load the old value of the high byte of the control word...
5804 unsigned OldCW =
5805 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005806 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005807
5808 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005809 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5810 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005811
5812 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005813 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005814
5815 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005816 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5817 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005818
5819 // Get the X86 opcode to use.
5820 unsigned Opc;
5821 switch (MI->getOpcode()) {
5822 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005823 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5824 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5825 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5826 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5827 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5828 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005829 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5830 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5831 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005832 }
5833
5834 X86AddressMode AM;
5835 MachineOperand &Op = MI->getOperand(0);
5836 if (Op.isRegister()) {
5837 AM.BaseType = X86AddressMode::RegBase;
5838 AM.Base.Reg = Op.getReg();
5839 } else {
5840 AM.BaseType = X86AddressMode::FrameIndexBase;
5841 AM.Base.FrameIndex = Op.getFrameIndex();
5842 }
5843 Op = MI->getOperand(1);
5844 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005845 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005846 Op = MI->getOperand(2);
5847 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005848 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005849 Op = MI->getOperand(3);
5850 if (Op.isGlobalAddress()) {
5851 AM.GV = Op.getGlobal();
5852 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005853 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005854 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005855 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5856 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005857
5858 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005859 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005860
5861 delete MI; // The pseudo instruction is gone now.
5862 return BB;
5863 }
5864 }
5865}
5866
5867//===----------------------------------------------------------------------===//
5868// X86 Optimization Hooks
5869//===----------------------------------------------------------------------===//
5870
Nate Begeman368e18d2006-02-16 21:11:51 +00005871void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5872 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005873 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005874 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005875 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005876 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005877 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005878 assert((Opc >= ISD::BUILTIN_OP_END ||
5879 Opc == ISD::INTRINSIC_WO_CHAIN ||
5880 Opc == ISD::INTRINSIC_W_CHAIN ||
5881 Opc == ISD::INTRINSIC_VOID) &&
5882 "Should use MaskedValueIsZero if you don't know whether Op"
5883 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005884
Evan Cheng865f0602006-04-05 06:11:20 +00005885 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005886 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005887 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005888 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005889 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5890 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005891 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005892}
Chris Lattner259e97c2006-01-31 19:43:35 +00005893
Evan Cheng206ee9d2006-07-07 08:33:52 +00005894/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5895/// element of the result of the vector shuffle.
5896static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5897 MVT::ValueType VT = N->getValueType(0);
5898 SDOperand PermMask = N->getOperand(2);
5899 unsigned NumElems = PermMask.getNumOperands();
5900 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5901 i %= NumElems;
5902 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5903 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005904 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005905 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5906 SDOperand Idx = PermMask.getOperand(i);
5907 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005908 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005909 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5910 }
5911 return SDOperand();
5912}
5913
5914/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5915/// node is a GlobalAddress + an offset.
5916static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005917 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005918 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005919 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5920 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5921 return true;
5922 }
Evan Cheng0085a282006-11-30 21:55:46 +00005923 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005924 SDOperand N1 = N->getOperand(0);
5925 SDOperand N2 = N->getOperand(1);
5926 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5927 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5928 if (V) {
5929 Offset += V->getSignExtended();
5930 return true;
5931 }
5932 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5933 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5934 if (V) {
5935 Offset += V->getSignExtended();
5936 return true;
5937 }
5938 }
5939 }
5940 return false;
5941}
5942
5943/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5944/// + Dist * Size.
5945static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5946 MachineFrameInfo *MFI) {
5947 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5948 return false;
5949
5950 SDOperand Loc = N->getOperand(1);
5951 SDOperand BaseLoc = Base->getOperand(1);
5952 if (Loc.getOpcode() == ISD::FrameIndex) {
5953 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5954 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005955 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5956 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005957 int FS = MFI->getObjectSize(FI);
5958 int BFS = MFI->getObjectSize(BFI);
5959 if (FS != BFS || FS != Size) return false;
5960 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5961 } else {
5962 GlobalValue *GV1 = NULL;
5963 GlobalValue *GV2 = NULL;
5964 int64_t Offset1 = 0;
5965 int64_t Offset2 = 0;
5966 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5967 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5968 if (isGA1 && isGA2 && GV1 == GV2)
5969 return Offset1 == (Offset2 + Dist*Size);
5970 }
5971
5972 return false;
5973}
5974
Evan Cheng1e60c092006-07-10 21:37:44 +00005975static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5976 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005977 GlobalValue *GV;
5978 int64_t Offset;
5979 if (isGAPlusOffset(Base, GV, Offset))
5980 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5981 else {
5982 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005983 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005984 if (BFI < 0)
5985 // Fixed objects do not specify alignment, however the offsets are known.
5986 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5987 (MFI->getObjectOffset(BFI) % 16) == 0);
5988 else
5989 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005990 }
5991 return false;
5992}
5993
5994
5995/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5996/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5997/// if the load addresses are consecutive, non-overlapping, and in the right
5998/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005999static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
6000 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006001 MachineFunction &MF = DAG.getMachineFunction();
6002 MachineFrameInfo *MFI = MF.getFrameInfo();
6003 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00006004 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006005 SDOperand PermMask = N->getOperand(2);
6006 int NumElems = (int)PermMask.getNumOperands();
6007 SDNode *Base = NULL;
6008 for (int i = 0; i < NumElems; ++i) {
6009 SDOperand Idx = PermMask.getOperand(i);
6010 if (Idx.getOpcode() == ISD::UNDEF) {
6011 if (!Base) return SDOperand();
6012 } else {
6013 SDOperand Arg =
6014 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00006015 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00006016 return SDOperand();
6017 if (!Base)
6018 Base = Arg.Val;
6019 else if (!isConsecutiveLoad(Arg.Val, Base,
6020 i, MVT::getSizeInBits(EVT)/8,MFI))
6021 return SDOperand();
6022 }
6023 }
6024
Evan Cheng1e60c092006-07-10 21:37:44 +00006025 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00006026 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00006027 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00006028 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006029 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00006030 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00006031 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6032 LD->getSrcValueOffset(), LD->isVolatile(),
6033 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00006034 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006035}
6036
Chris Lattner83e6c992006-10-04 06:57:07 +00006037/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6038static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6039 const X86Subtarget *Subtarget) {
6040 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006041
Chris Lattner83e6c992006-10-04 06:57:07 +00006042 // If we have SSE[12] support, try to form min/max nodes.
6043 if (Subtarget->hasSSE2() &&
6044 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6045 if (Cond.getOpcode() == ISD::SETCC) {
6046 // Get the LHS/RHS of the select.
6047 SDOperand LHS = N->getOperand(1);
6048 SDOperand RHS = N->getOperand(2);
6049 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006050
Evan Cheng8ca29322006-11-10 21:43:37 +00006051 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006052 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006053 switch (CC) {
6054 default: break;
6055 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6056 case ISD::SETULE:
6057 case ISD::SETLE:
6058 if (!UnsafeFPMath) break;
6059 // FALL THROUGH.
6060 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6061 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006062 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006063 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006064
Chris Lattner1907a7b2006-10-05 04:11:26 +00006065 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6066 case ISD::SETUGT:
6067 case ISD::SETGT:
6068 if (!UnsafeFPMath) break;
6069 // FALL THROUGH.
6070 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6071 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006072 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006073 break;
6074 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006075 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006076 switch (CC) {
6077 default: break;
6078 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6079 case ISD::SETUGT:
6080 case ISD::SETGT:
6081 if (!UnsafeFPMath) break;
6082 // FALL THROUGH.
6083 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6084 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006085 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006086 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006087
Chris Lattner1907a7b2006-10-05 04:11:26 +00006088 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6089 case ISD::SETULE:
6090 case ISD::SETLE:
6091 if (!UnsafeFPMath) break;
6092 // FALL THROUGH.
6093 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6094 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006095 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006096 break;
6097 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006098 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006099
Evan Cheng8ca29322006-11-10 21:43:37 +00006100 if (Opcode)
6101 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006102 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006103
Chris Lattner83e6c992006-10-04 06:57:07 +00006104 }
6105
6106 return SDOperand();
6107}
6108
6109
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006110SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006111 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006112 SelectionDAG &DAG = DCI.DAG;
6113 switch (N->getOpcode()) {
6114 default: break;
6115 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00006116 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00006117 case ISD::SELECT:
6118 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006119 }
6120
6121 return SDOperand();
6122}
6123
Evan Cheng60c07e12006-07-05 22:17:51 +00006124//===----------------------------------------------------------------------===//
6125// X86 Inline Assembly Support
6126//===----------------------------------------------------------------------===//
6127
Chris Lattnerf4dff842006-07-11 02:54:03 +00006128/// getConstraintType - Given a constraint letter, return the type of
6129/// constraint it is for this target.
6130X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006131X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6132 if (Constraint.size() == 1) {
6133 switch (Constraint[0]) {
6134 case 'A':
6135 case 'r':
6136 case 'R':
6137 case 'l':
6138 case 'q':
6139 case 'Q':
6140 case 'x':
6141 case 'Y':
6142 return C_RegisterClass;
6143 default:
6144 break;
6145 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006146 }
Chris Lattner4234f572007-03-25 02:14:49 +00006147 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006148}
6149
Chris Lattner48884cd2007-08-25 00:47:38 +00006150/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6151/// vector. If it is invalid, don't add anything to Ops.
6152void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6153 char Constraint,
6154 std::vector<SDOperand>&Ops,
6155 SelectionDAG &DAG) {
6156 SDOperand Result(0, 0);
6157
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006158 switch (Constraint) {
6159 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006160 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006162 if (C->getValue() <= 31) {
6163 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6164 break;
6165 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006166 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006167 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006168 case 'N':
6169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006170 if (C->getValue() <= 255) {
6171 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6172 break;
6173 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006174 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006175 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006176 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006177 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006178 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6179 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6180 break;
6181 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006182
Chris Lattnerdc43a882007-05-03 16:52:29 +00006183 // If we are in non-pic codegen mode, we allow the address of a global (with
6184 // an optional displacement) to be used with 'i'.
6185 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6186 int64_t Offset = 0;
6187
6188 // Match either (GA) or (GA+C)
6189 if (GA) {
6190 Offset = GA->getOffset();
6191 } else if (Op.getOpcode() == ISD::ADD) {
6192 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6193 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6194 if (C && GA) {
6195 Offset = GA->getOffset()+C->getValue();
6196 } else {
6197 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6198 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6199 if (C && GA)
6200 Offset = GA->getOffset()+C->getValue();
6201 else
6202 C = 0, GA = 0;
6203 }
6204 }
6205
6206 if (GA) {
6207 // If addressing this global requires a load (e.g. in PIC mode), we can't
6208 // match.
6209 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6210 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006211 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006212
Chris Lattnerdc43a882007-05-03 16:52:29 +00006213 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6214 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006215 Result = Op;
6216 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006217 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006218
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006219 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006220 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006221 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006222 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006223
6224 if (Result.Val) {
6225 Ops.push_back(Result);
6226 return;
6227 }
6228 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006229}
6230
Chris Lattner259e97c2006-01-31 19:43:35 +00006231std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006232getRegClassForInlineAsmConstraint(const std::string &Constraint,
6233 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006234 if (Constraint.size() == 1) {
6235 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006236 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006237 default: break; // Unknown constraint letter
6238 case 'A': // EAX/EDX
6239 if (VT == MVT::i32 || VT == MVT::i64)
6240 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6241 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006242 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6243 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006244 if (VT == MVT::i32)
6245 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6246 else if (VT == MVT::i16)
6247 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6248 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006249 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006250 else if (VT == MVT::i64)
6251 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6252 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006253 }
6254 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006255
Chris Lattner1efa40f2006-02-22 00:56:39 +00006256 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006257}
Chris Lattnerf76d1802006-07-31 23:26:50 +00006258
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006259std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00006260X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6261 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00006262 // First, see if this is a constraint that directly corresponds to an LLVM
6263 // register class.
6264 if (Constraint.size() == 1) {
6265 // GCC Constraint Letters
6266 switch (Constraint[0]) {
6267 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006268 case 'r': // GENERAL_REGS
6269 case 'R': // LEGACY_REGS
6270 case 'l': // INDEX_REGS
6271 if (VT == MVT::i64 && Subtarget->is64Bit())
6272 return std::make_pair(0U, X86::GR64RegisterClass);
6273 if (VT == MVT::i32)
6274 return std::make_pair(0U, X86::GR32RegisterClass);
6275 else if (VT == MVT::i16)
6276 return std::make_pair(0U, X86::GR16RegisterClass);
6277 else if (VT == MVT::i8)
6278 return std::make_pair(0U, X86::GR8RegisterClass);
6279 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00006280 case 'y': // MMX_REGS if MMX allowed.
6281 if (!Subtarget->hasMMX()) break;
6282 return std::make_pair(0U, X86::VR64RegisterClass);
6283 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006284 case 'Y': // SSE_REGS if SSE2 allowed
6285 if (!Subtarget->hasSSE2()) break;
6286 // FALL THROUGH.
6287 case 'x': // SSE_REGS if SSE1 allowed
6288 if (!Subtarget->hasSSE1()) break;
6289
6290 switch (VT) {
6291 default: break;
6292 // Scalar SSE types.
6293 case MVT::f32:
6294 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00006295 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006296 case MVT::f64:
6297 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00006298 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006299 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00006300 case MVT::v16i8:
6301 case MVT::v8i16:
6302 case MVT::v4i32:
6303 case MVT::v2i64:
6304 case MVT::v4f32:
6305 case MVT::v2f64:
6306 return std::make_pair(0U, X86::VR128RegisterClass);
6307 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006308 break;
6309 }
6310 }
6311
Chris Lattnerf76d1802006-07-31 23:26:50 +00006312 // Use the default implementation in TargetLowering to convert the register
6313 // constraint into a member of a register class.
6314 std::pair<unsigned, const TargetRegisterClass*> Res;
6315 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006316
6317 // Not found as a standard register?
6318 if (Res.second == 0) {
6319 // GCC calls "st(0)" just plain "st".
6320 if (StringsEqualNoCase("{st}", Constraint)) {
6321 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006322 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006323 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006324
Chris Lattner1a60aa72006-10-31 19:42:44 +00006325 return Res;
6326 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006327
Chris Lattnerf76d1802006-07-31 23:26:50 +00006328 // Otherwise, check to see if this is a register class of the wrong value
6329 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6330 // turn into {ax},{dx}.
6331 if (Res.second->hasType(VT))
6332 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006333
Chris Lattnerf76d1802006-07-31 23:26:50 +00006334 // All of the single-register GCC register classes map their values onto
6335 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6336 // really want an 8-bit or 32-bit register, map to the appropriate register
6337 // class and return the appropriate register.
6338 if (Res.second != X86::GR16RegisterClass)
6339 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006340
Chris Lattnerf76d1802006-07-31 23:26:50 +00006341 if (VT == MVT::i8) {
6342 unsigned DestReg = 0;
6343 switch (Res.first) {
6344 default: break;
6345 case X86::AX: DestReg = X86::AL; break;
6346 case X86::DX: DestReg = X86::DL; break;
6347 case X86::CX: DestReg = X86::CL; break;
6348 case X86::BX: DestReg = X86::BL; break;
6349 }
6350 if (DestReg) {
6351 Res.first = DestReg;
6352 Res.second = Res.second = X86::GR8RegisterClass;
6353 }
6354 } else if (VT == MVT::i32) {
6355 unsigned DestReg = 0;
6356 switch (Res.first) {
6357 default: break;
6358 case X86::AX: DestReg = X86::EAX; break;
6359 case X86::DX: DestReg = X86::EDX; break;
6360 case X86::CX: DestReg = X86::ECX; break;
6361 case X86::BX: DestReg = X86::EBX; break;
6362 case X86::SI: DestReg = X86::ESI; break;
6363 case X86::DI: DestReg = X86::EDI; break;
6364 case X86::BP: DestReg = X86::EBP; break;
6365 case X86::SP: DestReg = X86::ESP; break;
6366 }
6367 if (DestReg) {
6368 Res.first = DestReg;
6369 Res.second = Res.second = X86::GR32RegisterClass;
6370 }
Evan Cheng25ab6902006-09-08 06:48:29 +00006371 } else if (VT == MVT::i64) {
6372 unsigned DestReg = 0;
6373 switch (Res.first) {
6374 default: break;
6375 case X86::AX: DestReg = X86::RAX; break;
6376 case X86::DX: DestReg = X86::RDX; break;
6377 case X86::CX: DestReg = X86::RCX; break;
6378 case X86::BX: DestReg = X86::RBX; break;
6379 case X86::SI: DestReg = X86::RSI; break;
6380 case X86::DI: DestReg = X86::RDI; break;
6381 case X86::BP: DestReg = X86::RBP; break;
6382 case X86::SP: DestReg = X86::RSP; break;
6383 }
6384 if (DestReg) {
6385 Res.first = DestReg;
6386 Res.second = Res.second = X86::GR64RegisterClass;
6387 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00006388 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006389
Chris Lattnerf76d1802006-07-31 23:26:50 +00006390 return Res;
6391}