blob: 34a31788ed02bfc0fa3a4a817f48d17ce758001e [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000015#include "llvm/MC/MCAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000016#include "llvm/Target/TargetData.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000017#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner310968c2005-01-07 07:44:53 +000018#include "llvm/Target/TargetMachine.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000019#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000020#include "llvm/Target/TargetSubtarget.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/GlobalVariable.h"
Chris Lattnerdc879292006-03-31 00:28:56 +000022#include "llvm/DerivedTypes.h"
Evan Chengad4196b2008-05-12 19:56:52 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner071c62f2010-01-25 23:26:13 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000025#include "llvm/CodeGen/SelectionDAG.h"
Owen Anderson718cb662007-09-07 04:06:50 +000026#include "llvm/ADT/STLExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000028#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000029using namespace llvm;
30
Rafael Espindola9a580232009-02-27 13:37:18 +000031namespace llvm {
32TLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
33 bool isLocal = GV->hasLocalLinkage();
34 bool isDeclaration = GV->isDeclaration();
35 // FIXME: what should we do for protected and internal visibility?
36 // For variables, is internal different from hidden?
37 bool isHidden = GV->hasHiddenVisibility();
38
39 if (reloc == Reloc::PIC_) {
40 if (isLocal || isHidden)
41 return TLSModel::LocalDynamic;
42 else
43 return TLSModel::GeneralDynamic;
44 } else {
45 if (!isDeclaration || isHidden)
46 return TLSModel::LocalExec;
47 else
48 return TLSModel::InitialExec;
49 }
50}
51}
52
Evan Cheng56966222007-01-12 02:11:51 +000053/// InitLibcallNames - Set default libcall names.
54///
Evan Cheng79cca502007-01-12 22:51:10 +000055static void InitLibcallNames(const char **Names) {
Anton Korobeynikovc31642f2009-05-03 13:14:08 +000056 Names[RTLIB::SHL_I16] = "__ashlhi3";
Evan Cheng56966222007-01-12 02:11:51 +000057 Names[RTLIB::SHL_I32] = "__ashlsi3";
58 Names[RTLIB::SHL_I64] = "__ashldi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000059 Names[RTLIB::SHL_I128] = "__ashlti3";
Anton Korobeynikovc31642f2009-05-03 13:14:08 +000060 Names[RTLIB::SRL_I16] = "__lshrhi3";
Evan Cheng56966222007-01-12 02:11:51 +000061 Names[RTLIB::SRL_I32] = "__lshrsi3";
62 Names[RTLIB::SRL_I64] = "__lshrdi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000063 Names[RTLIB::SRL_I128] = "__lshrti3";
Anton Korobeynikovc31642f2009-05-03 13:14:08 +000064 Names[RTLIB::SRA_I16] = "__ashrhi3";
Evan Cheng56966222007-01-12 02:11:51 +000065 Names[RTLIB::SRA_I32] = "__ashrsi3";
66 Names[RTLIB::SRA_I64] = "__ashrdi3";
Duncan Sandsdddc6292008-07-11 16:52:29 +000067 Names[RTLIB::SRA_I128] = "__ashrti3";
Anton Korobeynikov8983da72009-11-07 17:14:39 +000068 Names[RTLIB::MUL_I8] = "__mulqi3";
Anton Korobeynikovc31642f2009-05-03 13:14:08 +000069 Names[RTLIB::MUL_I16] = "__mulhi3";
Evan Cheng56966222007-01-12 02:11:51 +000070 Names[RTLIB::MUL_I32] = "__mulsi3";
71 Names[RTLIB::MUL_I64] = "__muldi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000072 Names[RTLIB::MUL_I128] = "__multi3";
Anton Korobeynikov8983da72009-11-07 17:14:39 +000073 Names[RTLIB::SDIV_I8] = "__divqi3";
Anton Korobeynikov813090c2009-05-03 13:18:16 +000074 Names[RTLIB::SDIV_I16] = "__divhi3";
Evan Cheng56966222007-01-12 02:11:51 +000075 Names[RTLIB::SDIV_I32] = "__divsi3";
76 Names[RTLIB::SDIV_I64] = "__divdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000077 Names[RTLIB::SDIV_I128] = "__divti3";
Anton Korobeynikov8983da72009-11-07 17:14:39 +000078 Names[RTLIB::UDIV_I8] = "__udivqi3";
Anton Korobeynikovfb3f84f2009-05-08 18:50:54 +000079 Names[RTLIB::UDIV_I16] = "__udivhi3";
Evan Cheng56966222007-01-12 02:11:51 +000080 Names[RTLIB::UDIV_I32] = "__udivsi3";
81 Names[RTLIB::UDIV_I64] = "__udivdi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000082 Names[RTLIB::UDIV_I128] = "__udivti3";
Anton Korobeynikov8983da72009-11-07 17:14:39 +000083 Names[RTLIB::SREM_I8] = "__modqi3";
Anton Korobeynikov813090c2009-05-03 13:18:16 +000084 Names[RTLIB::SREM_I16] = "__modhi3";
Evan Cheng56966222007-01-12 02:11:51 +000085 Names[RTLIB::SREM_I32] = "__modsi3";
86 Names[RTLIB::SREM_I64] = "__moddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000087 Names[RTLIB::SREM_I128] = "__modti3";
Anton Korobeynikov8983da72009-11-07 17:14:39 +000088 Names[RTLIB::UREM_I8] = "__umodqi3";
Anton Korobeynikov9fe9c8e2009-05-03 13:19:57 +000089 Names[RTLIB::UREM_I16] = "__umodhi3";
Evan Cheng56966222007-01-12 02:11:51 +000090 Names[RTLIB::UREM_I32] = "__umodsi3";
91 Names[RTLIB::UREM_I64] = "__umoddi3";
Duncan Sands5ac319a2008-07-10 15:35:05 +000092 Names[RTLIB::UREM_I128] = "__umodti3";
Evan Cheng56966222007-01-12 02:11:51 +000093 Names[RTLIB::NEG_I32] = "__negsi2";
94 Names[RTLIB::NEG_I64] = "__negdi2";
95 Names[RTLIB::ADD_F32] = "__addsf3";
96 Names[RTLIB::ADD_F64] = "__adddf3";
Duncan Sands007f9842008-01-10 10:28:30 +000097 Names[RTLIB::ADD_F80] = "__addxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +000098 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
Evan Cheng56966222007-01-12 02:11:51 +000099 Names[RTLIB::SUB_F32] = "__subsf3";
100 Names[RTLIB::SUB_F64] = "__subdf3";
Duncan Sands007f9842008-01-10 10:28:30 +0000101 Names[RTLIB::SUB_F80] = "__subxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +0000102 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
Evan Cheng56966222007-01-12 02:11:51 +0000103 Names[RTLIB::MUL_F32] = "__mulsf3";
104 Names[RTLIB::MUL_F64] = "__muldf3";
Duncan Sands007f9842008-01-10 10:28:30 +0000105 Names[RTLIB::MUL_F80] = "__mulxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +0000106 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
Evan Cheng56966222007-01-12 02:11:51 +0000107 Names[RTLIB::DIV_F32] = "__divsf3";
108 Names[RTLIB::DIV_F64] = "__divdf3";
Duncan Sands007f9842008-01-10 10:28:30 +0000109 Names[RTLIB::DIV_F80] = "__divxf3";
Dale Johannesen161e8972007-10-05 20:04:43 +0000110 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
Evan Cheng56966222007-01-12 02:11:51 +0000111 Names[RTLIB::REM_F32] = "fmodf";
112 Names[RTLIB::REM_F64] = "fmod";
Duncan Sands007f9842008-01-10 10:28:30 +0000113 Names[RTLIB::REM_F80] = "fmodl";
Dale Johannesen161e8972007-10-05 20:04:43 +0000114 Names[RTLIB::REM_PPCF128] = "fmodl";
Evan Cheng56966222007-01-12 02:11:51 +0000115 Names[RTLIB::POWI_F32] = "__powisf2";
116 Names[RTLIB::POWI_F64] = "__powidf2";
Dale Johannesen161e8972007-10-05 20:04:43 +0000117 Names[RTLIB::POWI_F80] = "__powixf2";
118 Names[RTLIB::POWI_PPCF128] = "__powitf2";
Evan Cheng56966222007-01-12 02:11:51 +0000119 Names[RTLIB::SQRT_F32] = "sqrtf";
120 Names[RTLIB::SQRT_F64] = "sqrt";
Dale Johannesen161e8972007-10-05 20:04:43 +0000121 Names[RTLIB::SQRT_F80] = "sqrtl";
122 Names[RTLIB::SQRT_PPCF128] = "sqrtl";
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000123 Names[RTLIB::LOG_F32] = "logf";
124 Names[RTLIB::LOG_F64] = "log";
125 Names[RTLIB::LOG_F80] = "logl";
126 Names[RTLIB::LOG_PPCF128] = "logl";
127 Names[RTLIB::LOG2_F32] = "log2f";
128 Names[RTLIB::LOG2_F64] = "log2";
129 Names[RTLIB::LOG2_F80] = "log2l";
130 Names[RTLIB::LOG2_PPCF128] = "log2l";
131 Names[RTLIB::LOG10_F32] = "log10f";
132 Names[RTLIB::LOG10_F64] = "log10";
133 Names[RTLIB::LOG10_F80] = "log10l";
134 Names[RTLIB::LOG10_PPCF128] = "log10l";
135 Names[RTLIB::EXP_F32] = "expf";
136 Names[RTLIB::EXP_F64] = "exp";
137 Names[RTLIB::EXP_F80] = "expl";
138 Names[RTLIB::EXP_PPCF128] = "expl";
139 Names[RTLIB::EXP2_F32] = "exp2f";
140 Names[RTLIB::EXP2_F64] = "exp2";
141 Names[RTLIB::EXP2_F80] = "exp2l";
142 Names[RTLIB::EXP2_PPCF128] = "exp2l";
Evan Cheng56966222007-01-12 02:11:51 +0000143 Names[RTLIB::SIN_F32] = "sinf";
144 Names[RTLIB::SIN_F64] = "sin";
Duncan Sands007f9842008-01-10 10:28:30 +0000145 Names[RTLIB::SIN_F80] = "sinl";
146 Names[RTLIB::SIN_PPCF128] = "sinl";
Evan Cheng56966222007-01-12 02:11:51 +0000147 Names[RTLIB::COS_F32] = "cosf";
148 Names[RTLIB::COS_F64] = "cos";
Duncan Sands007f9842008-01-10 10:28:30 +0000149 Names[RTLIB::COS_F80] = "cosl";
150 Names[RTLIB::COS_PPCF128] = "cosl";
Dan Gohmane54be102007-10-11 23:09:10 +0000151 Names[RTLIB::POW_F32] = "powf";
152 Names[RTLIB::POW_F64] = "pow";
153 Names[RTLIB::POW_F80] = "powl";
154 Names[RTLIB::POW_PPCF128] = "powl";
Dan Gohman2bb1e3e2008-08-21 18:38:14 +0000155 Names[RTLIB::CEIL_F32] = "ceilf";
156 Names[RTLIB::CEIL_F64] = "ceil";
157 Names[RTLIB::CEIL_F80] = "ceill";
158 Names[RTLIB::CEIL_PPCF128] = "ceill";
159 Names[RTLIB::TRUNC_F32] = "truncf";
160 Names[RTLIB::TRUNC_F64] = "trunc";
161 Names[RTLIB::TRUNC_F80] = "truncl";
162 Names[RTLIB::TRUNC_PPCF128] = "truncl";
163 Names[RTLIB::RINT_F32] = "rintf";
164 Names[RTLIB::RINT_F64] = "rint";
165 Names[RTLIB::RINT_F80] = "rintl";
166 Names[RTLIB::RINT_PPCF128] = "rintl";
167 Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
168 Names[RTLIB::NEARBYINT_F64] = "nearbyint";
169 Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
170 Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
171 Names[RTLIB::FLOOR_F32] = "floorf";
172 Names[RTLIB::FLOOR_F64] = "floor";
173 Names[RTLIB::FLOOR_F80] = "floorl";
174 Names[RTLIB::FLOOR_PPCF128] = "floorl";
Evan Cheng56966222007-01-12 02:11:51 +0000175 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
176 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000177 Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
178 Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
179 Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
180 Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
Sanjiv Gupta7d8d36a2009-06-16 10:22:58 +0000181 Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfi8";
182 Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfi16";
Evan Cheng56966222007-01-12 02:11:51 +0000183 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
184 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000185 Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
Evan Cheng56966222007-01-12 02:11:51 +0000186 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
187 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000188 Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
Duncan Sandsbe1ad4d2008-07-10 15:33:02 +0000189 Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000190 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000191 Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000192 Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000193 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000194 Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
Sanjiv Gupta7d8d36a2009-06-16 10:22:58 +0000195 Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfi8";
196 Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfi16";
Evan Cheng56966222007-01-12 02:11:51 +0000197 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
198 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000199 Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
Evan Cheng56966222007-01-12 02:11:51 +0000200 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
201 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000202 Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
Dale Johannesen161e8972007-10-05 20:04:43 +0000203 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
204 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000205 Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
Duncan Sands041cde22008-06-25 20:24:48 +0000206 Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
Dale Johannesen161e8972007-10-05 20:04:43 +0000207 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
Dan Gohmana2e94852008-03-10 23:03:31 +0000208 Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
Evan Cheng56966222007-01-12 02:11:51 +0000209 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
210 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
Duncan Sands9bed0f52008-07-11 16:57:02 +0000211 Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
212 Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
Evan Cheng56966222007-01-12 02:11:51 +0000213 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
214 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
Dale Johannesen161e8972007-10-05 20:04:43 +0000215 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
216 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
Dan Gohmand91446d2008-03-05 01:08:17 +0000217 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
218 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
219 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
220 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
Evan Cheng56966222007-01-12 02:11:51 +0000221 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
222 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
Duncan Sandsac6cece2008-07-11 17:00:14 +0000223 Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
224 Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
Evan Cheng56966222007-01-12 02:11:51 +0000225 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
226 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
Duncan Sandsac6cece2008-07-11 17:00:14 +0000227 Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
228 Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
229 Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
230 Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
231 Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
232 Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
Evan Cheng56966222007-01-12 02:11:51 +0000233 Names[RTLIB::OEQ_F32] = "__eqsf2";
234 Names[RTLIB::OEQ_F64] = "__eqdf2";
235 Names[RTLIB::UNE_F32] = "__nesf2";
236 Names[RTLIB::UNE_F64] = "__nedf2";
237 Names[RTLIB::OGE_F32] = "__gesf2";
238 Names[RTLIB::OGE_F64] = "__gedf2";
239 Names[RTLIB::OLT_F32] = "__ltsf2";
240 Names[RTLIB::OLT_F64] = "__ltdf2";
241 Names[RTLIB::OLE_F32] = "__lesf2";
242 Names[RTLIB::OLE_F64] = "__ledf2";
243 Names[RTLIB::OGT_F32] = "__gtsf2";
244 Names[RTLIB::OGT_F64] = "__gtdf2";
245 Names[RTLIB::UO_F32] = "__unordsf2";
246 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +0000247 Names[RTLIB::O_F32] = "__unordsf2";
248 Names[RTLIB::O_F64] = "__unorddf2";
Sanjiv Guptaa114baa2009-07-30 09:12:56 +0000249 Names[RTLIB::MEMCPY] = "memcpy";
250 Names[RTLIB::MEMMOVE] = "memmove";
251 Names[RTLIB::MEMSET] = "memset";
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000252 Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
Evan Chengd385fd62007-01-31 09:29:11 +0000253}
254
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000255/// InitLibcallCallingConvs - Set default libcall CallingConvs.
256///
257static void InitLibcallCallingConvs(CallingConv::ID *CCs) {
258 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
259 CCs[i] = CallingConv::C;
260 }
261}
262
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000263/// getFPEXT - Return the FPEXT_*_* value for the given types, or
264/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000265RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 if (OpVT == MVT::f32) {
267 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000268 return FPEXT_F32_F64;
269 }
270 return UNKNOWN_LIBCALL;
271}
272
273/// getFPROUND - Return the FPROUND_*_* value for the given types, or
274/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000275RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 if (RetVT == MVT::f32) {
277 if (OpVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000278 return FPROUND_F64_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 if (OpVT == MVT::f80)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000280 return FPROUND_F80_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 if (OpVT == MVT::ppcf128)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000282 return FPROUND_PPCF128_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 } else if (RetVT == MVT::f64) {
284 if (OpVT == MVT::f80)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000285 return FPROUND_F80_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 if (OpVT == MVT::ppcf128)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +0000287 return FPROUND_PPCF128_F64;
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000288 }
289 return UNKNOWN_LIBCALL;
290}
291
292/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
293/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000294RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 if (OpVT == MVT::f32) {
296 if (RetVT == MVT::i8)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000297 return FPTOSINT_F32_I8;
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 if (RetVT == MVT::i16)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000299 return FPTOSINT_F32_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000301 return FPTOSINT_F32_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000303 return FPTOSINT_F32_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000305 return FPTOSINT_F32_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 } else if (OpVT == MVT::f64) {
307 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000308 return FPTOSINT_F64_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000310 return FPTOSINT_F64_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000312 return FPTOSINT_F64_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 } else if (OpVT == MVT::f80) {
314 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000315 return FPTOSINT_F80_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000317 return FPTOSINT_F80_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000319 return FPTOSINT_F80_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 } else if (OpVT == MVT::ppcf128) {
321 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000322 return FPTOSINT_PPCF128_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000324 return FPTOSINT_PPCF128_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000326 return FPTOSINT_PPCF128_I128;
327 }
328 return UNKNOWN_LIBCALL;
329}
330
331/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
332/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000333RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 if (OpVT == MVT::f32) {
335 if (RetVT == MVT::i8)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000336 return FPTOUINT_F32_I8;
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 if (RetVT == MVT::i16)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000338 return FPTOUINT_F32_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000340 return FPTOUINT_F32_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000342 return FPTOUINT_F32_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000344 return FPTOUINT_F32_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 } else if (OpVT == MVT::f64) {
346 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000347 return FPTOUINT_F64_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000349 return FPTOUINT_F64_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000351 return FPTOUINT_F64_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 } else if (OpVT == MVT::f80) {
353 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000354 return FPTOUINT_F80_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000356 return FPTOUINT_F80_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000358 return FPTOUINT_F80_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 } else if (OpVT == MVT::ppcf128) {
360 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000361 return FPTOUINT_PPCF128_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000363 return FPTOUINT_PPCF128_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000365 return FPTOUINT_PPCF128_I128;
366 }
367 return UNKNOWN_LIBCALL;
368}
369
370/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
371/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000372RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 if (OpVT == MVT::i32) {
374 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000375 return SINTTOFP_I32_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000377 return SINTTOFP_I32_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000379 return SINTTOFP_I32_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000381 return SINTTOFP_I32_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 } else if (OpVT == MVT::i64) {
383 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000384 return SINTTOFP_I64_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000386 return SINTTOFP_I64_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000388 return SINTTOFP_I64_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000390 return SINTTOFP_I64_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 } else if (OpVT == MVT::i128) {
392 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000393 return SINTTOFP_I128_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000395 return SINTTOFP_I128_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000397 return SINTTOFP_I128_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000399 return SINTTOFP_I128_PPCF128;
400 }
401 return UNKNOWN_LIBCALL;
402}
403
404/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
405/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000406RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 if (OpVT == MVT::i32) {
408 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000409 return UINTTOFP_I32_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000411 return UINTTOFP_I32_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000413 return UINTTOFP_I32_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000415 return UINTTOFP_I32_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 } else if (OpVT == MVT::i64) {
417 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000418 return UINTTOFP_I64_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000420 return UINTTOFP_I64_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000422 return UINTTOFP_I64_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000424 return UINTTOFP_I64_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 } else if (OpVT == MVT::i128) {
426 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000427 return UINTTOFP_I128_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 else if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000429 return UINTTOFP_I128_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 else if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000431 return UINTTOFP_I128_F80;
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 else if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000433 return UINTTOFP_I128_PPCF128;
434 }
435 return UNKNOWN_LIBCALL;
436}
437
Evan Chengd385fd62007-01-31 09:29:11 +0000438/// InitCmpLibcallCCs - Set default comparison libcall CC.
439///
440static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
441 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
442 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
443 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
444 CCs[RTLIB::UNE_F32] = ISD::SETNE;
445 CCs[RTLIB::UNE_F64] = ISD::SETNE;
446 CCs[RTLIB::OGE_F32] = ISD::SETGE;
447 CCs[RTLIB::OGE_F64] = ISD::SETGE;
448 CCs[RTLIB::OLT_F32] = ISD::SETLT;
449 CCs[RTLIB::OLT_F64] = ISD::SETLT;
450 CCs[RTLIB::OLE_F32] = ISD::SETLE;
451 CCs[RTLIB::OLE_F64] = ISD::SETLE;
452 CCs[RTLIB::OGT_F32] = ISD::SETGT;
453 CCs[RTLIB::OGT_F64] = ISD::SETGT;
454 CCs[RTLIB::UO_F32] = ISD::SETNE;
455 CCs[RTLIB::UO_F64] = ISD::SETNE;
456 CCs[RTLIB::O_F32] = ISD::SETEQ;
457 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000458}
459
Chris Lattnerf0144122009-07-28 03:13:23 +0000460/// NOTE: The constructor takes ownership of TLOF.
461TargetLowering::TargetLowering(TargetMachine &tm,TargetLoweringObjectFile *tlof)
462 : TM(tm), TD(TM.getTargetData()), TLOF(*tlof) {
Chris Lattnercba82f92005-01-16 07:28:11 +0000463 // All operations default to being supported.
464 memset(OpActions, 0, sizeof(OpActions));
Evan Cheng03294662008-10-14 21:26:46 +0000465 memset(LoadExtActions, 0, sizeof(LoadExtActions));
Chris Lattnerddf89562008-01-17 19:59:44 +0000466 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
Chris Lattnerc9133f92008-01-18 19:36:20 +0000467 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
468 memset(ConvertActions, 0, sizeof(ConvertActions));
Evan Cheng7f042682008-10-15 02:05:31 +0000469 memset(CondCodeActions, 0, sizeof(CondCodeActions));
Dan Gohman93f81e22007-07-09 20:49:44 +0000470
Chris Lattner1a3048b2007-12-22 20:47:56 +0000471 // Set default actions for various operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
Chris Lattner1a3048b2007-12-22 20:47:56 +0000473 // Default all indexed load / store to expand.
Evan Cheng5ff839f2006-11-09 18:56:43 +0000474 for (unsigned IM = (unsigned)ISD::PRE_INC;
475 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
477 setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000478 }
Chris Lattner1a3048b2007-12-22 20:47:56 +0000479
480 // These operations default to expand.
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
482 setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
Evan Cheng5ff839f2006-11-09 18:56:43 +0000483 }
Evan Chengd2cde682008-03-10 19:38:10 +0000484
485 // Most targets ignore the @llvm.prefetch intrinsic.
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
Nate Begemane1795842008-02-14 08:57:00 +0000487
488 // ConstantFP nodes default to expand. Targets can either change this to
Evan Chengeb2f9692009-10-27 19:56:55 +0000489 // Legal, in which case all fp constants are legal, or use isFPImmLegal()
Nate Begemane1795842008-02-14 08:57:00 +0000490 // to optimize expansions for certain constants.
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
492 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
493 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Chris Lattner310968c2005-01-07 07:44:53 +0000494
Dale Johannesen0bb41602008-09-22 21:57:32 +0000495 // These library functions default to expand.
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FLOG , MVT::f64, Expand);
497 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
498 setOperationAction(ISD::FLOG10,MVT::f64, Expand);
499 setOperationAction(ISD::FEXP , MVT::f64, Expand);
500 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
501 setOperationAction(ISD::FLOG , MVT::f32, Expand);
502 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
503 setOperationAction(ISD::FLOG10,MVT::f32, Expand);
504 setOperationAction(ISD::FEXP , MVT::f32, Expand);
505 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
Dale Johannesen0bb41602008-09-22 21:57:32 +0000506
Chris Lattner41bab0b2008-01-15 21:58:08 +0000507 // Default ISD::TRAP to expand (which turns it into abort).
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::TRAP, MVT::Other, Expand);
Chris Lattner41bab0b2008-01-15 21:58:08 +0000509
Owen Andersona69571c2006-05-03 01:29:57 +0000510 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000511 UsesGlobalOffsetTable = false;
Owen Anderson1d0be152009-08-13 21:58:54 +0000512 ShiftAmountTy = PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Owen Anderson718cb662007-09-07 04:06:50 +0000514 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000515 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000516 benefitFromCodePlacementOpt = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000517 UseUnderscoreSetJmp = false;
518 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000519 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000520 IntDivIsCheap = false;
521 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000522 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000523 ExceptionPointerRegister = 0;
524 ExceptionSelectorRegister = 0;
Duncan Sands03228082008-11-23 15:47:28 +0000525 BooleanContents = UndefinedBooleanContent;
Evan Cheng0577a222006-01-25 18:52:42 +0000526 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000527 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000528 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000529 IfCvtBlockSizeLimit = 2;
Evan Chengfb8075d2008-02-28 00:43:03 +0000530 IfCvtDupBlockSizeLimit = 0;
531 PrefLoopAlignment = 0;
Evan Cheng56966222007-01-12 02:11:51 +0000532
533 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000534 InitCmpLibcallCCs(CmpLibcallCCs);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000535 InitLibcallCallingConvs(LibcallCallingConvs);
Chris Lattner310968c2005-01-07 07:44:53 +0000536}
537
Chris Lattnerf0144122009-07-28 03:13:23 +0000538TargetLowering::~TargetLowering() {
539 delete &TLOF;
540}
Chris Lattnercba82f92005-01-16 07:28:11 +0000541
Owen Anderson23b9b192009-08-12 00:36:31 +0000542static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
543 unsigned &NumIntermediates,
544 EVT &RegisterVT,
545 TargetLowering* TLI) {
546 // Figure out the right, legal destination reg to copy into.
547 unsigned NumElts = VT.getVectorNumElements();
548 MVT EltTy = VT.getVectorElementType();
549
550 unsigned NumVectorRegs = 1;
551
552 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
553 // could break down into LHS/RHS like LegalizeDAG does.
554 if (!isPowerOf2_32(NumElts)) {
555 NumVectorRegs = NumElts;
556 NumElts = 1;
557 }
558
559 // Divide the input until we get to a supported size. This will always
560 // end with a scalar if the target doesn't support vectors.
561 while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
562 NumElts >>= 1;
563 NumVectorRegs <<= 1;
564 }
565
566 NumIntermediates = NumVectorRegs;
567
568 MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
569 if (!TLI->isTypeLegal(NewVT))
570 NewVT = EltTy;
571 IntermediateVT = NewVT;
572
573 EVT DestVT = TLI->getRegisterType(NewVT);
574 RegisterVT = DestVT;
575 if (EVT(DestVT).bitsLT(NewVT)) {
576 // Value is expanded, e.g. i64 -> i16.
577 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
578 } else {
579 // Otherwise, promotion or legal types use the same number of registers as
580 // the vector decimated to the appropriate level.
581 return NumVectorRegs;
582 }
583
584 return 1;
585}
586
Chris Lattner310968c2005-01-07 07:44:53 +0000587/// computeRegisterProperties - Once all of the register classes are added,
588/// this allows us to compute derived properties we expose.
589void TargetLowering::computeRegisterProperties() {
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000591 "Too many value types for ValueTypeActions to hold!");
592
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000593 // Everything defaults to needing one register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000595 NumRegistersForVT[i] = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000597 }
598 // ...except isVoid, which doesn't need any registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 NumRegistersForVT[MVT::isVoid] = 0;
Misha Brukmanf976c852005-04-21 22:55:34 +0000600
Chris Lattner310968c2005-01-07 07:44:53 +0000601 // Find the largest integer register class.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
Chris Lattner310968c2005-01-07 07:44:53 +0000603 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
Chris Lattner310968c2005-01-07 07:44:53 +0000605
606 // Every integer value type larger than this largest register takes twice as
607 // many registers to represent as the previous ValueType.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000608 for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
Dan Gohman8a55ce42009-09-23 21:02:20 +0000609 EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
610 if (!ExpandedVT.isInteger())
Duncan Sands83ec4b62008-06-06 12:08:01 +0000611 break;
Dan Gohmanb9f10192007-06-21 14:42:22 +0000612 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
614 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
Dan Gohman8a55ce42009-09-23 21:02:20 +0000615 ValueTypeActions.setTypeAction(ExpandedVT, Expand);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000616 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000617
618 // Inspect all of the ValueType's smaller than the largest integer
619 // register to see which ones need promotion.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000620 unsigned LegalIntReg = LargestIntReg;
621 for (unsigned IntReg = LargestIntReg - 1;
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 IntReg >= (unsigned)MVT::i1; --IntReg) {
623 EVT IVT = (MVT::SimpleValueType)IntReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000624 if (isTypeLegal(IVT)) {
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000625 LegalIntReg = IntReg;
626 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000627 RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 (MVT::SimpleValueType)LegalIntReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 ValueTypeActions.setTypeAction(IVT, Promote);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000630 }
631 }
632
Dale Johannesen161e8972007-10-05 20:04:43 +0000633 // ppcf128 type is really two f64's.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 if (!isTypeLegal(MVT::ppcf128)) {
635 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
636 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
637 TransformToType[MVT::ppcf128] = MVT::f64;
638 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
Dale Johannesen161e8972007-10-05 20:04:43 +0000639 }
640
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000641 // Decide how to handle f64. If the target does not have native f64 support,
642 // expand it to i64 and we will be generating soft float library calls.
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 if (!isTypeLegal(MVT::f64)) {
644 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
645 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
646 TransformToType[MVT::f64] = MVT::i64;
647 ValueTypeActions.setTypeAction(MVT::f64, Expand);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000648 }
649
650 // Decide how to handle f32. If the target does not have native support for
651 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 if (!isTypeLegal(MVT::f32)) {
653 if (isTypeLegal(MVT::f64)) {
654 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
655 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
656 TransformToType[MVT::f32] = MVT::f64;
657 ValueTypeActions.setTypeAction(MVT::f32, Promote);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000658 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
660 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
661 TransformToType[MVT::f32] = MVT::i32;
662 ValueTypeActions.setTypeAction(MVT::f32, Expand);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000663 }
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000664 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000665
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000666 // Loop over all of the vector value types to see which need transformations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
668 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000669 MVT VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000670 if (!isTypeLegal(VT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000671 MVT IntermediateVT;
672 EVT RegisterVT;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000673 unsigned NumIntermediates;
674 NumRegistersForVT[i] =
Owen Anderson23b9b192009-08-12 00:36:31 +0000675 getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
676 RegisterVT, this);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000677 RegisterTypeForVT[i] = RegisterVT;
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000678
679 // Determine if there is a legal wider type.
680 bool IsLegalWiderType = false;
Owen Andersone50ed302009-08-10 22:56:29 +0000681 EVT EltVT = VT.getVectorElementType();
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000682 unsigned NElts = VT.getVectorNumElements();
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
684 EVT SVT = (MVT::SimpleValueType)nVT;
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000685 if (isTypeLegal(SVT) && SVT.getVectorElementType() == EltVT &&
Mon P Wang6fb474b2010-01-24 00:24:43 +0000686 SVT.getVectorNumElements() > NElts && NElts != 1) {
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000687 TransformToType[i] = SVT;
688 ValueTypeActions.setTypeAction(VT, Promote);
689 IsLegalWiderType = true;
690 break;
691 }
692 }
693 if (!IsLegalWiderType) {
Owen Andersone50ed302009-08-10 22:56:29 +0000694 EVT NVT = VT.getPow2VectorType();
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000695 if (NVT == VT) {
696 // Type is already a power of 2. The default action is to split.
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 TransformToType[i] = MVT::Other;
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000698 ValueTypeActions.setTypeAction(VT, Expand);
699 } else {
700 TransformToType[i] = NVT;
701 ValueTypeActions.setTypeAction(VT, Promote);
702 }
703 }
Dan Gohman7f321562007-06-25 16:23:39 +0000704 }
Chris Lattner3a5935842006-03-16 19:50:01 +0000705 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000706}
Chris Lattnercba82f92005-01-16 07:28:11 +0000707
Evan Cheng72261582005-12-20 06:22:03 +0000708const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
709 return NULL;
710}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000711
Scott Michel5b8f82e2008-03-10 15:42:14 +0000712
Owen Anderson825b72b2009-08-11 20:47:22 +0000713MVT::SimpleValueType TargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson1d0be152009-08-13 21:58:54 +0000714 return PointerTy.SimpleTy;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000715}
716
Sanjiv Gupta8f17a362009-12-28 02:40:33 +0000717MVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
718 return MVT::i32; // return the default value
719}
720
Dan Gohman7f321562007-06-25 16:23:39 +0000721/// getVectorTypeBreakdown - Vector types are broken down into some number of
Owen Anderson825b72b2009-08-11 20:47:22 +0000722/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
723/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
724/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
Chris Lattnerdc879292006-03-31 00:28:56 +0000725///
Dan Gohman7f321562007-06-25 16:23:39 +0000726/// This method returns the number of registers needed, and the VT for each
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000727/// register. It also returns the VT and quantity of the intermediate values
728/// before they are promoted/expanded.
Chris Lattnerdc879292006-03-31 00:28:56 +0000729///
Owen Anderson23b9b192009-08-12 00:36:31 +0000730unsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
Owen Andersone50ed302009-08-10 22:56:29 +0000731 EVT &IntermediateVT,
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000732 unsigned &NumIntermediates,
Owen Anderson23b9b192009-08-12 00:36:31 +0000733 EVT &RegisterVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000734 // Figure out the right, legal destination reg to copy into.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000735 unsigned NumElts = VT.getVectorNumElements();
Owen Andersone50ed302009-08-10 22:56:29 +0000736 EVT EltTy = VT.getVectorElementType();
Chris Lattnerdc879292006-03-31 00:28:56 +0000737
738 unsigned NumVectorRegs = 1;
739
Nate Begemand73ab882007-11-27 19:28:48 +0000740 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
741 // could break down into LHS/RHS like LegalizeDAG does.
742 if (!isPowerOf2_32(NumElts)) {
743 NumVectorRegs = NumElts;
744 NumElts = 1;
745 }
746
Chris Lattnerdc879292006-03-31 00:28:56 +0000747 // Divide the input until we get to a supported size. This will always
748 // end with a scalar if the target doesn't support vectors.
Owen Anderson23b9b192009-08-12 00:36:31 +0000749 while (NumElts > 1 && !isTypeLegal(
750 EVT::getVectorVT(Context, EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000751 NumElts >>= 1;
752 NumVectorRegs <<= 1;
753 }
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000754
755 NumIntermediates = NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000756
Owen Anderson23b9b192009-08-12 00:36:31 +0000757 EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
Dan Gohman7f321562007-06-25 16:23:39 +0000758 if (!isTypeLegal(NewVT))
759 NewVT = EltTy;
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000760 IntermediateVT = NewVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000761
Owen Anderson23b9b192009-08-12 00:36:31 +0000762 EVT DestVT = getRegisterType(Context, NewVT);
Dan Gohmanb6f5b002007-06-28 23:29:44 +0000763 RegisterVT = DestVT;
Duncan Sands8e4eb092008-06-08 20:54:56 +0000764 if (DestVT.bitsLT(NewVT)) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000765 // Value is expanded, e.g. i64 -> i16.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
Chris Lattnerdc879292006-03-31 00:28:56 +0000767 } else {
768 // Otherwise, promotion or legal types use the same number of registers as
769 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000770 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000771 }
772
Evan Chenge9b3da12006-05-17 18:10:06 +0000773 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000774}
775
Mon P Wang0c397192008-10-30 08:01:45 +0000776/// getWidenVectorType: given a vector type, returns the type to widen to
777/// (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000778/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +0000779/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +0000780/// scalarizing vs using the wider vector type.
Owen Andersone50ed302009-08-10 22:56:29 +0000781EVT TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +0000782 assert(VT.isVector());
783 if (isTypeLegal(VT))
784 return VT;
785
786 // Default is not to widen until moved to LegalizeTypes
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +0000788}
789
Evan Cheng3ae05432008-01-24 00:22:01 +0000790/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000791/// function arguments in the caller parameter area. This is the actual
792/// alignment, not its logarithm.
Evan Cheng3ae05432008-01-24 00:22:01 +0000793unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000794 return TD->getCallFrameTypeAlignment(Ty);
Evan Cheng3ae05432008-01-24 00:22:01 +0000795}
796
Chris Lattner071c62f2010-01-25 23:26:13 +0000797/// getJumpTableEncoding - Return the entry encoding for a jump table in the
798/// current function. The returned value is a member of the
799/// MachineJumpTableInfo::JTEntryKind enum.
800unsigned TargetLowering::getJumpTableEncoding() const {
801 // In non-pic modes, just use the address of a block.
802 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
803 return MachineJumpTableInfo::EK_BlockAddress;
804
805 // In PIC mode, if the target supports a GPRel32 directive, use it.
806 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
807 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
808
809 // Otherwise, use a label difference.
810 return MachineJumpTableInfo::EK_LabelDifference32;
811}
812
Dan Gohman475871a2008-07-27 21:46:04 +0000813SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
814 SelectionDAG &DAG) const {
Evan Chengcc415862007-11-09 01:32:10 +0000815 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000816 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000817 return Table;
818}
819
Chris Lattner13e97a22010-01-26 05:30:30 +0000820/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
821/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
822/// MCExpr.
823const MCExpr *
824TargetLowering::getPICJumpTableRelocBaseExpr(const MachineJumpTableInfo *MJTI,
825 unsigned JTI,
826 MCContext &Ctx) const {
827 assert(0 && "FIXME: IMPLEMENT ME");
828}
829
830
Dan Gohman6520e202008-10-18 02:06:02 +0000831bool
832TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
833 // Assume that everything is safe in static mode.
834 if (getTargetMachine().getRelocationModel() == Reloc::Static)
835 return true;
836
837 // In dynamic-no-pic mode, assume that known defined values are safe.
838 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
839 GA &&
840 !GA->getGlobal()->isDeclaration() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000841 !GA->getGlobal()->isWeakForLinker())
Dan Gohman6520e202008-10-18 02:06:02 +0000842 return true;
843
844 // Otherwise assume nothing is safe.
845 return false;
846}
847
Chris Lattnereb8146b2006-02-04 02:13:02 +0000848//===----------------------------------------------------------------------===//
849// Optimization Methods
850//===----------------------------------------------------------------------===//
851
Nate Begeman368e18d2006-02-16 21:11:51 +0000852/// ShrinkDemandedConstant - Check to see if the specified operand of the
853/// specified instruction is a constant integer. If so, check to see if there
854/// are any bits set in the constant that are not demanded. If so, shrink the
855/// constant and return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000856bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000857 const APInt &Demanded) {
Dale Johannesende064702009-02-06 21:50:26 +0000858 DebugLoc dl = Op.getDebugLoc();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000859
Chris Lattnerec665152006-02-26 23:36:02 +0000860 // FIXME: ISD::SELECT, ISD::SELECT_CC
Dan Gohmane5af2d32009-01-29 01:59:02 +0000861 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000862 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000863 case ISD::XOR:
Bill Wendling36ae6c12009-03-04 00:18:06 +0000864 case ISD::AND:
865 case ISD::OR: {
866 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
867 if (!C) return false;
868
869 if (Op.getOpcode() == ISD::XOR &&
870 (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
871 return false;
872
873 // if we can expand it to have all bits set, do it
874 if (C->getAPIntValue().intersects(~Demanded)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000875 EVT VT = Op.getValueType();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000876 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
877 DAG.getConstant(Demanded &
878 C->getAPIntValue(),
879 VT));
880 return CombineTo(Op, New);
881 }
882
Nate Begemande996292006-02-03 22:24:05 +0000883 break;
884 }
Bill Wendling36ae6c12009-03-04 00:18:06 +0000885 }
886
Nate Begemande996292006-02-03 22:24:05 +0000887 return false;
888}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000889
Dan Gohman97121ba2009-04-08 00:15:30 +0000890/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
891/// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
892/// cast, but it could be generalized for targets with other types of
893/// implicit widening casts.
894bool
895TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
896 unsigned BitWidth,
897 const APInt &Demanded,
898 DebugLoc dl) {
899 assert(Op.getNumOperands() == 2 &&
900 "ShrinkDemandedOp only supports binary operators!");
901 assert(Op.getNode()->getNumValues() == 1 &&
902 "ShrinkDemandedOp only supports nodes with one result!");
903
904 // Don't do this if the node has another user, which may require the
905 // full value.
906 if (!Op.getNode()->hasOneUse())
907 return false;
908
909 // Search for the smallest integer type with free casts to and from
910 // Op's type. For expedience, just check power-of-2 integer types.
911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
912 unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
913 if (!isPowerOf2_32(SmallVTBits))
914 SmallVTBits = NextPowerOf2(SmallVTBits);
915 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000916 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
Dan Gohman97121ba2009-04-08 00:15:30 +0000917 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
918 TLI.isZExtFree(SmallVT, Op.getValueType())) {
919 // We found a type with free casts.
920 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
921 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
922 Op.getNode()->getOperand(0)),
923 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
924 Op.getNode()->getOperand(1)));
925 SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
926 return CombineTo(Op, Z);
927 }
928 }
929 return false;
930}
931
Nate Begeman368e18d2006-02-16 21:11:51 +0000932/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
933/// DemandedMask bits of the result of Op are ever used downstream. If we can
934/// use this information to simplify Op, create a new simplified DAG node and
935/// return true, returning the original and new nodes in Old and New. Otherwise,
936/// analyze the expression and return a mask of KnownOne and KnownZero bits for
937/// the expression (used to simplify the caller). The KnownZero/One bits may
938/// only be accurate for those bits in the DemandedMask.
Dan Gohman475871a2008-07-27 21:46:04 +0000939bool TargetLowering::SimplifyDemandedBits(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000940 const APInt &DemandedMask,
941 APInt &KnownZero,
942 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000943 TargetLoweringOpt &TLO,
944 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000945 unsigned BitWidth = DemandedMask.getBitWidth();
Dan Gohman87862e72009-12-11 21:31:27 +0000946 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000947 "Mask size mismatches value type size!");
948 APInt NewMask = DemandedMask;
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000949 DebugLoc dl = Op.getDebugLoc();
Chris Lattner3fc5b012007-05-17 18:19:23 +0000950
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000951 // Don't know anything.
952 KnownZero = KnownOne = APInt(BitWidth, 0);
953
Nate Begeman368e18d2006-02-16 21:11:51 +0000954 // Other users may use these bits.
Gabor Greifba36cb52008-08-28 21:40:38 +0000955 if (!Op.getNode()->hasOneUse()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000956 if (Depth != 0) {
957 // If not at the root, Just compute the KnownZero/KnownOne bits to
958 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000959 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000960 return false;
961 }
962 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000963 // just set the NewMask to all bits.
964 NewMask = APInt::getAllOnesValue(BitWidth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000965 } else if (DemandedMask == 0) {
966 // Not demanding any bits from Op.
967 if (Op.getOpcode() != ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +0000968 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
Nate Begeman368e18d2006-02-16 21:11:51 +0000969 return false;
970 } else if (Depth == 6) { // Limit search depth.
971 return false;
972 }
973
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000974 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000975 switch (Op.getOpcode()) {
976 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000977 // We know all of the bits for a constant!
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000978 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
979 KnownZero = ~KnownOne & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000980 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000981 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000982 // If the RHS is a constant, check to see if the LHS would be zero without
983 // using the bits from the RHS. Below, we use knowledge about the RHS to
984 // simplify the LHS, here we're using information from the LHS to simplify
985 // the RHS.
986 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000987 APInt LHSZero, LHSOne;
988 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
Dan Gohmanea859be2007-06-22 14:59:07 +0000989 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000990 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000991 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000992 return TLO.CombineTo(Op, Op.getOperand(0));
993 // If any of the set bits in the RHS are known zero on the LHS, shrink
994 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000995 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000996 return true;
997 }
998
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000999 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00001000 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001001 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +00001002 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001003 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +00001004 KnownZero2, KnownOne2, TLO, Depth+1))
1005 return true;
1006 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1007
1008 // If all of the demanded bits are known one on one side, return the other.
1009 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001010 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001011 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001012 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001013 return TLO.CombineTo(Op, Op.getOperand(1));
1014 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001015 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +00001016 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1017 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001018 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001019 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +00001020 // If the operation can be done in a smaller type, do so.
Evan Chengd40d03e2010-01-06 19:38:29 +00001021 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001022 return true;
1023
Nate Begeman368e18d2006-02-16 21:11:51 +00001024 // Output known-1 bits are only known if set in both the LHS & RHS.
1025 KnownOne &= KnownOne2;
1026 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1027 KnownZero |= KnownZero2;
1028 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001029 case ISD::OR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001030 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00001031 KnownOne, TLO, Depth+1))
1032 return true;
1033 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001034 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +00001035 KnownZero2, KnownOne2, TLO, Depth+1))
1036 return true;
1037 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1038
1039 // If all of the demanded bits are known zero on one side, return the other.
1040 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001041 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001042 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001043 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001044 return TLO.CombineTo(Op, Op.getOperand(1));
1045 // If all of the potentially set bits on one side are known to be set on
1046 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001047 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001048 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001049 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001050 return TLO.CombineTo(Op, Op.getOperand(1));
1051 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001052 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001053 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +00001054 // If the operation can be done in a smaller type, do so.
Evan Chengd40d03e2010-01-06 19:38:29 +00001055 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001056 return true;
1057
Nate Begeman368e18d2006-02-16 21:11:51 +00001058 // Output known-0 bits are only known if clear in both the LHS & RHS.
1059 KnownZero &= KnownZero2;
1060 // Output known-1 are known to be set if set in either the LHS | RHS.
1061 KnownOne |= KnownOne2;
1062 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001063 case ISD::XOR:
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001064 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00001065 KnownOne, TLO, Depth+1))
1066 return true;
1067 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001068 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +00001069 KnownOne2, TLO, Depth+1))
1070 return true;
1071 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1072
1073 // If all of the demanded bits are known zero on one side, return the other.
1074 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001075 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +00001076 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001077 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +00001078 return TLO.CombineTo(Op, Op.getOperand(1));
Dan Gohman97121ba2009-04-08 00:15:30 +00001079 // If the operation can be done in a smaller type, do so.
Evan Chengd40d03e2010-01-06 19:38:29 +00001080 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001081 return true;
1082
Chris Lattner3687c1a2006-11-27 21:50:02 +00001083 // If all of the unknown bits are known to be zero on one side or the other
1084 // (but not both) turn this into an *inclusive* or.
1085 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001086 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Dale Johannesende064702009-02-06 21:50:26 +00001087 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
Chris Lattner3687c1a2006-11-27 21:50:02 +00001088 Op.getOperand(0),
1089 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +00001090
1091 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1092 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1093 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1094 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1095
Nate Begeman368e18d2006-02-16 21:11:51 +00001096 // If all of the demanded bits on one side are known, and all of the set
1097 // bits on that side are also known to be set on the other side, turn this
1098 // into an AND, as we know the bits will be cleared.
1099 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001100 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
Nate Begeman368e18d2006-02-16 21:11:51 +00001101 if ((KnownOne & KnownOne2) == KnownOne) {
Owen Andersone50ed302009-08-10 22:56:29 +00001102 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001103 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001104 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1105 Op.getOperand(0), ANDC));
Nate Begeman368e18d2006-02-16 21:11:51 +00001106 }
1107 }
1108
1109 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +00001110 // for XOR, we prefer to force bits to 1 if they will make a -1.
1111 // if we can't force bits, try to shrink constant
1112 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1113 APInt Expanded = C->getAPIntValue() | (~NewMask);
1114 // if we can expand it to have all bits set, do it
1115 if (Expanded.isAllOnesValue()) {
1116 if (Expanded != C->getAPIntValue()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001117 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001118 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
Torok Edwin4fea2e92008-04-06 21:23:02 +00001119 TLO.DAG.getConstant(Expanded, VT));
1120 return TLO.CombineTo(Op, New);
1121 }
1122 // if it already has all the bits set, nothing to change
1123 // but don't shrink either!
1124 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
1125 return true;
1126 }
1127 }
1128
Nate Begeman368e18d2006-02-16 21:11:51 +00001129 KnownZero = KnownZeroOut;
1130 KnownOne = KnownOneOut;
1131 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001132 case ISD::SELECT:
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001133 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00001134 KnownOne, TLO, Depth+1))
1135 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001136 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +00001137 KnownOne2, TLO, Depth+1))
1138 return true;
1139 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1140 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1141
1142 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001143 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +00001144 return true;
1145
1146 // Only known if known in both the LHS and RHS.
1147 KnownOne &= KnownOne2;
1148 KnownZero &= KnownZero2;
1149 break;
Chris Lattnerec665152006-02-26 23:36:02 +00001150 case ISD::SELECT_CC:
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001151 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +00001152 KnownOne, TLO, Depth+1))
1153 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001154 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +00001155 KnownOne2, TLO, Depth+1))
1156 return true;
1157 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1158 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1159
1160 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001161 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +00001162 return true;
1163
1164 // Only known if known in both the LHS and RHS.
1165 KnownOne &= KnownOne2;
1166 KnownZero &= KnownZero2;
1167 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001168 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +00001169 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001170 unsigned ShAmt = SA->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001171 SDValue InOp = Op.getOperand(0);
Chris Lattner895c4ab2007-04-17 21:14:16 +00001172
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001173 // If the shift count is an invalid immediate, don't do anything.
1174 if (ShAmt >= BitWidth)
1175 break;
1176
Chris Lattner895c4ab2007-04-17 21:14:16 +00001177 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1178 // single shift. We can do this if the bottom bits (which are shifted
1179 // out) are never demanded.
1180 if (InOp.getOpcode() == ISD::SRL &&
1181 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001182 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001183 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +00001184 unsigned Opc = ISD::SHL;
1185 int Diff = ShAmt-C1;
1186 if (Diff < 0) {
1187 Diff = -Diff;
1188 Opc = ISD::SRL;
1189 }
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191 SDValue NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +00001192 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00001193 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001194 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +00001195 InOp.getOperand(0), NewSA));
1196 }
1197 }
1198
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001199 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +00001200 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001201 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001202 KnownZero <<= SA->getZExtValue();
1203 KnownOne <<= SA->getZExtValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001204 // low bits known zero.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001205 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001206 }
1207 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001208 case ISD::SRL:
1209 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +00001210 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001211 unsigned ShAmt = SA->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001212 unsigned VTSize = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00001213 SDValue InOp = Op.getOperand(0);
Chris Lattner895c4ab2007-04-17 21:14:16 +00001214
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001215 // If the shift count is an invalid immediate, don't do anything.
1216 if (ShAmt >= BitWidth)
1217 break;
1218
Chris Lattner895c4ab2007-04-17 21:14:16 +00001219 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1220 // single shift. We can do this if the top bits (which are shifted out)
1221 // are never demanded.
1222 if (InOp.getOpcode() == ISD::SHL &&
1223 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001224 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001225 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +00001226 unsigned Opc = ISD::SRL;
1227 int Diff = ShAmt-C1;
1228 if (Diff < 0) {
1229 Diff = -Diff;
1230 Opc = ISD::SHL;
1231 }
1232
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +00001234 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001235 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +00001236 InOp.getOperand(0), NewSA));
1237 }
1238 }
Nate Begeman368e18d2006-02-16 21:11:51 +00001239
1240 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001241 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +00001242 KnownZero, KnownOne, TLO, Depth+1))
1243 return true;
1244 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001245 KnownZero = KnownZero.lshr(ShAmt);
1246 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +00001247
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001248 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +00001249 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +00001250 }
1251 break;
1252 case ISD::SRA:
Dan Gohmane5af2d32009-01-29 01:59:02 +00001253 // If this is an arithmetic shift right and only the low-bit is set, we can
1254 // always convert this into a logical shr, even if the shift amount is
1255 // variable. The low bit of the shift cannot be an input sign bit unless
1256 // the shift amount is >= the size of the datatype, which is undefined.
1257 if (DemandedMask == 1)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001258 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
Dan Gohmane5af2d32009-01-29 01:59:02 +00001259 Op.getOperand(0), Op.getOperand(1)));
1260
Nate Begeman368e18d2006-02-16 21:11:51 +00001261 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +00001262 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001263 unsigned ShAmt = SA->getZExtValue();
Nate Begeman368e18d2006-02-16 21:11:51 +00001264
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001265 // If the shift count is an invalid immediate, don't do anything.
1266 if (ShAmt >= BitWidth)
1267 break;
1268
1269 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +00001270
1271 // If any of the demanded bits are produced by the sign extension, we also
1272 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001273 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1274 if (HighBits.intersects(NewMask))
Dan Gohman87862e72009-12-11 21:31:27 +00001275 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
Chris Lattner1b737132006-05-08 17:22:53 +00001276
1277 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +00001278 KnownZero, KnownOne, TLO, Depth+1))
1279 return true;
1280 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001281 KnownZero = KnownZero.lshr(ShAmt);
1282 KnownOne = KnownOne.lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +00001283
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001284 // Handle the sign bit, adjusted to where it is now in the mask.
1285 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Nate Begeman368e18d2006-02-16 21:11:51 +00001286
1287 // If the input sign bit is known to be zero, or if none of the top bits
1288 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001289 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001290 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1291 Op.getOperand(0),
Nate Begeman368e18d2006-02-16 21:11:51 +00001292 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001293 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +00001294 KnownOne |= HighBits;
1295 }
1296 }
1297 break;
1298 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001299 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Nate Begeman368e18d2006-02-16 21:11:51 +00001300
Chris Lattnerec665152006-02-26 23:36:02 +00001301 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +00001302 // present in the input.
Dan Gohmand1996362010-01-09 02:13:55 +00001303 APInt NewBits =
1304 APInt::getHighBitsSet(BitWidth,
1305 BitWidth - EVT.getScalarType().getSizeInBits()) &
1306 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +00001307
Chris Lattnerec665152006-02-26 23:36:02 +00001308 // If none of the extended bits are demanded, eliminate the sextinreg.
1309 if (NewBits == 0)
1310 return TLO.CombineTo(Op, Op.getOperand(0));
1311
Dan Gohmand1996362010-01-09 02:13:55 +00001312 APInt InSignBit = APInt::getSignBit(EVT.getScalarType().getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001313 InSignBit.zext(BitWidth);
Dan Gohmand1996362010-01-09 02:13:55 +00001314 APInt InputDemandedBits =
1315 APInt::getLowBitsSet(BitWidth,
1316 EVT.getScalarType().getSizeInBits()) &
1317 NewMask;
Nate Begeman368e18d2006-02-16 21:11:51 +00001318
Chris Lattnerec665152006-02-26 23:36:02 +00001319 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +00001320 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +00001321 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +00001322
1323 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1324 KnownZero, KnownOne, TLO, Depth+1))
1325 return true;
1326 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1327
1328 // If the sign bit of the input is known set or clear, then we know the
1329 // top bits of the result.
1330
Chris Lattnerec665152006-02-26 23:36:02 +00001331 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001332 if (KnownZero.intersects(InSignBit))
Chris Lattnerec665152006-02-26 23:36:02 +00001333 return TLO.CombineTo(Op,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001334 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
Chris Lattnerec665152006-02-26 23:36:02 +00001335
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001336 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +00001337 KnownOne |= NewBits;
1338 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +00001339 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +00001340 KnownZero &= ~NewBits;
1341 KnownOne &= ~NewBits;
1342 }
1343 break;
1344 }
Chris Lattnerec665152006-02-26 23:36:02 +00001345 case ISD::ZERO_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +00001346 unsigned OperandBitWidth =
1347 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001348 APInt InMask = NewMask;
1349 InMask.trunc(OperandBitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001350
1351 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001352 APInt NewBits =
1353 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
1354 if (!NewBits.intersects(NewMask))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001355 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
Chris Lattnerec665152006-02-26 23:36:02 +00001356 Op.getValueType(),
1357 Op.getOperand(0)));
1358
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001359 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001360 KnownZero, KnownOne, TLO, Depth+1))
1361 return true;
1362 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001363 KnownZero.zext(BitWidth);
1364 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001365 KnownZero |= NewBits;
1366 break;
1367 }
1368 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001369 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohmand1996362010-01-09 02:13:55 +00001370 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001371 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +00001372 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001373 APInt NewBits = ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001374
1375 // If none of the top bits are demanded, convert this into an any_extend.
1376 if (NewBits == 0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001377 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1378 Op.getValueType(),
1379 Op.getOperand(0)));
Chris Lattnerec665152006-02-26 23:36:02 +00001380
1381 // Since some of the sign extended bits are demanded, we know that the sign
1382 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001383 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001384 InDemandedBits |= InSignBit;
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001385 InDemandedBits.trunc(InBits);
Chris Lattnerec665152006-02-26 23:36:02 +00001386
1387 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1388 KnownOne, TLO, Depth+1))
1389 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001390 KnownZero.zext(BitWidth);
1391 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001392
1393 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001394 if (KnownZero.intersects(InSignBit))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001395 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
Chris Lattnerec665152006-02-26 23:36:02 +00001396 Op.getValueType(),
1397 Op.getOperand(0)));
1398
1399 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001400 if (KnownOne.intersects(InSignBit)) {
Chris Lattnerec665152006-02-26 23:36:02 +00001401 KnownOne |= NewBits;
1402 KnownZero &= ~NewBits;
1403 } else { // Otherwise, top bits aren't known.
1404 KnownOne &= ~NewBits;
1405 KnownZero &= ~NewBits;
1406 }
1407 break;
1408 }
1409 case ISD::ANY_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +00001410 unsigned OperandBitWidth =
1411 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001412 APInt InMask = NewMask;
1413 InMask.trunc(OperandBitWidth);
1414 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001415 KnownZero, KnownOne, TLO, Depth+1))
1416 return true;
1417 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001418 KnownZero.zext(BitWidth);
1419 KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001420 break;
1421 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001422 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001423 // Simplify the input, using demanded bit information, and compute the known
1424 // zero/one bits live out.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001425 APInt TruncMask = NewMask;
1426 TruncMask.zext(Op.getOperand(0).getValueSizeInBits());
1427 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001428 KnownZero, KnownOne, TLO, Depth+1))
1429 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001430 KnownZero.trunc(BitWidth);
1431 KnownOne.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001432
1433 // If the input is only used by this truncate, see if we can shrink it based
1434 // on the known demanded bits.
Gabor Greifba36cb52008-08-28 21:40:38 +00001435 if (Op.getOperand(0).getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue In = Op.getOperand(0);
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001437 unsigned InBitWidth = In.getValueSizeInBits();
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001438 switch (In.getOpcode()) {
1439 default: break;
1440 case ISD::SRL:
1441 // Shrink SRL by a constant if none of the high bits shifted in are
1442 // demanded.
1443 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001444 APInt HighBits = APInt::getHighBitsSet(InBitWidth,
1445 InBitWidth - BitWidth);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001446 HighBits = HighBits.lshr(ShAmt->getZExtValue());
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001447 HighBits.trunc(BitWidth);
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001448
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001449 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001450 // None of the shifted in bits are needed. Add a truncate of the
1451 // shift input, then shift it.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001452 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001453 Op.getValueType(),
1454 In.getOperand(0));
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001455 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1456 Op.getValueType(),
1457 NewTrunc,
1458 In.getOperand(1)));
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001459 }
1460 }
1461 break;
1462 }
1463 }
1464
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001465 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001466 break;
1467 }
Chris Lattnerec665152006-02-26 23:36:02 +00001468 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001470 APInt InMask = APInt::getLowBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001471 VT.getSizeInBits());
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001472 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001473 KnownZero, KnownOne, TLO, Depth+1))
1474 return true;
1475 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001476 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001477 break;
1478 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001479 case ISD::BIT_CONVERT:
1480#if 0
1481 // If this is an FP->Int bitcast and if the sign bit is the only thing that
1482 // is demanded, turn this into a FGETSIGN.
Owen Andersone50ed302009-08-10 22:56:29 +00001483 if (NewMask == EVT::getIntegerVTSignBit(Op.getValueType()) &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
1485 !MVT::isVector(Op.getOperand(0).getValueType())) {
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001486 // Only do this xform if FGETSIGN is valid or if before legalize.
1487 if (!TLO.AfterLegalize ||
1488 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
1489 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1490 // place. We expect the SHL to be eliminated by other optimizations.
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001492 Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001493 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Dan Gohman475871a2008-07-27 21:46:04 +00001494 SDValue ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001495 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
1496 Sign, ShAmt));
1497 }
1498 }
1499#endif
1500 break;
Dan Gohman97121ba2009-04-08 00:15:30 +00001501 case ISD::ADD:
1502 case ISD::MUL:
1503 case ISD::SUB: {
1504 // Add, Sub, and Mul don't demand any bits in positions beyond that
1505 // of the highest bit demanded of them.
1506 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1507 BitWidth - NewMask.countLeadingZeros());
1508 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1509 KnownOne2, TLO, Depth+1))
1510 return true;
1511 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1512 KnownOne2, TLO, Depth+1))
1513 return true;
1514 // See if the operation should be performed at a smaller bit width.
Evan Chengd40d03e2010-01-06 19:38:29 +00001515 if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001516 return true;
1517 }
1518 // FALL THROUGH
Dan Gohman54eed372008-05-06 00:53:29 +00001519 default:
Chris Lattner1482b5f2006-04-02 06:15:09 +00001520 // Just use ComputeMaskedBits to compute output bits.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001521 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001522 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001523 }
Chris Lattnerec665152006-02-26 23:36:02 +00001524
1525 // If we know the value of all of the demanded bits, return this as a
1526 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001527 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001528 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1529
Nate Begeman368e18d2006-02-16 21:11:51 +00001530 return false;
1531}
1532
Nate Begeman368e18d2006-02-16 21:11:51 +00001533/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1534/// in Mask are known to be either zero or one and return them in the
1535/// KnownZero/KnownOne bitsets.
Dan Gohman475871a2008-07-27 21:46:04 +00001536void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001537 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001538 APInt &KnownZero,
1539 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001541 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001542 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1543 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1544 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1545 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001546 "Should use MaskedValueIsZero if you don't know whether Op"
1547 " is a target node!");
Dan Gohman977a76f2008-02-13 22:28:48 +00001548 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001549}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001550
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001551/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1552/// targets that want to expose additional information about sign bits to the
1553/// DAG Combiner.
Dan Gohman475871a2008-07-27 21:46:04 +00001554unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001555 unsigned Depth) const {
1556 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1557 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1558 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1559 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1560 "Should use ComputeNumSignBits if you don't know whether Op"
1561 " is a target node!");
1562 return 1;
1563}
1564
Dan Gohman97d11632009-02-15 23:59:32 +00001565/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1566/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1567/// determine which bit is set.
1568///
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001569static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
Dan Gohman97d11632009-02-15 23:59:32 +00001570 // A left-shift of a constant one will have exactly one bit set, because
1571 // shifting the bit off the end is undefined.
1572 if (Val.getOpcode() == ISD::SHL)
1573 if (ConstantSDNode *C =
1574 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1575 if (C->getAPIntValue() == 1)
1576 return true;
Dan Gohmane5af2d32009-01-29 01:59:02 +00001577
Dan Gohman97d11632009-02-15 23:59:32 +00001578 // Similarly, a right-shift of a constant sign-bit will have exactly
1579 // one bit set.
1580 if (Val.getOpcode() == ISD::SRL)
1581 if (ConstantSDNode *C =
1582 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1583 if (C->getAPIntValue().isSignBit())
1584 return true;
1585
1586 // More could be done here, though the above checks are enough
1587 // to handle some common cases.
1588
1589 // Fall back to ComputeMaskedBits to catch other known cases.
Owen Andersone50ed302009-08-10 22:56:29 +00001590 EVT OpVT = Val.getValueType();
Dan Gohmane5af2d32009-01-29 01:59:02 +00001591 unsigned BitWidth = OpVT.getSizeInBits();
1592 APInt Mask = APInt::getAllOnesValue(BitWidth);
1593 APInt KnownZero, KnownOne;
1594 DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001595 return (KnownZero.countPopulation() == BitWidth - 1) &&
1596 (KnownOne.countPopulation() == 1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001597}
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001598
Evan Chengfa1eb272007-02-08 22:13:59 +00001599/// SimplifySetCC - Try to simplify a setcc built with the specified operands
Dan Gohman475871a2008-07-27 21:46:04 +00001600/// and cc. If it is unable to simplify it, return a null SDValue.
1601SDValue
Owen Andersone50ed302009-08-10 22:56:29 +00001602TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001603 ISD::CondCode Cond, bool foldBooleans,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001604 DAGCombinerInfo &DCI, DebugLoc dl) const {
Evan Chengfa1eb272007-02-08 22:13:59 +00001605 SelectionDAG &DAG = DCI.DAG;
Owen Anderson23b9b192009-08-12 00:36:31 +00001606 LLVMContext &Context = *DAG.getContext();
Evan Chengfa1eb272007-02-08 22:13:59 +00001607
1608 // These setcc operations always fold.
1609 switch (Cond) {
1610 default: break;
1611 case ISD::SETFALSE:
1612 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1613 case ISD::SETTRUE:
1614 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1615 }
1616
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001617 if (isa<ConstantSDNode>(N0.getNode())) {
1618 // Ensure that the constant occurs on the RHS, and fold constant
1619 // comparisons.
1620 return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1621 }
1622
Gabor Greifba36cb52008-08-28 21:40:38 +00001623 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001624 const APInt &C1 = N1C->getAPIntValue();
Dale Johannesen89217a62008-11-07 01:28:02 +00001625
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001626 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1627 // equality comparison, then we're just comparing whether X itself is
1628 // zero.
1629 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1630 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1631 N0.getOperand(1).getOpcode() == ISD::Constant) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001632 const APInt &ShAmt
1633 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001634 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1635 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1636 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1637 // (srl (ctlz x), 5) == 0 -> X != 0
1638 // (srl (ctlz x), 5) != 1 -> X != 0
1639 Cond = ISD::SETNE;
1640 } else {
1641 // (srl (ctlz x), 5) != 0 -> X == 0
1642 // (srl (ctlz x), 5) == 1 -> X == 0
1643 Cond = ISD::SETEQ;
1644 }
1645 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1646 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1647 Zero, Cond);
1648 }
1649 }
1650
1651 // If the LHS is '(and load, const)', the RHS is 0,
1652 // the test is for equality or unsigned, and all 1 bits of the const are
1653 // in the same partial word, see if we can shorten the load.
1654 if (DCI.isBeforeLegalize() &&
1655 N0.getOpcode() == ISD::AND && C1 == 0 &&
1656 N0.getNode()->hasOneUse() &&
1657 isa<LoadSDNode>(N0.getOperand(0)) &&
1658 N0.getOperand(0).getNode()->hasOneUse() &&
1659 isa<ConstantSDNode>(N0.getOperand(1))) {
1660 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
Evan Cheng347a9cb2010-01-07 20:58:44 +00001661 APInt bestMask;
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001662 unsigned bestWidth = 0, bestOffset = 0;
Evan Cheng347a9cb2010-01-07 20:58:44 +00001663 if (!Lod->isVolatile() && Lod->isUnindexed()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001664 unsigned origWidth = N0.getValueType().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001665 unsigned maskWidth = origWidth;
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001666 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
1667 // 8 bits, but have to be careful...
1668 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1669 origWidth = Lod->getMemoryVT().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001670 const APInt &Mask =
1671 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001672 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001673 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001674 for (unsigned offset=0; offset<origWidth/width; offset++) {
1675 if ((newMask & Mask) == Mask) {
1676 if (!TD->isLittleEndian())
1677 bestOffset = (origWidth/width - offset - 1) * (width/8);
1678 else
1679 bestOffset = (uint64_t)offset * (width/8);
Evan Cheng347a9cb2010-01-07 20:58:44 +00001680 bestMask = Mask.lshr(offset * (width/8) * 8);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001681 bestWidth = width;
1682 break;
Dale Johannesen89217a62008-11-07 01:28:02 +00001683 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001684 newMask = newMask << width;
Dale Johannesen89217a62008-11-07 01:28:02 +00001685 }
1686 }
1687 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001688 if (bestWidth) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001689 EVT newVT = EVT::getIntegerVT(Context, bestWidth);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001690 if (newVT.isRound()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001691 EVT PtrType = Lod->getOperand(1).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001692 SDValue Ptr = Lod->getBasePtr();
1693 if (bestOffset != 0)
1694 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1695 DAG.getConstant(bestOffset, PtrType));
1696 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1697 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
1698 Lod->getSrcValue(),
1699 Lod->getSrcValueOffset() + bestOffset,
1700 false, NewAlign);
1701 return DAG.getSetCC(dl, VT,
1702 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001703 DAG.getConstant(bestMask.trunc(bestWidth),
1704 newVT)),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001705 DAG.getConstant(0LL, newVT), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001706 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001707 }
1708 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001709
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001710 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1711 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1712 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1713
1714 // If the comparison constant has bits in the upper part, the
1715 // zero-extended value could never match.
1716 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1717 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001718 switch (Cond) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001719 case ISD::SETUGT:
1720 case ISD::SETUGE:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001721 case ISD::SETEQ: return DAG.getConstant(0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001722 case ISD::SETULT:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001723 case ISD::SETULE:
1724 case ISD::SETNE: return DAG.getConstant(1, VT);
1725 case ISD::SETGT:
1726 case ISD::SETGE:
1727 // True if the sign bit of C1 is set.
1728 return DAG.getConstant(C1.isNegative(), VT);
1729 case ISD::SETLT:
1730 case ISD::SETLE:
1731 // True if the sign bit of C1 isn't set.
1732 return DAG.getConstant(C1.isNonNegative(), VT);
1733 default:
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001734 break;
1735 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001736 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001737
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001738 // Otherwise, we can perform the comparison with the low bits.
1739 switch (Cond) {
1740 case ISD::SETEQ:
1741 case ISD::SETNE:
1742 case ISD::SETUGT:
1743 case ISD::SETUGE:
1744 case ISD::SETULT:
1745 case ISD::SETULE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT newVT = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001747 if (DCI.isBeforeLegalizeOps() ||
1748 (isOperationLegal(ISD::SETCC, newVT) &&
1749 getCondCodeAction(Cond, newVT)==Legal))
1750 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1751 DAG.getConstant(APInt(C1).trunc(InSize), newVT),
1752 Cond);
1753 break;
1754 }
1755 default:
1756 break; // todo, be more careful with signed comparisons
1757 }
1758 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1759 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001760 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001761 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00001762 EVT ExtDstTy = N0.getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001763 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1764
1765 // If the extended part has any inconsistent bits, it cannot ever
1766 // compare equal. In other words, they have to be all ones or all
1767 // zeros.
1768 APInt ExtBits =
1769 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
1770 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1771 return DAG.getConstant(Cond == ISD::SETNE, VT);
1772
1773 SDValue ZextOp;
Owen Andersone50ed302009-08-10 22:56:29 +00001774 EVT Op0Ty = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001775 if (Op0Ty == ExtSrcTy) {
1776 ZextOp = N0.getOperand(0);
1777 } else {
1778 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1779 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1780 DAG.getConstant(Imm, Op0Ty));
1781 }
1782 if (!DCI.isCalledByLegalizer())
1783 DCI.AddToWorklist(ZextOp.getNode());
1784 // Otherwise, make this a use of a zext.
1785 return DAG.getSetCC(dl, VT, ZextOp,
1786 DAG.getConstant(C1 & APInt::getLowBitsSet(
1787 ExtDstTyBits,
1788 ExtSrcTyBits),
1789 ExtDstTy),
1790 Cond);
1791 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1792 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1793
1794 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1795 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001796 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001797 if (TrueWhenTrue)
1798 return N0;
Evan Chengfa1eb272007-02-08 22:13:59 +00001799
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001800 // Invert the condition.
1801 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1802 CC = ISD::getSetCCInverse(CC,
1803 N0.getOperand(0).getValueType().isInteger());
1804 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
Evan Chengfa1eb272007-02-08 22:13:59 +00001805 }
1806
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001807 if ((N0.getOpcode() == ISD::XOR ||
1808 (N0.getOpcode() == ISD::AND &&
1809 N0.getOperand(0).getOpcode() == ISD::XOR &&
1810 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1811 isa<ConstantSDNode>(N0.getOperand(1)) &&
1812 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1813 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1814 // can only do this if the top bits are known zero.
1815 unsigned BitWidth = N0.getValueSizeInBits();
1816 if (DAG.MaskedValueIsZero(N0,
1817 APInt::getHighBitsSet(BitWidth,
1818 BitWidth-1))) {
1819 // Okay, get the un-inverted input value.
1820 SDValue Val;
1821 if (N0.getOpcode() == ISD::XOR)
1822 Val = N0.getOperand(0);
1823 else {
1824 assert(N0.getOpcode() == ISD::AND &&
1825 N0.getOperand(0).getOpcode() == ISD::XOR);
1826 // ((X^1)&1)^1 -> X & 1
1827 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1828 N0.getOperand(0).getOperand(0),
1829 N0.getOperand(1));
1830 }
1831 return DAG.getSetCC(dl, VT, Val, N1,
1832 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1833 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001834 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001835 }
1836
1837 APInt MinVal, MaxVal;
1838 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1839 if (ISD::isSignedIntSetCC(Cond)) {
1840 MinVal = APInt::getSignedMinValue(OperandBitSize);
1841 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1842 } else {
1843 MinVal = APInt::getMinValue(OperandBitSize);
1844 MaxVal = APInt::getMaxValue(OperandBitSize);
1845 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001846
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001847 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1848 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1849 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1850 // X >= C0 --> X > (C0-1)
1851 return DAG.getSetCC(dl, VT, N0,
1852 DAG.getConstant(C1-1, N1.getValueType()),
1853 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1854 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001855
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001856 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1857 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1858 // X <= C0 --> X < (C0+1)
1859 return DAG.getSetCC(dl, VT, N0,
1860 DAG.getConstant(C1+1, N1.getValueType()),
1861 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1862 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001863
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001864 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1865 return DAG.getConstant(0, VT); // X < MIN --> false
1866 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1867 return DAG.getConstant(1, VT); // X >= MIN --> true
1868 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1869 return DAG.getConstant(0, VT); // X > MAX --> false
1870 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1871 return DAG.getConstant(1, VT); // X <= MAX --> true
Evan Chengfa1eb272007-02-08 22:13:59 +00001872
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001873 // Canonicalize setgt X, Min --> setne X, Min
1874 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1875 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1876 // Canonicalize setlt X, Max --> setne X, Max
1877 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1878 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
Evan Chengfa1eb272007-02-08 22:13:59 +00001879
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001880 // If we have setult X, 1, turn it into seteq X, 0
1881 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1882 return DAG.getSetCC(dl, VT, N0,
1883 DAG.getConstant(MinVal, N0.getValueType()),
1884 ISD::SETEQ);
1885 // If we have setugt X, Max-1, turn it into seteq X, Max
1886 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1887 return DAG.getSetCC(dl, VT, N0,
1888 DAG.getConstant(MaxVal, N0.getValueType()),
1889 ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001890
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001891 // If we have "setcc X, C0", check to see if we can shrink the immediate
1892 // by changing cc.
Evan Chengfa1eb272007-02-08 22:13:59 +00001893
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001894 // SETUGT X, SINTMAX -> SETLT X, 0
1895 if (Cond == ISD::SETUGT &&
1896 C1 == APInt::getSignedMaxValue(OperandBitSize))
1897 return DAG.getSetCC(dl, VT, N0,
1898 DAG.getConstant(0, N1.getValueType()),
1899 ISD::SETLT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001900
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001901 // SETULT X, SINTMIN -> SETGT X, -1
1902 if (Cond == ISD::SETULT &&
1903 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1904 SDValue ConstMinusOne =
1905 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1906 N1.getValueType());
1907 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1908 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001909
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001910 // Fold bit comparisons when we can.
1911 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001912 (VT == N0.getValueType() ||
1913 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1914 N0.getOpcode() == ISD::AND)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001915 if (ConstantSDNode *AndRHS =
1916 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +00001917 EVT ShiftTy = DCI.isBeforeLegalize() ?
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001918 getPointerTy() : getShiftAmountTy();
1919 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1920 // Perform the xform if the AND RHS is a single bit.
Evan Cheng347a9cb2010-01-07 20:58:44 +00001921 if (AndRHS->getAPIntValue().isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001922 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1923 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001924 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001925 }
Evan Cheng347a9cb2010-01-07 20:58:44 +00001926 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001927 // (X & 8) == 8 --> (X & 8) >> 3
1928 // Perform the xform if C1 is a single bit.
1929 if (C1.isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001930 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1931 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1932 DAG.getConstant(C1.logBase2(), ShiftTy)));
Evan Chengfa1eb272007-02-08 22:13:59 +00001933 }
1934 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001935 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001936 }
1937
Gabor Greifba36cb52008-08-28 21:40:38 +00001938 if (isa<ConstantFPSDNode>(N0.getNode())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001939 // Constant fold or commute setcc.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001940 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 if (O.getNode()) return O;
1942 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
Chris Lattner63079f02007-12-29 08:37:08 +00001943 // If the RHS of an FP comparison is a constant, simplify it away in
1944 // some cases.
1945 if (CFP->getValueAPF().isNaN()) {
1946 // If an operand is known to be a nan, we can fold it.
1947 switch (ISD::getUnorderedFlavor(Cond)) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001948 default: llvm_unreachable("Unknown flavor!");
Chris Lattner63079f02007-12-29 08:37:08 +00001949 case 0: // Known false.
1950 return DAG.getConstant(0, VT);
1951 case 1: // Known true.
1952 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001953 case 2: // Undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00001954 return DAG.getUNDEF(VT);
Chris Lattner63079f02007-12-29 08:37:08 +00001955 }
1956 }
1957
1958 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1959 // constant if knowing that the operand is non-nan is enough. We prefer to
1960 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1961 // materialize 0.0.
1962 if (Cond == ISD::SETO || Cond == ISD::SETUO)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001963 return DAG.getSetCC(dl, VT, N0, N0, Cond);
Dan Gohman11eab022009-09-26 15:24:17 +00001964
1965 // If the condition is not legal, see if we can find an equivalent one
1966 // which is legal.
1967 if (!isCondCodeLegal(Cond, N0.getValueType())) {
1968 // If the comparison was an awkward floating-point == or != and one of
1969 // the comparison operands is infinity or negative infinity, convert the
1970 // condition to a less-awkward <= or >=.
1971 if (CFP->getValueAPF().isInfinity()) {
1972 if (CFP->getValueAPF().isNegative()) {
1973 if (Cond == ISD::SETOEQ &&
1974 isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
1975 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
1976 if (Cond == ISD::SETUEQ &&
1977 isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
1978 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
1979 if (Cond == ISD::SETUNE &&
1980 isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
1981 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
1982 if (Cond == ISD::SETONE &&
1983 isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
1984 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
1985 } else {
1986 if (Cond == ISD::SETOEQ &&
1987 isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
1988 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
1989 if (Cond == ISD::SETUEQ &&
1990 isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
1991 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
1992 if (Cond == ISD::SETUNE &&
1993 isCondCodeLegal(ISD::SETULT, N0.getValueType()))
1994 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
1995 if (Cond == ISD::SETONE &&
1996 isCondCodeLegal(ISD::SETULT, N0.getValueType()))
1997 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
1998 }
1999 }
2000 }
Evan Chengfa1eb272007-02-08 22:13:59 +00002001 }
2002
2003 if (N0 == N1) {
2004 // We can always fold X == X for integer setcc's.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002005 if (N0.getValueType().isInteger())
Evan Chengfa1eb272007-02-08 22:13:59 +00002006 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2007 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2008 if (UOF == 2) // FP operators that are undefined on NaNs.
2009 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2010 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2011 return DAG.getConstant(UOF, VT);
2012 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2013 // if it is not already.
2014 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2015 if (NewCond != Cond)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002016 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002017 }
2018
2019 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002020 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002021 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2022 N0.getOpcode() == ISD::XOR) {
2023 // Simplify (X+Y) == (X+Z) --> Y == Z
2024 if (N0.getOpcode() == N1.getOpcode()) {
2025 if (N0.getOperand(0) == N1.getOperand(0))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002026 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002027 if (N0.getOperand(1) == N1.getOperand(1))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002028 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002029 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2030 // If X op Y == Y op X, try other combinations.
2031 if (N0.getOperand(0) == N1.getOperand(1))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002032 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2033 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002034 if (N0.getOperand(1) == N1.getOperand(0))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002035 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2036 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002037 }
2038 }
2039
2040 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2041 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2042 // Turn (X+C1) == C2 --> X == C2-C1
Gabor Greifba36cb52008-08-28 21:40:38 +00002043 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002044 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002045 DAG.getConstant(RHSC->getAPIntValue()-
2046 LHSR->getAPIntValue(),
Evan Chengfa1eb272007-02-08 22:13:59 +00002047 N0.getValueType()), Cond);
2048 }
2049
2050 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2051 if (N0.getOpcode() == ISD::XOR)
2052 // If we know that all of the inverted bits are zero, don't bother
2053 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002054 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
2055 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002056 DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002057 DAG.getConstant(LHSR->getAPIntValue() ^
2058 RHSC->getAPIntValue(),
2059 N0.getValueType()),
2060 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002061 }
2062
2063 // Turn (C1-X) == C2 --> X == C1-C2
2064 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002065 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002066 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002067 DAG.getSetCC(dl, VT, N0.getOperand(1),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002068 DAG.getConstant(SUBC->getAPIntValue() -
2069 RHSC->getAPIntValue(),
2070 N0.getValueType()),
2071 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002072 }
2073 }
2074 }
2075
2076 // Simplify (X+Z) == X --> Z == 0
2077 if (N0.getOperand(0) == N1)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002078 return DAG.getSetCC(dl, VT, N0.getOperand(1),
Evan Chengfa1eb272007-02-08 22:13:59 +00002079 DAG.getConstant(0, N0.getValueType()), Cond);
2080 if (N0.getOperand(1) == N1) {
2081 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002082 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Evan Chengfa1eb272007-02-08 22:13:59 +00002083 DAG.getConstant(0, N0.getValueType()), Cond);
Gabor Greifba36cb52008-08-28 21:40:38 +00002084 else if (N0.getNode()->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002085 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2086 // (Z-X) == X --> Z == X<<1
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002087 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
Evan Chengfa1eb272007-02-08 22:13:59 +00002088 N1,
2089 DAG.getConstant(1, getShiftAmountTy()));
2090 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002091 DCI.AddToWorklist(SH.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002092 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002093 }
2094 }
2095 }
2096
2097 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2098 N1.getOpcode() == ISD::XOR) {
2099 // Simplify X == (X+Z) --> Z == 0
2100 if (N1.getOperand(0) == N0) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002101 return DAG.getSetCC(dl, VT, N1.getOperand(1),
Evan Chengfa1eb272007-02-08 22:13:59 +00002102 DAG.getConstant(0, N1.getValueType()), Cond);
2103 } else if (N1.getOperand(1) == N0) {
2104 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002105 return DAG.getSetCC(dl, VT, N1.getOperand(0),
Evan Chengfa1eb272007-02-08 22:13:59 +00002106 DAG.getConstant(0, N1.getValueType()), Cond);
Gabor Greifba36cb52008-08-28 21:40:38 +00002107 } else if (N1.getNode()->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002108 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2109 // X == (Z-X) --> X<<1 == Z
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002110 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
Evan Chengfa1eb272007-02-08 22:13:59 +00002111 DAG.getConstant(1, getShiftAmountTy()));
2112 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002113 DCI.AddToWorklist(SH.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002114 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002115 }
2116 }
2117 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00002118
Dan Gohman2c65c3d2009-01-29 16:18:12 +00002119 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002120 // Note that where y is variable and is known to have at most
2121 // one bit set (for example, if it is z&1) we cannot do this;
2122 // the expressions are not equivalent when y==0.
Dan Gohmane5af2d32009-01-29 01:59:02 +00002123 if (N0.getOpcode() == ISD::AND)
2124 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002125 if (ValueHasExactlyOneBitSet(N1, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00002126 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2127 SDValue Zero = DAG.getConstant(0, N1.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002128 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00002129 }
2130 }
2131 if (N1.getOpcode() == ISD::AND)
2132 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002133 if (ValueHasExactlyOneBitSet(N0, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00002134 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2135 SDValue Zero = DAG.getConstant(0, N0.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002136 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00002137 }
2138 }
Evan Chengfa1eb272007-02-08 22:13:59 +00002139 }
2140
2141 // Fold away ALL boolean setcc's.
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue Temp;
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002144 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002145 default: llvm_unreachable("Unknown integer setcc!");
Bob Wilson4c245462009-01-22 17:39:32 +00002146 case ISD::SETEQ: // X == Y -> ~(X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2148 N0 = DAG.getNOT(dl, Temp, MVT::i1);
Evan Chengfa1eb272007-02-08 22:13:59 +00002149 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002151 break;
2152 case ISD::SETNE: // X != Y --> (X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002153 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
Evan Chengfa1eb272007-02-08 22:13:59 +00002154 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002155 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
2156 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
Owen Anderson825b72b2009-08-11 20:47:22 +00002157 Temp = DAG.getNOT(dl, N0, MVT::i1);
2158 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002159 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002160 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002161 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002162 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
2163 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
Owen Anderson825b72b2009-08-11 20:47:22 +00002164 Temp = DAG.getNOT(dl, N1, MVT::i1);
2165 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002166 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002167 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002168 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002169 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
2170 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 Temp = DAG.getNOT(dl, N0, MVT::i1);
2172 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002173 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002174 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002175 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002176 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
2177 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 Temp = DAG.getNOT(dl, N1, MVT::i1);
2179 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002180 break;
2181 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002182 if (VT != MVT::i1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002183 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002184 DCI.AddToWorklist(N0.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002185 // FIXME: If running after legalize, we probably can't do this.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002186 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
Evan Chengfa1eb272007-02-08 22:13:59 +00002187 }
2188 return N0;
2189 }
2190
2191 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00002192 return SDValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00002193}
2194
Evan Chengad4196b2008-05-12 19:56:52 +00002195/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2196/// node is a GlobalAddress + offset.
2197bool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA,
2198 int64_t &Offset) const {
2199 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00002200 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
2201 GA = GASD->getGlobal();
2202 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00002203 return true;
2204 }
2205
2206 if (N->getOpcode() == ISD::ADD) {
Dan Gohman475871a2008-07-27 21:46:04 +00002207 SDValue N1 = N->getOperand(0);
2208 SDValue N2 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002210 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2211 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002212 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002213 return true;
2214 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002215 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002216 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2217 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002218 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002219 return true;
2220 }
2221 }
2222 }
2223 return false;
2224}
2225
2226
Dan Gohman475871a2008-07-27 21:46:04 +00002227SDValue TargetLowering::
Chris Lattner00ffed02006-03-01 04:52:55 +00002228PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
2229 // Default implementation: no optimization.
Dan Gohman475871a2008-07-27 21:46:04 +00002230 return SDValue();
Chris Lattner00ffed02006-03-01 04:52:55 +00002231}
2232
Chris Lattnereb8146b2006-02-04 02:13:02 +00002233//===----------------------------------------------------------------------===//
2234// Inline Assembler Implementation Methods
2235//===----------------------------------------------------------------------===//
2236
Chris Lattner4376fea2008-04-27 00:09:47 +00002237
Chris Lattnereb8146b2006-02-04 02:13:02 +00002238TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002239TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00002240 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00002241 if (Constraint.size() == 1) {
2242 switch (Constraint[0]) {
2243 default: break;
2244 case 'r': return C_RegisterClass;
2245 case 'm': // memory
2246 case 'o': // offsetable
2247 case 'V': // not offsetable
2248 return C_Memory;
2249 case 'i': // Simple Integer or Relocatable Constant
2250 case 'n': // Simple Integer
2251 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00002252 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00002253 case 'I': // Target registers.
2254 case 'J':
2255 case 'K':
2256 case 'L':
2257 case 'M':
2258 case 'N':
2259 case 'O':
2260 case 'P':
2261 return C_Other;
2262 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002263 }
Chris Lattner065421f2007-03-25 02:18:14 +00002264
2265 if (Constraint.size() > 1 && Constraint[0] == '{' &&
2266 Constraint[Constraint.size()-1] == '}')
2267 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00002268 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002269}
2270
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002271/// LowerXConstraint - try to replace an X constraint, which matches anything,
2272/// with another that has more specific requirements based on the type of the
2273/// corresponding operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002274const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00002275 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00002276 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00002278 return "f"; // works for many targets
2279 return 0;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002280}
2281
Chris Lattner48884cd2007-08-25 00:47:38 +00002282/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2283/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00002284void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00002285 char ConstraintLetter,
Evan Chengda43bcf2008-09-24 00:05:32 +00002286 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00002287 std::vector<SDValue> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00002288 SelectionDAG &DAG) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00002289 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002290 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002291 case 'X': // Allows any operand; labels (basic block) use this.
2292 if (Op.getOpcode() == ISD::BasicBlock) {
2293 Ops.push_back(Op);
2294 return;
2295 }
2296 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00002297 case 'i': // Simple Integer or Relocatable Constant
2298 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002299 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002300 // These operands are interested in values of the form (GV+C), where C may
2301 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2302 // is possible and fine if either GV or C are missing.
2303 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2304 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2305
2306 // If we have "(add GV, C)", pull out GV/C
2307 if (Op.getOpcode() == ISD::ADD) {
2308 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2309 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2310 if (C == 0 || GA == 0) {
2311 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2312 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2313 }
2314 if (C == 0 || GA == 0)
2315 C = 0, GA = 0;
2316 }
2317
2318 // If we find a valid operand, map to the TargetXXX version so that the
2319 // value itself doesn't get selected.
2320 if (GA) { // Either &GV or &GV+C
2321 if (ConstraintLetter != 'n') {
2322 int64_t Offs = GA->getOffset();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002323 if (C) Offs += C->getZExtValue();
Chris Lattner48884cd2007-08-25 00:47:38 +00002324 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
2325 Op.getValueType(), Offs));
2326 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002327 }
2328 }
2329 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002330 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00002331 if (ConstraintLetter != 's') {
Dale Johannesen78e3e522009-02-12 20:58:09 +00002332 // gcc prints these as sign extended. Sign extend value to 64 bits
2333 // now; without this it would get ZExt'd later in
2334 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2335 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 MVT::i64));
Chris Lattner48884cd2007-08-25 00:47:38 +00002337 return;
2338 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002339 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002340 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002341 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002342 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002343}
2344
Chris Lattner4ccb0702006-01-26 20:37:03 +00002345std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002346getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00002347 EVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00002348 return std::vector<unsigned>();
2349}
2350
2351
2352std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00002353getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00002354 EVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00002355 if (Constraint[0] != '{')
2356 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00002357 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2358
2359 // Remove the braces from around the name.
Benjamin Kramer05872ea2009-11-12 20:36:59 +00002360 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
Chris Lattner1efa40f2006-02-22 00:56:39 +00002361
2362 // Figure out which register class contains this reg.
Dan Gohman6f0d0242008-02-10 18:45:23 +00002363 const TargetRegisterInfo *RI = TM.getRegisterInfo();
2364 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00002365 E = RI->regclass_end(); RCI != E; ++RCI) {
2366 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00002367
2368 // If none of the the value types for this register class are valid, we
2369 // can't use it. For example, 64-bit reg classes on 32-bit targets.
2370 bool isLegal = false;
2371 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
2372 I != E; ++I) {
2373 if (isTypeLegal(*I)) {
2374 isLegal = true;
2375 break;
2376 }
2377 }
2378
2379 if (!isLegal) continue;
2380
Chris Lattner1efa40f2006-02-22 00:56:39 +00002381 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
2382 I != E; ++I) {
Benjamin Kramer05872ea2009-11-12 20:36:59 +00002383 if (RegName.equals_lower(RI->getName(*I)))
Chris Lattner1efa40f2006-02-22 00:56:39 +00002384 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00002385 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00002386 }
Chris Lattnera55079a2006-02-01 01:29:47 +00002387
Chris Lattner1efa40f2006-02-22 00:56:39 +00002388 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00002389}
Evan Cheng30b37b52006-03-13 23:18:16 +00002390
2391//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00002392// Constraint Selection.
2393
Chris Lattner6bdcda32008-10-17 16:47:46 +00002394/// isMatchingInputConstraint - Return true of this is an input operand that is
2395/// a matching constraint like "4".
2396bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
Chris Lattner58f15c42008-10-17 16:21:11 +00002397 assert(!ConstraintCode.empty() && "No known constraint!");
2398 return isdigit(ConstraintCode[0]);
2399}
2400
2401/// getMatchedOperand - If this is an input matching constraint, this method
2402/// returns the output operand it matches.
2403unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2404 assert(!ConstraintCode.empty() && "No known constraint!");
2405 return atoi(ConstraintCode.c_str());
2406}
2407
2408
Chris Lattner4376fea2008-04-27 00:09:47 +00002409/// getConstraintGenerality - Return an integer indicating how general CT
2410/// is.
2411static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2412 switch (CT) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002413 default: llvm_unreachable("Unknown constraint type!");
Chris Lattner4376fea2008-04-27 00:09:47 +00002414 case TargetLowering::C_Other:
2415 case TargetLowering::C_Unknown:
2416 return 0;
2417 case TargetLowering::C_Register:
2418 return 1;
2419 case TargetLowering::C_RegisterClass:
2420 return 2;
2421 case TargetLowering::C_Memory:
2422 return 3;
2423 }
2424}
2425
2426/// ChooseConstraint - If there are multiple different constraints that we
2427/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00002428/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00002429/// Other -> immediates and magic values
2430/// Register -> one specific register
2431/// RegisterClass -> a group of regs
2432/// Memory -> memory
2433/// Ideally, we would pick the most specific constraint possible: if we have
2434/// something that fits into a register, we would pick it. The problem here
2435/// is that if we have something that could either be in a register or in
2436/// memory that use of the register could cause selection of *other*
2437/// operands to fail: they might only succeed if we pick memory. Because of
2438/// this the heuristic we use is:
2439///
2440/// 1) If there is an 'other' constraint, and if the operand is valid for
2441/// that constraint, use it. This makes us take advantage of 'i'
2442/// constraints when available.
2443/// 2) Otherwise, pick the most general constraint present. This prefers
2444/// 'm' over 'r', for example.
2445///
2446static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Evan Chengda43bcf2008-09-24 00:05:32 +00002447 bool hasMemory, const TargetLowering &TLI,
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SDValue Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002449 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2450 unsigned BestIdx = 0;
2451 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2452 int BestGenerality = -1;
2453
2454 // Loop over the options, keeping track of the most general one.
2455 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2456 TargetLowering::ConstraintType CType =
2457 TLI.getConstraintType(OpInfo.Codes[i]);
2458
Chris Lattner5a096902008-04-27 00:37:18 +00002459 // If this is an 'other' constraint, see if the operand is valid for it.
2460 // For example, on X86 we might have an 'rI' constraint. If the operand
2461 // is an integer in the range [0..31] we want to use I (saving a load
2462 // of a register), otherwise we must use 'r'.
Gabor Greifba36cb52008-08-28 21:40:38 +00002463 if (CType == TargetLowering::C_Other && Op.getNode()) {
Chris Lattner5a096902008-04-27 00:37:18 +00002464 assert(OpInfo.Codes[i].size() == 1 &&
2465 "Unhandled multi-letter 'other' constraint");
Dan Gohman475871a2008-07-27 21:46:04 +00002466 std::vector<SDValue> ResultOps;
Evan Chengda43bcf2008-09-24 00:05:32 +00002467 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0], hasMemory,
Chris Lattner5a096902008-04-27 00:37:18 +00002468 ResultOps, *DAG);
2469 if (!ResultOps.empty()) {
2470 BestType = CType;
2471 BestIdx = i;
2472 break;
2473 }
2474 }
2475
Chris Lattner4376fea2008-04-27 00:09:47 +00002476 // This constraint letter is more general than the previous one, use it.
2477 int Generality = getConstraintGenerality(CType);
2478 if (Generality > BestGenerality) {
2479 BestType = CType;
2480 BestIdx = i;
2481 BestGenerality = Generality;
2482 }
2483 }
2484
2485 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2486 OpInfo.ConstraintType = BestType;
2487}
2488
2489/// ComputeConstraintToUse - Determines the constraint code and constraint
2490/// type to use for the specific AsmOperandInfo, setting
2491/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00002492void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
Dan Gohman475871a2008-07-27 21:46:04 +00002493 SDValue Op,
Evan Chengda43bcf2008-09-24 00:05:32 +00002494 bool hasMemory,
Chris Lattner5a096902008-04-27 00:37:18 +00002495 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00002496 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
2497
2498 // Single-letter constraints ('r') are very common.
2499 if (OpInfo.Codes.size() == 1) {
2500 OpInfo.ConstraintCode = OpInfo.Codes[0];
2501 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2502 } else {
Evan Chengda43bcf2008-09-24 00:05:32 +00002503 ChooseConstraint(OpInfo, hasMemory, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00002504 }
2505
2506 // 'X' matches anything.
2507 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2508 // Labels and constants are handled elsewhere ('X' is the only thing
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002509 // that matches labels). For Functions, the type here is the type of
Dale Johannesen5339c552009-07-20 23:27:39 +00002510 // the result, which is not what we want to look at; leave them alone.
2511 Value *v = OpInfo.CallOperandVal;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002512 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2513 OpInfo.CallOperandVal = v;
Chris Lattner4376fea2008-04-27 00:09:47 +00002514 return;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002515 }
Chris Lattner4376fea2008-04-27 00:09:47 +00002516
2517 // Otherwise, try to resolve it to something we know about by looking at
2518 // the actual operand type.
2519 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2520 OpInfo.ConstraintCode = Repl;
2521 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2522 }
2523 }
2524}
2525
2526//===----------------------------------------------------------------------===//
Evan Cheng30b37b52006-03-13 23:18:16 +00002527// Loop Strength Reduction hooks
2528//===----------------------------------------------------------------------===//
2529
Chris Lattner1436bb62007-03-30 23:14:50 +00002530/// isLegalAddressingMode - Return true if the addressing mode represented
2531/// by AM is legal for this target, for a load/store of the specified type.
2532bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
2533 const Type *Ty) const {
2534 // The default implementation of this implements a conservative RISCy, r+r and
2535 // r+i addr mode.
2536
2537 // Allows a sign-extended 16-bit immediate field.
2538 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
2539 return false;
2540
2541 // No global is ever allowed as a base.
2542 if (AM.BaseGV)
2543 return false;
2544
2545 // Only support r+r,
2546 switch (AM.Scale) {
2547 case 0: // "r+i" or just "i", depending on HasBaseReg.
2548 break;
2549 case 1:
2550 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
2551 return false;
2552 // Otherwise we have r+r or r+i.
2553 break;
2554 case 2:
2555 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
2556 return false;
2557 // Allow 2*r as r+r.
2558 break;
2559 }
2560
2561 return true;
2562}
2563
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002564/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2565/// return a DAG expression to select that will generate the same value by
2566/// multiplying by a magic number. See:
2567/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00002568SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
2569 std::vector<SDNode*>* Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002570 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002571 DebugLoc dl= N->getDebugLoc();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002572
2573 // Check to see if we can do this.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002574 // FIXME: We should be more aggressive here.
2575 if (!isTypeLegal(VT))
2576 return SDValue();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002577
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002578 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
Jay Foad4e5ea552009-04-30 10:15:35 +00002579 APInt::ms magics = d.magic();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002580
2581 // Multiply the numerator (operand 0) by the magic value
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002582 // FIXME: We should support doing a MUL in a wider type
Dan Gohman475871a2008-07-27 21:46:04 +00002583 SDValue Q;
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002584 if (isOperationLegalOrCustom(ISD::MULHS, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002585 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
Dan Gohman525178c2007-10-08 18:33:35 +00002586 DAG.getConstant(magics.m, VT));
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002587 else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002588 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohman525178c2007-10-08 18:33:35 +00002589 N->getOperand(0),
Gabor Greifba36cb52008-08-28 21:40:38 +00002590 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002591 else
Dan Gohman475871a2008-07-27 21:46:04 +00002592 return SDValue(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002593 // If d > 0 and m < 0, add the numerator
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002594 if (d.isStrictlyPositive() && magics.m.isNegative()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002595 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002596 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002597 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002598 }
2599 // If d < 0 and m > 0, subtract the numerator.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002600 if (d.isNegative() && magics.m.isStrictlyPositive()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002601 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002602 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002603 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002604 }
2605 // Shift right algebraic if shift value is nonzero
2606 if (magics.s > 0) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002607 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002608 DAG.getConstant(magics.s, getShiftAmountTy()));
2609 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002610 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002611 }
2612 // Extract the sign bit and add it to the quotient
Dan Gohman475871a2008-07-27 21:46:04 +00002613 SDValue T =
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002614 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002615 getShiftAmountTy()));
2616 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002617 Created->push_back(T.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002618 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002619}
2620
2621/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2622/// return a DAG expression to select that will generate the same value by
2623/// multiplying by a magic number. See:
2624/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00002625SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
2626 std::vector<SDNode*>* Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002627 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002628 DebugLoc dl = N->getDebugLoc();
Eli Friedman201c9772008-11-30 06:02:26 +00002629
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002630 // Check to see if we can do this.
Eli Friedman201c9772008-11-30 06:02:26 +00002631 // FIXME: We should be more aggressive here.
2632 if (!isTypeLegal(VT))
2633 return SDValue();
2634
2635 // FIXME: We should use a narrower constant when the upper
2636 // bits are known to be zero.
2637 ConstantSDNode *N1C = cast<ConstantSDNode>(N->getOperand(1));
Jay Foad4e5ea552009-04-30 10:15:35 +00002638 APInt::mu magics = N1C->getAPIntValue().magicu();
Eli Friedman201c9772008-11-30 06:02:26 +00002639
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002640 // Multiply the numerator (operand 0) by the magic value
Eli Friedman201c9772008-11-30 06:02:26 +00002641 // FIXME: We should support doing a MUL in a wider type
Dan Gohman475871a2008-07-27 21:46:04 +00002642 SDValue Q;
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002643 if (isOperationLegalOrCustom(ISD::MULHU, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002644 Q = DAG.getNode(ISD::MULHU, dl, VT, N->getOperand(0),
Dan Gohman525178c2007-10-08 18:33:35 +00002645 DAG.getConstant(magics.m, VT));
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002646 else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002647 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohman525178c2007-10-08 18:33:35 +00002648 N->getOperand(0),
Gabor Greifba36cb52008-08-28 21:40:38 +00002649 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002650 else
Dan Gohman475871a2008-07-27 21:46:04 +00002651 return SDValue(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002652 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002653 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002654
2655 if (magics.a == 0) {
Eli Friedman201c9772008-11-30 06:02:26 +00002656 assert(magics.s < N1C->getAPIntValue().getBitWidth() &&
2657 "We shouldn't generate an undefined shift!");
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002658 return DAG.getNode(ISD::SRL, dl, VT, Q,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002659 DAG.getConstant(magics.s, getShiftAmountTy()));
2660 } else {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002661 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002662 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002663 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002664 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002665 DAG.getConstant(1, getShiftAmountTy()));
2666 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002667 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002668 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002669 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002670 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002671 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002672 DAG.getConstant(magics.s-1, getShiftAmountTy()));
2673 }
2674}