TargetLowering.cpp revision 5b870aff81da0c07413f0241087bb3722954b83d
1310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//                     The LLVM Compiler Infrastructure
4310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===----------------------------------------------------------------------===//
9310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
10310968cbbb564c4141d4bd418a746e8103560222Chris Lattner// This implements the TargetLowering class.
11310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
12310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===----------------------------------------------------------------------===//
13310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
14310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/Target/TargetLowering.h"
15af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner#include "llvm/MC/MCAsmInfo.h"
16beeb93e6ba48af2661eabc4872d8b159fb43e5dbChris Lattner#include "llvm/MC/MCExpr.h"
1707000c6f01d8f57170f2d4c77a86d934bdc5c696Owen Anderson#include "llvm/Target/TargetData.h"
18f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetLoweringObjectFile.h"
19310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/Target/TargetMachine.h"
206f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman#include "llvm/Target/TargetRegisterInfo.h"
21f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetSubtarget.h"
22707e0184233f27e0e9f9aee0309f2daab8cfe7f8Dan Gohman#include "llvm/GlobalVariable.h"
23dc879296090a738c66968f5eec77db65d7e03623Chris Lattner#include "llvm/DerivedTypes.h"
24ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng#include "llvm/CodeGen/MachineFrameInfo.h"
25071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner#include "llvm/CodeGen/MachineJumpTableInfo.h"
26589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner#include "llvm/CodeGen/MachineFunction.h"
27310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/CodeGen/SelectionDAG.h"
28718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson#include "llvm/ADT/STLExtras.h"
29c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
30c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner#include "llvm/Support/MathExtras.h"
31310968cbbb564c4141d4bd418a746e8103560222Chris Lattnerusing namespace llvm;
32310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
339a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindolanamespace llvm {
349a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael EspindolaTLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
359a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isLocal = GV->hasLocalLinkage();
369a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isDeclaration = GV->isDeclaration();
379a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  // FIXME: what should we do for protected and internal visibility?
389a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  // For variables, is internal different from hidden?
399a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isHidden = GV->hasHiddenVisibility();
409a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola
419a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  if (reloc == Reloc::PIC_) {
429a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    if (isLocal || isHidden)
439a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::LocalDynamic;
449a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    else
459a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::GeneralDynamic;
469a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  } else {
479a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    if (!isDeclaration || isHidden)
489a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::LocalExec;
499a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    else
509a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::InitialExec;
519a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  }
529a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola}
539a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola}
549a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola
5556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng/// InitLibcallNames - Set default libcall names.
5656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng///
5779cca507fe51aa8af7a9006b380977c61b6f3feaEvan Chengstatic void InitLibcallNames(const char **Names) {
58c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SHL_I16] = "__ashlhi3";
5956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SHL_I32] = "__ashlsi3";
6056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SHL_I64] = "__ashldi3";
61dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SHL_I128] = "__ashlti3";
62c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SRL_I16] = "__lshrhi3";
6356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRL_I32] = "__lshrsi3";
6456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRL_I64] = "__lshrdi3";
65dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SRL_I128] = "__lshrti3";
66c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SRA_I16] = "__ashrhi3";
6756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRA_I32] = "__ashrsi3";
6856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRA_I64] = "__ashrdi3";
69dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SRA_I128] = "__ashrti3";
708983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::MUL_I8] = "__mulqi3";
71c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::MUL_I16] = "__mulhi3";
7256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_I32] = "__mulsi3";
7356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_I64] = "__muldi3";
745ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::MUL_I128] = "__multi3";
758983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::SDIV_I8] = "__divqi3";
76813090cf891325c715b9f6fb1546e6ce67fa8c8bAnton Korobeynikov  Names[RTLIB::SDIV_I16] = "__divhi3";
7756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SDIV_I32] = "__divsi3";
7856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SDIV_I64] = "__divdi3";
795ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::SDIV_I128] = "__divti3";
808983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::UDIV_I8] = "__udivqi3";
81fb3f84fe16219467cef4ed181530e846177cb4dfAnton Korobeynikov  Names[RTLIB::UDIV_I16] = "__udivhi3";
8256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UDIV_I32] = "__udivsi3";
8356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UDIV_I64] = "__udivdi3";
845ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::UDIV_I128] = "__udivti3";
858983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::SREM_I8] = "__modqi3";
86813090cf891325c715b9f6fb1546e6ce67fa8c8bAnton Korobeynikov  Names[RTLIB::SREM_I16] = "__modhi3";
8756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SREM_I32] = "__modsi3";
8856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SREM_I64] = "__moddi3";
895ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::SREM_I128] = "__modti3";
908983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::UREM_I8] = "__umodqi3";
919fe9c8ec7d7d1e260927cb66e91d5e69db8a6febAnton Korobeynikov  Names[RTLIB::UREM_I16] = "__umodhi3";
9256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UREM_I32] = "__umodsi3";
9356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UREM_I64] = "__umoddi3";
945ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::UREM_I128] = "__umodti3";
9556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::NEG_I32] = "__negsi2";
9656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::NEG_I64] = "__negdi2";
9756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::ADD_F32] = "__addsf3";
9856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::ADD_F64] = "__adddf3";
99007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::ADD_F80] = "__addxf3";
100161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
10156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SUB_F32] = "__subsf3";
10256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SUB_F64] = "__subdf3";
103007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SUB_F80] = "__subxf3";
104161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
10556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_F32] = "__mulsf3";
10656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_F64] = "__muldf3";
107007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::MUL_F80] = "__mulxf3";
108161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
10956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::DIV_F32] = "__divsf3";
11056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::DIV_F64] = "__divdf3";
111007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::DIV_F80] = "__divxf3";
112161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
11356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::REM_F32] = "fmodf";
11456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::REM_F64] = "fmod";
115007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::REM_F80] = "fmodl";
116161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::REM_PPCF128] = "fmodl";
11756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::POWI_F32] = "__powisf2";
11856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::POWI_F64] = "__powidf2";
119161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::POWI_F80] = "__powixf2";
120161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::POWI_PPCF128] = "__powitf2";
12156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SQRT_F32] = "sqrtf";
12256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SQRT_F64] = "sqrt";
123161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SQRT_F80] = "sqrtl";
124161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SQRT_PPCF128] = "sqrtl";
1257794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F32] = "logf";
1267794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F64] = "log";
1277794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F80] = "logl";
1287794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_PPCF128] = "logl";
1297794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F32] = "log2f";
1307794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F64] = "log2";
1317794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F80] = "log2l";
1327794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_PPCF128] = "log2l";
1337794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F32] = "log10f";
1347794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F64] = "log10";
1357794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F80] = "log10l";
1367794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_PPCF128] = "log10l";
1377794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F32] = "expf";
1387794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F64] = "exp";
1397794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F80] = "expl";
1407794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_PPCF128] = "expl";
1417794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F32] = "exp2f";
1427794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F64] = "exp2";
1437794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F80] = "exp2l";
1447794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_PPCF128] = "exp2l";
14556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SIN_F32] = "sinf";
14656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SIN_F64] = "sin";
147007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SIN_F80] = "sinl";
148007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SIN_PPCF128] = "sinl";
14956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::COS_F32] = "cosf";
15056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::COS_F64] = "cos";
151007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::COS_F80] = "cosl";
152007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::COS_PPCF128] = "cosl";
153e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F32] = "powf";
154e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F64] = "pow";
155e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F80] = "powl";
156e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_PPCF128] = "powl";
1572bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F32] = "ceilf";
1582bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F64] = "ceil";
1592bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F80] = "ceill";
1602bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_PPCF128] = "ceill";
1612bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F32] = "truncf";
1622bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F64] = "trunc";
1632bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F80] = "truncl";
1642bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_PPCF128] = "truncl";
1652bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F32] = "rintf";
1662bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F64] = "rint";
1672bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F80] = "rintl";
1682bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_PPCF128] = "rintl";
1692bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
1702bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F64] = "nearbyint";
1712bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
1722bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
1732bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F32] = "floorf";
1742bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F64] = "floor";
1752bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F80] = "floorl";
1762bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_PPCF128] = "floorl";
17756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
17856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
179e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
180e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
181e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
182e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
1837d8d36a69f4089d1829797aae90d276c9fb5b260Sanjiv Gupta  Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfi8";
1847d8d36a69f4089d1829797aae90d276c9fb5b260Sanjiv Gupta  Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfi16";
18556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
18656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
187a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
18856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
18956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
190a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
191be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands  Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
192161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
193a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
194041cde26eaf4ef6171ff1a44aeedd08d7a1cba6cDuncan Sands  Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
195161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
196a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
1977d8d36a69f4089d1829797aae90d276c9fb5b260Sanjiv Gupta  Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfi8";
1987d8d36a69f4089d1829797aae90d276c9fb5b260Sanjiv Gupta  Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfi16";
19956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
20056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
201a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
20256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
20356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
204a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
205161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
206161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
207a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
208041cde26eaf4ef6171ff1a44aeedd08d7a1cba6cDuncan Sands  Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
209161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
210a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
21156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
21256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
2139bed0f58eb7f5e9e22d062f3880a5fabfdd0dfddDuncan Sands  Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
2149bed0f58eb7f5e9e22d062f3880a5fabfdd0dfddDuncan Sands  Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
21556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
21656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
217161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
218161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
219d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
220d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
221d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
222d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
22356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
22456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
225ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
226ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
22756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
22856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
229ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
230ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
231ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
232ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
233ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
234ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
23556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OEQ_F32] = "__eqsf2";
23656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OEQ_F64] = "__eqdf2";
23756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UNE_F32] = "__nesf2";
23856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UNE_F64] = "__nedf2";
23956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGE_F32] = "__gesf2";
24056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGE_F64] = "__gedf2";
24156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLT_F32] = "__ltsf2";
24256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLT_F64] = "__ltdf2";
24356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLE_F32] = "__lesf2";
24456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLE_F64] = "__ledf2";
24556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGT_F32] = "__gtsf2";
24656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGT_F64] = "__gtdf2";
24756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UO_F32] = "__unordsf2";
24856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UO_F64] = "__unorddf2";
249d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  Names[RTLIB::O_F32] = "__unordsf2";
250d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  Names[RTLIB::O_F64] = "__unorddf2";
251a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMCPY] = "memcpy";
252a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMMOVE] = "memmove";
253a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMSET] = "memset";
254b0f1e1780c736c62fb99e5824825d2a60a53b53bDuncan Sands  Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
255d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng}
256d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng
25772977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov/// InitLibcallCallingConvs - Set default libcall CallingConvs.
25872977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov///
25972977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikovstatic void InitLibcallCallingConvs(CallingConv::ID *CCs) {
26072977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
26172977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov    CCs[i] = CallingConv::C;
26272977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  }
26372977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov}
26472977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov
265b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPEXT - Return the FPEXT_*_* value for the given types, or
266b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
267e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
268825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
269825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f64)
270b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPEXT_F32_F64;
271b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
272b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
273b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
274b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
275b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPROUND - Return the FPROUND_*_* value for the given types, or
276b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
277e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
278825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (RetVT == MVT::f32) {
279825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f64)
280b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPROUND_F64_F32;
281825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f80)
282e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_F80_F32;
283825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::ppcf128)
284e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_PPCF128_F32;
285825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (RetVT == MVT::f64) {
286825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f80)
287e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_F80_F64;
288825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::ppcf128)
289e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_PPCF128_F64;
290b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
291b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
292b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
293b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
294b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
295b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
296e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
297825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
298825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i8)
2998aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOSINT_F32_I8;
300825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i16)
3018aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOSINT_F32_I16;
302825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
303b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I32;
304825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
305b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I64;
306825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
307b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I128;
308825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f64) {
309825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
310b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I32;
311825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
312b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I64;
313825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
314b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I128;
315825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f80) {
316825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
317b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I32;
318825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
319b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I64;
320825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
321b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I128;
322825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::ppcf128) {
323825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
324b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I32;
325825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
326b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I64;
327825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
328b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I128;
329b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
330b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
331b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
332b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
333b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
334b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
335e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
336825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
337825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i8)
3388aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOUINT_F32_I8;
339825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i16)
3408aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOUINT_F32_I16;
341825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
342b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I32;
343825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
344b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I64;
345825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
346b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I128;
347825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f64) {
348825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
349b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I32;
350825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
351b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I64;
352825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
353b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I128;
354825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f80) {
355825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
356b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I32;
357825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
358b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I64;
359825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
360b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I128;
361825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::ppcf128) {
362825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
363b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I32;
364825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
365b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I64;
366825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
367b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I128;
368b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
369b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
370b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
371b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
372b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
373b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
374e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
375825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::i32) {
376825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
377b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F32;
378825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
379b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F64;
380825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
381b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F80;
382825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
383b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_PPCF128;
384825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i64) {
385825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
386b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F32;
387825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
388b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F64;
389825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
390b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F80;
391825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
392b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_PPCF128;
393825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i128) {
394825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
395b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F32;
396825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
397b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F64;
398825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
399b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F80;
400825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
401b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_PPCF128;
402b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
403b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
404b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
405b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
406b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
407b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
408e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
409825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::i32) {
410825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
411b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F32;
412825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
413b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F64;
414825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
415b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F80;
416825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
417b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_PPCF128;
418825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i64) {
419825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
420b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F32;
421825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
422b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F64;
423825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
424b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F80;
425825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
426b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_PPCF128;
427825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i128) {
428825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
429b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F32;
430825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
431b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F64;
432825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
433b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F80;
434825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
435b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_PPCF128;
436b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
437b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
438b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
439b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
440d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng/// InitCmpLibcallCCs - Set default comparison libcall CC.
441d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng///
442d385fd62cb43435b3ad70d789198d34bf148e579Evan Chengstatic void InitCmpLibcallCCs(ISD::CondCode *CCs) {
443d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
444d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
445d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
446d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UNE_F32] = ISD::SETNE;
447d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UNE_F64] = ISD::SETNE;
448d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGE_F32] = ISD::SETGE;
449d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGE_F64] = ISD::SETGE;
450d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLT_F32] = ISD::SETLT;
451d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLT_F64] = ISD::SETLT;
452d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLE_F32] = ISD::SETLE;
453d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLE_F64] = ISD::SETLE;
454d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGT_F32] = ISD::SETGT;
455d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGT_F64] = ISD::SETGT;
456d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UO_F32] = ISD::SETNE;
457d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UO_F64] = ISD::SETNE;
458d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::O_F32] = ISD::SETEQ;
459d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::O_F64] = ISD::SETEQ;
46056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng}
46156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng
462f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// NOTE: The constructor takes ownership of TLOF.
463f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLowering::TargetLowering(TargetMachine &tm,TargetLoweringObjectFile *tlof)
464f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  : TM(tm), TD(TM.getTargetData()), TLOF(*tlof) {
465cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  // All operations default to being supported.
466cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  memset(OpActions, 0, sizeof(OpActions));
4670329466b6b4927f4e6f5d144891fef06a027fec5Evan Cheng  memset(LoadExtActions, 0, sizeof(LoadExtActions));
468ddf89566a93081cb230bb9406a72ab2d3eada4a7Chris Lattner  memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
469c9133f97720a36218ddfa6bfbf36ba6e22c011f7Chris Lattner  memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
470c9133f97720a36218ddfa6bfbf36ba6e22c011f7Chris Lattner  memset(ConvertActions, 0, sizeof(ConvertActions));
4717f042681764c6f8eae22781d8b4cb4c218a86b76Evan Cheng  memset(CondCodeActions, 0, sizeof(CondCodeActions));
47293f81e2822aa67337f629398c81bc2844ef9400aDan Gohman
4731a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner  // Set default actions for various operations.
474825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
4751a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner    // Default all indexed load / store to expand.
4765ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng    for (unsigned IM = (unsigned)ISD::PRE_INC;
4775ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng         IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
478825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
479825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
4805ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng    }
4811a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner
4821a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner    // These operations default to expand.
483825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
484825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
4855ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng  }
486d2cde68855125b6815b1575f29cd96927614b0cdEvan Cheng
487d2cde68855125b6815b1575f29cd96927614b0cdEvan Cheng  // Most targets ignore the @llvm.prefetch intrinsic.
488825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
489e179584f9b740cf3a36bde70f8cab40de59b8081Nate Begeman
490e179584f9b740cf3a36bde70f8cab40de59b8081Nate Begeman  // ConstantFP nodes default to expand.  Targets can either change this to
491eb2f969a4ddfb0bc8fdcb5bce3b52e53abff321dEvan Cheng  // Legal, in which case all fp constants are legal, or use isFPImmLegal()
492e179584f9b740cf3a36bde70f8cab40de59b8081Nate Begeman  // to optimize expansions for certain constants.
493825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
494825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
495825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
496310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
4970bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen  // These library functions default to expand.
498825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG , MVT::f64, Expand);
499825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG2, MVT::f64, Expand);
500825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG10,MVT::f64, Expand);
501825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP , MVT::f64, Expand);
502825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP2, MVT::f64, Expand);
503825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG , MVT::f32, Expand);
504825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG2, MVT::f32, Expand);
505825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG10,MVT::f32, Expand);
506825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP , MVT::f32, Expand);
507825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP2, MVT::f32, Expand);
5080bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen
50941bab0beac2600701af99f63e23a6b65f71468acChris Lattner  // Default ISD::TRAP to expand (which turns it into abort).
510825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::TRAP, MVT::Other, Expand);
51141bab0beac2600701af99f63e23a6b65f71468acChris Lattner
512a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson  IsLittleEndian = TD->isLittleEndian();
5131d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  ShiftAmountTy = PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
514825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
515718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson  memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
516a03a5dc7ce876dac4b3b91bae14216de4233bacdEvan Cheng  maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
5176ebf7bc7405ee79d27d50b70f0c1a474cbea820dEvan Cheng  benefitFromCodePlacementOpt = false;
518d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreSetJmp = false;
519d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreLongJmp = false;
5206618039f9f1f2e41fe2742d83007c68bdebc3656Chris Lattner  SelectIsExpensive = false;
521405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  IntDivIsCheap = false;
522405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  Pow2DivIsCheap = false;
523ee4a76563a84839453588104e94d4891fc44d625Chris Lattner  StackPointerRegisterToSaveRestore = 0;
5249bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionPointerRegister = 0;
5259bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionSelectorRegister = 0;
52603228089d5235f8c90477f88809139464e9c6ea5Duncan Sands  BooleanContents = UndefinedBooleanContent;
5270577a22c678bd5e31047e6b8038c6917202271eeEvan Cheng  SchedPreferenceInfo = SchedulingForLatency;
5287acf5f39fea463df5ad196affbef0176f3b25e0fChris Lattner  JumpBufSize = 0;
5290c9e0ff24900aa2bcae9518f8b1bdc3fd41dc2edDuraid Madina  JumpBufAlignment = 0;
530d60483ef99a15630abba934001b1ff7d16a33b18Evan Cheng  IfCvtBlockSizeLimit = 2;
531fb8075d03f5c87bd57dcc9c5f2304f6b13c55aadEvan Cheng  IfCvtDupBlockSizeLimit = 0;
532fb8075d03f5c87bd57dcc9c5f2304f6b13c55aadEvan Cheng  PrefLoopAlignment = 0;
53356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng
53456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  InitLibcallNames(LibcallRoutineNames);
535d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  InitCmpLibcallCCs(CmpLibcallCCs);
53672977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  InitLibcallCallingConvs(LibcallCallingConvs);
537310968cbbb564c4141d4bd418a746e8103560222Chris Lattner}
538310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
539f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLowering::~TargetLowering() {
540f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  delete &TLOF;
541f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
542cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
543f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// canOpTrap - Returns true if the operation can trap for the value type.
544f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// VT must be a legal type.
545f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wangbool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
546f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  assert(isTypeLegal(VT));
547f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  switch (Op) {
548f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  default:
549f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return false;
550f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FDIV:
551f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FREM:
552f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SDIV:
553f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UDIV:
554f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SREM:
555f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UREM:
556f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return true;
557f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  }
558f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang}
559f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
560f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
56123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonstatic unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
56223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                       unsigned &NumIntermediates,
56323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                       EVT &RegisterVT,
56423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                       TargetLowering* TLI) {
56523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Figure out the right, legal destination reg to copy into.
56623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumElts = VT.getVectorNumElements();
56723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT EltTy = VT.getVectorElementType();
56823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
56923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumVectorRegs = 1;
57023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
57123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
57223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // could break down into LHS/RHS like LegalizeDAG does.
57323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!isPowerOf2_32(NumElts)) {
57423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs = NumElts;
57523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts = 1;
57623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
57723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
57823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Divide the input until we get to a supported size.  This will always
57923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // end with a scalar if the target doesn't support vectors.
58023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
58123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts >>= 1;
58223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs <<= 1;
58323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
58423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
58523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  NumIntermediates = NumVectorRegs;
58623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
58723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
58823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!TLI->isTypeLegal(NewVT))
58923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NewVT = EltTy;
59023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  IntermediateVT = NewVT;
59123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
59223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = TLI->getRegisterType(NewVT);
59323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  RegisterVT = DestVT;
59423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (EVT(DestVT).bitsLT(NewVT)) {
59523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    // Value is expanded, e.g. i64 -> i16.
59623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
59723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  } else {
59823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    // Otherwise, promotion or legal types use the same number of registers as
59923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    // the vector decimated to the appropriate level.
60023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    return NumVectorRegs;
60123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
60223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
60323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  return 1;
60423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson}
60523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
606310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// computeRegisterProperties - Once all of the register classes are added,
607310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// this allows us to compute derived properties we expose.
608310968cbbb564c4141d4bd418a746e8103560222Chris Lattnervoid TargetLowering::computeRegisterProperties() {
609825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
610bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner         "Too many value types for ValueTypeActions to hold!");
611bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
612b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Everything defaults to needing one register.
613825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
614b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[i] = 1;
615825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
616b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
617b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // ...except isVoid, which doesn't need any registers.
618825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  NumRegistersForVT[MVT::isVoid] = 0;
619f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
620310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Find the largest integer register class.
621825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
622310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
623825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
624310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
625310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Every integer value type larger than this largest register takes twice as
626310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // many registers to represent as the previous ValueType.
62783ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
6288a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
6298a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    if (!ExpandedVT.isInteger())
63083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      break;
631b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
632825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
633825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
6348a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    ValueTypeActions.setTypeAction(ExpandedVT, Expand);
635b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
636310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
637b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Inspect all of the ValueType's smaller than the largest integer
638b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // register to see which ones need promotion.
63983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned LegalIntReg = LargestIntReg;
64083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned IntReg = LargestIntReg - 1;
641825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       IntReg >= (unsigned)MVT::i1; --IntReg) {
642825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    EVT IVT = (MVT::SimpleValueType)IntReg;
64383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (isTypeLegal(IVT)) {
644b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      LegalIntReg = IntReg;
645b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
64683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
647825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        (MVT::SimpleValueType)LegalIntReg;
64883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      ValueTypeActions.setTypeAction(IVT, Promote);
649b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
650b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
651f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
652161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  // ppcf128 type is really two f64's.
653825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::ppcf128)) {
654825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
655825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
656825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::ppcf128] = MVT::f64;
657825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    ValueTypeActions.setTypeAction(MVT::ppcf128, Expand);
658161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  }
659161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen
660b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f64. If the target does not have native f64 support,
661b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // expand it to i64 and we will be generating soft float library calls.
662825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f64)) {
663825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
664825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
665825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::f64] = MVT::i64;
666825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    ValueTypeActions.setTypeAction(MVT::f64, Expand);
6671a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
668b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
669b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f32. If the target does not have native support for
670b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
671825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f32)) {
672825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (isTypeLegal(MVT::f64)) {
673825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
674825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
675825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::f64;
676825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      ValueTypeActions.setTypeAction(MVT::f32, Promote);
677b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
678825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
679825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
680825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::i32;
681825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      ValueTypeActions.setTypeAction(MVT::f32, Expand);
682b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
6831a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
6844ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman
685b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Loop over all of the vector value types to see which need transformations.
686825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
687825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
68823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    MVT VT = (MVT::SimpleValueType)i;
68983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (!isTypeLegal(VT)) {
69023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson      MVT IntermediateVT;
69123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson      EVT RegisterVT;
692b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      unsigned NumIntermediates;
693b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      NumRegistersForVT[i] =
69423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson        getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
69523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                  RegisterVT, this);
696b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      RegisterTypeForVT[i] = RegisterVT;
69787c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang
69887c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      // Determine if there is a legal wider type.
69987c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      bool IsLegalWiderType = false;
700e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT EltVT = VT.getVectorElementType();
70187c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      unsigned NElts = VT.getVectorNumElements();
702825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
703825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        EVT SVT = (MVT::SimpleValueType)nVT;
70487c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang        if (isTypeLegal(SVT) && SVT.getVectorElementType() == EltVT &&
7056fb474bd3c3e291973ae8e087787090cf2be73f8Mon P Wang            SVT.getVectorNumElements() > NElts && NElts != 1) {
70687c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          TransformToType[i] = SVT;
70787c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          ValueTypeActions.setTypeAction(VT, Promote);
70887c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          IsLegalWiderType = true;
70987c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          break;
71087c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang        }
71187c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      }
71287c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      if (!IsLegalWiderType) {
713e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT NVT = VT.getPow2VectorType();
71487c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang        if (NVT == VT) {
71587c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          // Type is already a power of 2.  The default action is to split.
716825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson          TransformToType[i] = MVT::Other;
71787c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          ValueTypeActions.setTypeAction(VT, Expand);
71887c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang        } else {
71987c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          TransformToType[i] = NVT;
72087c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang          ValueTypeActions.setTypeAction(VT, Promote);
72187c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang        }
72287c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      }
7237f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    }
7243a59358499f527ad9a8d1d4ed7d80b6bf0f1c12dChris Lattner  }
725bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner}
726cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
7277226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Chengconst char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
7287226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng  return NULL;
7297226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng}
7303a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng
7315b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
732825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen AndersonMVT::SimpleValueType TargetLowering::getSetCCResultType(EVT VT) const {
7331d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  return PointerTy.SimpleTy;
7345b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel}
7355b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
7368f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv GuptaMVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
7378f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta  return MVT::i32; // return the default value
7388f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta}
7398f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta
7407f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// getVectorTypeBreakdown - Vector types are broken down into some number of
741825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
742825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
743825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
744dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
7457f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// This method returns the number of registers needed, and the VT for each
746b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// register.  It also returns the VT and quantity of the intermediate values
747b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// before they are promoted/expanded.
748dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
74923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonunsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
750e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                                                EVT &IntermediateVT,
751b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman                                                unsigned &NumIntermediates,
75223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                                EVT &RegisterVT) const {
753dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // Figure out the right, legal destination reg to copy into.
75483ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned NumElts = VT.getVectorNumElements();
755e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT EltTy = VT.getVectorElementType();
756dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
757dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  unsigned NumVectorRegs = 1;
758dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
759d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
760d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  // could break down into LHS/RHS like LegalizeDAG does.
761d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  if (!isPowerOf2_32(NumElts)) {
762d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumVectorRegs = NumElts;
763d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumElts = 1;
764d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  }
765d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman
766dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // Divide the input until we get to a supported size.  This will always
767dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // end with a scalar if the target doesn't support vectors.
76823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !isTypeLegal(
76923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                   EVT::getVectorVT(Context, EltTy, NumElts))) {
770dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumElts >>= 1;
771dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumVectorRegs <<= 1;
772dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  }
773b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
774b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  NumIntermediates = NumVectorRegs;
775dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
77623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
7777f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman  if (!isTypeLegal(NewVT))
7787f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    NewVT = EltTy;
779b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  IntermediateVT = NewVT;
780dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
78123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = getRegisterType(Context, NewVT);
782b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  RegisterVT = DestVT;
7838e4eb09b1e3571965f49edcdfb56b1375b1b7551Duncan Sands  if (DestVT.bitsLT(NewVT)) {
784dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    // Value is expanded, e.g. i64 -> i16.
78583ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    return NumVectorRegs*(NewVT.getSizeInBits()/DestVT.getSizeInBits());
786dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  } else {
787dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    // Otherwise, promotion or legal types use the same number of registers as
788dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    // the vector decimated to the appropriate level.
78979227e2906656b2c92965f5dbebcd66a5774c87fChris Lattner    return NumVectorRegs;
790dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  }
791dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
792e9b3da17cdc5b54bb1194043f27d4d2914d77097Evan Cheng  return 1;
793dc879296090a738c66968f5eec77db65d7e03623Chris Lattner}
794dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
7950c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang/// getWidenVectorType: given a vector type, returns the type to widen to
7960c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang/// (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
797825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// If there is no vector type that we want to widen to, returns MVT::Other
798f007a8b931e229eb325319c97363be8507311e2eMon P Wang/// When and where to widen is target dependent based on the cost of
7990c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang/// scalarizing vs using the wider vector type.
800e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonEVT TargetLowering::getWidenVectorType(EVT VT) const {
8010c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang  assert(VT.isVector());
8020c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang  if (isTypeLegal(VT))
8030c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang    return VT;
8040c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang
8050c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang  // Default is not to widen until moved to LegalizeTypes
806825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  return MVT::Other;
8070c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang}
8080c39719bfc7d0b3e61fbd55e1115184a1d5f6ae7Mon P Wang
8093ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
81028d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// function arguments in the caller parameter area.  This is the actual
81128d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// alignment, not its logarithm.
8123ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Chengunsigned TargetLowering::getByValTypeAlignment(const Type *Ty) const {
81328d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen  return TD->getCallFrameTypeAlignment(Ty);
8143ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng}
8153ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng
816071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// getJumpTableEncoding - Return the entry encoding for a jump table in the
817071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// current function.  The returned value is a member of the
818071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// MachineJumpTableInfo::JTEntryKind enum.
819071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattnerunsigned TargetLowering::getJumpTableEncoding() const {
820071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In non-pic modes, just use the address of a block.
821071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
822071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_BlockAddress;
823071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner
824071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In PIC mode, if the target supports a GPRel32 directive, use it.
825071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
826071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_GPRel32BlockAddress;
827071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner
828071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // Otherwise, use a label difference.
829071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  return MachineJumpTableInfo::EK_LabelDifference32;
830071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner}
831071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner
832475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
833475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                 SelectionDAG &DAG) const {
834f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  // If our PIC model is GP relative, use the global offset table as the base.
835f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  if (getJumpTableEncoding() == MachineJumpTableInfo::EK_GPRel32BlockAddress)
836b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen    return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
837cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng  return Table;
838cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng}
839cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng
84013e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
84113e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
84213e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// MCExpr.
84313e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattnerconst MCExpr *
844589c6f620e8dcf3d59af1ae0e15372c934647c82Chris LattnerTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
845589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner                                             unsigned JTI,MCContext &Ctx) const{
846beeb93e6ba48af2661eabc4872d8b159fb43e5dbChris Lattner  // The normal PIC reloc base is the label at the start of the jump table.
847589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner  return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
84813e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner}
84913e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner
8506520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohmanbool
8516520e20e4fb31f2e65e25c38b372b19d33a83df4Dan GohmanTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8526520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Assume that everything is safe in static mode.
8536520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::Static)
8546520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
8556520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
8566520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // In dynamic-no-pic mode, assume that known defined values are safe.
8576520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
8586520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      GA &&
8596520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      !GA->getGlobal()->isDeclaration() &&
860667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands      !GA->getGlobal()->isWeakForLinker())
8616520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
8626520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
8636520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Otherwise assume nothing is safe.
8646520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  return false;
8656520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman}
8666520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
867eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
868eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Optimization Methods
869eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
870eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
871368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// ShrinkDemandedConstant - Check to see if the specified operand of the
872368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// specified instruction is a constant integer.  If so, check to see if there
873368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// are any bits set in the constant that are not demanded.  If so, shrink the
874368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// constant and return true.
875475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanbool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
8767b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                                        const APInt &Demanded) {
877de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen  DebugLoc dl = Op.getDebugLoc();
87836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
879ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // FIXME: ISD::SELECT, ISD::SELECT_CC
880e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  switch (Op.getOpcode()) {
881368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  default: break;
882368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
88336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::AND:
88436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::OR: {
88536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
88636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (!C) return false;
88736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
88836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (Op.getOpcode() == ISD::XOR &&
88936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling        (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
89036ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return false;
89136ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
89236ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    // if we can expand it to have all bits set, do it
89336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (C->getAPIntValue().intersects(~Demanded)) {
894e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
89536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
89636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                DAG.getConstant(Demanded &
89736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                                C->getAPIntValue(),
89836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                                VT));
89936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return CombineTo(Op, New);
90036ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    }
90136ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
902368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
903368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
90436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  }
90536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
906368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  return false;
907368e18d56a87308045d341e85584597bfe7426e9Nate Begeman}
908368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
90997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
91097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// casts are free.  This uses isZExtFree and ZERO_EXTEND for the widening
91197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// cast, but it could be generalized for targets with other types of
91297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// implicit widening casts.
91397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohmanbool
91497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan GohmanTargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
91597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    unsigned BitWidth,
91697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    const APInt &Demanded,
91797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    DebugLoc dl) {
91897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNumOperands() == 2 &&
91997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports binary operators!");
92097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNode()->getNumValues() == 1 &&
92197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports nodes with one result!");
92297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
92397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Don't do this if the node has another user, which may require the
92497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // full value.
92597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!Op.getNode()->hasOneUse())
92697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    return false;
92797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
92897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Search for the smallest integer type with free casts to and from
92997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Op's type. For expedience, just check power-of-2 integer types.
93097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
93197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
93297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!isPowerOf2_32(SmallVTBits))
93397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    SmallVTBits = NextPowerOf2(SmallVTBits);
93497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
93523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
93697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
93797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman        TLI.isZExtFree(SmallVT, Op.getValueType())) {
93897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      // We found a type with free casts.
93997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
94097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
94197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(0)),
94297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
94397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(1)));
94497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
94597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return CombineTo(Op, Z);
94697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    }
94797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
94897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  return false;
94997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman}
95097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
951368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
952368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// DemandedMask bits of the result of Op are ever used downstream.  If we can
953368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// use this information to simplify Op, create a new simplified DAG node and
954368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// return true, returning the original and new nodes in Old and New. Otherwise,
955368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// analyze the expression and return a mask of KnownOne and KnownZero bits for
956368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// the expression (used to simplify the caller).  The KnownZero/One bits may
957368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// only be accurate for those bits in the DemandedMask.
958475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanbool TargetLowering::SimplifyDemandedBits(SDValue Op,
9597b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          const APInt &DemandedMask,
9607b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownZero,
9617b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownOne,
962368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          TargetLoweringOpt &TLO,
963368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          unsigned Depth) const {
9647b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  unsigned BitWidth = DemandedMask.getBitWidth();
96587862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman  assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
9667b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman         "Mask size mismatches value type size!");
9677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt NewMask = DemandedMask;
9686f38cb61a94b3abab70f0ee463bdcf55d86d334eDale Johannesen  DebugLoc dl = Op.getDebugLoc();
9697b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
9707b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  // Don't know anything.
9717b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  KnownZero = KnownOne = APInt(BitWidth, 0);
9723fc5b01d38ddccbe53ff0b32cc0100351d698c66Chris Lattner
973368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  // Other users may use these bits.
974ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (!Op.getNode()->hasOneUse()) {
975368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Depth != 0) {
976368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // If not at the root, Just compute the KnownZero/KnownOne bits to
977368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // simplify things downstream.
978ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman      TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
979368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return false;
980368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
981368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If this is the root being simplified, allow it to have multiple uses,
9827b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    // just set the NewMask to all bits.
9837b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    NewMask = APInt::getAllOnesValue(BitWidth);
984368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  } else if (DemandedMask == 0) {
985368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Not demanding any bits from Op.
986368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Op.getOpcode() != ISD::UNDEF)
987e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
988de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    return false;
989368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  } else if (Depth == 6) {        // Limit search depth.
990368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    return false;
991368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
992368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
9937b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
994de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  switch (Op.getOpcode()) {
995368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::Constant:
996368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // We know all of the bits for a constant!
9977b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
9987b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero = ~KnownOne & NewMask;
999ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return false;   // Don't fall through, will infinitely loop.
1000de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::AND:
100181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // If the RHS is a constant, check to see if the LHS would be zero without
100281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // using the bits from the RHS.  Below, we use knowledge about the RHS to
100381cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // simplify the LHS, here we're using information from the LHS to simplify
100481cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // the RHS.
100581cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10067b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt LHSZero, LHSOne;
10077b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
1008ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman                                LHSZero, LHSOne, Depth+1);
100981cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If the LHS already has zeros where RHSC does, this and is dead.
10107b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
101181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return TLO.CombineTo(Op, Op.getOperand(0));
101281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If any of the set bits in the RHS are known zero on the LHS, shrink
101381cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // the constant.
10147b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
101581cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return true;
101681cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    }
101781cd35586f5b675faf5391e1f597908bdda5338eChris Lattner
10187b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1019368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1020368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1021368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
10227b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
1023368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1024368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1025368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1026368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1027368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known one on one side, return the other.
1028368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'and'.
10297b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1030368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
10317b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1032368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1033368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits in the inputs are known zeros, return zero.
10347b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
1035368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1036368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
10377b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
1038368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
103997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
1040d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
104197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
104297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1043368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 bits are only known if set in both the LHS & RHS.
1044368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1045368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 are known to be clear if zero in either the LHS | RHS.
1046368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero |= KnownZero2;
1047368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1048368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::OR:
10497b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1050368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1051368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1052368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
10537b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
1054368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1055368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1056368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1057368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1058368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1059368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'or'.
10607b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
1061368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
10627b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
1063368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1064368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the potentially set bits on one side are known to be set on
1065368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // the other side, just use the 'other' side.
10667b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1067368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
10687b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1069368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1070368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
10717b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1072368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
107397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
1074d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
107597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
107697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1077368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are only known if clear in both the LHS & RHS.
1078368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1079368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in either the LHS | RHS.
1080368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne |= KnownOne2;
1081368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1082368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
10837b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1084368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1085368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1086368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
10877b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
1088368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1089368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1090368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1091368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1092368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1093368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'xor'.
10947b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero & NewMask) == NewMask)
1095368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
10967b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero2 & NewMask) == NewMask)
1097368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
109897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
1099d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
110097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
110197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
11023687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // If all of the unknown bits are known to be zero on one side or the other
11033687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // (but not both) turn this into an *inclusive* or.
11043687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
11057b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
1106de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
11073687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(0),
11083687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(1)));
1109368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1110368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are known if clear or set in both the LHS & RHS.
1111368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1112368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in only one of the LHS, RHS.
1113368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1114368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1115368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits on one side are known, and all of the set
1116368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bits on that side are also known to be set on the other side, turn this
1117368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // into an AND, as we know the bits will be cleared.
1118368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
11197b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
1120368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      if ((KnownOne & KnownOne2) == KnownOne) {
1121e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT VT = Op.getValueType();
1122475871a144eb604ddaf37503397ba0941442e5fbDan Gohman        SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
1123ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1124ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0), ANDC));
1125368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1126de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1127368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1128368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
11294fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // for XOR, we prefer to force bits to 1 if they will make a -1.
11304fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // if we can't force bits, try to shrink constant
11314fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11324fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      APInt Expanded = C->getAPIntValue() | (~NewMask);
11334fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      // if we can expand it to have all bits set, do it
11344fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      if (Expanded.isAllOnesValue()) {
11354fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        if (Expanded != C->getAPIntValue()) {
1136e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1137ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
11384fea2e982d79132715711dfcfdc46abf15239217Torok Edwin                                          TLO.DAG.getConstant(Expanded, VT));
11394fea2e982d79132715711dfcfdc46abf15239217Torok Edwin          return TLO.CombineTo(Op, New);
11404fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        }
11414fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // if it already has all the bits set, nothing to change
11424fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // but don't shrink either!
11434fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
11444fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        return true;
11454fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      }
11464fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    }
11474fea2e982d79132715711dfcfdc46abf15239217Torok Edwin
1148368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero = KnownZeroOut;
1149368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne  = KnownOneOut;
1150368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1151368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SELECT:
11527b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
1153368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1154368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
11557b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
1156368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1157368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1158368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1159368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1160368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1161368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the operands are constants, see if we can simplify them.
11627b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1163368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1164368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1165368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Only known if known in both the LHS and RHS.
1166368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1167368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1168de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1169ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SELECT_CC:
11707b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
1171ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1172ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
11737b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
1174ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne2, TLO, Depth+1))
1175ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1176ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1177ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1178ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1179ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the operands are constants, see if we can simplify them.
11807b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1181ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1182ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1183ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Only known if known in both the LHS and RHS.
1184ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownOne &= KnownOne2;
1185ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero &= KnownZero2;
1186ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1187de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SHL:
1188368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1189f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1190475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1191895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
11927b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
11937b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
11947b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
11957b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1196895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1197895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the bottom bits (which are shifted
1198895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // out) are never demanded.
1199895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SRL &&
1200895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
12017b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
1202f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1203895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SHL;
1204895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1205895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1206895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1207895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SRL;
1208895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          }
1209895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
1210475871a144eb604ddaf37503397ba0941442e5fbDan Gohman          SDValue NewSA =
12114e7e6cd13ab8cab410ad11375101878865579325Chris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1212e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1213ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1214895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1215895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1216895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      }
1217895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
12187b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (SimplifyDemandedBits(Op.getOperand(0), NewMask.lshr(ShAmt),
1219368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1220368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1221f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero <<= SA->getZExtValue();
1222f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownOne  <<= SA->getZExtValue();
12237b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // low bits known zero.
1224f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
1225368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1226368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1227368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRL:
1228368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1229e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1230f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
123183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      unsigned VTSize = VT.getSizeInBits();
1232475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1233895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
12347b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
12357b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
12367b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
12377b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1238895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1239895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the top bits (which are shifted out)
1240895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // are never demanded.
1241895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SHL &&
1242895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
12437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
1244f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1245895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SRL;
1246895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1247895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1248895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1249895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SHL;
1250895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          }
1251895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
1252475871a144eb604ddaf37503397ba0941442e5fbDan Gohman          SDValue NewSA =
12538c7d2d56bf5aa2186e2d08ed1dc37c2f392b4aaeChris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1254ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1255895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1256895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1257895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      }
1258368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1259368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // Compute the new bits that are at the top now.
12607b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
1261368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1262368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1263368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
12647b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
12657b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1266c4fa603347e798c94f54430227df8265736ef183Chris Lattner
12677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1268c4fa603347e798c94f54430227df8265736ef183Chris Lattner      KnownZero |= HighBits;  // High bits known zero.
1269368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1270368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1271368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRA:
1272e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // If this is an arithmetic shift right and only the low-bit is set, we can
1273e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // always convert this into a logical shr, even if the shift amount is
1274e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // variable.  The low bit of the shift cannot be an input sign bit unless
1275e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // the shift amount is >= the size of the datatype, which is undefined.
1276e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (DemandedMask == 1)
1277ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
1278e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman                                               Op.getOperand(0), Op.getOperand(1)));
1279e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
1280368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1281e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1282f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1283368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
12847b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
12857b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
12867b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
12877b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
12887b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt InDemandedMask = (NewMask << ShAmt);
12891b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner
12901b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // If any of the demanded bits are produced by the sign extension, we also
12911b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // demand the input sign bit.
12927b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
12937b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (HighBits.intersects(NewMask))
129487862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman        InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
12951b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner
12961b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
1297368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1298368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1299368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13007b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
13017b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1302368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
13037b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // Handle the sign bit, adjusted to where it is now in the mask.
13047b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
1305368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1306368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // If the input sign bit is known to be zero, or if none of the top bits
1307368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // are demanded, turn this into an unsigned shift right.
13087b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
1309ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1310ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0),
1311368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                 Op.getOperand(1)));
13127b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
1313368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        KnownOne |= HighBits;
1314368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1315de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1316de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1317de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SIGN_EXTEND_INREG: {
1318e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1319368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1320ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Sign extension.  Compute the demanded bits in the result that are not
1321368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // present in the input.
1322d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt NewBits =
1323d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getHighBitsSet(BitWidth,
1324d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman                            BitWidth - EVT.getScalarType().getSizeInBits()) &
1325d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      NewMask;
1326368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1327ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the extended bits are demanded, eliminate the sextinreg.
1328ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    if (NewBits == 0)
1329ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return TLO.CombineTo(Op, Op.getOperand(0));
1330ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1331d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt InSignBit = APInt::getSignBit(EVT.getScalarType().getSizeInBits());
13327b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    InSignBit.zext(BitWidth);
1333d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt InputDemandedBits =
1334d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getLowBitsSet(BitWidth,
1335d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman                           EVT.getScalarType().getSizeInBits()) &
1336d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      NewMask;
1337368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1338ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since the sign extended bits are demanded, we know that the sign
1339368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bit is demanded.
1340ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InputDemandedBits |= InSignBit;
1341368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1342368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1343368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero, KnownOne, TLO, Depth+1))
1344de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman      return true;
1345368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1346368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1347368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the sign bit of the input is known set or clear, then we know the
1348368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // top bits of the result.
1349368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1350ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the input sign bit is known zero, convert this into a zero extension.
13517b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1352ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return TLO.CombineTo(Op,
1353ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                           TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
1354ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
13557b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {    // Input sign bit known set
1356368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne |= NewBits;
1357368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1358ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {                       // Input sign bit unknown
1359368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1360368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne &= ~NewBits;
1361de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1362de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1363de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1364ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ZERO_EXTEND: {
1365d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1366d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
13677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask = NewMask;
13687b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    InMask.trunc(OperandBitWidth);
1369ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1370ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
13717b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits =
13727b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
13737b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (!NewBits.intersects(NewMask))
1374ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1375ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getValueType(),
1376ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1377ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
13787b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1379ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1380ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1381ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13827b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero.zext(BitWidth);
13837b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne.zext(BitWidth);
1384ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero |= NewBits;
1385ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1386ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1387ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SIGN_EXTEND: {
1388e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT InVT = Op.getOperand(0).getValueType();
1389d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned InBits = InVT.getScalarType().getSizeInBits();
13907b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask    = APInt::getLowBitsSet(BitWidth, InBits);
13919736028d84de3a72dd8db5f49cfaa07280154a0eDan Gohman    APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
13927b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits   = ~InMask & NewMask;
1393ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1394ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
1395ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    if (NewBits == 0)
1396ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1397ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getValueType(),
1398ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getOperand(0)));
1399ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1400ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since some of the sign extended bits are demanded, we know that the sign
1401ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // bit is demanded.
14027b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InDemandedBits = InMask & NewMask;
1403ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InDemandedBits |= InSignBit;
14047b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    InDemandedBits.trunc(InBits);
1405ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1406ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1407ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1408ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
14097b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero.zext(BitWidth);
14107b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne.zext(BitWidth);
1411ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1412ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known zero, convert this to a zero extend.
14137b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1414ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
1415ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getValueType(),
1416ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1417ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1418ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known one, the top bits match.
14197b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {
1420ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  |= NewBits;
1421ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1422ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {   // Otherwise, top bits aren't known.
1423ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  &= ~NewBits;
1424ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1425ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    }
1426ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1427ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1428ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ANY_EXTEND: {
1429d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1430d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
14317b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask = NewMask;
14327b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    InMask.trunc(OperandBitWidth);
14337b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1434ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1435ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1436ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
14377b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero.zext(BitWidth);
14387b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne.zext(BitWidth);
1439ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1440ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1441fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  case ISD::TRUNCATE: {
1442c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // Simplify the input, using demanded bit information, and compute the known
1443c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // zero/one bits live out.
1444042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman    unsigned OperandBitWidth =
1445042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
14467b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt TruncMask = NewMask;
1447042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman    TruncMask.zext(OperandBitWidth);
14487b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
1449fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1450fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner      return true;
14517b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero.trunc(BitWidth);
14527b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne.trunc(BitWidth);
1453c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner
1454c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // If the input is only used by this truncate, see if we can shrink it based
1455c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // on the known demanded bits.
1456ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (Op.getOperand(0).getNode()->hasOneUse()) {
1457475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue In = Op.getOperand(0);
1458c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      switch (In.getOpcode()) {
1459c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      default: break;
1460c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      case ISD::SRL:
1461c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // Shrink SRL by a constant if none of the high bits shifted in are
1462c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // demanded.
1463c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
1464042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman          APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1465042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman                                                 OperandBitWidth - BitWidth);
1466f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          HighBits = HighBits.lshr(ShAmt->getZExtValue());
14677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman          HighBits.trunc(BitWidth);
1468c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner
1469f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1470c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner            // None of the shifted in bits are needed.  Add a truncate of the
1471c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner            // shift input, then shift it.
1472ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
1473c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner                                                 Op.getValueType(),
1474c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner                                                 In.getOperand(0));
1475ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1476ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                     Op.getValueType(),
1477ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                     NewTrunc,
1478ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                     In.getOperand(1)));
1479c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner          }
1480c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        }
1481c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        break;
1482c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      }
1483c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    }
1484c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner
1485fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1486fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner    break;
1487fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  }
1488ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::AssertZext: {
1489e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
14907b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask = APInt::getLowBitsSet(BitWidth,
149183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                                        VT.getSizeInBits());
14927b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask & NewMask,
1493ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1494ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1495ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
14967b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero |= ~InMask & NewMask;
1497ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1498ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
14992ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner  case ISD::BIT_CONVERT:
15002ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner#if 0
15012ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    // If this is an FP->Int bitcast and if the sign bit is the only thing that
15022ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    // is demanded, turn this into a FGETSIGN.
1503e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    if (NewMask == EVT::getIntegerVTSignBit(Op.getValueType()) &&
1504825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
1505825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        !MVT::isVector(Op.getOperand(0).getValueType())) {
15062ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner      // Only do this xform if FGETSIGN is valid or if before legalize.
15072ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner      if (!TLO.AfterLegalize ||
15082ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner          isOperationLegal(ISD::FGETSIGN, Op.getValueType())) {
15092ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // Make a FGETSIGN + SHL to move the sign bit into the appropriate
15102ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // place.  We expect the SHL to be eliminated by other optimizations.
1511475871a144eb604ddaf37503397ba0941442e5fbDan Gohman        SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, Op.getValueType(),
15122ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner                                         Op.getOperand(0));
151383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands        unsigned ShVal = Op.getValueType().getSizeInBits()-1;
1514475871a144eb604ddaf37503397ba0941442e5fbDan Gohman        SDValue ShAmt = TLO.DAG.getConstant(ShVal, getShiftAmountTy());
15152ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, Op.getValueType(),
15162ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner                                                 Sign, ShAmt));
15172ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner      }
15182ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    }
15192ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner#endif
15202ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    break;
152197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::ADD:
152297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::MUL:
152397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::SUB: {
152497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // Add, Sub, and Mul don't demand any bits in positions beyond that
152597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // of the highest bit demanded of them.
152697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    APInt LoMask = APInt::getLowBitsSet(BitWidth,
152797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                        BitWidth - NewMask.countLeadingZeros());
152897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
152997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
153097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
153197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
153297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
153397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
153497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // See if the operation should be performed at a smaller bit width.
1535d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng    if (TLO.ShrinkOps && TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
153697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
153797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
153897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // FALL THROUGH
153954eed371314af403ef8307b386b3b5b46c8aa6fbDan Gohman  default:
15401482b5fc7affd691fbd8ece7808ddd4559ad20aeChris Lattner    // Just use ComputeMaskedBits to compute output bits.
15417b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
1542a6bc5a4d2138ea3cba90f5a794dd525228ec2c73Chris Lattner    break;
1543de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1544ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1545ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // If we know the value of all of the demanded bits, return this as a
1546ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // constant.
15477b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  if ((NewMask & (KnownZero|KnownOne)) == NewMask)
1548ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1549ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
1550de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  return false;
1551de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman}
1552c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner
1553368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1554368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// in Mask are known to be either zero or one and return them in the
1555368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// KnownZero/KnownOne bitsets.
1556475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1557977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman                                                    const APInt &Mask,
1558fd29e0eb060ea8b4d490860329234d2ae5f5952eDan Gohman                                                    APInt &KnownZero,
1559fd29e0eb060ea8b4d490860329234d2ae5f5952eDan Gohman                                                    APInt &KnownOne,
1560ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman                                                    const SelectionDAG &DAG,
1561368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                    unsigned Depth) const {
15621b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
15631b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
15641b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
15651b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1566c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         "Should use MaskedValueIsZero if you don't know whether Op"
1567c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         " is a target node!");
1568977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
15693a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng}
15704ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner
15715c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// ComputeNumSignBitsForTargetNode - This method can be implemented by
15725c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// targets that want to expose additional information about sign bits to the
15735c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// DAG Combiner.
1574475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanunsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
15755c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner                                                         unsigned Depth) const {
15765c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
15775c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
15785c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
15795c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
15805c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         "Should use ComputeNumSignBits if you don't know whether Op"
15815c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         " is a target node!");
15825c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  return 1;
15835c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner}
15845c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
158597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
158697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
158797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// determine which bit is set.
158897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman///
158985b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesenstatic bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
159097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // A left-shift of a constant one will have exactly one bit set, because
159197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // shifting the bit off the end is undefined.
159297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SHL)
159397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
159497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
159597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue() == 1)
159697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
159797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
159897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Similarly, a right-shift of a constant sign-bit will have exactly
159997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // one bit set.
160097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SRL)
160197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
160297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
160397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue().isSignBit())
160497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
160597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
160697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // More could be done here, though the above checks are enough
160797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // to handle some common cases.
1608e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
160997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Fall back to ComputeMaskedBits to catch other known cases.
1610e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT OpVT = Val.getValueType();
16115b870aff81da0c07413f0241087bb3722954b83dDan Gohman  unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
1612e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt Mask = APInt::getAllOnesValue(BitWidth);
1613e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt KnownZero, KnownOne;
1614e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
161585b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen  return (KnownZero.countPopulation() == BitWidth - 1) &&
161685b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen         (KnownOne.countPopulation() == 1);
1617e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman}
16185c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
1619fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1620475871a144eb604ddaf37503397ba0941442e5fbDan Gohman/// and cc. If it is unable to simplify it, return a null SDValue.
1621475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue
1622e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonTargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1623fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                              ISD::CondCode Cond, bool foldBooleans,
1624ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                              DAGCombinerInfo &DCI, DebugLoc dl) const {
1625fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  SelectionDAG &DAG = DCI.DAG;
162623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  LLVMContext &Context = *DAG.getContext();
1627fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1628fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // These setcc operations always fold.
1629fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  switch (Cond) {
1630fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  default: break;
1631fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE:
1632fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1633fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE:
1634fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE2:  return DAG.getConstant(1, VT);
1635fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
1636fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1637b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman  if (isa<ConstantSDNode>(N0.getNode())) {
1638b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Ensure that the constant occurs on the RHS, and fold constant
1639b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // comparisons.
1640b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1641b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman  }
1642b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1643ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
16446c6cd1ccb5de1ebab7089c8ce339889f33fa2577Dan Gohman    const APInt &C1 = N1C->getAPIntValue();
1645b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1646b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1647b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // equality comparison, then we're just comparing whether X itself is
1648b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // zero.
1649b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1650b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1651b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(1).getOpcode() == ISD::Constant) {
1652347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      const APInt &ShAmt
1653347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1654b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1655b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1656b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1657b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 0  -> X != 0
1658b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 1  -> X != 0
1659b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETNE;
1660b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        } else {
1661b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 0  -> X == 0
1662b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 1  -> X == 0
1663b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETEQ;
1664fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
1665b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        SDValue Zero = DAG.getConstant(0, N0.getValueType());
1666b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1667b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                            Zero, Cond);
1668fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1669b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
167089217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen
1671b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(and load, const)', the RHS is 0,
1672b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // the test is for equality or unsigned, and all 1 bits of the const are
1673b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // in the same partial word, see if we can shorten the load.
1674b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (DCI.isBeforeLegalize() &&
1675b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOpcode() == ISD::AND && C1 == 0 &&
1676b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getNode()->hasOneUse() &&
1677b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<LoadSDNode>(N0.getOperand(0)) &&
1678b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getNode()->hasOneUse() &&
1679b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<ConstantSDNode>(N0.getOperand(1))) {
1680b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
1681347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      APInt bestMask;
1682b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned bestWidth = 0, bestOffset = 0;
1683347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      if (!Lod->isVolatile() && Lod->isUnindexed()) {
1684b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned origWidth = N0.getValueType().getSizeInBits();
1685347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        unsigned maskWidth = origWidth;
1686b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
1687b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // 8 bits, but have to be careful...
1688b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1689b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          origWidth = Lod->getMemoryVT().getSizeInBits();
1690347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        const APInt &Mask =
1691347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1692b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        for (unsigned width = origWidth / 2; width>=8; width /= 2) {
1693347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          APInt newMask = APInt::getLowBitsSet(maskWidth, width);
1694b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          for (unsigned offset=0; offset<origWidth/width; offset++) {
1695b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            if ((newMask & Mask) == Mask) {
1696b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              if (!TD->isLittleEndian())
1697b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (origWidth/width - offset - 1) * (width/8);
1698b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              else
1699b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (uint64_t)offset * (width/8);
1700347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng              bestMask = Mask.lshr(offset * (width/8) * 8);
1701b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              bestWidth = width;
1702b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              break;
170389217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen            }
1704b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            newMask = newMask << width;
170589217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen          }
170689217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
1707b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
1708b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (bestWidth) {
170923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson        EVT newVT = EVT::getIntegerVT(Context, bestWidth);
1710b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (newVT.isRound()) {
1711e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT PtrType = Lod->getOperand(1).getValueType();
1712b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Ptr = Lod->getBasePtr();
1713b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (bestOffset != 0)
1714b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1715b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(bestOffset, PtrType));
1716b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1717b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
1718b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                        Lod->getSrcValue(),
1719b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                        Lod->getSrcValueOffset() + bestOffset,
17201e559443a17d1b335f697551c6263ba60d5dd827David Greene                                        false, false, NewAlign);
1721b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT,
1722b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getNode(ISD::AND, dl, newVT, NewLoad,
1723347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                      DAG.getConstant(bestMask.trunc(bestWidth),
1724347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                                      newVT)),
1725b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(0LL, newVT), Cond);
172689217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
172789217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen      }
1728b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1729d0ab34bf8feb7633b70158b571425577670b6326Bill Wendling
1730b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1731b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1732b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1733fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1734b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // If the comparison constant has bits in the upper part, the
1735b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // zero-extended value could never match.
1736b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1737b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                              C1.getBitWidth() - InSize))) {
1738fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        switch (Cond) {
1739fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGT:
1740fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGE:
1741b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETEQ: return DAG.getConstant(0, VT);
1742fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETULT:
1743b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETULE:
1744b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETNE: return DAG.getConstant(1, VT);
1745b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGT:
1746b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGE:
1747b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 is set.
1748b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNegative(), VT);
1749b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLT:
1750b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLE:
1751b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 isn't set.
1752b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNonNegative(), VT);
1753fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        default:
1754b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          break;
1755fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
1756fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1757b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1758b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, we can perform the comparison with the low bits.
1759b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      switch (Cond) {
1760b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETEQ:
1761b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETNE:
1762b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGT:
1763b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGE:
1764b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULT:
1765b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULE: {
1766e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT newVT = N0.getOperand(0).getValueType();
1767b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DCI.isBeforeLegalizeOps() ||
1768b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            (isOperationLegal(ISD::SETCC, newVT) &&
1769b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              getCondCodeAction(Cond, newVT)==Legal))
1770b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, N0.getOperand(0),
1771b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(APInt(C1).trunc(InSize), newVT),
1772b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond);
1773b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;
1774b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
1775b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      default:
1776b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;   // todo, be more careful with signed comparisons
1777b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
1778b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
17792c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng               (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1780e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1781b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
1782e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtDstTy = N0.getValueType();
1783b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1784b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1785b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // If the extended part has any inconsistent bits, it cannot ever
1786b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // compare equal.  In other words, they have to be all ones or all
1787b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // zeros.
1788b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      APInt ExtBits =
1789b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        APInt::getHighBitsSet(ExtDstTyBits, ExtDstTyBits - ExtSrcTyBits);
1790b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1791b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getConstant(Cond == ISD::SETNE, VT);
1792fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1793b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ZextOp;
1794e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT Op0Ty = N0.getOperand(0).getValueType();
1795b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (Op0Ty == ExtSrcTy) {
1796b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = N0.getOperand(0);
1797fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else {
1798b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1799b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1800b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(Imm, Op0Ty));
1801fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1802b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (!DCI.isCalledByLegalizer())
1803b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        DCI.AddToWorklist(ZextOp.getNode());
1804b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, make this a use of a zext.
1805b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, ZextOp,
1806b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1 & APInt::getLowBitsSet(
1807b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                              ExtDstTyBits,
1808b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                              ExtSrcTyBits),
1809b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                          ExtDstTy),
1810b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          Cond);
1811b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1812b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1813b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // SETCC (SETCC), [0|1], [EQ|NE]  -> SETCC
18142c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      if (N0.getOpcode() == ISD::SETCC &&
18152c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
1816347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
1817b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (TrueWhenTrue)
18182c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
1819b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // Invert the condition.
1820b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1821b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        CC = ISD::getSetCCInverse(CC,
1822b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  N0.getOperand(0).getValueType().isInteger());
1823b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
1824fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
18252c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
1826b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((N0.getOpcode() == ISD::XOR ||
18272c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng           (N0.getOpcode() == ISD::AND &&
1828b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(0).getOpcode() == ISD::XOR &&
1829b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1830b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          isa<ConstantSDNode>(N0.getOperand(1)) &&
1831b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1832b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // If this is (X^1) == 0/1, swap the RHS and eliminate the xor.  We
1833b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // can only do this if the top bits are known zero.
1834b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned BitWidth = N0.getValueSizeInBits();
1835b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DAG.MaskedValueIsZero(N0,
1836b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  APInt::getHighBitsSet(BitWidth,
1837b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                        BitWidth-1))) {
1838b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Okay, get the un-inverted input value.
1839b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Val;
1840b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (N0.getOpcode() == ISD::XOR)
1841b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = N0.getOperand(0);
1842b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          else {
1843b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            assert(N0.getOpcode() == ISD::AND &&
1844b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                    N0.getOperand(0).getOpcode() == ISD::XOR);
1845b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            // ((X^1)&1)^1 -> X & 1
1846b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1847b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(0).getOperand(0),
1848b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(1));
1849b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
18502c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
1851b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, Val, N1,
1852b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1853b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        }
18542c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      } else if (N1C->getAPIntValue() == 1 &&
18552c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                 (VT == MVT::i1 ||
18562c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                  getBooleanContents() == ZeroOrOneBooleanContent)) {
18572c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        SDValue Op0 = N0;
18582c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if (Op0.getOpcode() == ISD::TRUNCATE)
18592c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Op0 = Op0.getOperand(0);
18602c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
18612c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if ((Op0.getOpcode() == ISD::XOR) &&
18622c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(0).getOpcode() == ISD::SETCC &&
18632c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(1).getOpcode() == ISD::SETCC) {
18642c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
18652c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
18662c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
18672c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond);
18682c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        } else if (Op0.getOpcode() == ISD::AND &&
18692c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                isa<ConstantSDNode>(Op0.getOperand(1)) &&
18702c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
18712c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
18722c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          if (Op0.getValueType() != VT)
18732c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0 = DAG.getNode(ISD::AND, dl, VT,
18742c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
18752c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getConstant(1, VT));
18762c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0,
18772c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              DAG.getConstant(0, Op0.getValueType()),
18782c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
18792c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        }
1880fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1881b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1882b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1883b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    APInt MinVal, MaxVal;
1884b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1885b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (ISD::isSignedIntSetCC(Cond)) {
1886b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getSignedMinValue(OperandBitSize);
1887b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1888b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else {
1889b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getMinValue(OperandBitSize);
1890b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getMaxValue(OperandBitSize);
1891b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1892fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1893b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1894b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1895b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MinVal) return DAG.getConstant(1, VT);   // X >= MIN --> true
1896b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X >= C0 --> X > (C0-1)
1897b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0,
1898b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1-1, N1.getValueType()),
1899b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1900b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1901fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1902b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1903b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MaxVal) return DAG.getConstant(1, VT);   // X <= MAX --> true
1904b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X <= C0 --> X < (C0+1)
1905b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0,
1906b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1+1, N1.getValueType()),
1907b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1908b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1909b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1910b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1911b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X < MIN --> false
1912b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1913b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X >= MIN --> true
1914b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1915b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X > MAX --> false
1916b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1917b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X <= MAX --> true
1918b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1919b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setgt X, Min --> setne X, Min
1920b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1921b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1922b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setlt X, Max --> setne X, Max
1923b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1924b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1925b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1926b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setult X, 1, turn it into seteq X, 0
1927b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1928b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0,
1929b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(MinVal, N0.getValueType()),
1930b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
1931b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setugt X, Max-1, turn it into seteq X, Max
1932b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1933b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0,
1934b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(MaxVal, N0.getValueType()),
1935b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
1936b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1937b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have "setcc X, C0", check to see if we can shrink the immediate
1938b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // by changing cc.
1939b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1940b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETUGT X, SINTMAX  -> SETLT X, 0
1941b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETUGT &&
1942b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMaxValue(OperandBitSize))
1943b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0,
1944b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(0, N1.getValueType()),
1945b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETLT);
1946b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1947b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETULT X, SINTMIN  -> SETGT X, -1
1948b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETULT &&
1949b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMinValue(OperandBitSize)) {
1950b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ConstMinusOne =
1951b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1952b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          N1.getValueType());
1953b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1954b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
1955b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1956b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Fold bit comparisons when we can.
1957b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1958d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        (VT == N0.getValueType() ||
1959d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng         (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1960d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        N0.getOpcode() == ISD::AND)
1961b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (ConstantSDNode *AndRHS =
1962b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                  dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1963e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT ShiftTy = DCI.isBeforeLegalize() ?
1964b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          getPointerTy() : getShiftAmountTy();
1965b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
1966b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if the AND RHS is a single bit.
1967347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          if (AndRHS->getAPIntValue().isPowerOf2()) {
1968d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
1969d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                              DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1970347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                   DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
1971b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
1972347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
1973b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (X & 8) == 8  -->  (X & 8) >> 3
1974b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if C1 is a single bit.
1975b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (C1.isPowerOf2()) {
1976d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
1977d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                               DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1978d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                                      DAG.getConstant(C1.logBase2(), ShiftTy)));
1979fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
1980fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
1981b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
1982fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
1983fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1984ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (isa<ConstantFPSDNode>(N0.getNode())) {
1985fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Constant fold or commute setcc.
1986ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
1987ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (O.getNode()) return O;
1988ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
198963079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // If the RHS of an FP comparison is a constant, simplify it away in
199063079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // some cases.
199163079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (CFP->getValueAPF().isNaN()) {
199263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      // If an operand is known to be a nan, we can fold it.
199363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      switch (ISD::getUnorderedFlavor(Cond)) {
1994c23197a26f34f559ea9797de51e187087c039c42Torok Edwin      default: llvm_unreachable("Unknown flavor!");
199563079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 0:  // Known false.
199663079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(0, VT);
199763079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 1:  // Known true.
199863079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(1, VT);
19991c3e1e2ed07597111f0b1b1bb93b8080c96c526bChris Lattner      case 2:  // Undefined.
2000e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen        return DAG.getUNDEF(VT);
200163079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      }
200263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    }
200363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner
200463079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // Otherwise, we know the RHS is not a NaN.  Simplify the node to drop the
200563079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // constant if knowing that the operand is non-nan is enough.  We prefer to
200663079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
200763079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // materialize 0.0.
200863079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (Cond == ISD::SETO || Cond == ISD::SETUO)
2009ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N0, Cond);
201011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman
201111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // If the condition is not legal, see if we can find an equivalent one
201211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // which is legal.
201311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    if (!isCondCodeLegal(Cond, N0.getValueType())) {
201411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // If the comparison was an awkward floating-point == or != and one of
201511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // the comparison operands is infinity or negative infinity, convert the
201611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // condition to a less-awkward <= or >=.
201711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      if (CFP->getValueAPF().isInfinity()) {
201811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        if (CFP->getValueAPF().isNegative()) {
201911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
202011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
202111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
202211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
202311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
202411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
202511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
202611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
202711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
202811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
202911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
203011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
203111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        } else {
203211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
203311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
203411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
203511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
203611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
203711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
203811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
203911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
204011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
204111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
204211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
204311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
204411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        }
204511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      }
204611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    }
2047fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2048fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2049fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if (N0 == N1) {
2050fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // We can always fold X == X for integer setcc's.
205183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (N0.getValueType().isInteger())
2052fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2053fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    unsigned UOF = ISD::getUnorderedFlavor(Cond);
2054fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == 2)   // FP operators that are undefined on NaNs.
2055fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2056fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2057fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(UOF, VT);
2058fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
2059fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // if it is not already.
2060fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2061fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (NewCond != Cond)
2062ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2063fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2064fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2065fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
206683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      N0.getValueType().isInteger()) {
2067fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2068fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N0.getOpcode() == ISD::XOR) {
2069fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Y) == (X+Z) -->  Y == Z
2070fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOpcode() == N1.getOpcode()) {
2071fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(0) == N1.getOperand(0))
2072ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2073fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(1) == N1.getOperand(1))
2074ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2075fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2076fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // If X op Y == Y op X, try other combinations.
2077fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(0) == N1.getOperand(1))
2078ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2079ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2080fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(1) == N1.getOperand(0))
2081ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2082ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2083fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2084fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2085fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2086fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2087fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2088fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X+C1) == C2 --> X == C2-C1
2089ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2090ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return DAG.getSetCC(dl, VT, N0.getOperand(0),
2091f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                DAG.getConstant(RHSC->getAPIntValue()-
2092f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                                LHSR->getAPIntValue(),
2093fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                N0.getValueType()), Cond);
2094fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2095fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2096fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2097fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOpcode() == ISD::XOR)
2098fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // If we know that all of the inverted bits are zero, don't bother
2099fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // performing the inversion.
21002e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
21012e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman              return
2102ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                DAG.getSetCC(dl, VT, N0.getOperand(0),
21032e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             DAG.getConstant(LHSR->getAPIntValue() ^
21042e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                               RHSC->getAPIntValue(),
21052e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             N0.getValueType()),
21062e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             Cond);
2107fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2108fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2109fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        // Turn (C1-X) == C2 --> X == C1-C2
2110fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2111ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
21122e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            return
2113ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen              DAG.getSetCC(dl, VT, N0.getOperand(1),
21142e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           DAG.getConstant(SUBC->getAPIntValue() -
21152e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             RHSC->getAPIntValue(),
21162e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                           N0.getValueType()),
21172e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           Cond);
2118fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2119fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2120fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2121fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2122fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Z) == X -->  Z == 0
2123fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(0) == N1)
2124ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N0.getOperand(1),
2125fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N0.getValueType()), Cond);
2126fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(1) == N1) {
2127fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode()))
2128ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0),
2129fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N0.getValueType()), Cond);
2130ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        else if (N0.getNode()->hasOneUse()) {
2131fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2132fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // (Z-X) == X  --> Z == X<<1
2133ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
2134fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                     N1,
2135fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                     DAG.getConstant(1, getShiftAmountTy()));
2136fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2137ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2138ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2139fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2140fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2141fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2142fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2143fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2144fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N1.getOpcode() == ISD::XOR) {
2145fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify  X == (X+Z) -->  Z == 0
2146fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N1.getOperand(0) == N0) {
2147ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N1.getOperand(1),
2148fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N1.getValueType()), Cond);
2149fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else if (N1.getOperand(1) == N0) {
2150fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N1.getOpcode())) {
2151ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1.getOperand(0),
2152fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N1.getValueType()), Cond);
2153ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        } else if (N1.getNode()->hasOneUse()) {
2154fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2155fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // X == (Z-X)  --> X<<1 == Z
2156ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
2157fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                     DAG.getConstant(1, getShiftAmountTy()));
2158fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2159ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2160ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2161fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2162fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2163fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2164e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
21652c65c3dfe6589a630d1bdde085aec1b9cdc43ea8Dan Gohman    // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
216685b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // Note that where y is variable and is known to have at most
216785b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // one bit set (for example, if it is z&1) we cannot do this;
216885b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // the expressions are not equivalent when y==0.
2169e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N0.getOpcode() == ISD::AND)
2170e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
217185b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N1, DAG)) {
2172e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2173e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N1.getValueType());
2174ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0, Zero, Cond);
2175e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2176e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2177e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N1.getOpcode() == ISD::AND)
2178e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
217985b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N0, DAG)) {
2180e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2181e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N0.getValueType());
2182ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1, Zero, Cond);
2183e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2184e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2185fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2186fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2187fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Fold away ALL boolean setcc's.
2188475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Temp;
2189825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (N0.getValueType() == MVT::i1 && foldBooleans) {
2190fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    switch (Cond) {
2191c23197a26f34f559ea9797de51e187087c039c42Torok Edwin    default: llvm_unreachable("Unknown integer setcc!");
21924c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETEQ:  // X == Y  -> ~(X^Y)
2193825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2194825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNOT(dl, Temp, MVT::i1);
2195fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2196ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2197fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2198fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    case ISD::SETNE:  // X != Y   -->  (X^Y)
2199825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2200fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
22014c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGT:  // X >s Y   -->  X == 0 & Y == 1  -->  ~X & Y
22024c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULT: // X <u Y   -->  X == 0 & Y == 1  -->  ~X & Y
2203825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2204825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2205fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2206ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2207fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
22084c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLT:  // X <s Y   --> X == 1 & Y == 0  -->  ~Y & X
22094c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGT: // X >u Y   --> X == 1 & Y == 0  -->  ~Y & X
2210825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2211825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2212fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2213ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2214fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
22154c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULE: // X <=u Y  --> X == 0 | Y == 1  -->  ~X | Y
22164c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGE:  // X >=s Y  --> X == 0 | Y == 1  -->  ~X | Y
2217825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2218825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2219fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2220ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2221fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
22224c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGE: // X >=u Y  --> X == 1 | Y == 0  -->  ~Y | X
22234c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLE:  // X <=s Y  --> X == 1 | Y == 0  -->  ~Y | X
2224825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2225825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2226fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2227fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2228825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (VT != MVT::i1) {
2229fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2230ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(N0.getNode());
2231fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // FIXME: If running after legalize, we probably can't do this.
2232ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2233fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2234fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    return N0;
2235fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2236fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2237fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Could not fold it.
2238475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
2239fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng}
2240fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2241ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2242ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// node is a GlobalAddress + offset.
2243ad4196b44ae714a6b95e238d9d96303df74b0429Evan Chengbool TargetLowering::isGAPlusOffset(SDNode *N, GlobalValue* &GA,
2244ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng                                    int64_t &Offset) const {
2245ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (isa<GlobalAddressSDNode>(N)) {
22469ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
22479ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GA = GASD->getGlobal();
22489ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    Offset += GASD->getOffset();
2249ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    return true;
2250ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
2251ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2252ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (N->getOpcode() == ISD::ADD) {
2253475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N1 = N->getOperand(0);
2254475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N2 = N->getOperand(1);
2255ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2256ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2257ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
22587810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2259ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2260ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2261ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2262ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2263ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
22647810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2265ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2266ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2267ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    }
2268ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
2269ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  return false;
2270ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng}
2271ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2272ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2273475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::
227400ffed0468ad406062b7c08c2ff46d79d2d1be4dChris LattnerPerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
227500ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner  // Default implementation: no optimization.
2276475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
227700ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner}
227800ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner
2279eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2280eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Inline Assembler Implementation Methods
2281eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2282eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
22834376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
2284eb8146b5ee4b9b66d6294f62a5ed556e332018abChris LattnerTargetLowering::ConstraintType
22854234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris LattnerTargetLowering::getConstraintType(const std::string &Constraint) const {
2286eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  // FIXME: lots more standard ones to handle.
22874234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  if (Constraint.size() == 1) {
22884234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    switch (Constraint[0]) {
22894234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    default: break;
22904234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'r': return C_RegisterClass;
22914234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'm':    // memory
22924234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'o':    // offsetable
22934234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'V':    // not offsetable
22944234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Memory;
22954234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'i':    // Simple Integer or Relocatable Constant
22964234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'n':    // Simple Integer
22974234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 's':    // Relocatable Constant
2298c13dd1cf4c0d83ac3ed2a6b0c36fab72e9d6c6e5Chris Lattner    case 'X':    // Allow ANY value.
22994234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'I':    // Target registers.
23004234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'J':
23014234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'K':
23024234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'L':
23034234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'M':
23044234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'N':
23054234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'O':
23064234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'P':
23074234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Other;
23084234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    }
2309eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
2310065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner
2311065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner  if (Constraint.size() > 1 && Constraint[0] == '{' &&
2312065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner      Constraint[Constraint.size()-1] == '}')
2313065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner    return C_Register;
23144234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  return C_Unknown;
2315eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2316eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
2317ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// LowerXConstraint - try to replace an X constraint, which matches anything,
2318ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// with another that has more specific requirements based on the type of the
2319ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// corresponding operand.
2320e50ed30282bb5b4a9ed952580523f2dda16215acOwen Andersonconst char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
232183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isInteger())
23225e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "r";
232383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isFloatingPoint())
23245e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "f";      // works for many targets
23255e764233f398b6929b67701672a5e78fec20ce2eChris Lattner  return 0;
2326ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen}
2327ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen
232848884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
232948884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// vector.  If it is invalid, don't add anything to Ops.
2330475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
233148884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner                                                  char ConstraintLetter,
2332da43bcf624acb56a3d77bb5ae9a02728af032613Evan Cheng                                                  bool hasMemory,
2333475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                  std::vector<SDValue> &Ops,
23345e764233f398b6929b67701672a5e78fec20ce2eChris Lattner                                                  SelectionDAG &DAG) const {
2335eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  switch (ConstraintLetter) {
23369ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner  default: break;
2337eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 'X':     // Allows any operand; labels (basic block) use this.
2338eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    if (Op.getOpcode() == ISD::BasicBlock) {
2339eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      Ops.push_back(Op);
2340eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      return;
2341eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    }
2342eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    // fall through
2343eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'i':    // Simple Integer or Relocatable Constant
2344eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'n':    // Simple Integer
2345eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 's': {  // Relocatable Constant
234675c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // These operands are interested in values of the form (GV+C), where C may
234775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // be folded in as an offset of GV, or it may be explicitly added.  Also, it
234875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // is possible and fine if either GV or C are missing.
234975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
235075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
235175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner
235275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we have "(add GV, C)", pull out GV/C
235375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (Op.getOpcode() == ISD::ADD) {
235475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
235575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
235675c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0) {
235775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
235875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
235975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
236075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0)
236175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = 0, GA = 0;
236275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
236375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner
236475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we find a valid operand, map to the TargetXXX version so that the
236575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // value itself doesn't get selected.
236675c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (GA) {   // Either &GV   or   &GV+C
236775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (ConstraintLetter != 'n') {
236875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        int64_t Offs = GA->getOffset();
2369f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman        if (C) Offs += C->getZExtValue();
237048884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
237148884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner                                                 Op.getValueType(), Offs));
237248884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
237375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
237475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
237575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (C) {   // just C, no GV.
23769ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner      // Simple constants are not allowed for 's'.
237748884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      if (ConstraintLetter != 's') {
237878e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // gcc prints these as sign extended.  Sign extend value to 64 bits
237978e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // now; without this it would get ZExt'd later in
238078e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // ScheduleDAGSDNodes::EmitNode, which is very generic.
238178e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
2382825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                                            MVT::i64));
238348884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
238448884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      }
23859ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    }
23869ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    break;
2387eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
238875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner  }
2389eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2390eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
23914ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattnerstd::vector<unsigned> TargetLowering::
23921efa40f6a4b561cf8f80fe018684236010645cd0Chris LattnergetRegClassForInlineAsmConstraint(const std::string &Constraint,
2393e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                                  EVT VT) const {
23941efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  return std::vector<unsigned>();
23951efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner}
23961efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
23971efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
23981efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattnerstd::pair<unsigned, const TargetRegisterClass*> TargetLowering::
23994217ca8dc175f7268a4335c8406dedd901e8e631Chris LattnergetRegForInlineAsmConstraint(const std::string &Constraint,
2400e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                             EVT VT) const {
24011efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  if (Constraint[0] != '{')
24021efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
2403a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2404a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner
2405a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  // Remove the braces from around the name.
240605872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer  StringRef RegName(Constraint.data()+1, Constraint.size()-2);
24071efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
24081efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  // Figure out which register class contains this reg.
24096f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  const TargetRegisterInfo *RI = TM.getRegisterInfo();
24106f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
24111efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner       E = RI->regclass_end(); RCI != E; ++RCI) {
24121efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    const TargetRegisterClass *RC = *RCI;
2413b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner
2414f451cb870efcf9e0302d25ed05f4cac6bb494e42Dan Gohman    // If none of the value types for this register class are valid, we
2415b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    // can't use it.  For example, 64-bit reg classes on 32-bit targets.
2416b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    bool isLegal = false;
2417b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
2418b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner         I != E; ++I) {
2419b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner      if (isTypeLegal(*I)) {
2420b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner        isLegal = true;
2421b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner        break;
2422b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner      }
2423b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    }
2424b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner
2425b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    if (!isLegal) continue;
2426b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner
24271efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
24281efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner         I != E; ++I) {
242905872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer      if (RegName.equals_lower(RI->getName(*I)))
24301efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        return std::make_pair(*I, RC);
24311efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    }
24324ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner  }
2433a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner
24341efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
24354ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner}
243630b37b5f29991874648d839d018aa2921b39355fEvan Cheng
243730b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
24384376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner// Constraint Selection.
24394376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
24406bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// isMatchingInputConstraint - Return true of this is an input operand that is
24416bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// a matching constraint like "4".
24426bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattnerbool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
244358f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
244458f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return isdigit(ConstraintCode[0]);
244558f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
244658f15c482a7129c78ca809792b46befa20ea337dChris Lattner
244758f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// getMatchedOperand - If this is an input matching constraint, this method
244858f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// returns the output operand it matches.
244958f15c482a7129c78ca809792b46befa20ea337dChris Lattnerunsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
245058f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
245158f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return atoi(ConstraintCode.c_str());
245258f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
245358f15c482a7129c78ca809792b46befa20ea337dChris Lattner
245458f15c482a7129c78ca809792b46befa20ea337dChris Lattner
24554376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// getConstraintGenerality - Return an integer indicating how general CT
24564376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is.
24574376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
24584376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  switch (CT) {
2459c23197a26f34f559ea9797de51e187087c039c42Torok Edwin  default: llvm_unreachable("Unknown constraint type!");
24604376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Other:
24614376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Unknown:
24624376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 0;
24634376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Register:
24644376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 1;
24654376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_RegisterClass:
24664376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 2;
24674376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Memory:
24684376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 3;
24694376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
24704376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
24714376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
24724376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ChooseConstraint - If there are multiple different constraints that we
24734376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
247424e1a9d3115a5bf4e1c21092ede0f6dfc0a810adChris Lattner/// This is somewhat tricky: constraints fall into four classes:
24754376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Other         -> immediates and magic values
24764376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Register      -> one specific register
24774376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    RegisterClass -> a group of regs
24784376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Memory        -> memory
24794376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// Ideally, we would pick the most specific constraint possible: if we have
24804376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// something that fits into a register, we would pick it.  The problem here
24814376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is that if we have something that could either be in a register or in
24824376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// memory that use of the register could cause selection of *other*
24834376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// operands to fail: they might only succeed if we pick memory.  Because of
24844376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// this the heuristic we use is:
24854376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
24864376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  1) If there is an 'other' constraint, and if the operand is valid for
24874376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     that constraint, use it.  This makes us take advantage of 'i'
24884376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     constraints when available.
24894376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  2) Otherwise, pick the most general constraint present.  This prefers
24904376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     'm' over 'r', for example.
24914376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
24924376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
2493da43bcf624acb56a3d77bb5ae9a02728af032613Evan Cheng                             bool hasMemory,  const TargetLowering &TLI,
2494475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                             SDValue Op, SelectionDAG *DAG) {
24954376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
24964376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  unsigned BestIdx = 0;
24974376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
24984376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  int BestGenerality = -1;
24994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25004376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Loop over the options, keeping track of the most general one.
25014376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
25024376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    TargetLowering::ConstraintType CType =
25034376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      TLI.getConstraintType(OpInfo.Codes[i]);
25044376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25055a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // If this is an 'other' constraint, see if the operand is valid for it.
25065a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // For example, on X86 we might have an 'rI' constraint.  If the operand
25075a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // is an integer in the range [0..31] we want to use I (saving a load
25085a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // of a register), otherwise we must use 'r'.
2509ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (CType == TargetLowering::C_Other && Op.getNode()) {
25105a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      assert(OpInfo.Codes[i].size() == 1 &&
25115a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner             "Unhandled multi-letter 'other' constraint");
2512475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      std::vector<SDValue> ResultOps;
2513da43bcf624acb56a3d77bb5ae9a02728af032613Evan Cheng      TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i][0], hasMemory,
25145a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                       ResultOps, *DAG);
25155a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      if (!ResultOps.empty()) {
25165a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestType = CType;
25175a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestIdx = i;
25185a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        break;
25195a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      }
25205a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    }
25215a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner
25224376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // This constraint letter is more general than the previous one, use it.
25234376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    int Generality = getConstraintGenerality(CType);
25244376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (Generality > BestGenerality) {
25254376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestType = CType;
25264376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestIdx = i;
25274376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestGenerality = Generality;
25284376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
25294376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
25304376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25314376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
25324376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintType = BestType;
25334376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
25344376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25354376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ComputeConstraintToUse - Determines the constraint code and constraint
25364376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// type to use for the specific AsmOperandInfo, setting
25374376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
25385a09690446a36f94f990db7d18d9b9ac8587888aChris Lattnervoid TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
2539475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue Op,
2540da43bcf624acb56a3d77bb5ae9a02728af032613Evan Cheng                                            bool hasMemory,
25415a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                            SelectionDAG *DAG) const {
25424376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
25434376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25444376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Single-letter constraints ('r') are very common.
25454376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.Codes.size() == 1) {
25464376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintCode = OpInfo.Codes[0];
25474376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
25484376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  } else {
2549da43bcf624acb56a3d77bb5ae9a02728af032613Evan Cheng    ChooseConstraint(OpInfo, hasMemory, *this, Op, DAG);
25504376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
25514376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25524376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // 'X' matches anything.
25534376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
25544376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Labels and constants are handled elsewhere ('X' is the only thing
25558ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    // that matches labels).  For Functions, the type here is the type of
25565339c551b508d8231672b417d9df1629e42af89bDale Johannesen    // the result, which is not what we want to look at; leave them alone.
25575339c551b508d8231672b417d9df1629e42af89bDale Johannesen    Value *v = OpInfo.CallOperandVal;
25588ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
25598ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen      OpInfo.CallOperandVal = v;
25604376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      return;
25618ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    }
25624376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25634376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Otherwise, try to resolve it to something we know about by looking at
25644376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // the actual operand type.
25654376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
25664376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintCode = Repl;
25674376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
25684376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
25694376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
25704376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
25714376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
25724376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner//===----------------------------------------------------------------------===//
257330b37b5f29991874648d839d018aa2921b39355fEvan Cheng//  Loop Strength Reduction hooks
257430b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
257530b37b5f29991874648d839d018aa2921b39355fEvan Cheng
25761436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// isLegalAddressingMode - Return true if the addressing mode represented
25771436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// by AM is legal for this target, for a load/store of the specified type.
25781436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattnerbool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
25791436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner                                           const Type *Ty) const {
25801436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // The default implementation of this implements a conservative RISCy, r+r and
25811436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // r+i addr mode.
25821436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
25831436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // Allows a sign-extended 16-bit immediate field.
25841436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
25851436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
25861436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
25871436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // No global is ever allowed as a base.
25881436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseGV)
25891436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
25901436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
25911436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // Only support r+r,
25921436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  switch (AM.Scale) {
25931436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 0:  // "r+i" or just "i", depending on HasBaseReg.
25941436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
25951436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 1:
25961436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
25971436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
25981436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Otherwise we have r+r or r+i.
25991436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
26001436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 2:
26011436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
26021436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
26031436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Allow 2*r as r+r.
26041436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
26051436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  }
26061436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
26071436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  return true;
26081436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner}
26091436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
2610dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2611dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
2612dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
2613dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2614475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
2615475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                  std::vector<SDNode*>* Created) const {
2616e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
2617ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl= N->getDebugLoc();
2618dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
2619dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
2620fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should be more aggressive here.
2621fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (!isTypeLegal(VT))
2622fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman    return SDValue();
2623dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
2624fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
26254e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt::ms magics = d.magic();
2626dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
2627dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
2628fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should support doing a MUL in a wider type
2629475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Q;
2630f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  if (isOperationLegalOrCustom(ISD::MULHS, VT))
2631ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
2632525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                    DAG.getConstant(magics.m, VT));
2633f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
2634ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
2635525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                              N->getOperand(0),
2636ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif                              DAG.getConstant(magics.m, VT)).getNode(), 1);
2637525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
2638475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhs or equvialent
2639dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d > 0 and m < 0, add the numerator
2640fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (d.isStrictlyPositive() && magics.m.isNegative()) {
2641ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
2642dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2643ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
2644dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
2645dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d < 0 and m > 0, subtract the numerator.
2646fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (d.isNegative() && magics.m.isStrictlyPositive()) {
2647ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
2648dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2649ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
2650dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
2651dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Shift right algebraic if shift value is nonzero
2652dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.s > 0) {
2653ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::SRA, dl, VT, Q,
2654dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth                    DAG.getConstant(magics.s, getShiftAmountTy()));
2655dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2656ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
2657dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
2658dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Extract the sign bit and add it to the quotient
2659475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue T =
2660ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
2661dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth                                                 getShiftAmountTy()));
2662dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
2663ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(T.getNode());
2664ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  return DAG.getNode(ISD::ADD, dl, VT, Q, T);
2665dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
2666dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
2667dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2668dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
2669dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
2670dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2671475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
2672475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                  std::vector<SDNode*>* Created) const {
2673e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
2674ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl = N->getDebugLoc();
2675201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
2676dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
2677201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should be more aggressive here.
2678201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  if (!isTypeLegal(VT))
2679201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman    return SDValue();
2680201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
2681201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should use a narrower constant when the upper
2682201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // bits are known to be zero.
2683201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  ConstantSDNode *N1C = cast<ConstantSDNode>(N->getOperand(1));
26844e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt::mu magics = N1C->getAPIntValue().magicu();
2685201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
2686dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
2687201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should support doing a MUL in a wider type
2688475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Q;
2689f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  if (isOperationLegalOrCustom(ISD::MULHU, VT))
2690ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::MULHU, dl, VT, N->getOperand(0),
2691525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                    DAG.getConstant(magics.m, VT));
2692f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
2693ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT),
2694525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                              N->getOperand(0),
2695ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif                              DAG.getConstant(magics.m, VT)).getNode(), 1);
2696525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
2697475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhu or equvialent
2698dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
2699ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(Q.getNode());
2700dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
2701dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.a == 0) {
2702201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman    assert(magics.s < N1C->getAPIntValue().getBitWidth() &&
2703201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman           "We shouldn't generate an undefined shift!");
2704ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    return DAG.getNode(ISD::SRL, dl, VT, Q,
2705dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth                       DAG.getConstant(magics.s, getShiftAmountTy()));
2706dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  } else {
2707ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
2708dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2709ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
2710ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
2711dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth                      DAG.getConstant(1, getShiftAmountTy()));
2712dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2713ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
2714ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
2715dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
2716ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
2717ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    return DAG.getNode(ISD::SRL, dl, VT, NPQ,
2718dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth                       DAG.getConstant(magics.s-1, getShiftAmountTy()));
2719dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
2720dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
2721