blob: 8d0d884c8142c8638493ac5dbf3331471a157437 [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/Target/TargetData.h"
18#include "llvm/Target/TargetLoweringObjectFile.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetRegisterInfo.h"
21#include "llvm/Target/TargetSubtarget.h"
22#include "llvm/GlobalVariable.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineJumpTableInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/MathExtras.h"
Shih-wei Liaoe4454322010-04-07 12:21:42 -070031#include <ctype.h>
Shih-wei Liaoe264f622010-02-10 11:10:31 -080032using namespace llvm;
33
34namespace llvm {
35TLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
36 bool isLocal = GV->hasLocalLinkage();
37 bool isDeclaration = GV->isDeclaration();
38 // FIXME: what should we do for protected and internal visibility?
39 // For variables, is internal different from hidden?
40 bool isHidden = GV->hasHiddenVisibility();
41
42 if (reloc == Reloc::PIC_) {
43 if (isLocal || isHidden)
44 return TLSModel::LocalDynamic;
45 else
46 return TLSModel::GeneralDynamic;
47 } else {
48 if (!isDeclaration || isHidden)
49 return TLSModel::LocalExec;
50 else
51 return TLSModel::InitialExec;
52 }
53}
54}
55
56/// InitLibcallNames - Set default libcall names.
57///
58static void InitLibcallNames(const char **Names) {
59 Names[RTLIB::SHL_I16] = "__ashlhi3";
60 Names[RTLIB::SHL_I32] = "__ashlsi3";
61 Names[RTLIB::SHL_I64] = "__ashldi3";
62 Names[RTLIB::SHL_I128] = "__ashlti3";
63 Names[RTLIB::SRL_I16] = "__lshrhi3";
64 Names[RTLIB::SRL_I32] = "__lshrsi3";
65 Names[RTLIB::SRL_I64] = "__lshrdi3";
66 Names[RTLIB::SRL_I128] = "__lshrti3";
67 Names[RTLIB::SRA_I16] = "__ashrhi3";
68 Names[RTLIB::SRA_I32] = "__ashrsi3";
69 Names[RTLIB::SRA_I64] = "__ashrdi3";
70 Names[RTLIB::SRA_I128] = "__ashrti3";
71 Names[RTLIB::MUL_I8] = "__mulqi3";
72 Names[RTLIB::MUL_I16] = "__mulhi3";
73 Names[RTLIB::MUL_I32] = "__mulsi3";
74 Names[RTLIB::MUL_I64] = "__muldi3";
75 Names[RTLIB::MUL_I128] = "__multi3";
76 Names[RTLIB::SDIV_I8] = "__divqi3";
77 Names[RTLIB::SDIV_I16] = "__divhi3";
78 Names[RTLIB::SDIV_I32] = "__divsi3";
79 Names[RTLIB::SDIV_I64] = "__divdi3";
80 Names[RTLIB::SDIV_I128] = "__divti3";
81 Names[RTLIB::UDIV_I8] = "__udivqi3";
82 Names[RTLIB::UDIV_I16] = "__udivhi3";
83 Names[RTLIB::UDIV_I32] = "__udivsi3";
84 Names[RTLIB::UDIV_I64] = "__udivdi3";
85 Names[RTLIB::UDIV_I128] = "__udivti3";
86 Names[RTLIB::SREM_I8] = "__modqi3";
87 Names[RTLIB::SREM_I16] = "__modhi3";
88 Names[RTLIB::SREM_I32] = "__modsi3";
89 Names[RTLIB::SREM_I64] = "__moddi3";
90 Names[RTLIB::SREM_I128] = "__modti3";
91 Names[RTLIB::UREM_I8] = "__umodqi3";
92 Names[RTLIB::UREM_I16] = "__umodhi3";
93 Names[RTLIB::UREM_I32] = "__umodsi3";
94 Names[RTLIB::UREM_I64] = "__umoddi3";
95 Names[RTLIB::UREM_I128] = "__umodti3";
96 Names[RTLIB::NEG_I32] = "__negsi2";
97 Names[RTLIB::NEG_I64] = "__negdi2";
98 Names[RTLIB::ADD_F32] = "__addsf3";
99 Names[RTLIB::ADD_F64] = "__adddf3";
100 Names[RTLIB::ADD_F80] = "__addxf3";
101 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
102 Names[RTLIB::SUB_F32] = "__subsf3";
103 Names[RTLIB::SUB_F64] = "__subdf3";
104 Names[RTLIB::SUB_F80] = "__subxf3";
105 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
106 Names[RTLIB::MUL_F32] = "__mulsf3";
107 Names[RTLIB::MUL_F64] = "__muldf3";
108 Names[RTLIB::MUL_F80] = "__mulxf3";
109 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
110 Names[RTLIB::DIV_F32] = "__divsf3";
111 Names[RTLIB::DIV_F64] = "__divdf3";
112 Names[RTLIB::DIV_F80] = "__divxf3";
113 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
114 Names[RTLIB::REM_F32] = "fmodf";
115 Names[RTLIB::REM_F64] = "fmod";
116 Names[RTLIB::REM_F80] = "fmodl";
117 Names[RTLIB::REM_PPCF128] = "fmodl";
118 Names[RTLIB::POWI_F32] = "__powisf2";
119 Names[RTLIB::POWI_F64] = "__powidf2";
120 Names[RTLIB::POWI_F80] = "__powixf2";
121 Names[RTLIB::POWI_PPCF128] = "__powitf2";
122 Names[RTLIB::SQRT_F32] = "sqrtf";
123 Names[RTLIB::SQRT_F64] = "sqrt";
124 Names[RTLIB::SQRT_F80] = "sqrtl";
125 Names[RTLIB::SQRT_PPCF128] = "sqrtl";
126 Names[RTLIB::LOG_F32] = "logf";
127 Names[RTLIB::LOG_F64] = "log";
128 Names[RTLIB::LOG_F80] = "logl";
129 Names[RTLIB::LOG_PPCF128] = "logl";
130 Names[RTLIB::LOG2_F32] = "log2f";
131 Names[RTLIB::LOG2_F64] = "log2";
132 Names[RTLIB::LOG2_F80] = "log2l";
133 Names[RTLIB::LOG2_PPCF128] = "log2l";
134 Names[RTLIB::LOG10_F32] = "log10f";
135 Names[RTLIB::LOG10_F64] = "log10";
136 Names[RTLIB::LOG10_F80] = "log10l";
137 Names[RTLIB::LOG10_PPCF128] = "log10l";
138 Names[RTLIB::EXP_F32] = "expf";
139 Names[RTLIB::EXP_F64] = "exp";
140 Names[RTLIB::EXP_F80] = "expl";
141 Names[RTLIB::EXP_PPCF128] = "expl";
142 Names[RTLIB::EXP2_F32] = "exp2f";
143 Names[RTLIB::EXP2_F64] = "exp2";
144 Names[RTLIB::EXP2_F80] = "exp2l";
145 Names[RTLIB::EXP2_PPCF128] = "exp2l";
146 Names[RTLIB::SIN_F32] = "sinf";
147 Names[RTLIB::SIN_F64] = "sin";
148 Names[RTLIB::SIN_F80] = "sinl";
149 Names[RTLIB::SIN_PPCF128] = "sinl";
150 Names[RTLIB::COS_F32] = "cosf";
151 Names[RTLIB::COS_F64] = "cos";
152 Names[RTLIB::COS_F80] = "cosl";
153 Names[RTLIB::COS_PPCF128] = "cosl";
154 Names[RTLIB::POW_F32] = "powf";
155 Names[RTLIB::POW_F64] = "pow";
156 Names[RTLIB::POW_F80] = "powl";
157 Names[RTLIB::POW_PPCF128] = "powl";
158 Names[RTLIB::CEIL_F32] = "ceilf";
159 Names[RTLIB::CEIL_F64] = "ceil";
160 Names[RTLIB::CEIL_F80] = "ceill";
161 Names[RTLIB::CEIL_PPCF128] = "ceill";
162 Names[RTLIB::TRUNC_F32] = "truncf";
163 Names[RTLIB::TRUNC_F64] = "trunc";
164 Names[RTLIB::TRUNC_F80] = "truncl";
165 Names[RTLIB::TRUNC_PPCF128] = "truncl";
166 Names[RTLIB::RINT_F32] = "rintf";
167 Names[RTLIB::RINT_F64] = "rint";
168 Names[RTLIB::RINT_F80] = "rintl";
169 Names[RTLIB::RINT_PPCF128] = "rintl";
170 Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
171 Names[RTLIB::NEARBYINT_F64] = "nearbyint";
172 Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
173 Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
174 Names[RTLIB::FLOOR_F32] = "floorf";
175 Names[RTLIB::FLOOR_F64] = "floor";
176 Names[RTLIB::FLOOR_F80] = "floorl";
177 Names[RTLIB::FLOOR_PPCF128] = "floorl";
178 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
179 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
180 Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
181 Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
182 Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
183 Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
184 Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfi8";
185 Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfi16";
186 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
187 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
188 Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
189 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
190 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
191 Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
192 Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
193 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
194 Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
195 Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
196 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
197 Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
198 Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfi8";
199 Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfi16";
200 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
201 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
202 Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
203 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
204 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
205 Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
206 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
207 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
208 Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
209 Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
210 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
211 Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
212 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
213 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
214 Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
215 Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
216 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
217 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
218 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
219 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
220 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
221 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
222 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
223 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
224 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
225 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
226 Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
227 Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
228 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
229 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
230 Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
231 Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
232 Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
233 Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
234 Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
235 Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
236 Names[RTLIB::OEQ_F32] = "__eqsf2";
237 Names[RTLIB::OEQ_F64] = "__eqdf2";
238 Names[RTLIB::UNE_F32] = "__nesf2";
239 Names[RTLIB::UNE_F64] = "__nedf2";
240 Names[RTLIB::OGE_F32] = "__gesf2";
241 Names[RTLIB::OGE_F64] = "__gedf2";
242 Names[RTLIB::OLT_F32] = "__ltsf2";
243 Names[RTLIB::OLT_F64] = "__ltdf2";
244 Names[RTLIB::OLE_F32] = "__lesf2";
245 Names[RTLIB::OLE_F64] = "__ledf2";
246 Names[RTLIB::OGT_F32] = "__gtsf2";
247 Names[RTLIB::OGT_F64] = "__gtdf2";
248 Names[RTLIB::UO_F32] = "__unordsf2";
249 Names[RTLIB::UO_F64] = "__unorddf2";
250 Names[RTLIB::O_F32] = "__unordsf2";
251 Names[RTLIB::O_F64] = "__unorddf2";
252 Names[RTLIB::MEMCPY] = "memcpy";
253 Names[RTLIB::MEMMOVE] = "memmove";
254 Names[RTLIB::MEMSET] = "memset";
255 Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
256}
257
258/// InitLibcallCallingConvs - Set default libcall CallingConvs.
259///
260static void InitLibcallCallingConvs(CallingConv::ID *CCs) {
261 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
262 CCs[i] = CallingConv::C;
263 }
264}
265
266/// getFPEXT - Return the FPEXT_*_* value for the given types, or
267/// UNKNOWN_LIBCALL if there is none.
268RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
269 if (OpVT == MVT::f32) {
270 if (RetVT == MVT::f64)
271 return FPEXT_F32_F64;
272 }
273 return UNKNOWN_LIBCALL;
274}
275
276/// getFPROUND - Return the FPROUND_*_* value for the given types, or
277/// UNKNOWN_LIBCALL if there is none.
278RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
279 if (RetVT == MVT::f32) {
280 if (OpVT == MVT::f64)
281 return FPROUND_F64_F32;
282 if (OpVT == MVT::f80)
283 return FPROUND_F80_F32;
284 if (OpVT == MVT::ppcf128)
285 return FPROUND_PPCF128_F32;
286 } else if (RetVT == MVT::f64) {
287 if (OpVT == MVT::f80)
288 return FPROUND_F80_F64;
289 if (OpVT == MVT::ppcf128)
290 return FPROUND_PPCF128_F64;
291 }
292 return UNKNOWN_LIBCALL;
293}
294
295/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
296/// UNKNOWN_LIBCALL if there is none.
297RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
298 if (OpVT == MVT::f32) {
299 if (RetVT == MVT::i8)
300 return FPTOSINT_F32_I8;
301 if (RetVT == MVT::i16)
302 return FPTOSINT_F32_I16;
303 if (RetVT == MVT::i32)
304 return FPTOSINT_F32_I32;
305 if (RetVT == MVT::i64)
306 return FPTOSINT_F32_I64;
307 if (RetVT == MVT::i128)
308 return FPTOSINT_F32_I128;
309 } else if (OpVT == MVT::f64) {
310 if (RetVT == MVT::i32)
311 return FPTOSINT_F64_I32;
312 if (RetVT == MVT::i64)
313 return FPTOSINT_F64_I64;
314 if (RetVT == MVT::i128)
315 return FPTOSINT_F64_I128;
316 } else if (OpVT == MVT::f80) {
317 if (RetVT == MVT::i32)
318 return FPTOSINT_F80_I32;
319 if (RetVT == MVT::i64)
320 return FPTOSINT_F80_I64;
321 if (RetVT == MVT::i128)
322 return FPTOSINT_F80_I128;
323 } else if (OpVT == MVT::ppcf128) {
324 if (RetVT == MVT::i32)
325 return FPTOSINT_PPCF128_I32;
326 if (RetVT == MVT::i64)
327 return FPTOSINT_PPCF128_I64;
328 if (RetVT == MVT::i128)
329 return FPTOSINT_PPCF128_I128;
330 }
331 return UNKNOWN_LIBCALL;
332}
333
334/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
335/// UNKNOWN_LIBCALL if there is none.
336RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
337 if (OpVT == MVT::f32) {
338 if (RetVT == MVT::i8)
339 return FPTOUINT_F32_I8;
340 if (RetVT == MVT::i16)
341 return FPTOUINT_F32_I16;
342 if (RetVT == MVT::i32)
343 return FPTOUINT_F32_I32;
344 if (RetVT == MVT::i64)
345 return FPTOUINT_F32_I64;
346 if (RetVT == MVT::i128)
347 return FPTOUINT_F32_I128;
348 } else if (OpVT == MVT::f64) {
349 if (RetVT == MVT::i32)
350 return FPTOUINT_F64_I32;
351 if (RetVT == MVT::i64)
352 return FPTOUINT_F64_I64;
353 if (RetVT == MVT::i128)
354 return FPTOUINT_F64_I128;
355 } else if (OpVT == MVT::f80) {
356 if (RetVT == MVT::i32)
357 return FPTOUINT_F80_I32;
358 if (RetVT == MVT::i64)
359 return FPTOUINT_F80_I64;
360 if (RetVT == MVT::i128)
361 return FPTOUINT_F80_I128;
362 } else if (OpVT == MVT::ppcf128) {
363 if (RetVT == MVT::i32)
364 return FPTOUINT_PPCF128_I32;
365 if (RetVT == MVT::i64)
366 return FPTOUINT_PPCF128_I64;
367 if (RetVT == MVT::i128)
368 return FPTOUINT_PPCF128_I128;
369 }
370 return UNKNOWN_LIBCALL;
371}
372
373/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
374/// UNKNOWN_LIBCALL if there is none.
375RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
376 if (OpVT == MVT::i32) {
377 if (RetVT == MVT::f32)
378 return SINTTOFP_I32_F32;
379 else if (RetVT == MVT::f64)
380 return SINTTOFP_I32_F64;
381 else if (RetVT == MVT::f80)
382 return SINTTOFP_I32_F80;
383 else if (RetVT == MVT::ppcf128)
384 return SINTTOFP_I32_PPCF128;
385 } else if (OpVT == MVT::i64) {
386 if (RetVT == MVT::f32)
387 return SINTTOFP_I64_F32;
388 else if (RetVT == MVT::f64)
389 return SINTTOFP_I64_F64;
390 else if (RetVT == MVT::f80)
391 return SINTTOFP_I64_F80;
392 else if (RetVT == MVT::ppcf128)
393 return SINTTOFP_I64_PPCF128;
394 } else if (OpVT == MVT::i128) {
395 if (RetVT == MVT::f32)
396 return SINTTOFP_I128_F32;
397 else if (RetVT == MVT::f64)
398 return SINTTOFP_I128_F64;
399 else if (RetVT == MVT::f80)
400 return SINTTOFP_I128_F80;
401 else if (RetVT == MVT::ppcf128)
402 return SINTTOFP_I128_PPCF128;
403 }
404 return UNKNOWN_LIBCALL;
405}
406
407/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
408/// UNKNOWN_LIBCALL if there is none.
409RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
410 if (OpVT == MVT::i32) {
411 if (RetVT == MVT::f32)
412 return UINTTOFP_I32_F32;
413 else if (RetVT == MVT::f64)
414 return UINTTOFP_I32_F64;
415 else if (RetVT == MVT::f80)
416 return UINTTOFP_I32_F80;
417 else if (RetVT == MVT::ppcf128)
418 return UINTTOFP_I32_PPCF128;
419 } else if (OpVT == MVT::i64) {
420 if (RetVT == MVT::f32)
421 return UINTTOFP_I64_F32;
422 else if (RetVT == MVT::f64)
423 return UINTTOFP_I64_F64;
424 else if (RetVT == MVT::f80)
425 return UINTTOFP_I64_F80;
426 else if (RetVT == MVT::ppcf128)
427 return UINTTOFP_I64_PPCF128;
428 } else if (OpVT == MVT::i128) {
429 if (RetVT == MVT::f32)
430 return UINTTOFP_I128_F32;
431 else if (RetVT == MVT::f64)
432 return UINTTOFP_I128_F64;
433 else if (RetVT == MVT::f80)
434 return UINTTOFP_I128_F80;
435 else if (RetVT == MVT::ppcf128)
436 return UINTTOFP_I128_PPCF128;
437 }
438 return UNKNOWN_LIBCALL;
439}
440
441/// InitCmpLibcallCCs - Set default comparison libcall CC.
442///
443static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
444 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
445 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
446 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
447 CCs[RTLIB::UNE_F32] = ISD::SETNE;
448 CCs[RTLIB::UNE_F64] = ISD::SETNE;
449 CCs[RTLIB::OGE_F32] = ISD::SETGE;
450 CCs[RTLIB::OGE_F64] = ISD::SETGE;
451 CCs[RTLIB::OLT_F32] = ISD::SETLT;
452 CCs[RTLIB::OLT_F64] = ISD::SETLT;
453 CCs[RTLIB::OLE_F32] = ISD::SETLE;
454 CCs[RTLIB::OLE_F64] = ISD::SETLE;
455 CCs[RTLIB::OGT_F32] = ISD::SETGT;
456 CCs[RTLIB::OGT_F64] = ISD::SETGT;
457 CCs[RTLIB::UO_F32] = ISD::SETNE;
458 CCs[RTLIB::UO_F64] = ISD::SETNE;
459 CCs[RTLIB::O_F32] = ISD::SETEQ;
460 CCs[RTLIB::O_F64] = ISD::SETEQ;
461}
462
463/// NOTE: The constructor takes ownership of TLOF.
464TargetLowering::TargetLowering(TargetMachine &tm,TargetLoweringObjectFile *tlof)
465 : TM(tm), TD(TM.getTargetData()), TLOF(*tlof) {
466 // All operations default to being supported.
467 memset(OpActions, 0, sizeof(OpActions));
468 memset(LoadExtActions, 0, sizeof(LoadExtActions));
469 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
470 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
471 memset(ConvertActions, 0, sizeof(ConvertActions));
472 memset(CondCodeActions, 0, sizeof(CondCodeActions));
473
474 // Set default actions for various operations.
475 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
476 // Default all indexed load / store to expand.
477 for (unsigned IM = (unsigned)ISD::PRE_INC;
478 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
479 setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
480 setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
481 }
482
483 // These operations default to expand.
484 setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
485 setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
486 }
487
488 // Most targets ignore the @llvm.prefetch intrinsic.
489 setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
490
491 // ConstantFP nodes default to expand. Targets can either change this to
492 // Legal, in which case all fp constants are legal, or use isFPImmLegal()
493 // to optimize expansions for certain constants.
494 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
495 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
496 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
497
498 // These library functions default to expand.
499 setOperationAction(ISD::FLOG , MVT::f64, Expand);
500 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
501 setOperationAction(ISD::FLOG10,MVT::f64, Expand);
502 setOperationAction(ISD::FEXP , MVT::f64, Expand);
503 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
504 setOperationAction(ISD::FLOG , MVT::f32, Expand);
505 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
506 setOperationAction(ISD::FLOG10,MVT::f32, Expand);
507 setOperationAction(ISD::FEXP , MVT::f32, Expand);
508 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
509
510 // Default ISD::TRAP to expand (which turns it into abort).
511 setOperationAction(ISD::TRAP, MVT::Other, Expand);
512
513 IsLittleEndian = TD->isLittleEndian();
514 ShiftAmountTy = PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
515 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
516 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
517 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
518 benefitFromCodePlacementOpt = false;
519 UseUnderscoreSetJmp = false;
520 UseUnderscoreLongJmp = false;
521 SelectIsExpensive = false;
522 IntDivIsCheap = false;
523 Pow2DivIsCheap = false;
524 StackPointerRegisterToSaveRestore = 0;
525 ExceptionPointerRegister = 0;
526 ExceptionSelectorRegister = 0;
527 BooleanContents = UndefinedBooleanContent;
528 SchedPreferenceInfo = SchedulingForLatency;
529 JumpBufSize = 0;
530 JumpBufAlignment = 0;
531 IfCvtBlockSizeLimit = 2;
532 IfCvtDupBlockSizeLimit = 0;
533 PrefLoopAlignment = 0;
534
535 InitLibcallNames(LibcallRoutineNames);
536 InitCmpLibcallCCs(CmpLibcallCCs);
537 InitLibcallCallingConvs(LibcallCallingConvs);
538}
539
540TargetLowering::~TargetLowering() {
541 delete &TLOF;
542}
543
Shih-wei Liaoe4454322010-04-07 12:21:42 -0700544/// canOpTrap - Returns true if the operation can trap for the value type.
545/// VT must be a legal type.
546bool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
547 assert(isTypeLegal(VT));
548 switch (Op) {
549 default:
550 return false;
551 case ISD::FDIV:
552 case ISD::FREM:
553 case ISD::SDIV:
554 case ISD::UDIV:
555 case ISD::SREM:
556 case ISD::UREM:
557 return true;
558 }
559}
560
561
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800562static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
563 unsigned &NumIntermediates,
564 EVT &RegisterVT,
565 TargetLowering* TLI) {
566 // Figure out the right, legal destination reg to copy into.
567 unsigned NumElts = VT.getVectorNumElements();
568 MVT EltTy = VT.getVectorElementType();
569
570 unsigned NumVectorRegs = 1;
571
572 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
573 // could break down into LHS/RHS like LegalizeDAG does.
574 if (!isPowerOf2_32(NumElts)) {
575 NumVectorRegs = NumElts;
576 NumElts = 1;
577 }
578
579 // Divide the input until we get to a supported size. This will always
580 // end with a scalar if the target doesn't support vectors.
581 while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
582 NumElts >>= 1;
583 NumVectorRegs <<= 1;
584 }
585
586 NumIntermediates = NumVectorRegs;
587
588 MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
589 if (!TLI->isTypeLegal(NewVT))
590 NewVT = EltTy;
591 IntermediateVT = NewVT;
592
593 EVT DestVT = TLI->getRegisterType(NewVT);
594 RegisterVT = DestVT;
595 if (EVT(DestVT).bitsLT(NewVT)) {
596 // Value is expanded, e.g. i64 -> i16.
597 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
598 } else {
599 // Otherwise, promotion or legal types use the same number of registers as
600 // the vector decimated to the appropriate level.
601 return NumVectorRegs;
602 }
603
604 return 1;
605}
606
607/// computeRegisterProperties - Once all of the register classes are added,
608/// this allows us to compute derived properties we expose.
609void TargetLowering::computeRegisterProperties() {
610 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
611 "Too many value types for ValueTypeActions to hold!");
612
613 // Everything defaults to needing one register.
614 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
615 NumRegistersForVT[i] = 1;
616 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
617 }
618 // ...except isVoid, which doesn't need any registers.
619 NumRegistersForVT[MVT::isVoid] = 0;
620
621 // Find the largest integer register class.
622 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
623 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
624 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
625
626 // Every integer value type larger than this largest register takes twice as
627 // many registers to represent as the previous ValueType.
628 for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
629 EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
630 if (!ExpandedVT.isInteger())
631 break;
632 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
633 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
634 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
635 ValueTypeActions.setTypeAction(ExpandedVT, Expand);
636 }
637
638 // Inspect all of the ValueType's smaller than the largest integer
639 // register to see which ones need promotion.
640 unsigned LegalIntReg = LargestIntReg;
641 for (unsigned IntReg = LargestIntReg - 1;
642 IntReg >= (unsigned)MVT::i1; --IntReg) {
643 EVT IVT = (MVT::SimpleValueType)IntReg;
644 if (isTypeLegal(IVT)) {
645 LegalIntReg = IntReg;
646 } else {
647 RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
648 (MVT::SimpleValueType)LegalIntReg;
649 ValueTypeActions.setTypeAction(IVT, Promote);
650 }
651 }
652
653 // ppcf128 type is really two f64's.
654 if (!isTypeLegal(MVT::ppcf128)) {
655 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
656 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
657 TransformToType[MVT::ppcf128] = MVT::f64;
658 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
659 }
660
661 // Decide how to handle f64. If the target does not have native f64 support,
662 // expand it to i64 and we will be generating soft float library calls.
663 if (!isTypeLegal(MVT::f64)) {
664 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
665 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
666 TransformToType[MVT::f64] = MVT::i64;
667 ValueTypeActions.setTypeAction(MVT::f64, Expand);
668 }
669
670 // Decide how to handle f32. If the target does not have native support for
671 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
672 if (!isTypeLegal(MVT::f32)) {
673 if (isTypeLegal(MVT::f64)) {
674 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
675 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
676 TransformToType[MVT::f32] = MVT::f64;
677 ValueTypeActions.setTypeAction(MVT::f32, Promote);
678 } else {
679 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
680 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
681 TransformToType[MVT::f32] = MVT::i32;
682 ValueTypeActions.setTypeAction(MVT::f32, Expand);
683 }
684 }
685
686 // Loop over all of the vector value types to see which need transformations.
687 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
688 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
689 MVT VT = (MVT::SimpleValueType)i;
690 if (!isTypeLegal(VT)) {
691 MVT IntermediateVT;
692 EVT RegisterVT;
693 unsigned NumIntermediates;
694 NumRegistersForVT[i] =
695 getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
696 RegisterVT, this);
697 RegisterTypeForVT[i] = RegisterVT;
698
699 // Determine if there is a legal wider type.
700 bool IsLegalWiderType = false;
701 EVT EltVT = VT.getVectorElementType();
702 unsigned NElts = VT.getVectorNumElements();
703 for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
704 EVT SVT = (MVT::SimpleValueType)nVT;
705 if (isTypeLegal(SVT) && SVT.getVectorElementType() == EltVT &&
706 SVT.getVectorNumElements() > NElts && NElts != 1) {
707 TransformToType[i] = SVT;
708 ValueTypeActions.setTypeAction(VT, Promote);
709 IsLegalWiderType = true;
710 break;
711 }
712 }
713 if (!IsLegalWiderType) {
714 EVT NVT = VT.getPow2VectorType();
715 if (NVT == VT) {
716 // Type is already a power of 2. The default action is to split.
717 TransformToType[i] = MVT::Other;
718 ValueTypeActions.setTypeAction(VT, Expand);
719 } else {
720 TransformToType[i] = NVT;
721 ValueTypeActions.setTypeAction(VT, Promote);
722 }
723 }
724 }
725 }
726}
727
728const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
729 return NULL;
730}
731
732
733MVT::SimpleValueType TargetLowering::getSetCCResultType(EVT VT) const {
734 return PointerTy.SimpleTy;
735}
736
737MVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
738 return MVT::i32; // return the default value
739}
740
741/// getVectorTypeBreakdown - Vector types are broken down into some number of
742/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
743/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
744/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
745///
746/// This method returns the number of registers needed, and the VT for each
747/// register. It also returns the VT and quantity of the intermediate values
748/// before they are promoted/expanded.
749///
750unsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
751 EVT &IntermediateVT,
752 unsigned &NumIntermediates,
753 EVT &RegisterVT) const {
754 // Figure out the right, legal destination reg to copy into.
755 unsigned NumElts = VT.getVectorNumElements();
756 EVT EltTy = VT.getVectorElementType();
757
758 unsigned NumVectorRegs = 1;
759
760 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
761 // could break down into LHS/RHS like LegalizeDAG does.
762 if (!isPowerOf2_32(NumElts)) {
763 NumVectorRegs = NumElts;
764 NumElts = 1;
765 }
766
767 // Divide the input until we get to a supported size. This will always
768 // end with a scalar if the target doesn't support vectors.
769 while (NumElts > 1 && !isTypeLegal(
770 EVT::getVectorVT(Context, EltTy, NumElts))) {
771 NumElts >>= 1;
772 NumVectorRegs <<= 1;
773 }
774
775 NumIntermediates = NumVectorRegs;
776
777 EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
778 if (!isTypeLegal(NewVT))
779 NewVT = EltTy;
780 IntermediateVT = NewVT;
781
782 EVT DestVT = getRegisterType(Context, NewVT);
783 RegisterVT = DestVT;
784 if (DestVT.bitsLT(NewVT)) {
785 // Value is expanded, e.g. i64 -> i16.
786 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
787 } else {
788 // Otherwise, promotion or legal types use the same number of registers as
789 // the vector decimated to the appropriate level.
790 return NumVectorRegs;
791 }
792
793 return 1;
794}
795
796/// getWidenVectorType: given a vector type, returns the type to widen to
797/// (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
798/// If there is no vector type that we want to widen to, returns MVT::Other
799/// When and where to widen is target dependent based on the cost of
800/// scalarizing vs using the wider vector type.
801EVT TargetLowering::getWidenVectorType(EVT VT) const {
802 assert(VT.isVector());
803 if (isTypeLegal(VT))
804 return VT;
805
806 // Default is not to widen until moved to LegalizeTypes
807 return MVT::Other;
808}
809
810/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
811/// function arguments in the caller parameter area. This is the actual
812/// alignment, not its logarithm.
813unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
814 return TD->getCallFrameTypeAlignment(Ty);
815}
816
817/// getJumpTableEncoding - Return the entry encoding for a jump table in the
818/// current function. The returned value is a member of the
819/// MachineJumpTableInfo::JTEntryKind enum.
820unsigned TargetLowering::getJumpTableEncoding() const {
821 // In non-pic modes, just use the address of a block.
822 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
823 return MachineJumpTableInfo::EK_BlockAddress;
824
825 // In PIC mode, if the target supports a GPRel32 directive, use it.
826 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
827 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
828
829 // Otherwise, use a label difference.
830 return MachineJumpTableInfo::EK_LabelDifference32;
831}
832
833SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
834 SelectionDAG &DAG) const {
835 // If our PIC model is GP relative, use the global offset table as the base.
836 if (getJumpTableEncoding() == MachineJumpTableInfo::EK_GPRel32BlockAddress)
837 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
838 return Table;
839}
840
841/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
842/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
843/// MCExpr.
844const MCExpr *
845TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
846 unsigned JTI,MCContext &Ctx) const{
847 // The normal PIC reloc base is the label at the start of the jump table.
848 return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
849}
850
851bool
852TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
853 // Assume that everything is safe in static mode.
854 if (getTargetMachine().getRelocationModel() == Reloc::Static)
855 return true;
856
857 // In dynamic-no-pic mode, assume that known defined values are safe.
858 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
859 GA &&
860 !GA->getGlobal()->isDeclaration() &&
861 !GA->getGlobal()->isWeakForLinker())
862 return true;
863
864 // Otherwise assume nothing is safe.
865 return false;
866}
867
868//===----------------------------------------------------------------------===//
869// Optimization Methods
870//===----------------------------------------------------------------------===//
871
872/// ShrinkDemandedConstant - Check to see if the specified operand of the
873/// specified instruction is a constant integer. If so, check to see if there
874/// are any bits set in the constant that are not demanded. If so, shrink the
875/// constant and return true.
876bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
877 const APInt &Demanded) {
878 DebugLoc dl = Op.getDebugLoc();
879
880 // FIXME: ISD::SELECT, ISD::SELECT_CC
881 switch (Op.getOpcode()) {
882 default: break;
883 case ISD::XOR:
884 case ISD::AND:
885 case ISD::OR: {
886 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
887 if (!C) return false;
888
889 if (Op.getOpcode() == ISD::XOR &&
890 (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
891 return false;
892
893 // if we can expand it to have all bits set, do it
894 if (C->getAPIntValue().intersects(~Demanded)) {
895 EVT VT = Op.getValueType();
896 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
897 DAG.getConstant(Demanded &
898 C->getAPIntValue(),
899 VT));
900 return CombineTo(Op, New);
901 }
902
903 break;
904 }
905 }
906
907 return false;
908}
909
910/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
911/// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
912/// cast, but it could be generalized for targets with other types of
913/// implicit widening casts.
914bool
915TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
916 unsigned BitWidth,
917 const APInt &Demanded,
918 DebugLoc dl) {
919 assert(Op.getNumOperands() == 2 &&
920 "ShrinkDemandedOp only supports binary operators!");
921 assert(Op.getNode()->getNumValues() == 1 &&
922 "ShrinkDemandedOp only supports nodes with one result!");
923
924 // Don't do this if the node has another user, which may require the
925 // full value.
926 if (!Op.getNode()->hasOneUse())
927 return false;
928
929 // Search for the smallest integer type with free casts to and from
930 // Op's type. For expedience, just check power-of-2 integer types.
931 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
932 unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
933 if (!isPowerOf2_32(SmallVTBits))
934 SmallVTBits = NextPowerOf2(SmallVTBits);
935 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
936 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
937 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
938 TLI.isZExtFree(SmallVT, Op.getValueType())) {
939 // We found a type with free casts.
940 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
941 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
942 Op.getNode()->getOperand(0)),
943 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
944 Op.getNode()->getOperand(1)));
945 SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
946 return CombineTo(Op, Z);
947 }
948 }
949 return false;
950}
951
952/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
953/// DemandedMask bits of the result of Op are ever used downstream. If we can
954/// use this information to simplify Op, create a new simplified DAG node and
955/// return true, returning the original and new nodes in Old and New. Otherwise,
956/// analyze the expression and return a mask of KnownOne and KnownZero bits for
957/// the expression (used to simplify the caller). The KnownZero/One bits may
958/// only be accurate for those bits in the DemandedMask.
959bool TargetLowering::SimplifyDemandedBits(SDValue Op,
960 const APInt &DemandedMask,
961 APInt &KnownZero,
962 APInt &KnownOne,
963 TargetLoweringOpt &TLO,
964 unsigned Depth) const {
965 unsigned BitWidth = DemandedMask.getBitWidth();
966 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
967 "Mask size mismatches value type size!");
968 APInt NewMask = DemandedMask;
969 DebugLoc dl = Op.getDebugLoc();
970
971 // Don't know anything.
972 KnownZero = KnownOne = APInt(BitWidth, 0);
973
974 // Other users may use these bits.
975 if (!Op.getNode()->hasOneUse()) {
976 if (Depth != 0) {
977 // If not at the root, Just compute the KnownZero/KnownOne bits to
978 // simplify things downstream.
979 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
980 return false;
981 }
982 // If this is the root being simplified, allow it to have multiple uses,
983 // just set the NewMask to all bits.
984 NewMask = APInt::getAllOnesValue(BitWidth);
985 } else if (DemandedMask == 0) {
986 // Not demanding any bits from Op.
987 if (Op.getOpcode() != ISD::UNDEF)
988 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
989 return false;
990 } else if (Depth == 6) { // Limit search depth.
991 return false;
992 }
993
994 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
995 switch (Op.getOpcode()) {
996 case ISD::Constant:
997 // We know all of the bits for a constant!
998 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
999 KnownZero = ~KnownOne & NewMask;
1000 return false; // Don't fall through, will infinitely loop.
1001 case ISD::AND:
1002 // If the RHS is a constant, check to see if the LHS would be zero without
1003 // using the bits from the RHS. Below, we use knowledge about the RHS to
1004 // simplify the LHS, here we're using information from the LHS to simplify
1005 // the RHS.
1006 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1007 APInt LHSZero, LHSOne;
1008 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
1009 LHSZero, LHSOne, Depth+1);
1010 // If the LHS already has zeros where RHSC does, this and is dead.
1011 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
1012 return TLO.CombineTo(Op, Op.getOperand(0));
1013 // If any of the set bits in the RHS are known zero on the LHS, shrink
1014 // the constant.
1015 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
1016 return true;
1017 }
1018
1019 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1020 KnownOne, TLO, Depth+1))
1021 return true;
1022 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1023 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
1024 KnownZero2, KnownOne2, TLO, Depth+1))
1025 return true;
1026 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1027
1028 // If all of the demanded bits are known one on one side, return the other.
1029 // These bits cannot contribute to the result of the 'and'.
1030 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1031 return TLO.CombineTo(Op, Op.getOperand(0));
1032 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1033 return TLO.CombineTo(Op, Op.getOperand(1));
1034 // If all of the demanded bits in the inputs are known zeros, return zero.
1035 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
1036 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1037 // If the RHS is a constant, see if we can simplify it.
1038 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
1039 return true;
1040 // If the operation can be done in a smaller type, do so.
1041 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1042 return true;
1043
1044 // Output known-1 bits are only known if set in both the LHS & RHS.
1045 KnownOne &= KnownOne2;
1046 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1047 KnownZero |= KnownZero2;
1048 break;
1049 case ISD::OR:
1050 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1051 KnownOne, TLO, Depth+1))
1052 return true;
1053 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1054 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
1055 KnownZero2, KnownOne2, TLO, Depth+1))
1056 return true;
1057 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1058
1059 // If all of the demanded bits are known zero on one side, return the other.
1060 // These bits cannot contribute to the result of the 'or'.
1061 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
1062 return TLO.CombineTo(Op, Op.getOperand(0));
1063 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
1064 return TLO.CombineTo(Op, Op.getOperand(1));
1065 // If all of the potentially set bits on one side are known to be set on
1066 // the other side, just use the 'other' side.
1067 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1068 return TLO.CombineTo(Op, Op.getOperand(0));
1069 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1070 return TLO.CombineTo(Op, Op.getOperand(1));
1071 // If the RHS is a constant, see if we can simplify it.
1072 if (TLO.ShrinkDemandedConstant(Op, NewMask))
1073 return true;
1074 // If the operation can be done in a smaller type, do so.
1075 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1076 return true;
1077
1078 // Output known-0 bits are only known if clear in both the LHS & RHS.
1079 KnownZero &= KnownZero2;
1080 // Output known-1 are known to be set if set in either the LHS | RHS.
1081 KnownOne |= KnownOne2;
1082 break;
1083 case ISD::XOR:
1084 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1085 KnownOne, TLO, Depth+1))
1086 return true;
1087 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1088 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
1089 KnownOne2, TLO, Depth+1))
1090 return true;
1091 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1092
1093 // If all of the demanded bits are known zero on one side, return the other.
1094 // These bits cannot contribute to the result of the 'xor'.
1095 if ((KnownZero & NewMask) == NewMask)
1096 return TLO.CombineTo(Op, Op.getOperand(0));
1097 if ((KnownZero2 & NewMask) == NewMask)
1098 return TLO.CombineTo(Op, Op.getOperand(1));
1099 // If the operation can be done in a smaller type, do so.
1100 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1101 return true;
1102
1103 // If all of the unknown bits are known to be zero on one side or the other
1104 // (but not both) turn this into an *inclusive* or.
1105 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1106 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
1107 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
1108 Op.getOperand(0),
1109 Op.getOperand(1)));
1110
1111 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1112 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1113 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1114 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1115
1116 // If all of the demanded bits on one side are known, and all of the set
1117 // bits on that side are also known to be set on the other side, turn this
1118 // into an AND, as we know the bits will be cleared.
1119 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1120 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
1121 if ((KnownOne & KnownOne2) == KnownOne) {
1122 EVT VT = Op.getValueType();
1123 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
1124 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1125 Op.getOperand(0), ANDC));
1126 }
1127 }
1128
1129 // If the RHS is a constant, see if we can simplify it.
1130 // for XOR, we prefer to force bits to 1 if they will make a -1.
1131 // if we can't force bits, try to shrink constant
1132 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1133 APInt Expanded = C->getAPIntValue() | (~NewMask);
1134 // if we can expand it to have all bits set, do it
1135 if (Expanded.isAllOnesValue()) {
1136 if (Expanded != C->getAPIntValue()) {
1137 EVT VT = Op.getValueType();
1138 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
1139 TLO.DAG.getConstant(Expanded, VT));
1140 return TLO.CombineTo(Op, New);
1141 }
1142 // if it already has all the bits set, nothing to change
1143 // but don't shrink either!
1144 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
1145 return true;
1146 }
1147 }
1148
1149 KnownZero = KnownZeroOut;
1150 KnownOne = KnownOneOut;
1151 break;
1152 case ISD::SELECT:
1153 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
1154 KnownOne, TLO, Depth+1))
1155 return true;
1156 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
1157 KnownOne2, TLO, Depth+1))
1158 return true;
1159 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1160 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1161
1162 // If the operands are constants, see if we can simplify them.
1163 if (TLO.ShrinkDemandedConstant(Op, NewMask))
1164 return true;
1165
1166 // Only known if known in both the LHS and RHS.
1167 KnownOne &= KnownOne2;
1168 KnownZero &= KnownZero2;
1169 break;
1170 case ISD::SELECT_CC:
1171 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
1172 KnownOne, TLO, Depth+1))
1173 return true;
1174 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
1175 KnownOne2, TLO, Depth+1))
1176 return true;
1177 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1178 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1179
1180 // If the operands are constants, see if we can simplify them.
1181 if (TLO.ShrinkDemandedConstant(Op, NewMask))
1182 return true;
1183
1184 // Only known if known in both the LHS and RHS.
1185 KnownOne &= KnownOne2;
1186 KnownZero &= KnownZero2;
1187 break;
1188 case ISD::SHL:
1189 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1190 unsigned ShAmt = SA->getZExtValue();
1191 SDValue InOp = Op.getOperand(0);
1192
1193 // If the shift count is an invalid immediate, don't do anything.
1194 if (ShAmt >= BitWidth)
1195 break;
1196
1197 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1198 // single shift. We can do this if the bottom bits (which are shifted
1199 // out) are never demanded.
1200 if (InOp.getOpcode() == ISD::SRL &&
1201 isa<ConstantSDNode>(InOp.getOperand(1))) {
1202 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
1203 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1204 unsigned Opc = ISD::SHL;
1205 int Diff = ShAmt-C1;
1206 if (Diff < 0) {
1207 Diff = -Diff;
1208 Opc = ISD::SRL;
1209 }
1210
1211 SDValue NewSA =
1212 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1213 EVT VT = Op.getValueType();
1214 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1215 InOp.getOperand(0), NewSA));
1216 }
1217 }
1218
1219 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
1220 KnownZero, KnownOne, TLO, Depth+1))
1221 return true;
1222 KnownZero <<= SA->getZExtValue();
1223 KnownOne <<= SA->getZExtValue();
1224 // low bits known zero.
1225 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
1226 }
1227 break;
1228 case ISD::SRL:
1229 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1230 EVT VT = Op.getValueType();
1231 unsigned ShAmt = SA->getZExtValue();
1232 unsigned VTSize = VT.getSizeInBits();
1233 SDValue InOp = Op.getOperand(0);
1234
1235 // If the shift count is an invalid immediate, don't do anything.
1236 if (ShAmt >= BitWidth)
1237 break;
1238
1239 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1240 // single shift. We can do this if the top bits (which are shifted out)
1241 // are never demanded.
1242 if (InOp.getOpcode() == ISD::SHL &&
1243 isa<ConstantSDNode>(InOp.getOperand(1))) {
1244 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
1245 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1246 unsigned Opc = ISD::SRL;
1247 int Diff = ShAmt-C1;
1248 if (Diff < 0) {
1249 Diff = -Diff;
1250 Opc = ISD::SHL;
1251 }
1252
1253 SDValue NewSA =
1254 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1255 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1256 InOp.getOperand(0), NewSA));
1257 }
1258 }
1259
1260 // Compute the new bits that are at the top now.
1261 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
1262 KnownZero, KnownOne, TLO, Depth+1))
1263 return true;
1264 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1265 KnownZero = KnownZero.lshr(ShAmt);
1266 KnownOne = KnownOne.lshr(ShAmt);
1267
1268 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1269 KnownZero |= HighBits; // High bits known zero.
1270 }
1271 break;
1272 case ISD::SRA:
1273 // If this is an arithmetic shift right and only the low-bit is set, we can
1274 // always convert this into a logical shr, even if the shift amount is
1275 // variable. The low bit of the shift cannot be an input sign bit unless
1276 // the shift amount is >= the size of the datatype, which is undefined.
1277 if (DemandedMask == 1)
1278 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
1279 Op.getOperand(0), Op.getOperand(1)));
1280
1281 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1282 EVT VT = Op.getValueType();
1283 unsigned ShAmt = SA->getZExtValue();
1284
1285 // If the shift count is an invalid immediate, don't do anything.
1286 if (ShAmt >= BitWidth)
1287 break;
1288
1289 APInt InDemandedMask = (NewMask << ShAmt);
1290
1291 // If any of the demanded bits are produced by the sign extension, we also
1292 // demand the input sign bit.
1293 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1294 if (HighBits.intersects(NewMask))
1295 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
1296
1297 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
1298 KnownZero, KnownOne, TLO, Depth+1))
1299 return true;
1300 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1301 KnownZero = KnownZero.lshr(ShAmt);
1302 KnownOne = KnownOne.lshr(ShAmt);
1303
1304 // Handle the sign bit, adjusted to where it is now in the mask.
1305 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
1306
1307 // If the input sign bit is known to be zero, or if none of the top bits
1308 // are demanded, turn this into an unsigned shift right.
1309 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
1310 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1311 Op.getOperand(0),
1312 Op.getOperand(1)));
1313 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
1314 KnownOne |= HighBits;
1315 }
1316 }
1317 break;
1318 case ISD::SIGN_EXTEND_INREG: {
1319 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1320
1321 // Sign extension. Compute the demanded bits in the result that are not
1322 // present in the input.
1323 APInt NewBits =
1324 APInt::getHighBitsSet(BitWidth,
1325 BitWidth - EVT.getScalarType().getSizeInBits()) &
1326 NewMask;
1327
1328 // If none of the extended bits are demanded, eliminate the sextinreg.
1329 if (NewBits == 0)
1330 return TLO.CombineTo(Op, Op.getOperand(0));
1331
1332 APInt InSignBit = APInt::getSignBit(EVT.getScalarType().getSizeInBits());
1333 InSignBit.zext(BitWidth);
1334 APInt InputDemandedBits =
1335 APInt::getLowBitsSet(BitWidth,
1336 EVT.getScalarType().getSizeInBits()) &
1337 NewMask;
1338
1339 // Since the sign extended bits are demanded, we know that the sign
1340 // bit is demanded.
1341 InputDemandedBits |= InSignBit;
1342
1343 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1344 KnownZero, KnownOne, TLO, Depth+1))
1345 return true;
1346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1347
1348 // If the sign bit of the input is known set or clear, then we know the
1349 // top bits of the result.
1350
1351 // If the input sign bit is known zero, convert this into a zero extension.
1352 if (KnownZero.intersects(InSignBit))
1353 return TLO.CombineTo(Op,
1354 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
1355
1356 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
1357 KnownOne |= NewBits;
1358 KnownZero &= ~NewBits;
1359 } else { // Input sign bit unknown
1360 KnownZero &= ~NewBits;
1361 KnownOne &= ~NewBits;
1362 }
1363 break;
1364 }
1365 case ISD::ZERO_EXTEND: {
1366 unsigned OperandBitWidth =
1367 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
1368 APInt InMask = NewMask;
1369 InMask.trunc(OperandBitWidth);
1370
1371 // If none of the top bits are demanded, convert this into an any_extend.
1372 APInt NewBits =
1373 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
1374 if (!NewBits.intersects(NewMask))
1375 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1376 Op.getValueType(),
1377 Op.getOperand(0)));
1378
1379 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1380 KnownZero, KnownOne, TLO, Depth+1))
1381 return true;
1382 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1383 KnownZero.zext(BitWidth);
1384 KnownOne.zext(BitWidth);
1385 KnownZero |= NewBits;
1386 break;
1387 }
1388 case ISD::SIGN_EXTEND: {
1389 EVT InVT = Op.getOperand(0).getValueType();
1390 unsigned InBits = InVT.getScalarType().getSizeInBits();
1391 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
1392 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
1393 APInt NewBits = ~InMask & NewMask;
1394
1395 // If none of the top bits are demanded, convert this into an any_extend.
1396 if (NewBits == 0)
1397 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1398 Op.getValueType(),
1399 Op.getOperand(0)));
1400
1401 // Since some of the sign extended bits are demanded, we know that the sign
1402 // bit is demanded.
1403 APInt InDemandedBits = InMask & NewMask;
1404 InDemandedBits |= InSignBit;
1405 InDemandedBits.trunc(InBits);
1406
1407 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1408 KnownOne, TLO, Depth+1))
1409 return true;
1410 KnownZero.zext(BitWidth);
1411 KnownOne.zext(BitWidth);
1412
1413 // If the sign bit is known zero, convert this to a zero extend.
1414 if (KnownZero.intersects(InSignBit))
1415 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
1416 Op.getValueType(),
1417 Op.getOperand(0)));
1418
1419 // If the sign bit is known one, the top bits match.
1420 if (KnownOne.intersects(InSignBit)) {
1421 KnownOne |= NewBits;
1422 KnownZero &= ~NewBits;
1423 } else { // Otherwise, top bits aren't known.
1424 KnownOne &= ~NewBits;
1425 KnownZero &= ~NewBits;
1426 }
1427 break;
1428 }
1429 case ISD::ANY_EXTEND: {
1430 unsigned OperandBitWidth =
1431 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
1432 APInt InMask = NewMask;
1433 InMask.trunc(OperandBitWidth);
1434 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1435 KnownZero, KnownOne, TLO, Depth+1))
1436 return true;
1437 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1438 KnownZero.zext(BitWidth);
1439 KnownOne.zext(BitWidth);
1440 break;
1441 }
1442 case ISD::TRUNCATE: {
1443 // Simplify the input, using demanded bit information, and compute the known
1444 // zero/one bits live out.
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001445 unsigned OperandBitWidth =
1446 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001447 APInt TruncMask = NewMask;
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001448 TruncMask.zext(OperandBitWidth);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001449 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
1450 KnownZero, KnownOne, TLO, Depth+1))
1451 return true;
1452 KnownZero.trunc(BitWidth);
1453 KnownOne.trunc(BitWidth);
1454
1455 // If the input is only used by this truncate, see if we can shrink it based
1456 // on the known demanded bits.
1457 if (Op.getOperand(0).getNode()->hasOneUse()) {
1458 SDValue In = Op.getOperand(0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001459 switch (In.getOpcode()) {
1460 default: break;
1461 case ISD::SRL:
1462 // Shrink SRL by a constant if none of the high bits shifted in are
1463 // demanded.
1464 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001465 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1466 OperandBitWidth - BitWidth);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001467 HighBits = HighBits.lshr(ShAmt->getZExtValue());
1468 HighBits.trunc(BitWidth);
1469
1470 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1471 // None of the shifted in bits are needed. Add a truncate of the
1472 // shift input, then shift it.
1473 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
1474 Op.getValueType(),
1475 In.getOperand(0));
1476 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1477 Op.getValueType(),
1478 NewTrunc,
1479 In.getOperand(1)));
1480 }
1481 }
1482 break;
1483 }
1484 }
1485
1486 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1487 break;
1488 }
1489 case ISD::AssertZext: {
1490 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1491 APInt InMask = APInt::getLowBitsSet(BitWidth,
1492 VT.getSizeInBits());
1493 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
1494 KnownZero, KnownOne, TLO, Depth+1))
1495 return true;
1496 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1497 KnownZero |= ~InMask & NewMask;
1498 break;
1499 }
1500 case ISD::BIT_CONVERT:
1501#if 0
1502 // If this is an FP->Int bitcast and if the sign bit is the only thing that
1503 // is demanded, turn this into a FGETSIGN.
1504 if (NewMask == EVT::getIntegerVTSignBit(Op.getValueType()) &&
1505 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
1506 !MVT::isVector(Op.getOperand(0).getValueType())) {
1507 // Only do this xform if FGETSIGN is valid or if before legalize.
1508 if (!TLO.AfterLegalize ||
1509 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
1510 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1511 // place. We expect the SHL to be eliminated by other optimizations.
1512 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
1513 Op.getOperand(0));
1514 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
1515 SDValue ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
1516 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
1517 Sign, ShAmt));
1518 }
1519 }
1520#endif
1521 break;
1522 case ISD::ADD:
1523 case ISD::MUL:
1524 case ISD::SUB: {
1525 // Add, Sub, and Mul don't demand any bits in positions beyond that
1526 // of the highest bit demanded of them.
1527 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1528 BitWidth - NewMask.countLeadingZeros());
1529 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1530 KnownOne2, TLO, Depth+1))
1531 return true;
1532 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1533 KnownOne2, TLO, Depth+1))
1534 return true;
1535 // See if the operation should be performed at a smaller bit width.
1536 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1537 return true;
1538 }
1539 // FALL THROUGH
1540 default:
1541 // Just use ComputeMaskedBits to compute output bits.
1542 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
1543 break;
1544 }
1545
1546 // If we know the value of all of the demanded bits, return this as a
1547 // constant.
1548 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
1549 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1550
1551 return false;
1552}
1553
1554/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1555/// in Mask are known to be either zero or one and return them in the
1556/// KnownZero/KnownOne bitsets.
1557void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1558 const APInt &Mask,
1559 APInt &KnownZero,
1560 APInt &KnownOne,
1561 const SelectionDAG &DAG,
1562 unsigned Depth) const {
1563 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1564 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1565 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1566 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1567 "Should use MaskedValueIsZero if you don't know whether Op"
1568 " is a target node!");
1569 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
1570}
1571
1572/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1573/// targets that want to expose additional information about sign bits to the
1574/// DAG Combiner.
1575unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
1576 unsigned Depth) const {
1577 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1578 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1579 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1580 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1581 "Should use ComputeNumSignBits if you don't know whether Op"
1582 " is a target node!");
1583 return 1;
1584}
1585
1586/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1587/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1588/// determine which bit is set.
1589///
1590static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
1591 // A left-shift of a constant one will have exactly one bit set, because
1592 // shifting the bit off the end is undefined.
1593 if (Val.getOpcode() == ISD::SHL)
1594 if (ConstantSDNode *C =
1595 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1596 if (C->getAPIntValue() == 1)
1597 return true;
1598
1599 // Similarly, a right-shift of a constant sign-bit will have exactly
1600 // one bit set.
1601 if (Val.getOpcode() == ISD::SRL)
1602 if (ConstantSDNode *C =
1603 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1604 if (C->getAPIntValue().isSignBit())
1605 return true;
1606
1607 // More could be done here, though the above checks are enough
1608 // to handle some common cases.
1609
1610 // Fall back to ComputeMaskedBits to catch other known cases.
1611 EVT OpVT = Val.getValueType();
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001612 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001613 APInt Mask = APInt::getAllOnesValue(BitWidth);
1614 APInt KnownZero, KnownOne;
1615 DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
1616 return (KnownZero.countPopulation() == BitWidth - 1) &&
1617 (KnownOne.countPopulation() == 1);
1618}
1619
1620/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1621/// and cc. If it is unable to simplify it, return a null SDValue.
1622SDValue
1623TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1624 ISD::CondCode Cond, bool foldBooleans,
1625 DAGCombinerInfo &DCI, DebugLoc dl) const {
1626 SelectionDAG &DAG = DCI.DAG;
1627 LLVMContext &Context = *DAG.getContext();
1628
1629 // These setcc operations always fold.
1630 switch (Cond) {
1631 default: break;
1632 case ISD::SETFALSE:
1633 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1634 case ISD::SETTRUE:
1635 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1636 }
1637
1638 if (isa<ConstantSDNode>(N0.getNode())) {
1639 // Ensure that the constant occurs on the RHS, and fold constant
1640 // comparisons.
1641 return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1642 }
1643
1644 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
1645 const APInt &C1 = N1C->getAPIntValue();
1646
1647 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1648 // equality comparison, then we're just comparing whether X itself is
1649 // zero.
1650 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1651 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1652 N0.getOperand(1).getOpcode() == ISD::Constant) {
1653 const APInt &ShAmt
1654 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1655 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1656 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1657 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1658 // (srl (ctlz x), 5) == 0 -> X != 0
1659 // (srl (ctlz x), 5) != 1 -> X != 0
1660 Cond = ISD::SETNE;
1661 } else {
1662 // (srl (ctlz x), 5) != 0 -> X == 0
1663 // (srl (ctlz x), 5) == 1 -> X == 0
1664 Cond = ISD::SETEQ;
1665 }
1666 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1667 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1668 Zero, Cond);
1669 }
1670 }
1671
1672 // If the LHS is '(and load, const)', the RHS is 0,
1673 // the test is for equality or unsigned, and all 1 bits of the const are
1674 // in the same partial word, see if we can shorten the load.
1675 if (DCI.isBeforeLegalize() &&
1676 N0.getOpcode() == ISD::AND && C1 == 0 &&
1677 N0.getNode()->hasOneUse() &&
1678 isa<LoadSDNode>(N0.getOperand(0)) &&
1679 N0.getOperand(0).getNode()->hasOneUse() &&
1680 isa<ConstantSDNode>(N0.getOperand(1))) {
1681 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
1682 APInt bestMask;
1683 unsigned bestWidth = 0, bestOffset = 0;
1684 if (!Lod->isVolatile() && Lod->isUnindexed()) {
1685 unsigned origWidth = N0.getValueType().getSizeInBits();
1686 unsigned maskWidth = origWidth;
1687 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
1688 // 8 bits, but have to be careful...
1689 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1690 origWidth = Lod->getMemoryVT().getSizeInBits();
1691 const APInt &Mask =
1692 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1693 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
1694 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
1695 for (unsigned offset=0; offset<origWidth/width; offset++) {
1696 if ((newMask & Mask) == Mask) {
1697 if (!TD->isLittleEndian())
1698 bestOffset = (origWidth/width - offset - 1) * (width/8);
1699 else
1700 bestOffset = (uint64_t)offset * (width/8);
1701 bestMask = Mask.lshr(offset * (width/8) * 8);
1702 bestWidth = width;
1703 break;
1704 }
1705 newMask = newMask << width;
1706 }
1707 }
1708 }
1709 if (bestWidth) {
1710 EVT newVT = EVT::getIntegerVT(Context, bestWidth);
1711 if (newVT.isRound()) {
1712 EVT PtrType = Lod->getOperand(1).getValueType();
1713 SDValue Ptr = Lod->getBasePtr();
1714 if (bestOffset != 0)
1715 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1716 DAG.getConstant(bestOffset, PtrType));
1717 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1718 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
1719 Lod->getSrcValue(),
1720 Lod->getSrcValueOffset() + bestOffset,
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001721 false, false, NewAlign);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001722 return DAG.getSetCC(dl, VT,
1723 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
1724 DAG.getConstant(bestMask.trunc(bestWidth),
1725 newVT)),
1726 DAG.getConstant(0LL, newVT), Cond);
1727 }
1728 }
1729 }
1730
1731 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1732 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1733 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1734
1735 // If the comparison constant has bits in the upper part, the
1736 // zero-extended value could never match.
1737 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1738 C1.getBitWidth() - InSize))) {
1739 switch (Cond) {
1740 case ISD::SETUGT:
1741 case ISD::SETUGE:
1742 case ISD::SETEQ: return DAG.getConstant(0, VT);
1743 case ISD::SETULT:
1744 case ISD::SETULE:
1745 case ISD::SETNE: return DAG.getConstant(1, VT);
1746 case ISD::SETGT:
1747 case ISD::SETGE:
1748 // True if the sign bit of C1 is set.
1749 return DAG.getConstant(C1.isNegative(), VT);
1750 case ISD::SETLT:
1751 case ISD::SETLE:
1752 // True if the sign bit of C1 isn't set.
1753 return DAG.getConstant(C1.isNonNegative(), VT);
1754 default:
1755 break;
1756 }
1757 }
1758
1759 // Otherwise, we can perform the comparison with the low bits.
1760 switch (Cond) {
1761 case ISD::SETEQ:
1762 case ISD::SETNE:
1763 case ISD::SETUGT:
1764 case ISD::SETUGE:
1765 case ISD::SETULT:
1766 case ISD::SETULE: {
1767 EVT newVT = N0.getOperand(0).getValueType();
1768 if (DCI.isBeforeLegalizeOps() ||
1769 (isOperationLegal(ISD::SETCC, newVT) &&
1770 getCondCodeAction(Cond, newVT)==Legal))
1771 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1772 DAG.getConstant(APInt(C1).trunc(InSize), newVT),
1773 Cond);
1774 break;
1775 }
1776 default:
1777 break; // todo, be more careful with signed comparisons
1778 }
1779 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001780 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001781 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1782 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
1783 EVT ExtDstTy = N0.getValueType();
1784 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1785
1786 // If the extended part has any inconsistent bits, it cannot ever
1787 // compare equal. In other words, they have to be all ones or all
1788 // zeros.
1789 APInt ExtBits =
1790 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
1791 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1792 return DAG.getConstant(Cond == ISD::SETNE, VT);
1793
1794 SDValue ZextOp;
1795 EVT Op0Ty = N0.getOperand(0).getValueType();
1796 if (Op0Ty == ExtSrcTy) {
1797 ZextOp = N0.getOperand(0);
1798 } else {
1799 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1800 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1801 DAG.getConstant(Imm, Op0Ty));
1802 }
1803 if (!DCI.isCalledByLegalizer())
1804 DCI.AddToWorklist(ZextOp.getNode());
1805 // Otherwise, make this a use of a zext.
1806 return DAG.getSetCC(dl, VT, ZextOp,
1807 DAG.getConstant(C1 & APInt::getLowBitsSet(
1808 ExtDstTyBits,
1809 ExtSrcTyBits),
1810 ExtDstTy),
1811 Cond);
1812 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1813 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001814 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001815 if (N0.getOpcode() == ISD::SETCC &&
1816 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001817 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
1818 if (TrueWhenTrue)
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001819 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001820 // Invert the condition.
1821 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1822 CC = ISD::getSetCCInverse(CC,
1823 N0.getOperand(0).getValueType().isInteger());
1824 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
1825 }
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001826
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001827 if ((N0.getOpcode() == ISD::XOR ||
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001828 (N0.getOpcode() == ISD::AND &&
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001829 N0.getOperand(0).getOpcode() == ISD::XOR &&
1830 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1831 isa<ConstantSDNode>(N0.getOperand(1)) &&
1832 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1833 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1834 // can only do this if the top bits are known zero.
1835 unsigned BitWidth = N0.getValueSizeInBits();
1836 if (DAG.MaskedValueIsZero(N0,
1837 APInt::getHighBitsSet(BitWidth,
1838 BitWidth-1))) {
1839 // Okay, get the un-inverted input value.
1840 SDValue Val;
1841 if (N0.getOpcode() == ISD::XOR)
1842 Val = N0.getOperand(0);
1843 else {
1844 assert(N0.getOpcode() == ISD::AND &&
1845 N0.getOperand(0).getOpcode() == ISD::XOR);
1846 // ((X^1)&1)^1 -> X & 1
1847 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1848 N0.getOperand(0).getOperand(0),
1849 N0.getOperand(1));
1850 }
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001851
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001852 return DAG.getSetCC(dl, VT, Val, N1,
1853 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1854 }
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001855 } else if (N1C->getAPIntValue() == 1 &&
1856 (VT == MVT::i1 ||
1857 getBooleanContents() == ZeroOrOneBooleanContent)) {
1858 SDValue Op0 = N0;
1859 if (Op0.getOpcode() == ISD::TRUNCATE)
1860 Op0 = Op0.getOperand(0);
1861
1862 if ((Op0.getOpcode() == ISD::XOR) &&
1863 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1864 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1865 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1866 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1867 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1868 Cond);
1869 } else if (Op0.getOpcode() == ISD::AND &&
1870 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1871 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
1872 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
1873 if (Op0.getValueType() != VT)
1874 Op0 = DAG.getNode(ISD::AND, dl, VT,
1875 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1876 DAG.getConstant(1, VT));
1877 return DAG.getSetCC(dl, VT, Op0,
1878 DAG.getConstant(0, Op0.getValueType()),
1879 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1880 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001881 }
1882 }
1883
1884 APInt MinVal, MaxVal;
1885 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1886 if (ISD::isSignedIntSetCC(Cond)) {
1887 MinVal = APInt::getSignedMinValue(OperandBitSize);
1888 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1889 } else {
1890 MinVal = APInt::getMinValue(OperandBitSize);
1891 MaxVal = APInt::getMaxValue(OperandBitSize);
1892 }
1893
1894 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1895 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1896 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1897 // X >= C0 --> X > (C0-1)
1898 return DAG.getSetCC(dl, VT, N0,
1899 DAG.getConstant(C1-1, N1.getValueType()),
1900 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1901 }
1902
1903 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1904 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1905 // X <= C0 --> X < (C0+1)
1906 return DAG.getSetCC(dl, VT, N0,
1907 DAG.getConstant(C1+1, N1.getValueType()),
1908 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1909 }
1910
1911 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1912 return DAG.getConstant(0, VT); // X < MIN --> false
1913 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1914 return DAG.getConstant(1, VT); // X >= MIN --> true
1915 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1916 return DAG.getConstant(0, VT); // X > MAX --> false
1917 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1918 return DAG.getConstant(1, VT); // X <= MAX --> true
1919
1920 // Canonicalize setgt X, Min --> setne X, Min
1921 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1922 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1923 // Canonicalize setlt X, Max --> setne X, Max
1924 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1925 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1926
1927 // If we have setult X, 1, turn it into seteq X, 0
1928 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1929 return DAG.getSetCC(dl, VT, N0,
1930 DAG.getConstant(MinVal, N0.getValueType()),
1931 ISD::SETEQ);
1932 // If we have setugt X, Max-1, turn it into seteq X, Max
1933 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1934 return DAG.getSetCC(dl, VT, N0,
1935 DAG.getConstant(MaxVal, N0.getValueType()),
1936 ISD::SETEQ);
1937
1938 // If we have "setcc X, C0", check to see if we can shrink the immediate
1939 // by changing cc.
1940
1941 // SETUGT X, SINTMAX -> SETLT X, 0
1942 if (Cond == ISD::SETUGT &&
1943 C1 == APInt::getSignedMaxValue(OperandBitSize))
1944 return DAG.getSetCC(dl, VT, N0,
1945 DAG.getConstant(0, N1.getValueType()),
1946 ISD::SETLT);
1947
1948 // SETULT X, SINTMIN -> SETGT X, -1
1949 if (Cond == ISD::SETULT &&
1950 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1951 SDValue ConstMinusOne =
1952 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1953 N1.getValueType());
1954 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1955 }
1956
1957 // Fold bit comparisons when we can.
1958 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1959 (VT == N0.getValueType() ||
1960 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1961 N0.getOpcode() == ISD::AND)
1962 if (ConstantSDNode *AndRHS =
1963 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1964 EVT ShiftTy = DCI.isBeforeLegalize() ?
1965 getPointerTy() : getShiftAmountTy();
1966 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1967 // Perform the xform if the AND RHS is a single bit.
1968 if (AndRHS->getAPIntValue().isPowerOf2()) {
1969 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1970 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1971 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
1972 }
1973 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
1974 // (X & 8) == 8 --> (X & 8) >> 3
1975 // Perform the xform if C1 is a single bit.
1976 if (C1.isPowerOf2()) {
1977 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1978 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1979 DAG.getConstant(C1.logBase2(), ShiftTy)));
1980 }
1981 }
1982 }
1983 }
1984
1985 if (isa<ConstantFPSDNode>(N0.getNode())) {
1986 // Constant fold or commute setcc.
1987 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
1988 if (O.getNode()) return O;
1989 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1990 // If the RHS of an FP comparison is a constant, simplify it away in
1991 // some cases.
1992 if (CFP->getValueAPF().isNaN()) {
1993 // If an operand is known to be a nan, we can fold it.
1994 switch (ISD::getUnorderedFlavor(Cond)) {
1995 default: llvm_unreachable("Unknown flavor!");
1996 case 0: // Known false.
1997 return DAG.getConstant(0, VT);
1998 case 1: // Known true.
1999 return DAG.getConstant(1, VT);
2000 case 2: // Undefined.
2001 return DAG.getUNDEF(VT);
2002 }
2003 }
2004
2005 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
2006 // constant if knowing that the operand is non-nan is enough. We prefer to
2007 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
2008 // materialize 0.0.
2009 if (Cond == ISD::SETO || Cond == ISD::SETUO)
2010 return DAG.getSetCC(dl, VT, N0, N0, Cond);
2011
2012 // If the condition is not legal, see if we can find an equivalent one
2013 // which is legal.
2014 if (!isCondCodeLegal(Cond, N0.getValueType())) {
2015 // If the comparison was an awkward floating-point == or != and one of
2016 // the comparison operands is infinity or negative infinity, convert the
2017 // condition to a less-awkward <= or >=.
2018 if (CFP->getValueAPF().isInfinity()) {
2019 if (CFP->getValueAPF().isNegative()) {
2020 if (Cond == ISD::SETOEQ &&
2021 isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
2022 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
2023 if (Cond == ISD::SETUEQ &&
2024 isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
2025 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
2026 if (Cond == ISD::SETUNE &&
2027 isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
2028 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
2029 if (Cond == ISD::SETONE &&
2030 isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
2031 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
2032 } else {
2033 if (Cond == ISD::SETOEQ &&
2034 isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
2035 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
2036 if (Cond == ISD::SETUEQ &&
2037 isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
2038 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
2039 if (Cond == ISD::SETUNE &&
2040 isCondCodeLegal(ISD::SETULT, N0.getValueType()))
2041 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
2042 if (Cond == ISD::SETONE &&
2043 isCondCodeLegal(ISD::SETULT, N0.getValueType()))
2044 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
2045 }
2046 }
2047 }
2048 }
2049
2050 if (N0 == N1) {
2051 // We can always fold X == X for integer setcc's.
2052 if (N0.getValueType().isInteger())
2053 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2054 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2055 if (UOF == 2) // FP operators that are undefined on NaNs.
2056 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2057 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2058 return DAG.getConstant(UOF, VT);
2059 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2060 // if it is not already.
2061 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2062 if (NewCond != Cond)
2063 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2064 }
2065
2066 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2067 N0.getValueType().isInteger()) {
2068 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2069 N0.getOpcode() == ISD::XOR) {
2070 // Simplify (X+Y) == (X+Z) --> Y == Z
2071 if (N0.getOpcode() == N1.getOpcode()) {
2072 if (N0.getOperand(0) == N1.getOperand(0))
2073 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2074 if (N0.getOperand(1) == N1.getOperand(1))
2075 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2076 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2077 // If X op Y == Y op X, try other combinations.
2078 if (N0.getOperand(0) == N1.getOperand(1))
2079 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2080 Cond);
2081 if (N0.getOperand(1) == N1.getOperand(0))
2082 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2083 Cond);
2084 }
2085 }
2086
2087 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2088 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2089 // Turn (X+C1) == C2 --> X == C2-C1
2090 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2091 return DAG.getSetCC(dl, VT, N0.getOperand(0),
2092 DAG.getConstant(RHSC->getAPIntValue()-
2093 LHSR->getAPIntValue(),
2094 N0.getValueType()), Cond);
2095 }
2096
2097 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2098 if (N0.getOpcode() == ISD::XOR)
2099 // If we know that all of the inverted bits are zero, don't bother
2100 // performing the inversion.
2101 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
2102 return
2103 DAG.getSetCC(dl, VT, N0.getOperand(0),
2104 DAG.getConstant(LHSR->getAPIntValue() ^
2105 RHSC->getAPIntValue(),
2106 N0.getValueType()),
2107 Cond);
2108 }
2109
2110 // Turn (C1-X) == C2 --> X == C1-C2
2111 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2112 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
2113 return
2114 DAG.getSetCC(dl, VT, N0.getOperand(1),
2115 DAG.getConstant(SUBC->getAPIntValue() -
2116 RHSC->getAPIntValue(),
2117 N0.getValueType()),
2118 Cond);
2119 }
2120 }
2121 }
2122
2123 // Simplify (X+Z) == X --> Z == 0
2124 if (N0.getOperand(0) == N1)
2125 return DAG.getSetCC(dl, VT, N0.getOperand(1),
2126 DAG.getConstant(0, N0.getValueType()), Cond);
2127 if (N0.getOperand(1) == N1) {
2128 if (DAG.isCommutativeBinOp(N0.getOpcode()))
2129 return DAG.getSetCC(dl, VT, N0.getOperand(0),
2130 DAG.getConstant(0, N0.getValueType()), Cond);
2131 else if (N0.getNode()->hasOneUse()) {
2132 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2133 // (Z-X) == X --> Z == X<<1
2134 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
2135 N1,
2136 DAG.getConstant(1, getShiftAmountTy()));
2137 if (!DCI.isCalledByLegalizer())
2138 DCI.AddToWorklist(SH.getNode());
2139 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2140 }
2141 }
2142 }
2143
2144 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2145 N1.getOpcode() == ISD::XOR) {
2146 // Simplify X == (X+Z) --> Z == 0
2147 if (N1.getOperand(0) == N0) {
2148 return DAG.getSetCC(dl, VT, N1.getOperand(1),
2149 DAG.getConstant(0, N1.getValueType()), Cond);
2150 } else if (N1.getOperand(1) == N0) {
2151 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
2152 return DAG.getSetCC(dl, VT, N1.getOperand(0),
2153 DAG.getConstant(0, N1.getValueType()), Cond);
2154 } else if (N1.getNode()->hasOneUse()) {
2155 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2156 // X == (Z-X) --> X<<1 == Z
2157 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
2158 DAG.getConstant(1, getShiftAmountTy()));
2159 if (!DCI.isCalledByLegalizer())
2160 DCI.AddToWorklist(SH.getNode());
2161 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2162 }
2163 }
2164 }
2165
2166 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
2167 // Note that where y is variable and is known to have at most
2168 // one bit set (for example, if it is z&1) we cannot do this;
2169 // the expressions are not equivalent when y==0.
2170 if (N0.getOpcode() == ISD::AND)
2171 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
2172 if (ValueHasExactlyOneBitSet(N1, DAG)) {
2173 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2174 SDValue Zero = DAG.getConstant(0, N1.getValueType());
2175 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
2176 }
2177 }
2178 if (N1.getOpcode() == ISD::AND)
2179 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
2180 if (ValueHasExactlyOneBitSet(N0, DAG)) {
2181 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2182 SDValue Zero = DAG.getConstant(0, N0.getValueType());
2183 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
2184 }
2185 }
2186 }
2187
2188 // Fold away ALL boolean setcc's.
2189 SDValue Temp;
2190 if (N0.getValueType() == MVT::i1 && foldBooleans) {
2191 switch (Cond) {
2192 default: llvm_unreachable("Unknown integer setcc!");
2193 case ISD::SETEQ: // X == Y -> ~(X^Y)
2194 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2195 N0 = DAG.getNOT(dl, Temp, MVT::i1);
2196 if (!DCI.isCalledByLegalizer())
2197 DCI.AddToWorklist(Temp.getNode());
2198 break;
2199 case ISD::SETNE: // X != Y --> (X^Y)
2200 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2201 break;
2202 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
2203 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
2204 Temp = DAG.getNOT(dl, N0, MVT::i1);
2205 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2206 if (!DCI.isCalledByLegalizer())
2207 DCI.AddToWorklist(Temp.getNode());
2208 break;
2209 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
2210 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
2211 Temp = DAG.getNOT(dl, N1, MVT::i1);
2212 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2213 if (!DCI.isCalledByLegalizer())
2214 DCI.AddToWorklist(Temp.getNode());
2215 break;
2216 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
2217 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
2218 Temp = DAG.getNOT(dl, N0, MVT::i1);
2219 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2220 if (!DCI.isCalledByLegalizer())
2221 DCI.AddToWorklist(Temp.getNode());
2222 break;
2223 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
2224 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
2225 Temp = DAG.getNOT(dl, N1, MVT::i1);
2226 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2227 break;
2228 }
2229 if (VT != MVT::i1) {
2230 if (!DCI.isCalledByLegalizer())
2231 DCI.AddToWorklist(N0.getNode());
2232 // FIXME: If running after legalize, we probably can't do this.
2233 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2234 }
2235 return N0;
2236 }
2237
2238 // Could not fold it.
2239 return SDValue();
2240}
2241
2242/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2243/// node is a GlobalAddress + offset.
2244bool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA,
2245 int64_t &Offset) const {
2246 if (isa<GlobalAddressSDNode>(N)) {
2247 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
2248 GA = GASD->getGlobal();
2249 Offset += GASD->getOffset();
2250 return true;
2251 }
2252
2253 if (N->getOpcode() == ISD::ADD) {
2254 SDValue N1 = N->getOperand(0);
2255 SDValue N2 = N->getOperand(1);
2256 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2257 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2258 if (V) {
2259 Offset += V->getSExtValue();
2260 return true;
2261 }
2262 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2263 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2264 if (V) {
2265 Offset += V->getSExtValue();
2266 return true;
2267 }
2268 }
2269 }
2270 return false;
2271}
2272
2273
2274SDValue TargetLowering::
2275PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
2276 // Default implementation: no optimization.
2277 return SDValue();
2278}
2279
2280//===----------------------------------------------------------------------===//
2281// Inline Assembler Implementation Methods
2282//===----------------------------------------------------------------------===//
2283
2284
2285TargetLowering::ConstraintType
2286TargetLowering::getConstraintType(const std::string &Constraint) const {
2287 // FIXME: lots more standard ones to handle.
2288 if (Constraint.size() == 1) {
2289 switch (Constraint[0]) {
2290 default: break;
2291 case 'r': return C_RegisterClass;
2292 case 'm': // memory
2293 case 'o': // offsetable
2294 case 'V': // not offsetable
2295 return C_Memory;
2296 case 'i': // Simple Integer or Relocatable Constant
2297 case 'n': // Simple Integer
2298 case 's': // Relocatable Constant
2299 case 'X': // Allow ANY value.
2300 case 'I': // Target registers.
2301 case 'J':
2302 case 'K':
2303 case 'L':
2304 case 'M':
2305 case 'N':
2306 case 'O':
2307 case 'P':
2308 return C_Other;
2309 }
2310 }
2311
2312 if (Constraint.size() > 1 && Constraint[0] == '{' &&
2313 Constraint[Constraint.size()-1] == '}')
2314 return C_Register;
2315 return C_Unknown;
2316}
2317
2318/// LowerXConstraint - try to replace an X constraint, which matches anything,
2319/// with another that has more specific requirements based on the type of the
2320/// corresponding operand.
2321const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
2322 if (ConstraintVT.isInteger())
2323 return "r";
2324 if (ConstraintVT.isFloatingPoint())
2325 return "f"; // works for many targets
2326 return 0;
2327}
2328
2329/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2330/// vector. If it is invalid, don't add anything to Ops.
2331void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2332 char ConstraintLetter,
2333 bool hasMemory,
2334 std::vector<SDValue> &Ops,
2335 SelectionDAG &DAG) const {
2336 switch (ConstraintLetter) {
2337 default: break;
2338 case 'X': // Allows any operand; labels (basic block) use this.
2339 if (Op.getOpcode() == ISD::BasicBlock) {
2340 Ops.push_back(Op);
2341 return;
2342 }
2343 // fall through
2344 case 'i': // Simple Integer or Relocatable Constant
2345 case 'n': // Simple Integer
2346 case 's': { // Relocatable Constant
2347 // These operands are interested in values of the form (GV+C), where C may
2348 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2349 // is possible and fine if either GV or C are missing.
2350 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2351 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2352
2353 // If we have "(add GV, C)", pull out GV/C
2354 if (Op.getOpcode() == ISD::ADD) {
2355 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2356 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2357 if (C == 0 || GA == 0) {
2358 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2359 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2360 }
2361 if (C == 0 || GA == 0)
2362 C = 0, GA = 0;
2363 }
2364
2365 // If we find a valid operand, map to the TargetXXX version so that the
2366 // value itself doesn't get selected.
2367 if (GA) { // Either &GV or &GV+C
2368 if (ConstraintLetter != 'n') {
2369 int64_t Offs = GA->getOffset();
2370 if (C) Offs += C->getZExtValue();
2371 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
2372 Op.getValueType(), Offs));
2373 return;
2374 }
2375 }
2376 if (C) { // just C, no GV.
2377 // Simple constants are not allowed for 's'.
2378 if (ConstraintLetter != 's') {
2379 // gcc prints these as sign extended. Sign extend value to 64 bits
2380 // now; without this it would get ZExt'd later in
2381 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2382 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
2383 MVT::i64));
2384 return;
2385 }
2386 }
2387 break;
2388 }
2389 }
2390}
2391
2392std::vector<unsigned> TargetLowering::
2393getRegClassForInlineAsmConstraint(const std::string &Constraint,
2394 EVT VT) const {
2395 return std::vector<unsigned>();
2396}
2397
2398
2399std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
2400getRegForInlineAsmConstraint(const std::string &Constraint,
2401 EVT VT) const {
2402 if (Constraint[0] != '{')
2403 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
2404 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2405
2406 // Remove the braces from around the name.
2407 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
2408
2409 // Figure out which register class contains this reg.
2410 const TargetRegisterInfo *RI = TM.getRegisterInfo();
2411 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
2412 E = RI->regclass_end(); RCI != E; ++RCI) {
2413 const TargetRegisterClass *RC = *RCI;
2414
2415 // If none of the value types for this register class are valid, we
2416 // can't use it. For example, 64-bit reg classes on 32-bit targets.
2417 bool isLegal = false;
2418 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
2419 I != E; ++I) {
2420 if (isTypeLegal(*I)) {
2421 isLegal = true;
2422 break;
2423 }
2424 }
2425
2426 if (!isLegal) continue;
2427
2428 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
2429 I != E; ++I) {
2430 if (RegName.equals_lower(RI->getName(*I)))
2431 return std::make_pair(*I, RC);
2432 }
2433 }
2434
2435 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
2436}
2437
2438//===----------------------------------------------------------------------===//
2439// Constraint Selection.
2440
2441/// isMatchingInputConstraint - Return true of this is an input operand that is
2442/// a matching constraint like "4".
2443bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
2444 assert(!ConstraintCode.empty() && "No known constraint!");
2445 return isdigit(ConstraintCode[0]);
2446}
2447
2448/// getMatchedOperand - If this is an input matching constraint, this method
2449/// returns the output operand it matches.
2450unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2451 assert(!ConstraintCode.empty() && "No known constraint!");
2452 return atoi(ConstraintCode.c_str());
2453}
2454
2455
2456/// getConstraintGenerality - Return an integer indicating how general CT
2457/// is.
2458static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2459 switch (CT) {
2460 default: llvm_unreachable("Unknown constraint type!");
2461 case TargetLowering::C_Other:
2462 case TargetLowering::C_Unknown:
2463 return 0;
2464 case TargetLowering::C_Register:
2465 return 1;
2466 case TargetLowering::C_RegisterClass:
2467 return 2;
2468 case TargetLowering::C_Memory:
2469 return 3;
2470 }
2471}
2472
2473/// ChooseConstraint - If there are multiple different constraints that we
2474/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
2475/// This is somewhat tricky: constraints fall into four classes:
2476/// Other -> immediates and magic values
2477/// Register -> one specific register
2478/// RegisterClass -> a group of regs
2479/// Memory -> memory
2480/// Ideally, we would pick the most specific constraint possible: if we have
2481/// something that fits into a register, we would pick it. The problem here
2482/// is that if we have something that could either be in a register or in
2483/// memory that use of the register could cause selection of *other*
2484/// operands to fail: they might only succeed if we pick memory. Because of
2485/// this the heuristic we use is:
2486///
2487/// 1) If there is an 'other' constraint, and if the operand is valid for
2488/// that constraint, use it. This makes us take advantage of 'i'
2489/// constraints when available.
2490/// 2) Otherwise, pick the most general constraint present. This prefers
2491/// 'm' over 'r', for example.
2492///
2493static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
2494 bool hasMemory, const TargetLowering &TLI,
2495 SDValue Op, SelectionDAG *DAG) {
2496 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2497 unsigned BestIdx = 0;
2498 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2499 int BestGenerality = -1;
2500
2501 // Loop over the options, keeping track of the most general one.
2502 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2503 TargetLowering::ConstraintType CType =
2504 TLI.getConstraintType(OpInfo.Codes[i]);
2505
2506 // If this is an 'other' constraint, see if the operand is valid for it.
2507 // For example, on X86 we might have an 'rI' constraint. If the operand
2508 // is an integer in the range [0..31] we want to use I (saving a load
2509 // of a register), otherwise we must use 'r'.
2510 if (CType == TargetLowering::C_Other && Op.getNode()) {
2511 assert(OpInfo.Codes[i].size() == 1 &&
2512 "Unhandled multi-letter 'other' constraint");
2513 std::vector<SDValue> ResultOps;
2514 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0], hasMemory,
2515 ResultOps, *DAG);
2516 if (!ResultOps.empty()) {
2517 BestType = CType;
2518 BestIdx = i;
2519 break;
2520 }
2521 }
2522
2523 // This constraint letter is more general than the previous one, use it.
2524 int Generality = getConstraintGenerality(CType);
2525 if (Generality > BestGenerality) {
2526 BestType = CType;
2527 BestIdx = i;
2528 BestGenerality = Generality;
2529 }
2530 }
2531
2532 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2533 OpInfo.ConstraintType = BestType;
2534}
2535
2536/// ComputeConstraintToUse - Determines the constraint code and constraint
2537/// type to use for the specific AsmOperandInfo, setting
2538/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
2539void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
2540 SDValue Op,
2541 bool hasMemory,
2542 SelectionDAG *DAG) const {
2543 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
2544
2545 // Single-letter constraints ('r') are very common.
2546 if (OpInfo.Codes.size() == 1) {
2547 OpInfo.ConstraintCode = OpInfo.Codes[0];
2548 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2549 } else {
2550 ChooseConstraint(OpInfo, hasMemory, *this, Op, DAG);
2551 }
2552
2553 // 'X' matches anything.
2554 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2555 // Labels and constants are handled elsewhere ('X' is the only thing
2556 // that matches labels). For Functions, the type here is the type of
2557 // the result, which is not what we want to look at; leave them alone.
2558 Value *v = OpInfo.CallOperandVal;
2559 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2560 OpInfo.CallOperandVal = v;
2561 return;
2562 }
2563
2564 // Otherwise, try to resolve it to something we know about by looking at
2565 // the actual operand type.
2566 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2567 OpInfo.ConstraintCode = Repl;
2568 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2569 }
2570 }
2571}
2572
2573//===----------------------------------------------------------------------===//
2574// Loop Strength Reduction hooks
2575//===----------------------------------------------------------------------===//
2576
2577/// isLegalAddressingMode - Return true if the addressing mode represented
2578/// by AM is legal for this target, for a load/store of the specified type.
2579bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
2580 const Type *Ty) const {
2581 // The default implementation of this implements a conservative RISCy, r+r and
2582 // r+i addr mode.
2583
2584 // Allows a sign-extended 16-bit immediate field.
2585 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
2586 return false;
2587
2588 // No global is ever allowed as a base.
2589 if (AM.BaseGV)
2590 return false;
2591
2592 // Only support r+r,
2593 switch (AM.Scale) {
2594 case 0: // "r+i" or just "i", depending on HasBaseReg.
2595 break;
2596 case 1:
2597 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
2598 return false;
2599 // Otherwise we have r+r or r+i.
2600 break;
2601 case 2:
2602 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
2603 return false;
2604 // Allow 2*r as r+r.
2605 break;
2606 }
2607
2608 return true;
2609}
2610
2611/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2612/// return a DAG expression to select that will generate the same value by
2613/// multiplying by a magic number. See:
2614/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2615SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
2616 std::vector<SDNode*>* Created) const {
2617 EVT VT = N->getValueType(0);
2618 DebugLoc dl= N->getDebugLoc();
2619
2620 // Check to see if we can do this.
2621 // FIXME: We should be more aggressive here.
2622 if (!isTypeLegal(VT))
2623 return SDValue();
2624
2625 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
2626 APInt::ms magics = d.magic();
2627
2628 // Multiply the numerator (operand 0) by the magic value
2629 // FIXME: We should support doing a MUL in a wider type
2630 SDValue Q;
2631 if (isOperationLegalOrCustom(ISD::MULHS, VT))
2632 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
2633 DAG.getConstant(magics.m, VT));
2634 else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
2635 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
2636 N->getOperand(0),
2637 DAG.getConstant(magics.m, VT)).getNode(), 1);
2638 else
2639 return SDValue(); // No mulhs or equvialent
2640 // If d > 0 and m < 0, add the numerator
2641 if (d.isStrictlyPositive() && magics.m.isNegative()) {
2642 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
2643 if (Created)
2644 Created->push_back(Q.getNode());
2645 }
2646 // If d < 0 and m > 0, subtract the numerator.
2647 if (d.isNegative() && magics.m.isStrictlyPositive()) {
2648 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
2649 if (Created)
2650 Created->push_back(Q.getNode());
2651 }
2652 // Shift right algebraic if shift value is nonzero
2653 if (magics.s > 0) {
2654 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
2655 DAG.getConstant(magics.s, getShiftAmountTy()));
2656 if (Created)
2657 Created->push_back(Q.getNode());
2658 }
2659 // Extract the sign bit and add it to the quotient
2660 SDValue T =
2661 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
2662 getShiftAmountTy()));
2663 if (Created)
2664 Created->push_back(T.getNode());
2665 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
2666}
2667
2668/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2669/// return a DAG expression to select that will generate the same value by
2670/// multiplying by a magic number. See:
2671/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2672SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
2673 std::vector<SDNode*>* Created) const {
2674 EVT VT = N->getValueType(0);
2675 DebugLoc dl = N->getDebugLoc();
2676
2677 // Check to see if we can do this.
2678 // FIXME: We should be more aggressive here.
2679 if (!isTypeLegal(VT))
2680 return SDValue();
2681
2682 // FIXME: We should use a narrower constant when the upper
2683 // bits are known to be zero.
2684 ConstantSDNode *N1C = cast<ConstantSDNode>(N->getOperand(1));
2685 APInt::mu magics = N1C->getAPIntValue().magicu();
2686
2687 // Multiply the numerator (operand 0) by the magic value
2688 // FIXME: We should support doing a MUL in a wider type
2689 SDValue Q;
2690 if (isOperationLegalOrCustom(ISD::MULHU, VT))
2691 Q = DAG.getNode(ISD::MULHU, dl, VT, N->getOperand(0),
2692 DAG.getConstant(magics.m, VT));
2693 else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
2694 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT),
2695 N->getOperand(0),
2696 DAG.getConstant(magics.m, VT)).getNode(), 1);
2697 else
2698 return SDValue(); // No mulhu or equvialent
2699 if (Created)
2700 Created->push_back(Q.getNode());
2701
2702 if (magics.a == 0) {
2703 assert(magics.s < N1C->getAPIntValue().getBitWidth() &&
2704 "We shouldn't generate an undefined shift!");
2705 return DAG.getNode(ISD::SRL, dl, VT, Q,
2706 DAG.getConstant(magics.s, getShiftAmountTy()));
2707 } else {
2708 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
2709 if (Created)
2710 Created->push_back(NPQ.getNode());
2711 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
2712 DAG.getConstant(1, getShiftAmountTy()));
2713 if (Created)
2714 Created->push_back(NPQ.getNode());
2715 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
2716 if (Created)
2717 Created->push_back(NPQ.getNode());
2718 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
2719 DAG.getConstant(magics.s-1, getShiftAmountTy()));
2720 }
2721}