LLVM API Documentation
00001 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements the TargetLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/TargetAsmInfo.h" 00015 #include "llvm/Target/TargetLowering.h" 00016 #include "llvm/Target/TargetSubtarget.h" 00017 #include "llvm/Target/TargetData.h" 00018 #include "llvm/Target/TargetMachine.h" 00019 #include "llvm/Target/TargetRegisterInfo.h" 00020 #include "llvm/GlobalVariable.h" 00021 #include "llvm/DerivedTypes.h" 00022 #include "llvm/CodeGen/MachineFrameInfo.h" 00023 #include "llvm/CodeGen/SelectionDAG.h" 00024 #include "llvm/ADT/StringExtras.h" 00025 #include "llvm/ADT/STLExtras.h" 00026 #include "llvm/Support/MathExtras.h" 00027 using namespace llvm; 00028 00029 /// InitLibcallNames - Set default libcall names. 00030 /// 00031 static void InitLibcallNames(const char **Names) { 00032 Names[RTLIB::SHL_I16] = "__ashli16"; 00033 Names[RTLIB::SHL_I32] = "__ashlsi3"; 00034 Names[RTLIB::SHL_I64] = "__ashldi3"; 00035 Names[RTLIB::SHL_I128] = "__ashlti3"; 00036 Names[RTLIB::SRL_I16] = "__lshri16"; 00037 Names[RTLIB::SRL_I32] = "__lshrsi3"; 00038 Names[RTLIB::SRL_I64] = "__lshrdi3"; 00039 Names[RTLIB::SRL_I128] = "__lshrti3"; 00040 Names[RTLIB::SRA_I16] = "__ashri16"; 00041 Names[RTLIB::SRA_I32] = "__ashrsi3"; 00042 Names[RTLIB::SRA_I64] = "__ashrdi3"; 00043 Names[RTLIB::SRA_I128] = "__ashrti3"; 00044 Names[RTLIB::MUL_I16] = "__muli16"; 00045 Names[RTLIB::MUL_I32] = "__mulsi3"; 00046 Names[RTLIB::MUL_I64] = "__muldi3"; 00047 Names[RTLIB::MUL_I128] = "__multi3"; 00048 Names[RTLIB::SDIV_I32] = "__divsi3"; 00049 Names[RTLIB::SDIV_I64] = "__divdi3"; 00050 Names[RTLIB::SDIV_I128] = "__divti3"; 00051 Names[RTLIB::UDIV_I32] = "__udivsi3"; 00052 Names[RTLIB::UDIV_I64] = "__udivdi3"; 00053 Names[RTLIB::UDIV_I128] = "__udivti3"; 00054 Names[RTLIB::SREM_I32] = "__modsi3"; 00055 Names[RTLIB::SREM_I64] = "__moddi3"; 00056 Names[RTLIB::SREM_I128] = "__modti3"; 00057 Names[RTLIB::UREM_I32] = "__umodsi3"; 00058 Names[RTLIB::UREM_I64] = "__umoddi3"; 00059 Names[RTLIB::UREM_I128] = "__umodti3"; 00060 Names[RTLIB::NEG_I32] = "__negsi2"; 00061 Names[RTLIB::NEG_I64] = "__negdi2"; 00062 Names[RTLIB::ADD_F32] = "__addsf3"; 00063 Names[RTLIB::ADD_F64] = "__adddf3"; 00064 Names[RTLIB::ADD_F80] = "__addxf3"; 00065 Names[RTLIB::ADD_PPCF128] = "__gcc_qadd"; 00066 Names[RTLIB::SUB_F32] = "__subsf3"; 00067 Names[RTLIB::SUB_F64] = "__subdf3"; 00068 Names[RTLIB::SUB_F80] = "__subxf3"; 00069 Names[RTLIB::SUB_PPCF128] = "__gcc_qsub"; 00070 Names[RTLIB::MUL_F32] = "__mulsf3"; 00071 Names[RTLIB::MUL_F64] = "__muldf3"; 00072 Names[RTLIB::MUL_F80] = "__mulxf3"; 00073 Names[RTLIB::MUL_PPCF128] = "__gcc_qmul"; 00074 Names[RTLIB::DIV_F32] = "__divsf3"; 00075 Names[RTLIB::DIV_F64] = "__divdf3"; 00076 Names[RTLIB::DIV_F80] = "__divxf3"; 00077 Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv"; 00078 Names[RTLIB::REM_F32] = "fmodf"; 00079 Names[RTLIB::REM_F64] = "fmod"; 00080 Names[RTLIB::REM_F80] = "fmodl"; 00081 Names[RTLIB::REM_PPCF128] = "fmodl"; 00082 Names[RTLIB::POWI_F32] = "__powisf2"; 00083 Names[RTLIB::POWI_F64] = "__powidf2"; 00084 Names[RTLIB::POWI_F80] = "__powixf2"; 00085 Names[RTLIB::POWI_PPCF128] = "__powitf2"; 00086 Names[RTLIB::SQRT_F32] = "sqrtf"; 00087 Names[RTLIB::SQRT_F64] = "sqrt"; 00088 Names[RTLIB::SQRT_F80] = "sqrtl"; 00089 Names[RTLIB::SQRT_PPCF128] = "sqrtl"; 00090 Names[RTLIB::LOG_F32] = "logf"; 00091 Names[RTLIB::LOG_F64] = "log"; 00092 Names[RTLIB::LOG_F80] = "logl"; 00093 Names[RTLIB::LOG_PPCF128] = "logl"; 00094 Names[RTLIB::LOG2_F32] = "log2f"; 00095 Names[RTLIB::LOG2_F64] = "log2"; 00096 Names[RTLIB::LOG2_F80] = "log2l"; 00097 Names[RTLIB::LOG2_PPCF128] = "log2l"; 00098 Names[RTLIB::LOG10_F32] = "log10f"; 00099 Names[RTLIB::LOG10_F64] = "log10"; 00100 Names[RTLIB::LOG10_F80] = "log10l"; 00101 Names[RTLIB::LOG10_PPCF128] = "log10l"; 00102 Names[RTLIB::EXP_F32] = "expf"; 00103 Names[RTLIB::EXP_F64] = "exp"; 00104 Names[RTLIB::EXP_F80] = "expl"; 00105 Names[RTLIB::EXP_PPCF128] = "expl"; 00106 Names[RTLIB::EXP2_F32] = "exp2f"; 00107 Names[RTLIB::EXP2_F64] = "exp2"; 00108 Names[RTLIB::EXP2_F80] = "exp2l"; 00109 Names[RTLIB::EXP2_PPCF128] = "exp2l"; 00110 Names[RTLIB::SIN_F32] = "sinf"; 00111 Names[RTLIB::SIN_F64] = "sin"; 00112 Names[RTLIB::SIN_F80] = "sinl"; 00113 Names[RTLIB::SIN_PPCF128] = "sinl"; 00114 Names[RTLIB::COS_F32] = "cosf"; 00115 Names[RTLIB::COS_F64] = "cos"; 00116 Names[RTLIB::COS_F80] = "cosl"; 00117 Names[RTLIB::COS_PPCF128] = "cosl"; 00118 Names[RTLIB::POW_F32] = "powf"; 00119 Names[RTLIB::POW_F64] = "pow"; 00120 Names[RTLIB::POW_F80] = "powl"; 00121 Names[RTLIB::POW_PPCF128] = "powl"; 00122 Names[RTLIB::CEIL_F32] = "ceilf"; 00123 Names[RTLIB::CEIL_F64] = "ceil"; 00124 Names[RTLIB::CEIL_F80] = "ceill"; 00125 Names[RTLIB::CEIL_PPCF128] = "ceill"; 00126 Names[RTLIB::TRUNC_F32] = "truncf"; 00127 Names[RTLIB::TRUNC_F64] = "trunc"; 00128 Names[RTLIB::TRUNC_F80] = "truncl"; 00129 Names[RTLIB::TRUNC_PPCF128] = "truncl"; 00130 Names[RTLIB::RINT_F32] = "rintf"; 00131 Names[RTLIB::RINT_F64] = "rint"; 00132 Names[RTLIB::RINT_F80] = "rintl"; 00133 Names[RTLIB::RINT_PPCF128] = "rintl"; 00134 Names[RTLIB::NEARBYINT_F32] = "nearbyintf"; 00135 Names[RTLIB::NEARBYINT_F64] = "nearbyint"; 00136 Names[RTLIB::NEARBYINT_F80] = "nearbyintl"; 00137 Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl"; 00138 Names[RTLIB::FLOOR_F32] = "floorf"; 00139 Names[RTLIB::FLOOR_F64] = "floor"; 00140 Names[RTLIB::FLOOR_F80] = "floorl"; 00141 Names[RTLIB::FLOOR_PPCF128] = "floorl"; 00142 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2"; 00143 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2"; 00144 Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2"; 00145 Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2"; 00146 Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2"; 00147 Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2"; 00148 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi"; 00149 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi"; 00150 Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti"; 00151 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi"; 00152 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi"; 00153 Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti"; 00154 Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi"; 00155 Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi"; 00156 Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti"; 00157 Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi"; 00158 Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi"; 00159 Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti"; 00160 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi"; 00161 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi"; 00162 Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti"; 00163 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi"; 00164 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi"; 00165 Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti"; 00166 Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi"; 00167 Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi"; 00168 Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti"; 00169 Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi"; 00170 Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi"; 00171 Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti"; 00172 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf"; 00173 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf"; 00174 Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf"; 00175 Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf"; 00176 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf"; 00177 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf"; 00178 Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf"; 00179 Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf"; 00180 Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf"; 00181 Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf"; 00182 Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf"; 00183 Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf"; 00184 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf"; 00185 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf"; 00186 Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf"; 00187 Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf"; 00188 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf"; 00189 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf"; 00190 Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf"; 00191 Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf"; 00192 Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf"; 00193 Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf"; 00194 Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf"; 00195 Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf"; 00196 Names[RTLIB::OEQ_F32] = "__eqsf2"; 00197 Names[RTLIB::OEQ_F64] = "__eqdf2"; 00198 Names[RTLIB::UNE_F32] = "__nesf2"; 00199 Names[RTLIB::UNE_F64] = "__nedf2"; 00200 Names[RTLIB::OGE_F32] = "__gesf2"; 00201 Names[RTLIB::OGE_F64] = "__gedf2"; 00202 Names[RTLIB::OLT_F32] = "__ltsf2"; 00203 Names[RTLIB::OLT_F64] = "__ltdf2"; 00204 Names[RTLIB::OLE_F32] = "__lesf2"; 00205 Names[RTLIB::OLE_F64] = "__ledf2"; 00206 Names[RTLIB::OGT_F32] = "__gtsf2"; 00207 Names[RTLIB::OGT_F64] = "__gtdf2"; 00208 Names[RTLIB::UO_F32] = "__unordsf2"; 00209 Names[RTLIB::UO_F64] = "__unorddf2"; 00210 Names[RTLIB::O_F32] = "__unordsf2"; 00211 Names[RTLIB::O_F64] = "__unorddf2"; 00212 } 00213 00214 /// getFPEXT - Return the FPEXT_*_* value for the given types, or 00215 /// UNKNOWN_LIBCALL if there is none. 00216 RTLIB::Libcall RTLIB::getFPEXT(MVT OpVT, MVT RetVT) { 00217 if (OpVT == MVT::f32) { 00218 if (RetVT == MVT::f64) 00219 return FPEXT_F32_F64; 00220 } 00221 return UNKNOWN_LIBCALL; 00222 } 00223 00224 /// getFPROUND - Return the FPROUND_*_* value for the given types, or 00225 /// UNKNOWN_LIBCALL if there is none. 00226 RTLIB::Libcall RTLIB::getFPROUND(MVT OpVT, MVT RetVT) { 00227 if (RetVT == MVT::f32) { 00228 if (OpVT == MVT::f64) 00229 return FPROUND_F64_F32; 00230 if (OpVT == MVT::f80) 00231 return FPROUND_F80_F32; 00232 if (OpVT == MVT::ppcf128) 00233 return FPROUND_PPCF128_F32; 00234 } else if (RetVT == MVT::f64) { 00235 if (OpVT == MVT::f80) 00236 return FPROUND_F80_F64; 00237 if (OpVT == MVT::ppcf128) 00238 return FPROUND_PPCF128_F64; 00239 } 00240 return UNKNOWN_LIBCALL; 00241 } 00242 00243 /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or 00244 /// UNKNOWN_LIBCALL if there is none. 00245 RTLIB::Libcall RTLIB::getFPTOSINT(MVT OpVT, MVT RetVT) { 00246 if (OpVT == MVT::f32) { 00247 if (RetVT == MVT::i32) 00248 return FPTOSINT_F32_I32; 00249 if (RetVT == MVT::i64) 00250 return FPTOSINT_F32_I64; 00251 if (RetVT == MVT::i128) 00252 return FPTOSINT_F32_I128; 00253 } else if (OpVT == MVT::f64) { 00254 if (RetVT == MVT::i32) 00255 return FPTOSINT_F64_I32; 00256 if (RetVT == MVT::i64) 00257 return FPTOSINT_F64_I64; 00258 if (RetVT == MVT::i128) 00259 return FPTOSINT_F64_I128; 00260 } else if (OpVT == MVT::f80) { 00261 if (RetVT == MVT::i32) 00262 return FPTOSINT_F80_I32; 00263 if (RetVT == MVT::i64) 00264 return FPTOSINT_F80_I64; 00265 if (RetVT == MVT::i128) 00266 return FPTOSINT_F80_I128; 00267 } else if (OpVT == MVT::ppcf128) { 00268 if (RetVT == MVT::i32) 00269 return FPTOSINT_PPCF128_I32; 00270 if (RetVT == MVT::i64) 00271 return FPTOSINT_PPCF128_I64; 00272 if (RetVT == MVT::i128) 00273 return FPTOSINT_PPCF128_I128; 00274 } 00275 return UNKNOWN_LIBCALL; 00276 } 00277 00278 /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or 00279 /// UNKNOWN_LIBCALL if there is none. 00280 RTLIB::Libcall RTLIB::getFPTOUINT(MVT OpVT, MVT RetVT) { 00281 if (OpVT == MVT::f32) { 00282 if (RetVT == MVT::i32) 00283 return FPTOUINT_F32_I32; 00284 if (RetVT == MVT::i64) 00285 return FPTOUINT_F32_I64; 00286 if (RetVT == MVT::i128) 00287 return FPTOUINT_F32_I128; 00288 } else if (OpVT == MVT::f64) { 00289 if (RetVT == MVT::i32) 00290 return FPTOUINT_F64_I32; 00291 if (RetVT == MVT::i64) 00292 return FPTOUINT_F64_I64; 00293 if (RetVT == MVT::i128) 00294 return FPTOUINT_F64_I128; 00295 } else if (OpVT == MVT::f80) { 00296 if (RetVT == MVT::i32) 00297 return FPTOUINT_F80_I32; 00298 if (RetVT == MVT::i64) 00299 return FPTOUINT_F80_I64; 00300 if (RetVT == MVT::i128) 00301 return FPTOUINT_F80_I128; 00302 } else if (OpVT == MVT::ppcf128) { 00303 if (RetVT == MVT::i32) 00304 return FPTOUINT_PPCF128_I32; 00305 if (RetVT == MVT::i64) 00306 return FPTOUINT_PPCF128_I64; 00307 if (RetVT == MVT::i128) 00308 return FPTOUINT_PPCF128_I128; 00309 } 00310 return UNKNOWN_LIBCALL; 00311 } 00312 00313 /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or 00314 /// UNKNOWN_LIBCALL if there is none. 00315 RTLIB::Libcall RTLIB::getSINTTOFP(MVT OpVT, MVT RetVT) { 00316 if (OpVT == MVT::i32) { 00317 if (RetVT == MVT::f32) 00318 return SINTTOFP_I32_F32; 00319 else if (RetVT == MVT::f64) 00320 return SINTTOFP_I32_F64; 00321 else if (RetVT == MVT::f80) 00322 return SINTTOFP_I32_F80; 00323 else if (RetVT == MVT::ppcf128) 00324 return SINTTOFP_I32_PPCF128; 00325 } else if (OpVT == MVT::i64) { 00326 if (RetVT == MVT::f32) 00327 return SINTTOFP_I64_F32; 00328 else if (RetVT == MVT::f64) 00329 return SINTTOFP_I64_F64; 00330 else if (RetVT == MVT::f80) 00331 return SINTTOFP_I64_F80; 00332 else if (RetVT == MVT::ppcf128) 00333 return SINTTOFP_I64_PPCF128; 00334 } else if (OpVT == MVT::i128) { 00335 if (RetVT == MVT::f32) 00336 return SINTTOFP_I128_F32; 00337 else if (RetVT == MVT::f64) 00338 return SINTTOFP_I128_F64; 00339 else if (RetVT == MVT::f80) 00340 return SINTTOFP_I128_F80; 00341 else if (RetVT == MVT::ppcf128) 00342 return SINTTOFP_I128_PPCF128; 00343 } 00344 return UNKNOWN_LIBCALL; 00345 } 00346 00347 /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or 00348 /// UNKNOWN_LIBCALL if there is none. 00349 RTLIB::Libcall RTLIB::getUINTTOFP(MVT OpVT, MVT RetVT) { 00350 if (OpVT == MVT::i32) { 00351 if (RetVT == MVT::f32) 00352 return UINTTOFP_I32_F32; 00353 else if (RetVT == MVT::f64) 00354 return UINTTOFP_I32_F64; 00355 else if (RetVT == MVT::f80) 00356 return UINTTOFP_I32_F80; 00357 else if (RetVT == MVT::ppcf128) 00358 return UINTTOFP_I32_PPCF128; 00359 } else if (OpVT == MVT::i64) { 00360 if (RetVT == MVT::f32) 00361 return UINTTOFP_I64_F32; 00362 else if (RetVT == MVT::f64) 00363 return UINTTOFP_I64_F64; 00364 else if (RetVT == MVT::f80) 00365 return UINTTOFP_I64_F80; 00366 else if (RetVT == MVT::ppcf128) 00367 return UINTTOFP_I64_PPCF128; 00368 } else if (OpVT == MVT::i128) { 00369 if (RetVT == MVT::f32) 00370 return UINTTOFP_I128_F32; 00371 else if (RetVT == MVT::f64) 00372 return UINTTOFP_I128_F64; 00373 else if (RetVT == MVT::f80) 00374 return UINTTOFP_I128_F80; 00375 else if (RetVT == MVT::ppcf128) 00376 return UINTTOFP_I128_PPCF128; 00377 } 00378 return UNKNOWN_LIBCALL; 00379 } 00380 00381 /// InitCmpLibcallCCs - Set default comparison libcall CC. 00382 /// 00383 static void InitCmpLibcallCCs(ISD::CondCode *CCs) { 00384 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL); 00385 CCs[RTLIB::OEQ_F32] = ISD::SETEQ; 00386 CCs[RTLIB::OEQ_F64] = ISD::SETEQ; 00387 CCs[RTLIB::UNE_F32] = ISD::SETNE; 00388 CCs[RTLIB::UNE_F64] = ISD::SETNE; 00389 CCs[RTLIB::OGE_F32] = ISD::SETGE; 00390 CCs[RTLIB::OGE_F64] = ISD::SETGE; 00391 CCs[RTLIB::OLT_F32] = ISD::SETLT; 00392 CCs[RTLIB::OLT_F64] = ISD::SETLT; 00393 CCs[RTLIB::OLE_F32] = ISD::SETLE; 00394 CCs[RTLIB::OLE_F64] = ISD::SETLE; 00395 CCs[RTLIB::OGT_F32] = ISD::SETGT; 00396 CCs[RTLIB::OGT_F64] = ISD::SETGT; 00397 CCs[RTLIB::UO_F32] = ISD::SETNE; 00398 CCs[RTLIB::UO_F64] = ISD::SETNE; 00399 CCs[RTLIB::O_F32] = ISD::SETEQ; 00400 CCs[RTLIB::O_F64] = ISD::SETEQ; 00401 } 00402 00403 TargetLowering::TargetLowering(TargetMachine &tm) 00404 : TM(tm), TD(TM.getTargetData()) { 00405 // All operations default to being supported. 00406 memset(OpActions, 0, sizeof(OpActions)); 00407 memset(LoadExtActions, 0, sizeof(LoadExtActions)); 00408 memset(TruncStoreActions, 0, sizeof(TruncStoreActions)); 00409 memset(IndexedModeActions, 0, sizeof(IndexedModeActions)); 00410 memset(ConvertActions, 0, sizeof(ConvertActions)); 00411 memset(CondCodeActions, 0, sizeof(CondCodeActions)); 00412 00413 // Set default actions for various operations. 00414 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) { 00415 // Default all indexed load / store to expand. 00416 for (unsigned IM = (unsigned)ISD::PRE_INC; 00417 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) { 00418 setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand); 00419 setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand); 00420 } 00421 00422 // These operations default to expand. 00423 setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand); 00424 } 00425 00426 // Most targets ignore the @llvm.prefetch intrinsic. 00427 setOperationAction(ISD::PREFETCH, MVT::Other, Expand); 00428 00429 // ConstantFP nodes default to expand. Targets can either change this to 00430 // Legal, in which case all fp constants are legal, or use addLegalFPImmediate 00431 // to optimize expansions for certain constants. 00432 setOperationAction(ISD::ConstantFP, MVT::f32, Expand); 00433 setOperationAction(ISD::ConstantFP, MVT::f64, Expand); 00434 setOperationAction(ISD::ConstantFP, MVT::f80, Expand); 00435 00436 // These library functions default to expand. 00437 setOperationAction(ISD::FLOG , MVT::f64, Expand); 00438 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 00439 setOperationAction(ISD::FLOG10,MVT::f64, Expand); 00440 setOperationAction(ISD::FEXP , MVT::f64, Expand); 00441 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 00442 setOperationAction(ISD::FLOG , MVT::f32, Expand); 00443 setOperationAction(ISD::FLOG2, MVT::f32, Expand); 00444 setOperationAction(ISD::FLOG10,MVT::f32, Expand); 00445 setOperationAction(ISD::FEXP , MVT::f32, Expand); 00446 setOperationAction(ISD::FEXP2, MVT::f32, Expand); 00447 00448 // Default ISD::TRAP to expand (which turns it into abort). 00449 setOperationAction(ISD::TRAP, MVT::Other, Expand); 00450 00451 IsLittleEndian = TD->isLittleEndian(); 00452 UsesGlobalOffsetTable = false; 00453 ShiftAmountTy = PointerTy = getValueType(TD->getIntPtrType()); 00454 ShiftAmtHandling = Undefined; 00455 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*)); 00456 memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray)); 00457 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8; 00458 allowUnalignedMemoryAccesses = false; 00459 UseUnderscoreSetJmp = false; 00460 UseUnderscoreLongJmp = false; 00461 SelectIsExpensive = false; 00462 IntDivIsCheap = false; 00463 Pow2DivIsCheap = false; 00464 StackPointerRegisterToSaveRestore = 0; 00465 ExceptionPointerRegister = 0; 00466 ExceptionSelectorRegister = 0; 00467 BooleanContents = UndefinedBooleanContent; 00468 SchedPreferenceInfo = SchedulingForLatency; 00469 JumpBufSize = 0; 00470 JumpBufAlignment = 0; 00471 IfCvtBlockSizeLimit = 2; 00472 IfCvtDupBlockSizeLimit = 0; 00473 PrefLoopAlignment = 0; 00474 00475 InitLibcallNames(LibcallRoutineNames); 00476 InitCmpLibcallCCs(CmpLibcallCCs); 00477 00478 // Tell Legalize whether the assembler supports DEBUG_LOC. 00479 const TargetAsmInfo *TASM = TM.getTargetAsmInfo(); 00480 if (!TASM || !TASM->hasDotLocAndDotFile()) 00481 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); 00482 } 00483 00484 TargetLowering::~TargetLowering() {} 00485 00486 /// computeRegisterProperties - Once all of the register classes are added, 00487 /// this allows us to compute derived properties we expose. 00488 void TargetLowering::computeRegisterProperties() { 00489 assert(MVT::LAST_VALUETYPE <= 32 && 00490 "Too many value types for ValueTypeActions to hold!"); 00491 00492 // Everything defaults to needing one register. 00493 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) { 00494 NumRegistersForVT[i] = 1; 00495 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i; 00496 } 00497 // ...except isVoid, which doesn't need any registers. 00498 NumRegistersForVT[MVT::isVoid] = 0; 00499 00500 // Find the largest integer register class. 00501 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE; 00502 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg) 00503 assert(LargestIntReg != MVT::i1 && "No integer registers defined!"); 00504 00505 // Every integer value type larger than this largest register takes twice as 00506 // many registers to represent as the previous ValueType. 00507 for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) { 00508 MVT EVT = (MVT::SimpleValueType)ExpandedReg; 00509 if (!EVT.isInteger()) 00510 break; 00511 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1]; 00512 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg; 00513 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1); 00514 ValueTypeActions.setTypeAction(EVT, Expand); 00515 } 00516 00517 // Inspect all of the ValueType's smaller than the largest integer 00518 // register to see which ones need promotion. 00519 unsigned LegalIntReg = LargestIntReg; 00520 for (unsigned IntReg = LargestIntReg - 1; 00521 IntReg >= (unsigned)MVT::i1; --IntReg) { 00522 MVT IVT = (MVT::SimpleValueType)IntReg; 00523 if (isTypeLegal(IVT)) { 00524 LegalIntReg = IntReg; 00525 } else { 00526 RegisterTypeForVT[IntReg] = TransformToType[IntReg] = 00527 (MVT::SimpleValueType)LegalIntReg; 00528 ValueTypeActions.setTypeAction(IVT, Promote); 00529 } 00530 } 00531 00532 // ppcf128 type is really two f64's. 00533 if (!isTypeLegal(MVT::ppcf128)) { 00534 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64]; 00535 RegisterTypeForVT[MVT::ppcf128] = MVT::f64; 00536 TransformToType[MVT::ppcf128] = MVT::f64; 00537 ValueTypeActions.setTypeAction(MVT::ppcf128, Expand); 00538 } 00539 00540 // Decide how to handle f64. If the target does not have native f64 support, 00541 // expand it to i64 and we will be generating soft float library calls. 00542 if (!isTypeLegal(MVT::f64)) { 00543 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64]; 00544 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64]; 00545 TransformToType[MVT::f64] = MVT::i64; 00546 ValueTypeActions.setTypeAction(MVT::f64, Expand); 00547 } 00548 00549 // Decide how to handle f32. If the target does not have native support for 00550 // f32, promote it to f64 if it is legal. Otherwise, expand it to i32. 00551 if (!isTypeLegal(MVT::f32)) { 00552 if (isTypeLegal(MVT::f64)) { 00553 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64]; 00554 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64]; 00555 TransformToType[MVT::f32] = MVT::f64; 00556 ValueTypeActions.setTypeAction(MVT::f32, Promote); 00557 } else { 00558 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32]; 00559 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32]; 00560 TransformToType[MVT::f32] = MVT::i32; 00561 ValueTypeActions.setTypeAction(MVT::f32, Expand); 00562 } 00563 } 00564 00565 // Loop over all of the vector value types to see which need transformations. 00566 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE; 00567 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) { 00568 MVT VT = (MVT::SimpleValueType)i; 00569 if (!isTypeLegal(VT)) { 00570 MVT IntermediateVT, RegisterVT; 00571 unsigned NumIntermediates; 00572 NumRegistersForVT[i] = 00573 getVectorTypeBreakdown(VT, 00574 IntermediateVT, NumIntermediates, 00575 RegisterVT); 00576 RegisterTypeForVT[i] = RegisterVT; 00577 00578 // Determine if there is a legal wider type. 00579 bool IsLegalWiderType = false; 00580 MVT EltVT = VT.getVectorElementType(); 00581 unsigned NElts = VT.getVectorNumElements(); 00582 for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) { 00583 MVT SVT = (MVT::SimpleValueType)nVT; 00584 if (isTypeLegal(SVT) && SVT.getVectorElementType() == EltVT && 00585 SVT.getVectorNumElements() > NElts) { 00586 TransformToType[i] = SVT; 00587 ValueTypeActions.setTypeAction(VT, Promote); 00588 IsLegalWiderType = true; 00589 break; 00590 } 00591 } 00592 if (!IsLegalWiderType) { 00593 MVT NVT = VT.getPow2VectorType(); 00594 if (NVT == VT) { 00595 // Type is already a power of 2. The default action is to split. 00596 TransformToType[i] = MVT::Other; 00597 ValueTypeActions.setTypeAction(VT, Expand); 00598 } else { 00599 TransformToType[i] = NVT; 00600 ValueTypeActions.setTypeAction(VT, Promote); 00601 } 00602 } 00603 } 00604 } 00605 } 00606 00607 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const { 00608 return NULL; 00609 } 00610 00611 00612 MVT TargetLowering::getSetCCResultType(MVT VT) const { 00613 return getValueType(TD->getIntPtrType()); 00614 } 00615 00616 00617 /// getVectorTypeBreakdown - Vector types are broken down into some number of 00618 /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32 00619 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack. 00620 /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86. 00621 /// 00622 /// This method returns the number of registers needed, and the VT for each 00623 /// register. It also returns the VT and quantity of the intermediate values 00624 /// before they are promoted/expanded. 00625 /// 00626 unsigned TargetLowering::getVectorTypeBreakdown(MVT VT, 00627 MVT &IntermediateVT, 00628 unsigned &NumIntermediates, 00629 MVT &RegisterVT) const { 00630 // Figure out the right, legal destination reg to copy into. 00631 unsigned NumElts = VT.getVectorNumElements(); 00632 MVT EltTy = VT.getVectorElementType(); 00633 00634 unsigned NumVectorRegs = 1; 00635 00636 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we 00637 // could break down into LHS/RHS like LegalizeDAG does. 00638 if (!isPowerOf2_32(NumElts)) { 00639 NumVectorRegs = NumElts; 00640 NumElts = 1; 00641 } 00642 00643 // Divide the input until we get to a supported size. This will always 00644 // end with a scalar if the target doesn't support vectors. 00645 while (NumElts > 1 && !isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) { 00646 NumElts >>= 1; 00647 NumVectorRegs <<= 1; 00648 } 00649 00650 NumIntermediates = NumVectorRegs; 00651 00652 MVT NewVT = MVT::getVectorVT(EltTy, NumElts); 00653 if (!isTypeLegal(NewVT)) 00654 NewVT = EltTy; 00655 IntermediateVT = NewVT; 00656 00657 MVT DestVT = getTypeToTransformTo(NewVT); 00658 RegisterVT = DestVT; 00659 if (DestVT.bitsLT(NewVT)) { 00660 // Value is expanded, e.g. i64 -> i16. 00661 return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits()); 00662 } else { 00663 // Otherwise, promotion or legal types use the same number of registers as 00664 // the vector decimated to the appropriate level. 00665 return NumVectorRegs; 00666 } 00667 00668 return 1; 00669 } 00670 00671 /// getWidenVectorType: given a vector type, returns the type to widen to 00672 /// (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself. 00673 /// If there is no vector type that we want to widen to, returns MVT::Other 00674 /// When and where to widen is target dependent based on the cost of 00675 /// scalarizing vs using the wider vector type. 00676 MVT TargetLowering::getWidenVectorType(MVT VT) const { 00677 assert(VT.isVector()); 00678 if (isTypeLegal(VT)) 00679 return VT; 00680 00681 // Default is not to widen until moved to LegalizeTypes 00682 return MVT::Other; 00683 } 00684 00685 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate 00686 /// function arguments in the caller parameter area. This is the actual 00687 /// alignment, not its logarithm. 00688 unsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const { 00689 return TD->getCallFrameTypeAlignment(Ty); 00690 } 00691 00692 SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table, 00693 SelectionDAG &DAG) const { 00694 if (usesGlobalOffsetTable()) 00695 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy()); 00696 return Table; 00697 } 00698 00699 bool 00700 TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 00701 // Assume that everything is safe in static mode. 00702 if (getTargetMachine().getRelocationModel() == Reloc::Static) 00703 return true; 00704 00705 // In dynamic-no-pic mode, assume that known defined values are safe. 00706 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC && 00707 GA && 00708 !GA->getGlobal()->isDeclaration() && 00709 !GA->getGlobal()->mayBeOverridden()) 00710 return true; 00711 00712 // Otherwise assume nothing is safe. 00713 return false; 00714 } 00715 00716 //===----------------------------------------------------------------------===// 00717 // Optimization Methods 00718 //===----------------------------------------------------------------------===// 00719 00720 /// ShrinkDemandedConstant - Check to see if the specified operand of the 00721 /// specified instruction is a constant integer. If so, check to see if there 00722 /// are any bits set in the constant that are not demanded. If so, shrink the 00723 /// constant and return true. 00724 bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op, 00725 const APInt &Demanded) { 00726 // FIXME: ISD::SELECT, ISD::SELECT_CC 00727 switch (Op.getOpcode()) { 00728 default: break; 00729 case ISD::AND: 00730 case ISD::OR: 00731 case ISD::XOR: 00732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 00733 if (C->getAPIntValue().intersects(~Demanded)) { 00734 MVT VT = Op.getValueType(); 00735 SDValue New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0), 00736 DAG.getConstant(Demanded & 00737 C->getAPIntValue(), 00738 VT)); 00739 return CombineTo(Op, New); 00740 } 00741 break; 00742 } 00743 return false; 00744 } 00745 00746 /// SimplifyDemandedBits - Look at Op. At this point, we know that only the 00747 /// DemandedMask bits of the result of Op are ever used downstream. If we can 00748 /// use this information to simplify Op, create a new simplified DAG node and 00749 /// return true, returning the original and new nodes in Old and New. Otherwise, 00750 /// analyze the expression and return a mask of KnownOne and KnownZero bits for 00751 /// the expression (used to simplify the caller). The KnownZero/One bits may 00752 /// only be accurate for those bits in the DemandedMask. 00753 bool TargetLowering::SimplifyDemandedBits(SDValue Op, 00754 const APInt &DemandedMask, 00755 APInt &KnownZero, 00756 APInt &KnownOne, 00757 TargetLoweringOpt &TLO, 00758 unsigned Depth) const { 00759 unsigned BitWidth = DemandedMask.getBitWidth(); 00760 assert(Op.getValueSizeInBits() == BitWidth && 00761 "Mask size mismatches value type size!"); 00762 APInt NewMask = DemandedMask; 00763 DebugLoc dl = Op.getNode()->getDebugLoc(); 00764 00765 // Don't know anything. 00766 KnownZero = KnownOne = APInt(BitWidth, 0); 00767 00768 // Other users may use these bits. 00769 if (!Op.getNode()->hasOneUse()) { 00770 if (Depth != 0) { 00771 // If not at the root, Just compute the KnownZero/KnownOne bits to 00772 // simplify things downstream. 00773 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth); 00774 return false; 00775 } 00776 // If this is the root being simplified, allow it to have multiple uses, 00777 // just set the NewMask to all bits. 00778 NewMask = APInt::getAllOnesValue(BitWidth); 00779 } else if (DemandedMask == 0) { 00780 // Not demanding any bits from Op. 00781 if (Op.getOpcode() != ISD::UNDEF) 00782 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, dl, 00783 Op.getValueType())); 00784 return false; 00785 } else if (Depth == 6) { // Limit search depth. 00786 return false; 00787 } 00788 00789 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut; 00790 switch (Op.getOpcode()) { 00791 case ISD::Constant: 00792 // We know all of the bits for a constant! 00793 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask; 00794 KnownZero = ~KnownOne & NewMask; 00795 return false; // Don't fall through, will infinitely loop. 00796 case ISD::AND: 00797 // If the RHS is a constant, check to see if the LHS would be zero without 00798 // using the bits from the RHS. Below, we use knowledge about the RHS to 00799 // simplify the LHS, here we're using information from the LHS to simplify 00800 // the RHS. 00801 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 00802 APInt LHSZero, LHSOne; 00803 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask, 00804 LHSZero, LHSOne, Depth+1); 00805 // If the LHS already has zeros where RHSC does, this and is dead. 00806 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask)) 00807 return TLO.CombineTo(Op, Op.getOperand(0)); 00808 // If any of the set bits in the RHS are known zero on the LHS, shrink 00809 // the constant. 00810 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask)) 00811 return true; 00812 } 00813 00814 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 00815 KnownOne, TLO, Depth+1)) 00816 return true; 00817 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 00818 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask, 00819 KnownZero2, KnownOne2, TLO, Depth+1)) 00820 return true; 00821 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 00822 00823 // If all of the demanded bits are known one on one side, return the other. 00824 // These bits cannot contribute to the result of the 'and'. 00825 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask)) 00826 return TLO.CombineTo(Op, Op.getOperand(0)); 00827 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask)) 00828 return TLO.CombineTo(Op, Op.getOperand(1)); 00829 // If all of the demanded bits in the inputs are known zeros, return zero. 00830 if ((NewMask & (KnownZero|KnownZero2)) == NewMask) 00831 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType())); 00832 // If the RHS is a constant, see if we can simplify it. 00833 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask)) 00834 return true; 00835 00836 // Output known-1 bits are only known if set in both the LHS & RHS. 00837 KnownOne &= KnownOne2; 00838 // Output known-0 are known to be clear if zero in either the LHS | RHS. 00839 KnownZero |= KnownZero2; 00840 break; 00841 case ISD::OR: 00842 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 00843 KnownOne, TLO, Depth+1)) 00844 return true; 00845 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 00846 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask, 00847 KnownZero2, KnownOne2, TLO, Depth+1)) 00848 return true; 00849 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 00850 00851 // If all of the demanded bits are known zero on one side, return the other. 00852 // These bits cannot contribute to the result of the 'or'. 00853 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask)) 00854 return TLO.CombineTo(Op, Op.getOperand(0)); 00855 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask)) 00856 return TLO.CombineTo(Op, Op.getOperand(1)); 00857 // If all of the potentially set bits on one side are known to be set on 00858 // the other side, just use the 'other' side. 00859 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask)) 00860 return TLO.CombineTo(Op, Op.getOperand(0)); 00861 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask)) 00862 return TLO.CombineTo(Op, Op.getOperand(1)); 00863 // If the RHS is a constant, see if we can simplify it. 00864 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 00865 return true; 00866 00867 // Output known-0 bits are only known if clear in both the LHS & RHS. 00868 KnownZero &= KnownZero2; 00869 // Output known-1 are known to be set if set in either the LHS | RHS. 00870 KnownOne |= KnownOne2; 00871 break; 00872 case ISD::XOR: 00873 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero, 00874 KnownOne, TLO, Depth+1)) 00875 return true; 00876 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 00877 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2, 00878 KnownOne2, TLO, Depth+1)) 00879 return true; 00880 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 00881 00882 // If all of the demanded bits are known zero on one side, return the other. 00883 // These bits cannot contribute to the result of the 'xor'. 00884 if ((KnownZero & NewMask) == NewMask) 00885 return TLO.CombineTo(Op, Op.getOperand(0)); 00886 if ((KnownZero2 & NewMask) == NewMask) 00887 return TLO.CombineTo(Op, Op.getOperand(1)); 00888 00889 // If all of the unknown bits are known to be zero on one side or the other 00890 // (but not both) turn this into an *inclusive* or. 00891 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 00892 if ((NewMask & ~KnownZero & ~KnownZero2) == 0) 00893 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(), 00894 Op.getOperand(0), 00895 Op.getOperand(1))); 00896 00897 // Output known-0 bits are known if clear or set in both the LHS & RHS. 00898 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); 00899 // Output known-1 are known to be set if set in only one of the LHS, RHS. 00900 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); 00901 00902 // If all of the demanded bits on one side are known, and all of the set 00903 // bits on that side are also known to be set on the other side, turn this 00904 // into an AND, as we know the bits will be cleared. 00905 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 00906 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known 00907 if ((KnownOne & KnownOne2) == KnownOne) { 00908 MVT VT = Op.getValueType(); 00909 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT); 00910 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, 00911 Op.getOperand(0), ANDC)); 00912 } 00913 } 00914 00915 // If the RHS is a constant, see if we can simplify it. 00916 // for XOR, we prefer to force bits to 1 if they will make a -1. 00917 // if we can't force bits, try to shrink constant 00918 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 00919 APInt Expanded = C->getAPIntValue() | (~NewMask); 00920 // if we can expand it to have all bits set, do it 00921 if (Expanded.isAllOnesValue()) { 00922 if (Expanded != C->getAPIntValue()) { 00923 MVT VT = Op.getValueType(); 00924 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0), 00925 TLO.DAG.getConstant(Expanded, VT)); 00926 return TLO.CombineTo(Op, New); 00927 } 00928 // if it already has all the bits set, nothing to change 00929 // but don't shrink either! 00930 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) { 00931 return true; 00932 } 00933 } 00934 00935 KnownZero = KnownZeroOut; 00936 KnownOne = KnownOneOut; 00937 break; 00938 case ISD::SELECT: 00939 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero, 00940 KnownOne, TLO, Depth+1)) 00941 return true; 00942 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2, 00943 KnownOne2, TLO, Depth+1)) 00944 return true; 00945 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 00946 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 00947 00948 // If the operands are constants, see if we can simplify them. 00949 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 00950 return true; 00951 00952 // Only known if known in both the LHS and RHS. 00953 KnownOne &= KnownOne2; 00954 KnownZero &= KnownZero2; 00955 break; 00956 case ISD::SELECT_CC: 00957 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero, 00958 KnownOne, TLO, Depth+1)) 00959 return true; 00960 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2, 00961 KnownOne2, TLO, Depth+1)) 00962 return true; 00963 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 00964 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 00965 00966 // If the operands are constants, see if we can simplify them. 00967 if (TLO.ShrinkDemandedConstant(Op, NewMask)) 00968 return true; 00969 00970 // Only known if known in both the LHS and RHS. 00971 KnownOne &= KnownOne2; 00972 KnownZero &= KnownZero2; 00973 break; 00974 case ISD::SHL: 00975 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 00976 unsigned ShAmt = SA->getZExtValue(); 00977 SDValue InOp = Op.getOperand(0); 00978 00979 // If the shift count is an invalid immediate, don't do anything. 00980 if (ShAmt >= BitWidth) 00981 break; 00982 00983 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a 00984 // single shift. We can do this if the bottom bits (which are shifted 00985 // out) are never demanded. 00986 if (InOp.getOpcode() == ISD::SRL && 00987 isa<ConstantSDNode>(InOp.getOperand(1))) { 00988 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) { 00989 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue(); 00990 unsigned Opc = ISD::SHL; 00991 int Diff = ShAmt-C1; 00992 if (Diff < 0) { 00993 Diff = -Diff; 00994 Opc = ISD::SRL; 00995 } 00996 00997 SDValue NewSA = 00998 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType()); 00999 MVT VT = Op.getValueType(); 01000 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, 01001 InOp.getOperand(0), NewSA)); 01002 } 01003 } 01004 01005 if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt), 01006 KnownZero, KnownOne, TLO, Depth+1)) 01007 return true; 01008 KnownZero <<= SA->getZExtValue(); 01009 KnownOne <<= SA->getZExtValue(); 01010 // low bits known zero. 01011 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue()); 01012 } 01013 break; 01014 case ISD::SRL: 01015 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 01016 MVT VT = Op.getValueType(); 01017 unsigned ShAmt = SA->getZExtValue(); 01018 unsigned VTSize = VT.getSizeInBits(); 01019 SDValue InOp = Op.getOperand(0); 01020 01021 // If the shift count is an invalid immediate, don't do anything. 01022 if (ShAmt >= BitWidth) 01023 break; 01024 01025 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a 01026 // single shift. We can do this if the top bits (which are shifted out) 01027 // are never demanded. 01028 if (InOp.getOpcode() == ISD::SHL && 01029 isa<ConstantSDNode>(InOp.getOperand(1))) { 01030 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) { 01031 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue(); 01032 unsigned Opc = ISD::SRL; 01033 int Diff = ShAmt-C1; 01034 if (Diff < 0) { 01035 Diff = -Diff; 01036 Opc = ISD::SHL; 01037 } 01038 01039 SDValue NewSA = 01040 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType()); 01041 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, 01042 InOp.getOperand(0), NewSA)); 01043 } 01044 } 01045 01046 // Compute the new bits that are at the top now. 01047 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt), 01048 KnownZero, KnownOne, TLO, Depth+1)) 01049 return true; 01050 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01051 KnownZero = KnownZero.lshr(ShAmt); 01052 KnownOne = KnownOne.lshr(ShAmt); 01053 01054 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 01055 KnownZero |= HighBits; // High bits known zero. 01056 } 01057 break; 01058 case ISD::SRA: 01059 // If this is an arithmetic shift right and only the low-bit is set, we can 01060 // always convert this into a logical shr, even if the shift amount is 01061 // variable. The low bit of the shift cannot be an input sign bit unless 01062 // the shift amount is >= the size of the datatype, which is undefined. 01063 if (DemandedMask == 1) 01064 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(), 01065 Op.getOperand(0), Op.getOperand(1))); 01066 01067 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { 01068 MVT VT = Op.getValueType(); 01069 unsigned ShAmt = SA->getZExtValue(); 01070 01071 // If the shift count is an invalid immediate, don't do anything. 01072 if (ShAmt >= BitWidth) 01073 break; 01074 01075 APInt InDemandedMask = (NewMask << ShAmt); 01076 01077 // If any of the demanded bits are produced by the sign extension, we also 01078 // demand the input sign bit. 01079 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); 01080 if (HighBits.intersects(NewMask)) 01081 InDemandedMask |= APInt::getSignBit(VT.getSizeInBits()); 01082 01083 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask, 01084 KnownZero, KnownOne, TLO, Depth+1)) 01085 return true; 01086 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01087 KnownZero = KnownZero.lshr(ShAmt); 01088 KnownOne = KnownOne.lshr(ShAmt); 01089 01090 // Handle the sign bit, adjusted to where it is now in the mask. 01091 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt); 01092 01093 // If the input sign bit is known to be zero, or if none of the top bits 01094 // are demanded, turn this into an unsigned shift right. 01095 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) { 01096 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, 01097 Op.getOperand(0), 01098 Op.getOperand(1))); 01099 } else if (KnownOne.intersects(SignBit)) { // New bits are known one. 01100 KnownOne |= HighBits; 01101 } 01102 } 01103 break; 01104 case ISD::SIGN_EXTEND_INREG: { 01105 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 01106 01107 // Sign extension. Compute the demanded bits in the result that are not 01108 // present in the input. 01109 APInt NewBits = APInt::getHighBitsSet(BitWidth, 01110 BitWidth - EVT.getSizeInBits()) & 01111 NewMask; 01112 01113 // If none of the extended bits are demanded, eliminate the sextinreg. 01114 if (NewBits == 0) 01115 return TLO.CombineTo(Op, Op.getOperand(0)); 01116 01117 APInt InSignBit = APInt::getSignBit(EVT.getSizeInBits()); 01118 InSignBit.zext(BitWidth); 01119 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, 01120 EVT.getSizeInBits()) & 01121 NewMask; 01122 01123 // Since the sign extended bits are demanded, we know that the sign 01124 // bit is demanded. 01125 InputDemandedBits |= InSignBit; 01126 01127 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits, 01128 KnownZero, KnownOne, TLO, Depth+1)) 01129 return true; 01130 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01131 01132 // If the sign bit of the input is known set or clear, then we know the 01133 // top bits of the result. 01134 01135 // If the input sign bit is known zero, convert this into a zero extension. 01136 if (KnownZero.intersects(InSignBit)) 01137 return TLO.CombineTo(Op, 01138 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT)); 01139 01140 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set 01141 KnownOne |= NewBits; 01142 KnownZero &= ~NewBits; 01143 } else { // Input sign bit unknown 01144 KnownZero &= ~NewBits; 01145 KnownOne &= ~NewBits; 01146 } 01147 break; 01148 } 01149 case ISD::ZERO_EXTEND: { 01150 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits(); 01151 APInt InMask = NewMask; 01152 InMask.trunc(OperandBitWidth); 01153 01154 // If none of the top bits are demanded, convert this into an any_extend. 01155 APInt NewBits = 01156 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask; 01157 if (!NewBits.intersects(NewMask)) 01158 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, 01159 Op.getValueType(), 01160 Op.getOperand(0))); 01161 01162 if (SimplifyDemandedBits(Op.getOperand(0), InMask, 01163 KnownZero, KnownOne, TLO, Depth+1)) 01164 return true; 01165 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01166 KnownZero.zext(BitWidth); 01167 KnownOne.zext(BitWidth); 01168 KnownZero |= NewBits; 01169 break; 01170 } 01171 case ISD::SIGN_EXTEND: { 01172 MVT InVT = Op.getOperand(0).getValueType(); 01173 unsigned InBits = InVT.getSizeInBits(); 01174 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits); 01175 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits); 01176 APInt NewBits = ~InMask & NewMask; 01177 01178 // If none of the top bits are demanded, convert this into an any_extend. 01179 if (NewBits == 0) 01180 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl, 01181 Op.getValueType(), 01182 Op.getOperand(0))); 01183 01184 // Since some of the sign extended bits are demanded, we know that the sign 01185 // bit is demanded. 01186 APInt InDemandedBits = InMask & NewMask; 01187 InDemandedBits |= InSignBit; 01188 InDemandedBits.trunc(InBits); 01189 01190 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero, 01191 KnownOne, TLO, Depth+1)) 01192 return true; 01193 KnownZero.zext(BitWidth); 01194 KnownOne.zext(BitWidth); 01195 01196 // If the sign bit is known zero, convert this to a zero extend. 01197 if (KnownZero.intersects(InSignBit)) 01198 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, 01199 Op.getValueType(), 01200 Op.getOperand(0))); 01201 01202 // If the sign bit is known one, the top bits match. 01203 if (KnownOne.intersects(InSignBit)) { 01204 KnownOne |= NewBits; 01205 KnownZero &= ~NewBits; 01206 } else { // Otherwise, top bits aren't known. 01207 KnownOne &= ~NewBits; 01208 KnownZero &= ~NewBits; 01209 } 01210 break; 01211 } 01212 case ISD::ANY_EXTEND: { 01213 unsigned OperandBitWidth = Op.getOperand(0).getValueSizeInBits(); 01214 APInt InMask = NewMask; 01215 InMask.trunc(OperandBitWidth); 01216 if (SimplifyDemandedBits(Op.getOperand(0), InMask, 01217 KnownZero, KnownOne, TLO, Depth+1)) 01218 return true; 01219 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01220 KnownZero.zext(BitWidth); 01221 KnownOne.zext(BitWidth); 01222 break; 01223 } 01224 case ISD::TRUNCATE: { 01225 // Simplify the input, using demanded bit information, and compute the known 01226 // zero/one bits live out. 01227 APInt TruncMask = NewMask; 01228 TruncMask.zext(Op.getOperand(0).getValueSizeInBits()); 01229 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask, 01230 KnownZero, KnownOne, TLO, Depth+1)) 01231 return true; 01232 KnownZero.trunc(BitWidth); 01233 KnownOne.trunc(BitWidth); 01234 01235 // If the input is only used by this truncate, see if we can shrink it based 01236 // on the known demanded bits. 01237 if (Op.getOperand(0).getNode()->hasOneUse()) { 01238 SDValue In = Op.getOperand(0); 01239 unsigned InBitWidth = In.getValueSizeInBits(); 01240 switch (In.getOpcode()) { 01241 default: break; 01242 case ISD::SRL: 01243 // Shrink SRL by a constant if none of the high bits shifted in are 01244 // demanded. 01245 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){ 01246 APInt HighBits = APInt::getHighBitsSet(InBitWidth, 01247 InBitWidth - BitWidth); 01248 HighBits = HighBits.lshr(ShAmt->getZExtValue()); 01249 HighBits.trunc(BitWidth); 01250 01251 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) { 01252 // None of the shifted in bits are needed. Add a truncate of the 01253 // shift input, then shift it. 01254 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl, 01255 Op.getValueType(), 01256 In.getOperand(0)); 01257 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, 01258 Op.getValueType(), 01259 NewTrunc, 01260 In.getOperand(1))); 01261 } 01262 } 01263 break; 01264 } 01265 } 01266 01267 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01268 break; 01269 } 01270 case ISD::AssertZext: { 01271 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); 01272 APInt InMask = APInt::getLowBitsSet(BitWidth, 01273 VT.getSizeInBits()); 01274 if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask, 01275 KnownZero, KnownOne, TLO, Depth+1)) 01276 return true; 01277 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01278 KnownZero |= ~InMask & NewMask; 01279 break; 01280 } 01281 case ISD::BIT_CONVERT: 01282 #if 0 01283 // If this is an FP->Int bitcast and if the sign bit is the only thing that 01284 // is demanded, turn this into a FGETSIGN. 01285 if (NewMask == MVT::getIntegerVTSignBit(Op.getValueType()) && 01286 MVT::isFloatingPoint(Op.getOperand(0).getValueType()) && 01287 !MVT::isVector(Op.getOperand(0).getValueType())) { 01288 // Only do this xform if FGETSIGN is valid or if before legalize. 01289 if (!TLO.AfterLegalize || 01290 isOperationLegal(ISD::FGETSIGN, Op.getValueType())) { 01291 // Make a FGETSIGN + SHL to move the sign bit into the appropriate 01292 // place. We expect the SHL to be eliminated by other optimizations. 01293 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(), 01294 Op.getOperand(0)); 01295 unsigned ShVal = Op.getValueType().getSizeInBits()-1; 01296 SDValue ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy()); 01297 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(), 01298 Sign, ShAmt)); 01299 } 01300 } 01301 #endif 01302 break; 01303 default: 01304 // Just use ComputeMaskedBits to compute output bits. 01305 TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth); 01306 break; 01307 } 01308 01309 // If we know the value of all of the demanded bits, return this as a 01310 // constant. 01311 if ((NewMask & (KnownZero|KnownOne)) == NewMask) 01312 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType())); 01313 01314 return false; 01315 } 01316 01317 /// computeMaskedBitsForTargetNode - Determine which of the bits specified 01318 /// in Mask are known to be either zero or one and return them in the 01319 /// KnownZero/KnownOne bitsets. 01320 void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 01321 const APInt &Mask, 01322 APInt &KnownZero, 01323 APInt &KnownOne, 01324 const SelectionDAG &DAG, 01325 unsigned Depth) const { 01326 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 01327 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 01328 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 01329 Op.getOpcode() == ISD::INTRINSIC_VOID) && 01330 "Should use MaskedValueIsZero if you don't know whether Op" 01331 " is a target node!"); 01332 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); 01333 } 01334 01335 /// ComputeNumSignBitsForTargetNode - This method can be implemented by 01336 /// targets that want to expose additional information about sign bits to the 01337 /// DAG Combiner. 01338 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op, 01339 unsigned Depth) const { 01340 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || 01341 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 01342 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || 01343 Op.getOpcode() == ISD::INTRINSIC_VOID) && 01344 "Should use ComputeNumSignBits if you don't know whether Op" 01345 " is a target node!"); 01346 return 1; 01347 } 01348 01349 static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) { 01350 // Logical shift right or left won't ever introduce new set bits. 01351 // We check for this case because we don't care which bits are 01352 // set, but ComputeMaskedBits won't know anything unless it can 01353 // determine which specific bits may be set. 01354 if (Val.getOpcode() == ISD::SHL || Val.getOpcode() == ISD::SRL) 01355 return ValueHasExactlyOneBitSet(Val.getOperand(0), DAG); 01356 01357 MVT OpVT = Val.getValueType(); 01358 unsigned BitWidth = OpVT.getSizeInBits(); 01359 APInt Mask = APInt::getAllOnesValue(BitWidth); 01360 APInt KnownZero, KnownOne; 01361 DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne); 01362 return (KnownZero.countPopulation() == BitWidth - 1) && 01363 (KnownOne.countPopulation() == 1); 01364 } 01365 01366 /// SimplifySetCC - Try to simplify a setcc built with the specified operands 01367 /// and cc. If it is unable to simplify it, return a null SDValue. 01368 SDValue 01369 TargetLowering::SimplifySetCC(MVT VT, SDValue N0, SDValue N1, 01370 ISD::CondCode Cond, bool foldBooleans, 01371 DAGCombinerInfo &DCI, DebugLoc dl) const { 01372 SelectionDAG &DAG = DCI.DAG; 01373 01374 // These setcc operations always fold. 01375 switch (Cond) { 01376 default: break; 01377 case ISD::SETFALSE: 01378 case ISD::SETFALSE2: return DAG.getConstant(0, VT); 01379 case ISD::SETTRUE: 01380 case ISD::SETTRUE2: return DAG.getConstant(1, VT); 01381 } 01382 01383 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { 01384 const APInt &C1 = N1C->getAPIntValue(); 01385 if (isa<ConstantSDNode>(N0.getNode())) { 01386 return DAG.FoldSetCC(VT, N0, N1, Cond, dl); 01387 } else { 01388 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an 01389 // equality comparison, then we're just comparing whether X itself is 01390 // zero. 01391 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) && 01392 N0.getOperand(0).getOpcode() == ISD::CTLZ && 01393 N0.getOperand(1).getOpcode() == ISD::Constant) { 01394 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); 01395 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 01396 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) { 01397 if ((C1 == 0) == (Cond == ISD::SETEQ)) { 01398 // (srl (ctlz x), 5) == 0 -> X != 0 01399 // (srl (ctlz x), 5) != 1 -> X != 0 01400 Cond = ISD::SETNE; 01401 } else { 01402 // (srl (ctlz x), 5) != 0 -> X == 0 01403 // (srl (ctlz x), 5) == 1 -> X == 0 01404 Cond = ISD::SETEQ; 01405 } 01406 SDValue Zero = DAG.getConstant(0, N0.getValueType()); 01407 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0), 01408 Zero, Cond); 01409 } 01410 } 01411 01412 // If the LHS is '(and load, const)', the RHS is 0, 01413 // the test is for equality or unsigned, and all 1 bits of the const are 01414 // in the same partial word, see if we can shorten the load. 01415 if (DCI.isBeforeLegalize() && 01416 N0.getOpcode() == ISD::AND && C1 == 0 && 01417 isa<LoadSDNode>(N0.getOperand(0)) && 01418 N0.getOperand(0).getNode()->hasOneUse() && 01419 isa<ConstantSDNode>(N0.getOperand(1))) { 01420 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0)); 01421 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); 01422 uint64_t bestMask = 0; 01423 unsigned bestWidth = 0, bestOffset = 0; 01424 if (!Lod->isVolatile() && Lod->isUnindexed()) { 01425 unsigned origWidth = N0.getValueType().getSizeInBits(); 01426 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to 01427 // 8 bits, but have to be careful... 01428 if (Lod->getExtensionType() != ISD::NON_EXTLOAD) 01429 origWidth = Lod->getMemoryVT().getSizeInBits(); 01430 for (unsigned width = origWidth / 2; width>=8; width /= 2) { 01431 uint64_t newMask = (1ULL << width) - 1; 01432 for (unsigned offset=0; offset<origWidth/width; offset++) { 01433 if ((newMask & Mask)==Mask) { 01434 if (!TD->isLittleEndian()) 01435 bestOffset = (origWidth/width - offset - 1) * (width/8); 01436 else 01437 bestOffset = (uint64_t)offset * (width/8); 01438 bestMask = Mask >> (offset * (width/8) * 8); 01439 bestWidth = width; 01440 break; 01441 } 01442 newMask = newMask << width; 01443 } 01444 } 01445 } 01446 if (bestWidth) { 01447 MVT newVT = MVT::getIntegerVT(bestWidth); 01448 if (newVT.isRound()) { 01449 MVT PtrType = Lod->getOperand(1).getValueType(); 01450 SDValue Ptr = Lod->getBasePtr(); 01451 if (bestOffset != 0) 01452 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(), 01453 DAG.getConstant(bestOffset, PtrType)); 01454 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset); 01455 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr, 01456 Lod->getSrcValue(), 01457 Lod->getSrcValueOffset() + bestOffset, 01458 false, NewAlign); 01459 return DAG.getSetCC(dl, VT, 01460 DAG.getNode(ISD::AND, dl, newVT, NewLoad, 01461 DAG.getConstant(bestMask, newVT)), 01462 DAG.getConstant(0LL, newVT), Cond); 01463 } 01464 } 01465 } 01466 01467 // If the LHS is a ZERO_EXTEND, perform the comparison on the input. 01468 if (N0.getOpcode() == ISD::ZERO_EXTEND) { 01469 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits(); 01470 01471 // If the comparison constant has bits in the upper part, the 01472 // zero-extended value could never match. 01473 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(), 01474 C1.getBitWidth() - InSize))) { 01475 switch (Cond) { 01476 case ISD::SETUGT: 01477 case ISD::SETUGE: 01478 case ISD::SETEQ: return DAG.getConstant(0, VT); 01479 case ISD::SETULT: 01480 case ISD::SETULE: 01481 case ISD::SETNE: return DAG.getConstant(1, VT); 01482 case ISD::SETGT: 01483 case ISD::SETGE: 01484 // True if the sign bit of C1 is set. 01485 return DAG.getConstant(C1.isNegative(), VT); 01486 case ISD::SETLT: 01487 case ISD::SETLE: 01488 // True if the sign bit of C1 isn't set. 01489 return DAG.getConstant(C1.isNonNegative(), VT); 01490 default: 01491 break; 01492 } 01493 } 01494 01495 // Otherwise, we can perform the comparison with the low bits. 01496 switch (Cond) { 01497 case ISD::SETEQ: 01498 case ISD::SETNE: 01499 case ISD::SETUGT: 01500 case ISD::SETUGE: 01501 case ISD::SETULT: 01502 case ISD::SETULE: 01503 return DAG.getSetCC(dl, VT, N0.getOperand(0), 01504 DAG.getConstant(APInt(C1).trunc(InSize), 01505 N0.getOperand(0).getValueType()), 01506 Cond); 01507 default: 01508 break; // todo, be more careful with signed comparisons 01509 } 01510 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && 01511 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 01512 MVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); 01513 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits(); 01514 MVT ExtDstTy = N0.getValueType(); 01515 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits(); 01516 01517 // If the extended part has any inconsistent bits, it cannot ever 01518 // compare equal. In other words, they have to be all ones or all 01519 // zeros. 01520 APInt ExtBits = 01521 APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits); 01522 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) 01523 return DAG.getConstant(Cond == ISD::SETNE, VT); 01524 01525 SDValue ZextOp; 01526 MVT Op0Ty = N0.getOperand(0).getValueType(); 01527 if (Op0Ty == ExtSrcTy) { 01528 ZextOp = N0.getOperand(0); 01529 } else { 01530 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits); 01531 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0), 01532 DAG.getConstant(Imm, Op0Ty)); 01533 } 01534 if (!DCI.isCalledByLegalizer()) 01535 DCI.AddToWorklist(ZextOp.getNode()); 01536 // Otherwise, make this a use of a zext. 01537 return DAG.getSetCC(dl, VT, ZextOp, 01538 DAG.getConstant(C1 & APInt::getLowBitsSet( 01539 ExtDstTyBits, 01540 ExtSrcTyBits), 01541 ExtDstTy), 01542 Cond); 01543 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) && 01544 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { 01545 01546 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC 01547 if (N0.getOpcode() == ISD::SETCC) { 01548 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getZExtValue() != 1); 01549 if (TrueWhenTrue) 01550 return N0; 01551 01552 // Invert the condition. 01553 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 01554 CC = ISD::getSetCCInverse(CC, 01555 N0.getOperand(0).getValueType().isInteger()); 01556 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC); 01557 } 01558 01559 if ((N0.getOpcode() == ISD::XOR || 01560 (N0.getOpcode() == ISD::AND && 01561 N0.getOperand(0).getOpcode() == ISD::XOR && 01562 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && 01563 isa<ConstantSDNode>(N0.getOperand(1)) && 01564 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) { 01565 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We 01566 // can only do this if the top bits are known zero. 01567 unsigned BitWidth = N0.getValueSizeInBits(); 01568 if (DAG.MaskedValueIsZero(N0, 01569 APInt::getHighBitsSet(BitWidth, 01570 BitWidth-1))) { 01571 // Okay, get the un-inverted input value. 01572 SDValue Val; 01573 if (N0.getOpcode() == ISD::XOR) 01574 Val = N0.getOperand(0); 01575 else { 01576 assert(N0.getOpcode() == ISD::AND && 01577 N0.getOperand(0).getOpcode() == ISD::XOR); 01578 // ((X^1)&1)^1 -> X & 1 01579 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(), 01580 N0.getOperand(0).getOperand(0), 01581 N0.getOperand(1)); 01582 } 01583 return DAG.getSetCC(dl, VT, Val, N1, 01584 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); 01585 } 01586 } 01587 } 01588 01589 APInt MinVal, MaxVal; 01590 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits(); 01591 if (ISD::isSignedIntSetCC(Cond)) { 01592 MinVal = APInt::getSignedMinValue(OperandBitSize); 01593 MaxVal = APInt::getSignedMaxValue(OperandBitSize); 01594 } else { 01595 MinVal = APInt::getMinValue(OperandBitSize); 01596 MaxVal = APInt::getMaxValue(OperandBitSize); 01597 } 01598 01599 // Canonicalize GE/LE comparisons to use GT/LT comparisons. 01600 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { 01601 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true 01602 // X >= C0 --> X > (C0-1) 01603 return DAG.getSetCC(dl, VT, N0, 01604 DAG.getConstant(C1-1, N1.getValueType()), 01605 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); 01606 } 01607 01608 if (Cond == ISD::SETLE || Cond == ISD::SETULE) { 01609 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true 01610 // X <= C0 --> X < (C0+1) 01611 return DAG.getSetCC(dl, VT, N0, 01612 DAG.getConstant(C1+1, N1.getValueType()), 01613 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); 01614 } 01615 01616 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) 01617 return DAG.getConstant(0, VT); // X < MIN --> false 01618 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal) 01619 return DAG.getConstant(1, VT); // X >= MIN --> true 01620 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal) 01621 return DAG.getConstant(0, VT); // X > MAX --> false 01622 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal) 01623 return DAG.getConstant(1, VT); // X <= MAX --> true 01624 01625 // Canonicalize setgt X, Min --> setne X, Min 01626 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) 01627 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 01628 // Canonicalize setlt X, Max --> setne X, Max 01629 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) 01630 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); 01631 01632 // If we have setult X, 1, turn it into seteq X, 0 01633 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) 01634 return DAG.getSetCC(dl, VT, N0, 01635 DAG.getConstant(MinVal, N0.getValueType()), 01636 ISD::SETEQ); 01637 // If we have setugt X, Max-1, turn it into seteq X, Max 01638 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) 01639 return DAG.getSetCC(dl, VT, N0, 01640 DAG.getConstant(MaxVal, N0.getValueType()), 01641 ISD::SETEQ); 01642 01643 // If we have "setcc X, C0", check to see if we can shrink the immediate 01644 // by changing cc. 01645 01646 // SETUGT X, SINTMAX -> SETLT X, 0 01647 if (Cond == ISD::SETUGT && 01648 C1 == APInt::getSignedMaxValue(OperandBitSize)) 01649 return DAG.getSetCC(dl, VT, N0, 01650 DAG.getConstant(0, N1.getValueType()), 01651 ISD::SETLT); 01652 01653 // SETULT X, SINTMIN -> SETGT X, -1 01654 if (Cond == ISD::SETULT && 01655 C1 == APInt::getSignedMinValue(OperandBitSize)) { 01656 SDValue ConstMinusOne = 01657 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize), 01658 N1.getValueType()); 01659 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT); 01660 } 01661 01662 // Fold bit comparisons when we can. 01663 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 01664 VT == N0.getValueType() && N0.getOpcode() == ISD::AND) 01665 if (ConstantSDNode *AndRHS = 01666 dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 01667 MVT ShiftTy = DCI.isBeforeLegalize() ? 01668 getPointerTy() : getShiftAmountTy(); 01669 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 01670 // Perform the xform if the AND RHS is a single bit. 01671 if (isPowerOf2_64(AndRHS->getZExtValue())) { 01672 return DAG.getNode(ISD::SRL, dl, VT, N0, 01673 DAG.getConstant(Log2_64(AndRHS->getZExtValue()), 01674 ShiftTy)); 01675 } 01676 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getZExtValue()) { 01677 // (X & 8) == 8 --> (X & 8) >> 3 01678 // Perform the xform if C1 is a single bit. 01679 if (C1.isPowerOf2()) { 01680 return DAG.getNode(ISD::SRL, dl, VT, N0, 01681 DAG.getConstant(C1.logBase2(), ShiftTy)); 01682 } 01683 } 01684 } 01685 } 01686 } else if (isa<ConstantSDNode>(N0.getNode())) { 01687 // Ensure that the constant occurs on the RHS. 01688 return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); 01689 } 01690 01691 if (isa<ConstantFPSDNode>(N0.getNode())) { 01692 // Constant fold or commute setcc. 01693 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl); 01694 if (O.getNode()) return O; 01695 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) { 01696 // If the RHS of an FP comparison is a constant, simplify it away in 01697 // some cases. 01698 if (CFP->getValueAPF().isNaN()) { 01699 // If an operand is known to be a nan, we can fold it. 01700 switch (ISD::getUnorderedFlavor(Cond)) { 01701 default: assert(0 && "Unknown flavor!"); 01702 case 0: // Known false. 01703 return DAG.getConstant(0, VT); 01704 case 1: // Known true. 01705 return DAG.getConstant(1, VT); 01706 case 2: // Undefined. 01707 return DAG.getNode(ISD::UNDEF, VT); 01708 } 01709 } 01710 01711 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the 01712 // constant if knowing that the operand is non-nan is enough. We prefer to 01713 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to 01714 // materialize 0.0. 01715 if (Cond == ISD::SETO || Cond == ISD::SETUO) 01716 return DAG.getSetCC(dl, VT, N0, N0, Cond); 01717 } 01718 01719 if (N0 == N1) { 01720 // We can always fold X == X for integer setcc's. 01721 if (N0.getValueType().isInteger()) 01722 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); 01723 unsigned UOF = ISD::getUnorderedFlavor(Cond); 01724 if (UOF == 2) // FP operators that are undefined on NaNs. 01725 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); 01726 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) 01727 return DAG.getConstant(UOF, VT); 01728 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO 01729 // if it is not already. 01730 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; 01731 if (NewCond != Cond) 01732 return DAG.getSetCC(dl, VT, N0, N1, NewCond); 01733 } 01734 01735 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && 01736 N0.getValueType().isInteger()) { 01737 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || 01738 N0.getOpcode() == ISD::XOR) { 01739 // Simplify (X+Y) == (X+Z) --> Y == Z 01740 if (N0.getOpcode() == N1.getOpcode()) { 01741 if (N0.getOperand(0) == N1.getOperand(0)) 01742 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond); 01743 if (N0.getOperand(1) == N1.getOperand(1)) 01744 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond); 01745 if (DAG.isCommutativeBinOp(N0.getOpcode())) { 01746 // If X op Y == Y op X, try other combinations. 01747 if (N0.getOperand(0) == N1.getOperand(1)) 01748 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0), 01749 Cond); 01750 if (N0.getOperand(1) == N1.getOperand(0)) 01751 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1), 01752 Cond); 01753 } 01754 } 01755 01756 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { 01757 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 01758 // Turn (X+C1) == C2 --> X == C2-C1 01759 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) { 01760 return DAG.getSetCC(dl, VT, N0.getOperand(0), 01761 DAG.getConstant(RHSC->getAPIntValue()- 01762 LHSR->getAPIntValue(), 01763 N0.getValueType()), Cond); 01764 } 01765 01766 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. 01767 if (N0.getOpcode() == ISD::XOR) 01768 // If we know that all of the inverted bits are zero, don't bother 01769 // performing the inversion. 01770 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue())) 01771 return 01772 DAG.getSetCC(dl, VT, N0.getOperand(0), 01773 DAG.getConstant(LHSR->getAPIntValue() ^ 01774 RHSC->getAPIntValue(), 01775 N0.getValueType()), 01776 Cond); 01777 } 01778 01779 // Turn (C1-X) == C2 --> X == C1-C2 01780 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { 01781 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) { 01782 return 01783 DAG.getSetCC(dl, VT, N0.getOperand(1), 01784 DAG.getConstant(SUBC->getAPIntValue() - 01785 RHSC->getAPIntValue(), 01786 N0.getValueType()), 01787 Cond); 01788 } 01789 } 01790 } 01791 01792 // Simplify (X+Z) == X --> Z == 0 01793 if (N0.getOperand(0) == N1) 01794 return DAG.getSetCC(dl, VT, N0.getOperand(1), 01795 DAG.getConstant(0, N0.getValueType()), Cond); 01796 if (N0.getOperand(1) == N1) { 01797 if (DAG.isCommutativeBinOp(N0.getOpcode())) 01798 return DAG.getSetCC(dl, VT, N0.getOperand(0), 01799 DAG.getConstant(0, N0.getValueType()), Cond); 01800 else if (N0.getNode()->hasOneUse()) { 01801 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); 01802 // (Z-X) == X --> Z == X<<1 01803 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), 01804 N1, 01805 DAG.getConstant(1, getShiftAmountTy())); 01806 if (!DCI.isCalledByLegalizer()) 01807 DCI.AddToWorklist(SH.getNode()); 01808 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond); 01809 } 01810 } 01811 } 01812 01813 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || 01814 N1.getOpcode() == ISD::XOR) { 01815 // Simplify X == (X+Z) --> Z == 0 01816 if (N1.getOperand(0) == N0) { 01817 return DAG.getSetCC(dl, VT, N1.getOperand(1), 01818 DAG.getConstant(0, N1.getValueType()), Cond); 01819 } else if (N1.getOperand(1) == N0) { 01820 if (DAG.isCommutativeBinOp(N1.getOpcode())) { 01821 return DAG.getSetCC(dl, VT, N1.getOperand(0), 01822 DAG.getConstant(0, N1.getValueType()), Cond); 01823 } else if (N1.getNode()->hasOneUse()) { 01824 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); 01825 // X == (Z-X) --> X<<1 == Z 01826 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0, 01827 DAG.getConstant(1, getShiftAmountTy())); 01828 if (!DCI.isCalledByLegalizer()) 01829 DCI.AddToWorklist(SH.getNode()); 01830 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond); 01831 } 01832 } 01833 } 01834 01835 // Simplify x&y == y to x&y != 0 if y has exactly one bit set. 01836 // Note that where y is variable and is known to have at most 01837 // one bit set (for example, if it is z&1) we cannot do this; 01838 // the expressions are not equivalent when y==0. 01839 if (N0.getOpcode() == ISD::AND) 01840 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) { 01841 if (ValueHasExactlyOneBitSet(N1, DAG)) { 01842 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true); 01843 SDValue Zero = DAG.getConstant(0, N1.getValueType()); 01844 return DAG.getSetCC(dl, VT, N0, Zero, Cond); 01845 } 01846 } 01847 if (N1.getOpcode() == ISD::AND) 01848 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) { 01849 if (ValueHasExactlyOneBitSet(N0, DAG)) { 01850 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true); 01851 SDValue Zero = DAG.getConstant(0, N0.getValueType()); 01852 return DAG.getSetCC(dl, VT, N1, Zero, Cond); 01853 } 01854 } 01855 } 01856 01857 // Fold away ALL boolean setcc's. 01858 SDValue Temp; 01859 if (N0.getValueType() == MVT::i1 && foldBooleans) { 01860 switch (Cond) { 01861 default: assert(0 && "Unknown integer setcc!"); 01862 case ISD::SETEQ: // X == Y -> ~(X^Y) 01863 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1); 01864 N0 = DAG.getNOT(dl, Temp, MVT::i1); 01865 if (!DCI.isCalledByLegalizer()) 01866 DCI.AddToWorklist(Temp.getNode()); 01867 break; 01868 case ISD::SETNE: // X != Y --> (X^Y) 01869 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1); 01870 break; 01871 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y 01872 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y 01873 Temp = DAG.getNOT(dl, N0, MVT::i1); 01874 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); 01875 if (!DCI.isCalledByLegalizer()) 01876 DCI.AddToWorklist(Temp.getNode()); 01877 break; 01878 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X 01879 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X 01880 Temp = DAG.getNOT(dl, N1, MVT::i1); 01881 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp); 01882 if (!DCI.isCalledByLegalizer()) 01883 DCI.AddToWorklist(Temp.getNode()); 01884 break; 01885 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y 01886 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y 01887 Temp = DAG.getNOT(dl, N0, MVT::i1); 01888 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp); 01889 if (!DCI.isCalledByLegalizer()) 01890 DCI.AddToWorklist(Temp.getNode()); 01891 break; 01892 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X 01893 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X 01894 Temp = DAG.getNOT(dl, N1, MVT::i1); 01895 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp); 01896 break; 01897 } 01898 if (VT != MVT::i1) { 01899 if (!DCI.isCalledByLegalizer()) 01900 DCI.AddToWorklist(N0.getNode()); 01901 // FIXME: If running after legalize, we probably can't do this. 01902 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0); 01903 } 01904 return N0; 01905 } 01906 01907 // Could not fold it. 01908 return SDValue(); 01909 } 01910 01911 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the 01912 /// node is a GlobalAddress + offset. 01913 bool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA, 01914 int64_t &Offset) const { 01915 if (isa<GlobalAddressSDNode>(N)) { 01916 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N); 01917 GA = GASD->getGlobal(); 01918 Offset += GASD->getOffset(); 01919 return true; 01920 } 01921 01922 if (N->getOpcode() == ISD::ADD) { 01923 SDValue N1 = N->getOperand(0); 01924 SDValue N2 = N->getOperand(1); 01925 if (isGAPlusOffset(N1.getNode(), GA, Offset)) { 01926 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2); 01927 if (V) { 01928 Offset += V->getSExtValue(); 01929 return true; 01930 } 01931 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) { 01932 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1); 01933 if (V) { 01934 Offset += V->getSExtValue(); 01935 return true; 01936 } 01937 } 01938 } 01939 return false; 01940 } 01941 01942 01943 /// isConsecutiveLoad - Return true if LD (which must be a LoadSDNode) is 01944 /// loading 'Bytes' bytes from a location that is 'Dist' units away from the 01945 /// location that the 'Base' load is loading from. 01946 bool TargetLowering::isConsecutiveLoad(SDNode *LD, SDNode *Base, 01947 unsigned Bytes, int Dist, 01948 const MachineFrameInfo *MFI) const { 01949 if (LD->getOperand(0).getNode() != Base->getOperand(0).getNode()) 01950 return false; 01951 MVT VT = LD->getValueType(0); 01952 if (VT.getSizeInBits() / 8 != Bytes) 01953 return false; 01954 01955 SDValue Loc = LD->getOperand(1); 01956 SDValue BaseLoc = Base->getOperand(1); 01957 if (Loc.getOpcode() == ISD::FrameIndex) { 01958 if (BaseLoc.getOpcode() != ISD::FrameIndex) 01959 return false; 01960 int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); 01961 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); 01962 int FS = MFI->getObjectSize(FI); 01963 int BFS = MFI->getObjectSize(BFI); 01964 if (FS != BFS || FS != (int)Bytes) return false; 01965 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); 01966 } 01967 01968 GlobalValue *GV1 = NULL; 01969 GlobalValue *GV2 = NULL; 01970 int64_t Offset1 = 0; 01971 int64_t Offset2 = 0; 01972 bool isGA1 = isGAPlusOffset(Loc.getNode(), GV1, Offset1); 01973 bool isGA2 = isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); 01974 if (isGA1 && isGA2 && GV1 == GV2) 01975 return Offset1 == (Offset2 + Dist*Bytes); 01976 return false; 01977 } 01978 01979 01980 SDValue TargetLowering:: 01981 PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const { 01982 // Default implementation: no optimization. 01983 return SDValue(); 01984 } 01985 01986 //===----------------------------------------------------------------------===// 01987 // Inline Assembler Implementation Methods 01988 //===----------------------------------------------------------------------===// 01989 01990 01991 TargetLowering::ConstraintType 01992 TargetLowering::getConstraintType(const std::string &Constraint) const { 01993 // FIXME: lots more standard ones to handle. 01994 if (Constraint.size() == 1) { 01995 switch (Constraint[0]) { 01996 default: break; 01997 case 'r': return C_RegisterClass; 01998 case 'm': // memory 01999 case 'o': // offsetable 02000 case 'V': // not offsetable 02001 return C_Memory; 02002 case 'i': // Simple Integer or Relocatable Constant 02003 case 'n': // Simple Integer 02004 case 's': // Relocatable Constant 02005 case 'X': // Allow ANY value. 02006 case 'I': // Target registers. 02007 case 'J': 02008 case 'K': 02009 case 'L': 02010 case 'M': 02011 case 'N': 02012 case 'O': 02013 case 'P': 02014 return C_Other; 02015 } 02016 } 02017 02018 if (Constraint.size() > 1 && Constraint[0] == '{' && 02019 Constraint[Constraint.size()-1] == '}') 02020 return C_Register; 02021 return C_Unknown; 02022 } 02023 02024 /// LowerXConstraint - try to replace an X constraint, which matches anything, 02025 /// with another that has more specific requirements based on the type of the 02026 /// corresponding operand. 02027 const char *TargetLowering::LowerXConstraint(MVT ConstraintVT) const{ 02028 if (ConstraintVT.isInteger()) 02029 return "r"; 02030 if (ConstraintVT.isFloatingPoint()) 02031 return "f"; // works for many targets 02032 return 0; 02033 } 02034 02035 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 02036 /// vector. If it is invalid, don't add anything to Ops. 02037 void TargetLowering::LowerAsmOperandForConstraint(SDValue Op, 02038 char ConstraintLetter, 02039 bool hasMemory, 02040 std::vector<SDValue> &Ops, 02041 SelectionDAG &DAG) const { 02042 switch (ConstraintLetter) { 02043 default: break; 02044 case 'X': // Allows any operand; labels (basic block) use this. 02045 if (Op.getOpcode() == ISD::BasicBlock) { 02046 Ops.push_back(Op); 02047 return; 02048 } 02049 // fall through 02050 case 'i': // Simple Integer or Relocatable Constant 02051 case 'n': // Simple Integer 02052 case 's': { // Relocatable Constant 02053 // These operands are interested in values of the form (GV+C), where C may 02054 // be folded in as an offset of GV, or it may be explicitly added. Also, it 02055 // is possible and fine if either GV or C are missing. 02056 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 02057 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); 02058 02059 // If we have "(add GV, C)", pull out GV/C 02060 if (Op.getOpcode() == ISD::ADD) { 02061 C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 02062 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); 02063 if (C == 0 || GA == 0) { 02064 C = dyn_cast<ConstantSDNode>(Op.getOperand(0)); 02065 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1)); 02066 } 02067 if (C == 0 || GA == 0) 02068 C = 0, GA = 0; 02069 } 02070 02071 // If we find a valid operand, map to the TargetXXX version so that the 02072 // value itself doesn't get selected. 02073 if (GA) { // Either &GV or &GV+C 02074 if (ConstraintLetter != 'n') { 02075 int64_t Offs = GA->getOffset(); 02076 if (C) Offs += C->getZExtValue(); 02077 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(), 02078 Op.getValueType(), Offs)); 02079 return; 02080 } 02081 } 02082 if (C) { // just C, no GV. 02083 // Simple constants are not allowed for 's'. 02084 if (ConstraintLetter != 's') { 02085 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue(), 02086 Op.getValueType())); 02087 return; 02088 } 02089 } 02090 break; 02091 } 02092 } 02093 } 02094 02095 std::vector<unsigned> TargetLowering:: 02096 getRegClassForInlineAsmConstraint(const std::string &Constraint, 02097 MVT VT) const { 02098 return std::vector<unsigned>(); 02099 } 02100 02101 02102 std::pair<unsigned, const TargetRegisterClass*> TargetLowering:: 02103 getRegForInlineAsmConstraint(const std::string &Constraint, 02104 MVT VT) const { 02105 if (Constraint[0] != '{') 02106 return std::pair<unsigned, const TargetRegisterClass*>(0, 0); 02107 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?"); 02108 02109 // Remove the braces from around the name. 02110 std::string RegName(Constraint.begin()+1, Constraint.end()-1); 02111 02112 // Figure out which register class contains this reg. 02113 const TargetRegisterInfo *RI = TM.getRegisterInfo(); 02114 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(), 02115 E = RI->regclass_end(); RCI != E; ++RCI) { 02116 const TargetRegisterClass *RC = *RCI; 02117 02118 // If none of the the value types for this register class are valid, we 02119 // can't use it. For example, 64-bit reg classes on 32-bit targets. 02120 bool isLegal = false; 02121 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); 02122 I != E; ++I) { 02123 if (isTypeLegal(*I)) { 02124 isLegal = true; 02125 break; 02126 } 02127 } 02128 02129 if (!isLegal) continue; 02130 02131 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); 02132 I != E; ++I) { 02133 if (StringsEqualNoCase(RegName, RI->get(*I).AsmName)) 02134 return std::make_pair(*I, RC); 02135 } 02136 } 02137 02138 return std::pair<unsigned, const TargetRegisterClass*>(0, 0); 02139 } 02140 02141 //===----------------------------------------------------------------------===// 02142 // Constraint Selection. 02143 02144 /// isMatchingInputConstraint - Return true of this is an input operand that is 02145 /// a matching constraint like "4". 02146 bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const { 02147 assert(!ConstraintCode.empty() && "No known constraint!"); 02148 return isdigit(ConstraintCode[0]); 02149 } 02150 02151 /// getMatchedOperand - If this is an input matching constraint, this method 02152 /// returns the output operand it matches. 02153 unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const { 02154 assert(!ConstraintCode.empty() && "No known constraint!"); 02155 return atoi(ConstraintCode.c_str()); 02156 } 02157 02158 02159 /// getConstraintGenerality - Return an integer indicating how general CT 02160 /// is. 02161 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) { 02162 switch (CT) { 02163 default: assert(0 && "Unknown constraint type!"); 02164 case TargetLowering::C_Other: 02165 case TargetLowering::C_Unknown: 02166 return 0; 02167 case TargetLowering::C_Register: 02168 return 1; 02169 case TargetLowering::C_RegisterClass: 02170 return 2; 02171 case TargetLowering::C_Memory: 02172 return 3; 02173 } 02174 } 02175 02176 /// ChooseConstraint - If there are multiple different constraints that we 02177 /// could pick for this operand (e.g. "imr") try to pick the 'best' one. 02178 /// This is somewhat tricky: constraints fall into four classes: 02179 /// Other -> immediates and magic values 02180 /// Register -> one specific register 02181 /// RegisterClass -> a group of regs 02182 /// Memory -> memory 02183 /// Ideally, we would pick the most specific constraint possible: if we have 02184 /// something that fits into a register, we would pick it. The problem here 02185 /// is that if we have something that could either be in a register or in 02186 /// memory that use of the register could cause selection of *other* 02187 /// operands to fail: they might only succeed if we pick memory. Because of 02188 /// this the heuristic we use is: 02189 /// 02190 /// 1) If there is an 'other' constraint, and if the operand is valid for 02191 /// that constraint, use it. This makes us take advantage of 'i' 02192 /// constraints when available. 02193 /// 2) Otherwise, pick the most general constraint present. This prefers 02194 /// 'm' over 'r', for example. 02195 /// 02196 static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo, 02197 bool hasMemory, const TargetLowering &TLI, 02198 SDValue Op, SelectionDAG *DAG) { 02199 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options"); 02200 unsigned BestIdx = 0; 02201 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown; 02202 int BestGenerality = -1; 02203 02204 // Loop over the options, keeping track of the most general one. 02205 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) { 02206 TargetLowering::ConstraintType CType = 02207 TLI.getConstraintType(OpInfo.Codes[i]); 02208 02209 // If this is an 'other' constraint, see if the operand is valid for it. 02210 // For example, on X86 we might have an 'rI' constraint. If the operand 02211 // is an integer in the range [0..31] we want to use I (saving a load 02212 // of a register), otherwise we must use 'r'. 02213 if (CType == TargetLowering::C_Other && Op.getNode()) { 02214 assert(OpInfo.Codes[i].size() == 1 && 02215 "Unhandled multi-letter 'other' constraint"); 02216 std::vector<SDValue> ResultOps; 02217 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0], hasMemory, 02218 ResultOps, *DAG); 02219 if (!ResultOps.empty()) { 02220 BestType = CType; 02221 BestIdx = i; 02222 break; 02223 } 02224 } 02225 02226 // This constraint letter is more general than the previous one, use it. 02227 int Generality = getConstraintGenerality(CType); 02228 if (Generality > BestGenerality) { 02229 BestType = CType; 02230 BestIdx = i; 02231 BestGenerality = Generality; 02232 } 02233 } 02234 02235 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx]; 02236 OpInfo.ConstraintType = BestType; 02237 } 02238 02239 /// ComputeConstraintToUse - Determines the constraint code and constraint 02240 /// type to use for the specific AsmOperandInfo, setting 02241 /// OpInfo.ConstraintCode and OpInfo.ConstraintType. 02242 void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo, 02243 SDValue Op, 02244 bool hasMemory, 02245 SelectionDAG *DAG) const { 02246 assert(!OpInfo.Codes.empty() && "Must have at least one constraint"); 02247 02248 // Single-letter constraints ('r') are very common. 02249 if (OpInfo.Codes.size() == 1) { 02250 OpInfo.ConstraintCode = OpInfo.Codes[0]; 02251 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 02252 } else { 02253 ChooseConstraint(OpInfo, hasMemory, *this, Op, DAG); 02254 } 02255 02256 // 'X' matches anything. 02257 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) { 02258 // Labels and constants are handled elsewhere ('X' is the only thing 02259 // that matches labels). 02260 if (isa<BasicBlock>(OpInfo.CallOperandVal) || 02261 isa<ConstantInt>(OpInfo.CallOperandVal)) 02262 return; 02263 02264 // Otherwise, try to resolve it to something we know about by looking at 02265 // the actual operand type. 02266 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) { 02267 OpInfo.ConstraintCode = Repl; 02268 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode); 02269 } 02270 } 02271 } 02272 02273 //===----------------------------------------------------------------------===// 02274 // Loop Strength Reduction hooks 02275 //===----------------------------------------------------------------------===// 02276 02277 /// isLegalAddressingMode - Return true if the addressing mode represented 02278 /// by AM is legal for this target, for a load/store of the specified type. 02279 bool TargetLowering::isLegalAddressingMode(const AddrMode &AM, 02280 const Type *Ty) const { 02281 // The default implementation of this implements a conservative RISCy, r+r and 02282 // r+i addr mode. 02283 02284 // Allows a sign-extended 16-bit immediate field. 02285 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) 02286 return false; 02287 02288 // No global is ever allowed as a base. 02289 if (AM.BaseGV) 02290 return false; 02291 02292 // Only support r+r, 02293 switch (AM.Scale) { 02294 case 0: // "r+i" or just "i", depending on HasBaseReg. 02295 break; 02296 case 1: 02297 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. 02298 return false; 02299 // Otherwise we have r+r or r+i. 02300 break; 02301 case 2: 02302 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. 02303 return false; 02304 // Allow 2*r as r+r. 02305 break; 02306 } 02307 02308 return true; 02309 } 02310 02311 struct mu { 02312 APInt m; // magic number 02313 bool a; // add indicator 02314 unsigned s; // shift amount 02315 }; 02316 02317 /// magicu - calculate the magic numbers required to codegen an integer udiv as 02318 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. 02319 static mu magicu(const APInt& d) { 02320 unsigned p; 02321 APInt nc, delta, q1, r1, q2, r2; 02322 struct mu magu; 02323 magu.a = 0; // initialize "add" indicator 02324 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()); 02325 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); 02326 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); 02327 02328 nc = allOnes - (-d).urem(d); 02329 p = d.getBitWidth() - 1; // initialize p 02330 q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc 02331 r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc) 02332 q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d 02333 r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d) 02334 do { 02335 p = p + 1; 02336 if (r1.uge(nc - r1)) { 02337 q1 = q1 + q1 + 1; // update q1 02338 r1 = r1 + r1 - nc; // update r1 02339 } 02340 else { 02341 q1 = q1+q1; // update q1 02342 r1 = r1+r1; // update r1 02343 } 02344 if ((r2 + 1).uge(d - r2)) { 02345 if (q2.uge(signedMax)) magu.a = 1; 02346 q2 = q2+q2 + 1; // update q2 02347 r2 = r2+r2 + 1 - d; // update r2 02348 } 02349 else { 02350 if (q2.uge(signedMin)) magu.a = 1; 02351 q2 = q2+q2; // update q2 02352 r2 = r2+r2 + 1; // update r2 02353 } 02354 delta = d - 1 - r2; 02355 } while (p < d.getBitWidth()*2 && 02356 (q1.ult(delta) || (q1 == delta && r1 == 0))); 02357 magu.m = q2 + 1; // resulting magic number 02358 magu.s = p - d.getBitWidth(); // resulting shift 02359 return magu; 02360 } 02361 02362 // Magic for divide replacement 02363 struct ms { 02364 APInt m; // magic number 02365 unsigned s; // shift amount 02366 }; 02367 02368 /// magic - calculate the magic numbers required to codegen an integer sdiv as 02369 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, 02370 /// or -1. 02371 static ms magic(const APInt& d) { 02372 unsigned p; 02373 APInt ad, anc, delta, q1, r1, q2, r2, t; 02374 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()); 02375 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); 02376 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); 02377 struct ms mag; 02378 02379 ad = d.abs(); 02380 t = signedMin + (d.lshr(d.getBitWidth() - 1)); 02381 anc = t - 1 - t.urem(ad); // absolute value of nc 02382 p = d.getBitWidth() - 1; // initialize p 02383 q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc) 02384 r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc)) 02385 q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d) 02386 r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d)) 02387 do { 02388 p = p + 1; 02389 q1 = q1<<1; // update q1 = 2p/abs(nc) 02390 r1 = r1<<1; // update r1 = rem(2p/abs(nc)) 02391 if (r1.uge(anc)) { // must be unsigned comparison 02392 q1 = q1 + 1; 02393 r1 = r1 - anc; 02394 } 02395 q2 = q2<<1; // update q2 = 2p/abs(d) 02396 r2 = r2<<1; // update r2 = rem(2p/abs(d)) 02397 if (r2.uge(ad)) { // must be unsigned comparison 02398 q2 = q2 + 1; 02399 r2 = r2 - ad; 02400 } 02401 delta = ad - r2; 02402 } while (q1.ule(delta) || (q1 == delta && r1 == 0)); 02403 02404 mag.m = q2 + 1; 02405 if (d.isNegative()) mag.m = -mag.m; // resulting magic number 02406 mag.s = p - d.getBitWidth(); // resulting shift 02407 return mag; 02408 } 02409 02410 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, 02411 /// return a DAG expression to select that will generate the same value by 02412 /// multiplying by a magic number. See: 02413 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> 02414 SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG, 02415 std::vector<SDNode*>* Created) const { 02416 MVT VT = N->getValueType(0); 02417 DebugLoc dl= N->getDebugLoc(); 02418 02419 // Check to see if we can do this. 02420 // FIXME: We should be more aggressive here. 02421 if (!isTypeLegal(VT)) 02422 return SDValue(); 02423 02424 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue(); 02425 ms magics = magic(d); 02426 02427 // Multiply the numerator (operand 0) by the magic value 02428 // FIXME: We should support doing a MUL in a wider type 02429 SDValue Q; 02430 if (isOperationLegalOrCustom(ISD::MULHS, VT)) 02431 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0), 02432 DAG.getConstant(magics.m, VT)); 02433 else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) 02434 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), 02435 N->getOperand(0), 02436 DAG.getConstant(magics.m, VT)).getNode(), 1); 02437 else 02438 return SDValue(); // No mulhs or equvialent 02439 // If d > 0 and m < 0, add the numerator 02440 if (d.isStrictlyPositive() && magics.m.isNegative()) { 02441 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0)); 02442 if (Created) 02443 Created->push_back(Q.getNode()); 02444 } 02445 // If d < 0 and m > 0, subtract the numerator. 02446 if (d.isNegative() && magics.m.isStrictlyPositive()) { 02447 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0)); 02448 if (Created) 02449 Created->push_back(Q.getNode()); 02450 } 02451 // Shift right algebraic if shift value is nonzero 02452 if (magics.s > 0) { 02453 Q = DAG.getNode(ISD::SRA, dl, VT, Q, 02454 DAG.getConstant(magics.s, getShiftAmountTy())); 02455 if (Created) 02456 Created->push_back(Q.getNode()); 02457 } 02458 // Extract the sign bit and add it to the quotient 02459 SDValue T = 02460 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1, 02461 getShiftAmountTy())); 02462 if (Created) 02463 Created->push_back(T.getNode()); 02464 return DAG.getNode(ISD::ADD, dl, VT, Q, T); 02465 } 02466 02467 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, 02468 /// return a DAG expression to select that will generate the same value by 02469 /// multiplying by a magic number. See: 02470 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> 02471 SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG, 02472 std::vector<SDNode*>* Created) const { 02473 MVT VT = N->getValueType(0); 02474 DebugLoc dl = N->getDebugLoc(); 02475 02476 // Check to see if we can do this. 02477 // FIXME: We should be more aggressive here. 02478 if (!isTypeLegal(VT)) 02479 return SDValue(); 02480 02481 // FIXME: We should use a narrower constant when the upper 02482 // bits are known to be zero. 02483 ConstantSDNode *N1C = cast<ConstantSDNode>(N->getOperand(1)); 02484 mu magics = magicu(N1C->getAPIntValue()); 02485 02486 // Multiply the numerator (operand 0) by the magic value 02487 // FIXME: We should support doing a MUL in a wider type 02488 SDValue Q; 02489 if (isOperationLegalOrCustom(ISD::MULHU, VT)) 02490 Q = DAG.getNode(ISD::MULHU, dl, VT, N->getOperand(0), 02491 DAG.getConstant(magics.m, VT)); 02492 else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) 02493 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), 02494 N->getOperand(0), 02495 DAG.getConstant(magics.m, VT)).getNode(), 1); 02496 else 02497 return SDValue(); // No mulhu or equvialent 02498 if (Created) 02499 Created->push_back(Q.getNode()); 02500 02501 if (magics.a == 0) { 02502 assert(magics.s < N1C->getAPIntValue().getBitWidth() && 02503 "We shouldn't generate an undefined shift!"); 02504 return DAG.getNode(ISD::SRL, dl, VT, Q, 02505 DAG.getConstant(magics.s, getShiftAmountTy())); 02506 } else { 02507 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q); 02508 if (Created) 02509 Created->push_back(NPQ.getNode()); 02510 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ, 02511 DAG.getConstant(1, getShiftAmountTy())); 02512 if (Created) 02513 Created->push_back(NPQ.getNode()); 02514 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q); 02515 if (Created) 02516 Created->push_back(NPQ.getNode()); 02517 return DAG.getNode(ISD::SRL, dl, VT, NPQ, 02518 DAG.getConstant(magics.s-1, getShiftAmountTy())); 02519 } 02520 }