TargetLowering.cpp revision 8fb06b3e8f7fc92e472e17fecf5ee3ba44fbb6ab
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"
21707e0184233f27e0e9f9aee0309f2daab8cfe7f8Dan Gohman#include "llvm/GlobalVariable.h"
22dc879296090a738c66968f5eec77db65d7e03623Chris Lattner#include "llvm/DerivedTypes.h"
2384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman#include "llvm/CodeGen/Analysis.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"
29b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem#include "llvm/Support/CommandLine.h"
30c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
31c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner#include "llvm/Support/MathExtras.h"
32476b242fe7a61e5f9ac6214b0bc5c680d24f152eNick Lewycky#include <cctype>
33310968cbbb564c4141d4bd418a746e8103560222Chris Lattnerusing namespace llvm;
34310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
35b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem/// We are in the process of implementing a new TypeLegalization action
36b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem/// - the promotion of vector elements. This feature is disabled by default
37b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem/// and only enabled using this flag.
38b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotemstatic cl::opt<bool>
398fb06b3e8f7fc92e472e17fecf5ee3ba44fbb6abNadav RotemAllowPromoteIntElem("promote-elements", cl::Hidden, cl::init(true),
40b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem  cl::desc("Allow promotion of integer vector element types"));
41b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem
429a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindolanamespace llvm {
439a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael EspindolaTLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
449a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isLocal = GV->hasLocalLinkage();
459a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isDeclaration = GV->isDeclaration();
469a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  // FIXME: what should we do for protected and internal visibility?
479a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  // For variables, is internal different from hidden?
489a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  bool isHidden = GV->hasHiddenVisibility();
499a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola
509a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  if (reloc == Reloc::PIC_) {
519a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    if (isLocal || isHidden)
529a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::LocalDynamic;
539a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    else
549a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::GeneralDynamic;
559a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  } else {
569a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    if (!isDeclaration || isHidden)
579a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::LocalExec;
589a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola    else
599a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola      return TLSModel::InitialExec;
609a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola  }
619a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola}
629a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola}
639a58023c6ce1e77447a21d07a3de4c1ac98a3d24Rafael Espindola
6456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng/// InitLibcallNames - Set default libcall names.
6556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng///
6679cca507fe51aa8af7a9006b380977c61b6f3feaEvan Chengstatic void InitLibcallNames(const char **Names) {
67c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SHL_I16] = "__ashlhi3";
6856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SHL_I32] = "__ashlsi3";
6956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SHL_I64] = "__ashldi3";
70dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SHL_I128] = "__ashlti3";
71c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SRL_I16] = "__lshrhi3";
7256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRL_I32] = "__lshrsi3";
7356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRL_I64] = "__lshrdi3";
74dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SRL_I128] = "__lshrti3";
75c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::SRA_I16] = "__ashrhi3";
7656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRA_I32] = "__ashrsi3";
7756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SRA_I64] = "__ashrdi3";
78dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  Names[RTLIB::SRA_I128] = "__ashrti3";
798983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::MUL_I8] = "__mulqi3";
80c31642f7af64bfaed36064b52a5cb2366fd01064Anton Korobeynikov  Names[RTLIB::MUL_I16] = "__mulhi3";
8156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_I32] = "__mulsi3";
8256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_I64] = "__muldi3";
835ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::MUL_I128] = "__multi3";
84362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher  Names[RTLIB::MULO_I32] = "__mulosi4";
85362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher  Names[RTLIB::MULO_I64] = "__mulodi4";
86362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher  Names[RTLIB::MULO_I128] = "__muloti4";
878983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::SDIV_I8] = "__divqi3";
88813090cf891325c715b9f6fb1546e6ce67fa8c8bAnton Korobeynikov  Names[RTLIB::SDIV_I16] = "__divhi3";
8956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SDIV_I32] = "__divsi3";
9056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SDIV_I64] = "__divdi3";
915ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::SDIV_I128] = "__divti3";
928983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::UDIV_I8] = "__udivqi3";
93fb3f84fe16219467cef4ed181530e846177cb4dfAnton Korobeynikov  Names[RTLIB::UDIV_I16] = "__udivhi3";
9456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UDIV_I32] = "__udivsi3";
9556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UDIV_I64] = "__udivdi3";
965ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::UDIV_I128] = "__udivti3";
978983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::SREM_I8] = "__modqi3";
98813090cf891325c715b9f6fb1546e6ce67fa8c8bAnton Korobeynikov  Names[RTLIB::SREM_I16] = "__modhi3";
9956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SREM_I32] = "__modsi3";
10056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SREM_I64] = "__moddi3";
1015ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::SREM_I128] = "__modti3";
1028983da729aa1ca99a11a3b98ae6280dfcdbadb39Anton Korobeynikov  Names[RTLIB::UREM_I8] = "__umodqi3";
1039fe9c8ec7d7d1e260927cb66e91d5e69db8a6febAnton Korobeynikov  Names[RTLIB::UREM_I16] = "__umodhi3";
10456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UREM_I32] = "__umodsi3";
10556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UREM_I64] = "__umoddi3";
1065ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  Names[RTLIB::UREM_I128] = "__umodti3";
1078e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng
1088e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  // These are generally not available.
1098e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::SDIVREM_I8] = 0;
1108e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::SDIVREM_I16] = 0;
1118e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::SDIVREM_I32] = 0;
1128e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::SDIVREM_I64] = 0;
1138e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::SDIVREM_I128] = 0;
1148e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::UDIVREM_I8] = 0;
1158e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::UDIVREM_I16] = 0;
1168e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::UDIVREM_I32] = 0;
1178e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::UDIVREM_I64] = 0;
1188e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng  Names[RTLIB::UDIVREM_I128] = 0;
1198e23e815ad1136721acdfcce76975a37c8a2c036Evan Cheng
12056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::NEG_I32] = "__negsi2";
12156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::NEG_I64] = "__negdi2";
12256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::ADD_F32] = "__addsf3";
12356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::ADD_F64] = "__adddf3";
124007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::ADD_F80] = "__addxf3";
125161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
12656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SUB_F32] = "__subsf3";
12756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SUB_F64] = "__subdf3";
128007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SUB_F80] = "__subxf3";
129161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
13056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_F32] = "__mulsf3";
13156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::MUL_F64] = "__muldf3";
132007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::MUL_F80] = "__mulxf3";
133161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
13456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::DIV_F32] = "__divsf3";
13556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::DIV_F64] = "__divdf3";
136007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::DIV_F80] = "__divxf3";
137161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
13856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::REM_F32] = "fmodf";
13956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::REM_F64] = "fmod";
140007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::REM_F80] = "fmodl";
141161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::REM_PPCF128] = "fmodl";
14233390848a7eca75301d04a59b89b516d83e19ee0Cameron Zwarich  Names[RTLIB::FMA_F32] = "fmaf";
14333390848a7eca75301d04a59b89b516d83e19ee0Cameron Zwarich  Names[RTLIB::FMA_F64] = "fma";
14433390848a7eca75301d04a59b89b516d83e19ee0Cameron Zwarich  Names[RTLIB::FMA_F80] = "fmal";
14533390848a7eca75301d04a59b89b516d83e19ee0Cameron Zwarich  Names[RTLIB::FMA_PPCF128] = "fmal";
14656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::POWI_F32] = "__powisf2";
14756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::POWI_F64] = "__powidf2";
148161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::POWI_F80] = "__powixf2";
149161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::POWI_PPCF128] = "__powitf2";
15056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SQRT_F32] = "sqrtf";
15156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SQRT_F64] = "sqrt";
152161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SQRT_F80] = "sqrtl";
153161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SQRT_PPCF128] = "sqrtl";
1547794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F32] = "logf";
1557794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F64] = "log";
1567794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_F80] = "logl";
1577794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG_PPCF128] = "logl";
1587794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F32] = "log2f";
1597794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F64] = "log2";
1607794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_F80] = "log2l";
1617794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG2_PPCF128] = "log2l";
1627794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F32] = "log10f";
1637794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F64] = "log10";
1647794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_F80] = "log10l";
1657794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::LOG10_PPCF128] = "log10l";
1667794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F32] = "expf";
1677794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F64] = "exp";
1687794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_F80] = "expl";
1697794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP_PPCF128] = "expl";
1707794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F32] = "exp2f";
1717794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F64] = "exp2";
1727794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_F80] = "exp2l";
1737794f2a3a7778bdbc9bdd861db1fe914450e0470Dale Johannesen  Names[RTLIB::EXP2_PPCF128] = "exp2l";
17456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SIN_F32] = "sinf";
17556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SIN_F64] = "sin";
176007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SIN_F80] = "sinl";
177007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::SIN_PPCF128] = "sinl";
17856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::COS_F32] = "cosf";
17956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::COS_F64] = "cos";
180007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::COS_F80] = "cosl";
181007f9847c44ddbe7fd04cba362b4ec0f0f40964bDuncan Sands  Names[RTLIB::COS_PPCF128] = "cosl";
182e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F32] = "powf";
183e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F64] = "pow";
184e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_F80] = "powl";
185e54be104180d81b61c5fcc29ccb7960f9b78476eDan Gohman  Names[RTLIB::POW_PPCF128] = "powl";
1862bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F32] = "ceilf";
1872bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F64] = "ceil";
1882bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_F80] = "ceill";
1892bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::CEIL_PPCF128] = "ceill";
1902bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F32] = "truncf";
1912bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F64] = "trunc";
1922bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_F80] = "truncl";
1932bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::TRUNC_PPCF128] = "truncl";
1942bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F32] = "rintf";
1952bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F64] = "rint";
1962bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_F80] = "rintl";
1972bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::RINT_PPCF128] = "rintl";
1982bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
1992bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F64] = "nearbyint";
2002bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
2012bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
2022bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F32] = "floorf";
2032bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F64] = "floor";
2042bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_F80] = "floorl";
2052bb1e3eede14dd8a965506465e2876fb1ae765c2Dan Gohman  Names[RTLIB::FLOOR_PPCF128] = "floorl";
206d2c817eff1d158398fb0a620fa762bf95207922fDuncan Sands  Names[RTLIB::COPYSIGN_F32] = "copysignf";
207d2c817eff1d158398fb0a620fa762bf95207922fDuncan Sands  Names[RTLIB::COPYSIGN_F64] = "copysign";
208d2c817eff1d158398fb0a620fa762bf95207922fDuncan Sands  Names[RTLIB::COPYSIGN_F80] = "copysignl";
209d2c817eff1d158398fb0a620fa762bf95207922fDuncan Sands  Names[RTLIB::COPYSIGN_PPCF128] = "copysignl";
21056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
211927411b7ce0b7852fe4f392d8cd4faaa3881f852Anton Korobeynikov  Names[RTLIB::FPEXT_F16_F32] = "__gnu_h2f_ieee";
212927411b7ce0b7852fe4f392d8cd4faaa3881f852Anton Korobeynikov  Names[RTLIB::FPROUND_F32_F16] = "__gnu_f2h_ieee";
21356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
214e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
215e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
216e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
217e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes  Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
218de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfqi";
219de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfhi";
22056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
22156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
222a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
223de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOSINT_F64_I8] = "__fixdfqi";
224de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOSINT_F64_I16] = "__fixdfhi";
22556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
22656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
227a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
228be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands  Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
229161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
230a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
231041cde26eaf4ef6171ff1a44aeedd08d7a1cba6cDuncan Sands  Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
232161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
233a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
234de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfqi";
235de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfhi";
23656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
23756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
238a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
239de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOUINT_F64_I8] = "__fixunsdfqi";
240de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov  Names[RTLIB::FPTOUINT_F64_I16] = "__fixunsdfhi";
24156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
24256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
243a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
244161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
245161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
246a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
247041cde26eaf4ef6171ff1a44aeedd08d7a1cba6cDuncan Sands  Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
248161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
249a2e9485e34f1348526ed104dbdc194673e291077Dan Gohman  Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
25056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
25156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
2529bed0f58eb7f5e9e22d062f3880a5fabfdd0dfddDuncan Sands  Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
2539bed0f58eb7f5e9e22d062f3880a5fabfdd0dfddDuncan Sands  Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
25456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
25556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
256161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
257161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
258d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
259d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
260d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
261d91446de7a89a22c8ea1cbfd40fe2528467a4ccbDan Gohman  Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
26256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
26356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
264ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
265ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
26656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
26756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
268ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
269ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
270ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
271ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
272ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
273ac6cecec189ba9689c42543c3106c02b96d788daDuncan Sands  Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
27456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OEQ_F32] = "__eqsf2";
27556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OEQ_F64] = "__eqdf2";
27656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UNE_F32] = "__nesf2";
27756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UNE_F64] = "__nedf2";
27856966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGE_F32] = "__gesf2";
27956966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGE_F64] = "__gedf2";
28056966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLT_F32] = "__ltsf2";
28156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLT_F64] = "__ltdf2";
28256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLE_F32] = "__lesf2";
28356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OLE_F64] = "__ledf2";
28456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGT_F32] = "__gtsf2";
28556966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::OGT_F64] = "__gtdf2";
28656966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UO_F32] = "__unordsf2";
28756966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  Names[RTLIB::UO_F64] = "__unorddf2";
288d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  Names[RTLIB::O_F32] = "__unordsf2";
289d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  Names[RTLIB::O_F64] = "__unorddf2";
290a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMCPY] = "memcpy";
291a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMMOVE] = "memmove";
292a114baa5bf0d70aed8b8cf576a864005c0d6a128Sanjiv Gupta  Names[RTLIB::MEMSET] = "memset";
293b0f1e1780c736c62fb99e5824825d2a60a53b53bDuncan Sands  Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
294e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1";
295e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2";
296e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4";
297e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8";
298ef6eb9c7ab7967790566c5e2d47977d89fc060eeJim Grosbach  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_1] = "__sync_lock_test_and_set_1";
299ef6eb9c7ab7967790566c5e2d47977d89fc060eeJim Grosbach  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_2] = "__sync_lock_test_and_set_2";
300ef6eb9c7ab7967790566c5e2d47977d89fc060eeJim Grosbach  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_4] = "__sync_lock_test_and_set_4";
301ef6eb9c7ab7967790566c5e2d47977d89fc060eeJim Grosbach  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_8] = "__sync_lock_test_and_set_8";
302e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_ADD_1] = "__sync_fetch_and_add_1";
303e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_ADD_2] = "__sync_fetch_and_add_2";
304e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_ADD_4] = "__sync_fetch_and_add_4";
305e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_ADD_8] = "__sync_fetch_and_add_8";
306e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_SUB_1] = "__sync_fetch_and_sub_1";
307e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_SUB_2] = "__sync_fetch_and_sub_2";
308e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_SUB_4] = "__sync_fetch_and_sub_4";
309e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_SUB_8] = "__sync_fetch_and_sub_8";
310e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_AND_1] = "__sync_fetch_and_and_1";
311e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_AND_2] = "__sync_fetch_and_and_2";
312e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_AND_4] = "__sync_fetch_and_and_4";
313e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_AND_8] = "__sync_fetch_and_and_8";
314e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_OR_1] = "__sync_fetch_and_or_1";
315e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_OR_2] = "__sync_fetch_and_or_2";
316e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_OR_4] = "__sync_fetch_and_or_4";
317e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_OR_8] = "__sync_fetch_and_or_8";
318e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_XOR_1] = "__sync_fetch_and_xor_1";
319e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_XOR_2] = "__sync_fetch_and_xor_2";
320312b7c950a27f72a811b3fdbed26e2e08eb20c31Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_XOR_4] = "__sync_fetch_and_xor_4";
321e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_XOR_8] = "__sync_fetch_and_xor_8";
322e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_NAND_1] = "__sync_fetch_and_nand_1";
323e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_NAND_2] = "__sync_fetch_and_nand_2";
324e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_NAND_4] = "__sync_fetch_and_nand_4";
325e03262fcfc09356a0e3ec589041bc2e0248944e9Jim Grosbach  Names[RTLIB::SYNC_FETCH_AND_NAND_8] = "__sync_fetch_and_nand_8";
326d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng}
327d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng
32872977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov/// InitLibcallCallingConvs - Set default libcall CallingConvs.
32972977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov///
33072977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikovstatic void InitLibcallCallingConvs(CallingConv::ID *CCs) {
33172977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
33272977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov    CCs[i] = CallingConv::C;
33372977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  }
33472977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov}
33572977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov
336b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPEXT - Return the FPEXT_*_* value for the given types, or
337b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
338e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
339825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
340825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f64)
341b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPEXT_F32_F64;
342b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
343927411b7ce0b7852fe4f392d8cd4faaa3881f852Anton Korobeynikov
344b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
345b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
346b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
347b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPROUND - Return the FPROUND_*_* value for the given types, or
348b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
349e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
350825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (RetVT == MVT::f32) {
351825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f64)
352b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPROUND_F64_F32;
353825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f80)
354e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_F80_F32;
355825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::ppcf128)
356e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_PPCF128_F32;
357825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (RetVT == MVT::f64) {
358825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::f80)
359e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_F80_F64;
360825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (OpVT == MVT::ppcf128)
361e36bfe678df09f46e1eac60b15105c1ccd2c5382Bruno Cardoso Lopes      return FPROUND_PPCF128_F64;
362b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
363927411b7ce0b7852fe4f392d8cd4faaa3881f852Anton Korobeynikov
364b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
365b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
366b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
367b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
368b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
369e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
370825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
371825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i8)
3728aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOSINT_F32_I8;
373825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i16)
3748aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOSINT_F32_I16;
375825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
376b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I32;
377825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
378b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I64;
379825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
380b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F32_I128;
381825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f64) {
382de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov    if (RetVT == MVT::i8)
383de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov      return FPTOSINT_F64_I8;
384de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov    if (RetVT == MVT::i16)
385de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov      return FPTOSINT_F64_I16;
386825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
387b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I32;
388825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
389b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I64;
390825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
391b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F64_I128;
392825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f80) {
393825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
394b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I32;
395825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
396b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I64;
397825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
398b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_F80_I128;
399825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::ppcf128) {
400825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
401b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I32;
402825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
403b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I64;
404825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
405b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOSINT_PPCF128_I128;
406b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
407b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
408b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
409b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
410b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
411b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
412e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
413825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::f32) {
414825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i8)
4158aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOUINT_F32_I8;
416825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i16)
4178aa207ee306b6a5ce753511a5f811fc1cd2468e2Sanjiv Gupta      return FPTOUINT_F32_I16;
418825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
419b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I32;
420825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
421b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I64;
422825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
423b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F32_I128;
424825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f64) {
425de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov    if (RetVT == MVT::i8)
426de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov      return FPTOUINT_F64_I8;
427de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov    if (RetVT == MVT::i16)
428de0118c324a3fb8b0b5afa8e46996d9b81666bfdAnton Korobeynikov      return FPTOUINT_F64_I16;
429825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
430b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I32;
431825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
432b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I64;
433825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
434b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F64_I128;
435825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::f80) {
436825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
437b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I32;
438825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
439b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I64;
440825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
441b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_F80_I128;
442825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::ppcf128) {
443825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i32)
444b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I32;
445825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i64)
446b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I64;
447825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::i128)
448b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return FPTOUINT_PPCF128_I128;
449b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
450b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
451b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
452b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
453b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
454b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
455e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
456825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::i32) {
457825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
458b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F32;
459825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
460b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F64;
461825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
462b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_F80;
463825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
464b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I32_PPCF128;
465825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i64) {
466825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
467b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F32;
468825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
469b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F64;
470825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
471b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_F80;
472825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
473b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I64_PPCF128;
474825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i128) {
475825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
476b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F32;
477825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
478b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F64;
479825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
480b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_F80;
481825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
482b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return SINTTOFP_I128_PPCF128;
483b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
484b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
485b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
486b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
487b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
488b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands/// UNKNOWN_LIBCALL if there is none.
489e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonRTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
490825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (OpVT == MVT::i32) {
491825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
492b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F32;
493825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
494b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F64;
495825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
496b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_F80;
497825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
498b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I32_PPCF128;
499825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i64) {
500825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
501b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F32;
502825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
503b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F64;
504825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
505b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_F80;
506825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
507b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I64_PPCF128;
508825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  } else if (OpVT == MVT::i128) {
509825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (RetVT == MVT::f32)
510b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F32;
511825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f64)
512b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F64;
513825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::f80)
514b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_F80;
515825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    else if (RetVT == MVT::ppcf128)
516b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands      return UINTTOFP_I128_PPCF128;
517b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  }
518b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  return UNKNOWN_LIBCALL;
519b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands}
520b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands
521d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng/// InitCmpLibcallCCs - Set default comparison libcall CC.
522d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng///
523d385fd62cb43435b3ad70d789198d34bf148e579Evan Chengstatic void InitCmpLibcallCCs(ISD::CondCode *CCs) {
524d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
525d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
526d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
527d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UNE_F32] = ISD::SETNE;
528d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UNE_F64] = ISD::SETNE;
529d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGE_F32] = ISD::SETGE;
530d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGE_F64] = ISD::SETGE;
531d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLT_F32] = ISD::SETLT;
532d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLT_F64] = ISD::SETLT;
533d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLE_F32] = ISD::SETLE;
534d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OLE_F64] = ISD::SETLE;
535d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGT_F32] = ISD::SETGT;
536d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::OGT_F64] = ISD::SETGT;
537d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UO_F32] = ISD::SETNE;
538d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::UO_F64] = ISD::SETNE;
539d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::O_F32] = ISD::SETEQ;
540d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  CCs[RTLIB::O_F64] = ISD::SETEQ;
54156966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng}
54256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng
543f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// NOTE: The constructor takes ownership of TLOF.
544f0757b0edc1ef3d1998485d3f74cadaa3f7180a0Dan GohmanTargetLowering::TargetLowering(const TargetMachine &tm,
545f0757b0edc1ef3d1998485d3f74cadaa3f7180a0Dan Gohman                               const TargetLoweringObjectFile *tlof)
546b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem  : TM(tm), TD(TM.getTargetData()), TLOF(*tlof),
547b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem  mayPromoteElements(AllowPromoteIntElem) {
548cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  // All operations default to being supported.
549cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  memset(OpActions, 0, sizeof(OpActions));
5500329466b6b4927f4e6f5d144891fef06a027fec5Evan Cheng  memset(LoadExtActions, 0, sizeof(LoadExtActions));
551ddf89566a93081cb230bb9406a72ab2d3eada4a7Chris Lattner  memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
552c9133f97720a36218ddfa6bfbf36ba6e22c011f7Chris Lattner  memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
5537f042681764c6f8eae22781d8b4cb4c218a86b76Evan Cheng  memset(CondCodeActions, 0, sizeof(CondCodeActions));
55493f81e2822aa67337f629398c81bc2844ef9400aDan Gohman
5551a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner  // Set default actions for various operations.
556825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
5571a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner    // Default all indexed load / store to expand.
5585ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng    for (unsigned IM = (unsigned)ISD::PRE_INC;
5595ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng         IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
560825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
561825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
5625ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng    }
563bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
5641a3048bb8e0f3de0b4e56704c15c556c71565f4bChris Lattner    // These operations default to expand.
565825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
566825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
5675ff839fbabe8b1d26cf4db5c541eb5d7942c25d6Evan Cheng  }
568d2cde68855125b6815b1575f29cd96927614b0cdEvan Cheng
569d2cde68855125b6815b1575f29cd96927614b0cdEvan Cheng  // Most targets ignore the @llvm.prefetch intrinsic.
570825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
571bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
572bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // ConstantFP nodes default to expand.  Targets can either change this to
573eb2f969a4ddfb0bc8fdcb5bce3b52e53abff321dEvan Cheng  // Legal, in which case all fp constants are legal, or use isFPImmLegal()
574e179584f9b740cf3a36bde70f8cab40de59b8081Nate Begeman  // to optimize expansions for certain constants.
575825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
576825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
577825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
578310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
5790bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen  // These library functions default to expand.
580825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG , MVT::f64, Expand);
581825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG2, MVT::f64, Expand);
582825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG10,MVT::f64, Expand);
583825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP , MVT::f64, Expand);
584825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP2, MVT::f64, Expand);
585825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG , MVT::f32, Expand);
586825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG2, MVT::f32, Expand);
587825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FLOG10,MVT::f32, Expand);
588825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP , MVT::f32, Expand);
589825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::FEXP2, MVT::f32, Expand);
5900bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen
59141bab0beac2600701af99f63e23a6b65f71468acChris Lattner  // Default ISD::TRAP to expand (which turns it into abort).
592825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::TRAP, MVT::Other, Expand);
593bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
594a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson  IsLittleEndian = TD->isLittleEndian();
59595771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson  PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
596825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
597718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson  memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
598a03a5dc7ce876dac4b3b91bae14216de4233bacdEvan Cheng  maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
5990521928ae7cc492f3f45ef0e0cedc349102489c5Evan Cheng  maxStoresPerMemsetOptSize = maxStoresPerMemcpyOptSize
6000521928ae7cc492f3f45ef0e0cedc349102489c5Evan Cheng    = maxStoresPerMemmoveOptSize = 4;
6016ebf7bc7405ee79d27d50b70f0c1a474cbea820dEvan Cheng  benefitFromCodePlacementOpt = false;
602d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreSetJmp = false;
603d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreLongJmp = false;
6046618039f9f1f2e41fe2742d83007c68bdebc3656Chris Lattner  SelectIsExpensive = false;
605405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  IntDivIsCheap = false;
606405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  Pow2DivIsCheap = false;
607de189be53f5831737f38e720f10dbcdcce6876c6Chris Lattner  JumpIsExpensive = false;
608ee4a76563a84839453588104e94d4891fc44d625Chris Lattner  StackPointerRegisterToSaveRestore = 0;
6099bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionPointerRegister = 0;
6109bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionSelectorRegister = 0;
61103228089d5235f8c90477f88809139464e9c6ea5Duncan Sands  BooleanContents = UndefinedBooleanContent;
61228b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands  BooleanVectorContents = UndefinedBooleanContent;
613211ffa13519cadfb7f9baf4c8447fa055bf38fe8Evan Cheng  SchedPreferenceInfo = Sched::Latency;
6147acf5f39fea463df5ad196affbef0176f3b25e0fChris Lattner  JumpBufSize = 0;
6150c9e0ff24900aa2bcae9518f8b1bdc3fd41dc2edDuraid Madina  JumpBufAlignment = 0;
616fc5d305597ea6336d75bd7f3b741e8d57d6a5105Eli Friedman  MinFunctionAlignment = 0;
617fc5d305597ea6336d75bd7f3b741e8d57d6a5105Eli Friedman  PrefFunctionAlignment = 0;
618fb8075d03f5c87bd57dcc9c5f2304f6b13c55aadEvan Cheng  PrefLoopAlignment = 0;
619cbeeae23c31d32b833c9c7c3e8984e4cbcf22f45Rafael Espindola  MinStackArgumentAlignment = 1;
6209a526495e0c06c4014d7500788cad1929fd244d3Jim Grosbach  ShouldFoldAtomicFences = false;
62126689ac37ebec3b358588089415509285e558de9Eli Friedman  InsertFencesForAtomic = false;
62256966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng
62356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  InitLibcallNames(LibcallRoutineNames);
624d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  InitCmpLibcallCCs(CmpLibcallCCs);
62572977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  InitLibcallCallingConvs(LibcallCallingConvs);
626310968cbbb564c4141d4bd418a746e8103560222Chris Lattner}
627310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
628f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLowering::~TargetLowering() {
629f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  delete &TLOF;
630f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
631cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
63295771afbfd604ad003fa3723cac66c9370fed55dOwen AndersonMVT TargetLowering::getShiftAmountTy(EVT LHSTy) const {
63395771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson  return MVT::getIntegerVT(8*TD->getPointerSize());
63495771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson}
63595771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson
636f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// canOpTrap - Returns true if the operation can trap for the value type.
637f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// VT must be a legal type.
638f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wangbool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
639f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  assert(isTypeLegal(VT));
640f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  switch (Op) {
641f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  default:
642f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return false;
643f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FDIV:
644f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FREM:
645f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SDIV:
646f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UDIV:
647f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SREM:
648f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UREM:
649f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return true;
650f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  }
651f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang}
652f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
653f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
65423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonstatic unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
655598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          unsigned &NumIntermediates,
656598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          EVT &RegisterVT,
657598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          TargetLowering *TLI) {
65823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Figure out the right, legal destination reg to copy into.
65923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumElts = VT.getVectorNumElements();
66023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT EltTy = VT.getVectorElementType();
661bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
66223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumVectorRegs = 1;
663bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
664bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
66523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // could break down into LHS/RHS like LegalizeDAG does.
66623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!isPowerOf2_32(NumElts)) {
66723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs = NumElts;
66823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts = 1;
66923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
670bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
67123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Divide the input until we get to a supported size.  This will always
67223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // end with a scalar if the target doesn't support vectors.
67323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
67423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts >>= 1;
67523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs <<= 1;
67623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
67723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
67823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  NumIntermediates = NumVectorRegs;
679bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
68023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
68123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!TLI->isTypeLegal(NewVT))
68223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NewVT = EltTy;
68323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  IntermediateVT = NewVT;
68423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
6850c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  unsigned NewVTSize = NewVT.getSizeInBits();
6860c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
6870c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  // Convert sizes such as i33 to i64.
6880c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  if (!isPowerOf2_32(NewVTSize))
6890c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    NewVTSize = NextPowerOf2(NewVTSize);
6900c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
69123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = TLI->getRegisterType(NewVT);
69223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  RegisterVT = DestVT;
69332b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
6940c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
695bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
69632b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  // Otherwise, promotion or legal types use the same number of registers as
69732b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  // the vector decimated to the appropriate level.
69832b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  return NumVectorRegs;
69923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson}
70023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
70146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// isLegalRC - Return true if the value types that can be represented by the
70246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// specified register class are all legal.
70346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Chengbool TargetLowering::isLegalRC(const TargetRegisterClass *RC) const {
70446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
70546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng       I != E; ++I) {
70646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (isTypeLegal(*I))
70746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      return true;
70846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
70946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  return false;
71046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
71146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
71246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// hasLegalSuperRegRegClasses - Return true if the specified register class
71346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// has one or more super-reg register classes that are legal.
714d70f57b254114841892425a40944268d38ae0bcdEvan Chengbool
715d70f57b254114841892425a40944268d38ae0bcdEvan ChengTargetLowering::hasLegalSuperRegRegClasses(const TargetRegisterClass *RC) const{
71646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  if (*RC->superregclasses_begin() == 0)
71746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    return false;
71846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
71946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng         E = RC->superregclasses_end(); I != E; ++I) {
72046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    const TargetRegisterClass *RRC = *I;
72146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (isLegalRC(RRC))
72246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      return true;
72346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
72446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  return false;
72546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
72646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
72746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// findRepresentativeClass - Return the largest legal super-reg register class
7284f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng/// of the register class for the specified type and its associated "cost".
7294f6b4674be5473319ac5e70c76fd5cb964da2128Evan Chengstd::pair<const TargetRegisterClass*, uint8_t>
7304f6b4674be5473319ac5e70c76fd5cb964da2128Evan ChengTargetLowering::findRepresentativeClass(EVT VT) const {
7314f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  const TargetRegisterClass *RC = RegClassForVT[VT.getSimpleVT().SimpleTy];
7324f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  if (!RC)
7334f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    return std::make_pair(RC, 0);
73446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  const TargetRegisterClass *BestRC = RC;
73546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
73646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng         E = RC->superregclasses_end(); I != E; ++I) {
73746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    const TargetRegisterClass *RRC = *I;
73846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (RRC->isASubClass() || !isLegalRC(RRC))
73946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      continue;
74046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (!hasLegalSuperRegRegClasses(RRC))
7414f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng      return std::make_pair(RRC, 1);
74246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    BestRC = RRC;
74346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
7444f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  return std::make_pair(BestRC, 1);
74546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
74646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
747e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner
748310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// computeRegisterProperties - Once all of the register classes are added,
749310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// this allows us to compute derived properties we expose.
750310968cbbb564c4141d4bd418a746e8103560222Chris Lattnervoid TargetLowering::computeRegisterProperties() {
751825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
752bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner         "Too many value types for ValueTypeActions to hold!");
753bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
754b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Everything defaults to needing one register.
755825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
756b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[i] = 1;
757825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
758b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
759b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // ...except isVoid, which doesn't need any registers.
760825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  NumRegistersForVT[MVT::isVoid] = 0;
761f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
762310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Find the largest integer register class.
763825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
764310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
765825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
766310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
767310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Every integer value type larger than this largest register takes twice as
768310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // many registers to represent as the previous ValueType.
76983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
7708a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
7718a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    if (!ExpandedVT.isInteger())
77283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      break;
773b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
774825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
775825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
776b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(ExpandedVT, TypeExpandInteger);
777b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
778310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
779b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Inspect all of the ValueType's smaller than the largest integer
780b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // register to see which ones need promotion.
78183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned LegalIntReg = LargestIntReg;
78283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned IntReg = LargestIntReg - 1;
783825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       IntReg >= (unsigned)MVT::i1; --IntReg) {
784825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    EVT IVT = (MVT::SimpleValueType)IntReg;
78583ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (isTypeLegal(IVT)) {
786b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      LegalIntReg = IntReg;
787b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
78883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
789825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        (MVT::SimpleValueType)LegalIntReg;
790b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
791b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
792b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
793f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
794161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  // ppcf128 type is really two f64's.
795825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::ppcf128)) {
796825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
797825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
798825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::ppcf128] = MVT::f64;
799b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
800bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  }
801161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen
802b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f64. If the target does not have native f64 support,
803b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // expand it to i64 and we will be generating soft float library calls.
804825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f64)) {
805825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
806825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
807825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::f64] = MVT::i64;
808b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
8091a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
810b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
811b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f32. If the target does not have native support for
812b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
813825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f32)) {
814825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (isTypeLegal(MVT::f64)) {
815825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
816825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
817825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::f64;
818b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(MVT::f32, TypePromoteInteger);
819b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
820825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
821825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
822825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::i32;
823b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
824b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
8251a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
826bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
827b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Loop over all of the vector value types to see which need transformations.
828825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
829825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
83023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    MVT VT = (MVT::SimpleValueType)i;
831598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    if (isTypeLegal(VT)) continue;
832bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
833e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    // Determine if there is a legal wider type.  If so, we should promote to
834e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    // that wider vector type.
835e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    EVT EltVT = VT.getVectorElementType();
836e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    unsigned NElts = VT.getVectorNumElements();
837e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (NElts != 1) {
838e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      bool IsLegalWiderType = false;
839f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // If we allow the promotion of vector elements using a flag,
840f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // then return TypePromoteInteger on vector elements.
841f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // First try to promote the elements of integer vectors. If no legal
842f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // promotion was found, fallback to the widen-vector method.
843f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      if (mayPromoteElements)
844e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
845e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        EVT SVT = (MVT::SimpleValueType)nVT;
846f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        // Promote vectors of integers to vectors with the same number
847f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        // of elements, with a wider element type.
848f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        if (SVT.getVectorElementType().getSizeInBits() > EltVT.getSizeInBits()
849f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem            && SVT.getVectorNumElements() == NElts &&
850f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem            isTypeLegal(SVT) && SVT.getScalarType().isInteger()) {
851f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          TransformToType[i] = SVT;
852f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          RegisterTypeForVT[i] = SVT;
853f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          NumRegistersForVT[i] = 1;
854f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
855f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          IsLegalWiderType = true;
856f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          break;
857b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem        }
858f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      }
859f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem
860f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      if (IsLegalWiderType) continue;
861b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem
862f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // Try to widen the vector.
863f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
864f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        EVT SVT = (MVT::SimpleValueType)nVT;
865e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        if (SVT.getVectorElementType() == EltVT &&
866bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            SVT.getVectorNumElements() > NElts &&
867e93d99cf0742eebab859022e4cfdcf03cb9d5dfaDale Johannesen            isTypeLegal(SVT)) {
868e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          TransformToType[i] = SVT;
869e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          RegisterTypeForVT[i] = SVT;
870e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          NumRegistersForVT[i] = 1;
871b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem          ValueTypeActions.setTypeAction(VT, TypeWidenVector);
872e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          IsLegalWiderType = true;
873e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          break;
874e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        }
875e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      }
876e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      if (IsLegalWiderType) continue;
877e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    }
878bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
879598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    MVT IntermediateVT;
880598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    EVT RegisterVT;
881598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    unsigned NumIntermediates;
882598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    NumRegistersForVT[i] =
883598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner      getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
884598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                RegisterVT, this);
885598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    RegisterTypeForVT[i] = RegisterVT;
886bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
887e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    EVT NVT = VT.getPow2VectorType();
888e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (NVT == VT) {
889e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      // Type is already a power of 2.  The default action is to split.
890e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      TransformToType[i] = MVT::Other;
891b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      unsigned NumElts = VT.getVectorNumElements();
892b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(VT,
893b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem            NumElts > 1 ? TypeSplitVector : TypeScalarizeVector);
894e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    } else {
895e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      TransformToType[i] = NVT;
896b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(VT, TypeWidenVector);
8977f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    }
8983a59358499f527ad9a8d1d4ed7d80b6bf0f1c12dChris Lattner  }
89946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
90046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // Determine the 'representative' register class for each value type.
90146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // An representative register class is the largest (meaning one which is
90246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // not a sub-register class / subreg register class) legal register class for
90346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // a group of value types. For example, on i386, i8, i16, and i32
90446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // representative would be GR32; while on x86_64 it's GR64.
905d70f57b254114841892425a40944268d38ae0bcdEvan Cheng  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
9064f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    const TargetRegisterClass* RRC;
9074f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    uint8_t Cost;
9084f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    tie(RRC, Cost) =  findRepresentativeClass((MVT::SimpleValueType)i);
9094f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    RepRegClassForVT[i] = RRC;
9104f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    RepRegClassCostForVT[i] = Cost;
911d70f57b254114841892425a40944268d38ae0bcdEvan Cheng  }
912bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner}
913cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
9147226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Chengconst char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
9157226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng  return NULL;
9167226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng}
9173a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng
9185b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
91928b77e968d2b01fc9da724762bd8ddcd80650e32Duncan SandsEVT TargetLowering::getSetCCResultType(EVT VT) const {
92028b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands  assert(!VT.isVector() && "No default SetCC type for vectors!");
9211d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  return PointerTy.SimpleTy;
9225b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel}
9235b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
9248f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv GuptaMVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
9258f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta  return MVT::i32; // return the default value
9268f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta}
9278f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta
9287f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// getVectorTypeBreakdown - Vector types are broken down into some number of
929825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
930825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
931825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
932dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
9337f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// This method returns the number of registers needed, and the VT for each
934b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// register.  It also returns the VT and quantity of the intermediate values
935b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// before they are promoted/expanded.
936dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
93723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonunsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
938e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                                                EVT &IntermediateVT,
939b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman                                                unsigned &NumIntermediates,
94023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                                EVT &RegisterVT) const {
94183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned NumElts = VT.getVectorNumElements();
942bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
943e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // If there is a wider vector type with the same element type as this one,
944e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // we should widen to that legal vector type.  This handles things like
945e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // <2 x float> -> <4 x float>.
946b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem  if (NumElts != 1 && getTypeAction(Context, VT) == TypeWidenVector) {
947e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    RegisterVT = getTypeToTransformTo(Context, VT);
948e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (isTypeLegal(RegisterVT)) {
949e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      IntermediateVT = RegisterVT;
950e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      NumIntermediates = 1;
951e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      return 1;
952e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    }
953e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  }
954bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
955e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // Figure out the right, legal destination reg to copy into.
956e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT EltTy = VT.getVectorElementType();
957bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
958dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  unsigned NumVectorRegs = 1;
959bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
960bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
961d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  // could break down into LHS/RHS like LegalizeDAG does.
962d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  if (!isPowerOf2_32(NumElts)) {
963d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumVectorRegs = NumElts;
964d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumElts = 1;
965d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  }
966bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
967dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // Divide the input until we get to a supported size.  This will always
968dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // end with a scalar if the target doesn't support vectors.
96923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !isTypeLegal(
97023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                   EVT::getVectorVT(Context, EltTy, NumElts))) {
971dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumElts >>= 1;
972dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumVectorRegs <<= 1;
973dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  }
974b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
975b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  NumIntermediates = NumVectorRegs;
976bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
97723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
9787f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman  if (!isTypeLegal(NewVT))
9797f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    NewVT = EltTy;
980b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  IntermediateVT = NewVT;
981dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
98223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = getRegisterType(Context, NewVT);
983b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  RegisterVT = DestVT;
9840c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  unsigned NewVTSize = NewVT.getSizeInBits();
9850c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
9860c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  // Convert sizes such as i33 to i64.
9870c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  if (!isPowerOf2_32(NewVTSize))
9880c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    NewVTSize = NextPowerOf2(NewVTSize);
9890c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
990e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  if (DestVT.bitsLT(NewVT))   // Value is expanded, e.g. i64 -> i16.
9910c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
992bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
993e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // Otherwise, promotion or legal types use the same number of registers as
994e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // the vector decimated to the appropriate level.
995e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  return NumVectorRegs;
996dc879296090a738c66968f5eec77db65d7e03623Chris Lattner}
997dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
998bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// Get the EVTs and ArgFlags collections that represent the legalized return
99984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// type of the given function.  This does not require a DAG or a return value,
100084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// and is suitable for use before any DAGs for the function are constructed.
100184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// TODO: Move this out of TargetLowering.cpp.
1002db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnervoid llvm::GetReturnInfo(Type* ReturnType, Attributes attr,
100384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         SmallVectorImpl<ISD::OutputArg> &Outs,
100484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         const TargetLowering &TLI,
100584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         SmallVectorImpl<uint64_t> *Offsets) {
100684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  SmallVector<EVT, 4> ValueVTs;
100784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  ComputeValueVTs(TLI, ReturnType, ValueVTs);
100884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  unsigned NumValues = ValueVTs.size();
100984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  if (NumValues == 0) return;
101084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  unsigned Offset = 0;
101184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
101284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  for (unsigned j = 0, f = NumValues; j != f; ++j) {
101384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    EVT VT = ValueVTs[j];
101484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
101584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
101684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::SExt)
101784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      ExtendKind = ISD::SIGN_EXTEND;
101884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    else if (attr & Attribute::ZExt)
101984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      ExtendKind = ISD::ZERO_EXTEND;
102084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
102184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // FIXME: C calling convention requires the return type to be promoted to
102284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // at least 32-bit. But this is not necessary for non-C calling
102384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // conventions. The frontend should mark functions whose return values
102484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // require promoting with signext or zeroext attributes.
102584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
102684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
102784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      if (VT.bitsLT(MinVT))
102884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        VT = MinVT;
102984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    }
103084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
103184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
103284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
103384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
103484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                        PartVT.getTypeForEVT(ReturnType->getContext()));
103584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
103684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // 'inreg' on function refers to return value
103784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
103884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::InReg)
103984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setInReg();
104084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
104184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // Propagate extension type if any
104284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::SExt)
104384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setSExt();
104484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    else if (attr & Attribute::ZExt)
104584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setZExt();
104684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
104784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    for (unsigned i = 0; i < NumParts; ++i) {
104884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Outs.push_back(ISD::OutputArg(Flags, PartVT, /*isFixed=*/true));
104984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      if (Offsets) {
105084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        Offsets->push_back(Offset);
105184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        Offset += PartSize;
105284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      }
105384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    }
105484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  }
105584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman}
105684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
10573ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
105828d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// function arguments in the caller parameter area.  This is the actual
105928d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// alignment, not its logarithm.
1060db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerunsigned TargetLowering::getByValTypeAlignment(Type *Ty) const {
106128d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen  return TD->getCallFrameTypeAlignment(Ty);
10623ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng}
10633ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng
1064071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1065071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// current function.  The returned value is a member of the
1066071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// MachineJumpTableInfo::JTEntryKind enum.
1067071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattnerunsigned TargetLowering::getJumpTableEncoding() const {
1068071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In non-pic modes, just use the address of a block.
1069071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
1070071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_BlockAddress;
1071bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1072071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In PIC mode, if the target supports a GPRel32 directive, use it.
1073071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
1074071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_GPRel32BlockAddress;
1075bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1076071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // Otherwise, use a label difference.
1077071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  return MachineJumpTableInfo::EK_LabelDifference32;
1078071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner}
1079071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner
1080475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
1081475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                 SelectionDAG &DAG) const {
1082f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  // If our PIC model is GP relative, use the global offset table as the base.
1083f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  if (getJumpTableEncoding() == MachineJumpTableInfo::EK_GPRel32BlockAddress)
1084b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen    return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
1085cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng  return Table;
1086cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng}
1087cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng
108813e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
108913e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
109013e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// MCExpr.
109113e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattnerconst MCExpr *
1092589c6f620e8dcf3d59af1ae0e15372c934647c82Chris LattnerTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
1093589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner                                             unsigned JTI,MCContext &Ctx) const{
1094beeb93e6ba48af2661eabc4872d8b159fb43e5dbChris Lattner  // The normal PIC reloc base is the label at the start of the jump table.
1095589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner  return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
109613e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner}
109713e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner
10986520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohmanbool
10996520e20e4fb31f2e65e25c38b372b19d33a83df4Dan GohmanTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11006520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Assume that everything is safe in static mode.
11016520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::Static)
11026520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
11036520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
11046520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // In dynamic-no-pic mode, assume that known defined values are safe.
11056520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
11066520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      GA &&
11076520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      !GA->getGlobal()->isDeclaration() &&
1108667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands      !GA->getGlobal()->isWeakForLinker())
11096520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
11106520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
11116520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Otherwise assume nothing is safe.
11126520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  return false;
11136520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman}
11146520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
1115eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
1116eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Optimization Methods
1117eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
1118eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
1119bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// ShrinkDemandedConstant - Check to see if the specified operand of the
1120368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// specified instruction is a constant integer.  If so, check to see if there
1121368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// are any bits set in the constant that are not demanded.  If so, shrink the
1122368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// constant and return true.
1123bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckbool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
11247b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                                        const APInt &Demanded) {
1125de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen  DebugLoc dl = Op.getDebugLoc();
112636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1127ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // FIXME: ISD::SELECT, ISD::SELECT_CC
1128e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  switch (Op.getOpcode()) {
1129368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  default: break;
1130368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
113136ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::AND:
113236ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::OR: {
113336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
113436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (!C) return false;
113536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
113636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (Op.getOpcode() == ISD::XOR &&
113736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling        (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
113836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return false;
113936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
114036ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    // if we can expand it to have all bits set, do it
114136ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (C->getAPIntValue().intersects(~Demanded)) {
1142e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
114336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
114436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                DAG.getConstant(Demanded &
1145bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                C->getAPIntValue(),
114636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                                VT));
114736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return CombineTo(Op, New);
114836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    }
114936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1150368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1151368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
115236ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  }
115336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1154368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  return false;
1155368e18d56a87308045d341e85584597bfe7426e9Nate Begeman}
1156368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
115797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
115897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// casts are free.  This uses isZExtFree and ZERO_EXTEND for the widening
115997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// cast, but it could be generalized for targets with other types of
116097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// implicit widening casts.
116197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohmanbool
116297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan GohmanTargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
116397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    unsigned BitWidth,
116497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    const APInt &Demanded,
116597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    DebugLoc dl) {
116697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNumOperands() == 2 &&
116797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports binary operators!");
116897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNode()->getNumValues() == 1 &&
116997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports nodes with one result!");
117097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
117197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Don't do this if the node has another user, which may require the
117297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // full value.
117397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!Op.getNode()->hasOneUse())
117497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    return false;
117597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
117697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Search for the smallest integer type with free casts to and from
117797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Op's type. For expedience, just check power-of-2 integer types.
117897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
117997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
118097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!isPowerOf2_32(SmallVTBits))
118197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    SmallVTBits = NextPowerOf2(SmallVTBits);
118297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
118323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
118497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
118597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman        TLI.isZExtFree(SmallVT, Op.getValueType())) {
118697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      // We found a type with free casts.
118797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
118897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
118997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(0)),
119097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
119197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(1)));
119297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
119397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return CombineTo(Op, Z);
119497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    }
119597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
119697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  return false;
119797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman}
119897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1199368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
12008c1ec5a0a21dcb3364aace8174f29f209ff3224eChad Rosier/// DemandedMask bits of the result of Op are ever used downstream.  If we can
1201368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// use this information to simplify Op, create a new simplified DAG node and
1202368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// return true, returning the original and new nodes in Old and New. Otherwise,
1203368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// analyze the expression and return a mask of KnownOne and KnownZero bits for
1204368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// the expression (used to simplify the caller).  The KnownZero/One bits may
1205368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// only be accurate for those bits in the DemandedMask.
1206475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanbool TargetLowering::SimplifyDemandedBits(SDValue Op,
12077b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          const APInt &DemandedMask,
12087b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownZero,
12097b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownOne,
1210368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          TargetLoweringOpt &TLO,
1211368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          unsigned Depth) const {
12127b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  unsigned BitWidth = DemandedMask.getBitWidth();
121387862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman  assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
12147b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman         "Mask size mismatches value type size!");
12157b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt NewMask = DemandedMask;
12166f38cb61a94b3abab70f0ee463bdcf55d86d334eDale Johannesen  DebugLoc dl = Op.getDebugLoc();
12177b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
12187b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  // Don't know anything.
12197b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  KnownZero = KnownOne = APInt(BitWidth, 0);
12203fc5b01d38ddccbe53ff0b32cc0100351d698c66Chris Lattner
1221368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  // Other users may use these bits.
1222bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (!Op.getNode()->hasOneUse()) {
1223368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Depth != 0) {
1224bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      // If not at the root, Just compute the KnownZero/KnownOne bits to
1225368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // simplify things downstream.
1226ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman      TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
1227368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return false;
1228368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1229368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If this is the root being simplified, allow it to have multiple uses,
12307b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    // just set the NewMask to all bits.
12317b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    NewMask = APInt::getAllOnesValue(BitWidth);
1232bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  } else if (DemandedMask == 0) {
1233368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Not demanding any bits from Op.
1234368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Op.getOpcode() != ISD::UNDEF)
1235e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
1236de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    return false;
1237368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  } else if (Depth == 6) {        // Limit search depth.
1238368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    return false;
1239368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
1240368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
12417b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
1242de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  switch (Op.getOpcode()) {
1243368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::Constant:
1244368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // We know all of the bits for a constant!
12457b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
12467b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero = ~KnownOne & NewMask;
1247ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return false;   // Don't fall through, will infinitely loop.
1248de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::AND:
124981cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // If the RHS is a constant, check to see if the LHS would be zero without
125081cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // using the bits from the RHS.  Below, we use knowledge about the RHS to
125181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // simplify the LHS, here we're using information from the LHS to simplify
125281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // the RHS.
125381cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12547b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt LHSZero, LHSOne;
125597fd9a58de3c99a74b0890a908430fa3ab894bedDale Johannesen      // Do not increment Depth here; that can cause an infinite loop.
12567b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
125797fd9a58de3c99a74b0890a908430fa3ab894bedDale Johannesen                                LHSZero, LHSOne, Depth);
125881cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If the LHS already has zeros where RHSC does, this and is dead.
12597b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
126081cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return TLO.CombineTo(Op, Op.getOperand(0));
126181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If any of the set bits in the RHS are known zero on the LHS, shrink
126281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // the constant.
12637b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
126481cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return true;
126581cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    }
1266bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
12677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1268368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1269368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1270bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
12717b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
1272368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1273368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1274bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1275bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1276368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known one on one side, return the other.
1277368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'and'.
12787b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1279368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
12807b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1281368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1282368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits in the inputs are known zeros, return zero.
12837b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
1284368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1285368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
12867b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
1287368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
128897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
12894e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
129097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
129197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1292368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 bits are only known if set in both the LHS & RHS.
1293368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1294368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 are known to be clear if zero in either the LHS | RHS.
1295368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero |= KnownZero2;
1296368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1297368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::OR:
1298bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1299368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1300368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1301bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13027b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
1303368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1304368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1305bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1306bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1307368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1308368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'or'.
13097b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
1310368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13117b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
1312368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1313368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the potentially set bits on one side are known to be set on
1314368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // the other side, just use the 'other' side.
13157b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1316368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13177b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1318368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1319368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
13207b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1321368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
132297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
13234e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
132497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
132597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1326368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are only known if clear in both the LHS & RHS.
1327368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1328368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in either the LHS | RHS.
1329368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne |= KnownOne2;
1330368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1331368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
1332bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1333368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1334368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1335bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13367b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
1337368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1338368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1339bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1340bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1341368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1342368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'xor'.
13437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero & NewMask) == NewMask)
1344368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13457b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero2 & NewMask) == NewMask)
1346368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
134797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
13484e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
134997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
135097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
13513687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // If all of the unknown bits are known to be zero on one side or the other
13523687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // (but not both) turn this into an *inclusive* or.
13533687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
13547b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
1355de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
13563687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(0),
13573687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(1)));
1358bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1359368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are known if clear or set in both the LHS & RHS.
1360368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1361368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in only one of the LHS, RHS.
1362368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1363bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1364368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits on one side are known, and all of the set
1365368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bits on that side are also known to be set on the other side, turn this
1366368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // into an AND, as we know the bits will be cleared.
1367368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
13687b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
1369368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      if ((KnownOne & KnownOne2) == KnownOne) {
1370e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT VT = Op.getValueType();
1371475871a144eb604ddaf37503397ba0941442e5fbDan Gohman        SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
1372bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1373ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0), ANDC));
1374368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1375de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1376bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1377368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
13784fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // for XOR, we prefer to force bits to 1 if they will make a -1.
13794fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // if we can't force bits, try to shrink constant
13804fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
13814fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      APInt Expanded = C->getAPIntValue() | (~NewMask);
13824fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      // if we can expand it to have all bits set, do it
13834fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      if (Expanded.isAllOnesValue()) {
13844fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        if (Expanded != C->getAPIntValue()) {
1385e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1386ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
13874fea2e982d79132715711dfcfdc46abf15239217Torok Edwin                                          TLO.DAG.getConstant(Expanded, VT));
13884fea2e982d79132715711dfcfdc46abf15239217Torok Edwin          return TLO.CombineTo(Op, New);
13894fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        }
13904fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // if it already has all the bits set, nothing to change
13914fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // but don't shrink either!
13924fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
13934fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        return true;
13944fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      }
13954fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    }
13964fea2e982d79132715711dfcfdc46abf15239217Torok Edwin
1397368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero = KnownZeroOut;
1398368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne  = KnownOneOut;
1399368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1400368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SELECT:
1401bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
1402368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1403368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
14047b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
1405368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1406368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1407bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1408bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1409bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1410368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the operands are constants, see if we can simplify them.
14117b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1412368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1413bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1414368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Only known if known in both the LHS and RHS.
1415368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1416368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1417de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1418ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SELECT_CC:
1419bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
1420ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1421ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
14227b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
1423ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne2, TLO, Depth+1))
1424ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1425bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1426bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1427bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1428ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the operands are constants, see if we can simplify them.
14297b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1430ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1431bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1432ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Only known if known in both the LHS and RHS.
1433ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownOne &= KnownOne2;
1434ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero &= KnownZero2;
1435ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1436de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SHL:
1437368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1438f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1439475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1440895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
14417b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
14427b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
14437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
14447b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1445895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1446895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the bottom bits (which are shifted
1447895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // out) are never demanded.
1448895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SRL &&
1449895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
14507b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
1451f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1452895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SHL;
1453895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1454895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1455895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1456895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SRL;
1457bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          }
1458bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1459bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          SDValue NewSA =
14604e7e6cd13ab8cab410ad11375101878865579325Chris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1461e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1462ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1463895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1464895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1465bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      }
1466bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1467a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
1468368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1469368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1470a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman
1471a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
1472a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      // are not demanded. This will likely allow the anyext to be folded away.
1473a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
1474a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        SDValue InnerOp = InOp.getNode()->getOperand(0);
1475a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        EVT InnerVT = InnerOp.getValueType();
1476a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        if ((APInt::getHighBitsSet(BitWidth,
1477a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman                                   BitWidth - InnerVT.getSizeInBits()) &
1478a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman               DemandedMask) == 0 &&
1479a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            isTypeDesirableForOp(ISD::SHL, InnerVT)) {
148095771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson          EVT ShTy = getShiftAmountTy(InnerVT);
1481cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman          if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
1482cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman            ShTy = InnerVT;
1483a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman          SDValue NarrowShl =
1484a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
1485cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman                            TLO.DAG.getConstant(ShAmt, ShTy));
1486a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman          return
1487a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            TLO.CombineTo(Op,
1488a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman                          TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
1489a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman                                          NarrowShl));
1490a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        }
1491a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      }
1492a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman
1493f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero <<= SA->getZExtValue();
1494f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownOne  <<= SA->getZExtValue();
14957b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // low bits known zero.
1496f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
1497368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1498368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1499368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRL:
1500368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1501e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1502f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
150383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      unsigned VTSize = VT.getSizeInBits();
1504475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1505bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15067b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
15077b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
15087b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
15097b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1510895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1511895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the top bits (which are shifted out)
1512895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // are never demanded.
1513895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SHL &&
1514895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
15157b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
1516f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1517895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SRL;
1518895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1519895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1520895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1521895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SHL;
1522bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          }
1523bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1524475871a144eb604ddaf37503397ba0941442e5fbDan Gohman          SDValue NewSA =
15258c7d2d56bf5aa2186e2d08ed1dc37c2f392b4aaeChris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1526ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1527895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1528895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1529bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      }
1530bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1531368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // Compute the new bits that are at the top now.
15327b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
1533368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1534368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1535bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
15367b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
15377b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1538c4fa603347e798c94f54430227df8265736ef183Chris Lattner
15397b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1540c4fa603347e798c94f54430227df8265736ef183Chris Lattner      KnownZero |= HighBits;  // High bits known zero.
1541368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1542368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1543368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRA:
1544e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // If this is an arithmetic shift right and only the low-bit is set, we can
1545e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // always convert this into a logical shr, even if the shift amount is
1546e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // variable.  The low bit of the shift cannot be an input sign bit unless
1547e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // the shift amount is >= the size of the datatype, which is undefined.
1548e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (DemandedMask == 1)
1549e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng      return TLO.CombineTo(Op,
1550e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                           TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
1551e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                           Op.getOperand(0), Op.getOperand(1)));
1552e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
1553368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1554e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1555f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1556bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15577b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
15587b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
15597b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
15607b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
15617b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt InDemandedMask = (NewMask << ShAmt);
15621b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner
15631b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // If any of the demanded bits are produced by the sign extension, we also
15641b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // demand the input sign bit.
15657b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
15667b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (HighBits.intersects(NewMask))
156787862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman        InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
1568bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15691b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
1570368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1571368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1572bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
15737b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
15747b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1575bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15767b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // Handle the sign bit, adjusted to where it is now in the mask.
15777b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
1578bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1579368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // If the input sign bit is known to be zero, or if none of the top bits
1580368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // are demanded, turn this into an unsigned shift right.
15817b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
1582bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1583ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0),
1584368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                 Op.getOperand(1)));
15857b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
1586368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        KnownOne |= HighBits;
1587368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1588de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1589de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1590de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SIGN_EXTEND_INREG: {
1591e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1592368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1593bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    // Sign extension.  Compute the demanded bits in the result that are not
1594368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // present in the input.
1595d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt NewBits =
1596d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getHighBitsSet(BitWidth,
15971d17d199a4cd6190fdf82e35c42b617eaae2cd35Eli Friedman                            BitWidth - EVT.getScalarType().getSizeInBits());
1598bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1599ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the extended bits are demanded, eliminate the sextinreg.
16001d17d199a4cd6190fdf82e35c42b617eaae2cd35Eli Friedman    if ((NewBits & NewMask) == 0)
1601ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return TLO.CombineTo(Op, Op.getOperand(0));
1602ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
160340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InSignBit =
160440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad      APInt::getSignBit(EVT.getScalarType().getSizeInBits()).zext(BitWidth);
1605d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt InputDemandedBits =
1606d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getLowBitsSet(BitWidth,
1607d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman                           EVT.getScalarType().getSizeInBits()) &
1608d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      NewMask;
1609bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1610ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since the sign extended bits are demanded, we know that the sign
1611368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bit is demanded.
1612ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InputDemandedBits |= InSignBit;
1613368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1614368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1615368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero, KnownOne, TLO, Depth+1))
1616de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman      return true;
1617bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1618368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1619368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the sign bit of the input is known set or clear, then we know the
1620368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // top bits of the result.
1621bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1622ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the input sign bit is known zero, convert this into a zero extension.
16237b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1624bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return TLO.CombineTo(Op,
1625ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                           TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
1626bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
16277b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {    // Input sign bit known set
1628368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne |= NewBits;
1629368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1630ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {                       // Input sign bit unknown
1631368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1632368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne &= ~NewBits;
1633de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1634de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1635de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1636ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ZERO_EXTEND: {
1637d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1638d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
163940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InMask = NewMask.trunc(OperandBitWidth);
1640bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1641ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
16427b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits =
16437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
16447b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (!NewBits.intersects(NewMask))
1645ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1646bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                               Op.getValueType(),
1647ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1648bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
16497b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1650ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1651ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1652bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
165340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
165440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1655ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero |= NewBits;
1656ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1657ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1658ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SIGN_EXTEND: {
1659e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT InVT = Op.getOperand(0).getValueType();
1660d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned InBits = InVT.getScalarType().getSizeInBits();
16617b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask    = APInt::getLowBitsSet(BitWidth, InBits);
16629736028d84de3a72dd8db5f49cfaa07280154a0eDan Gohman    APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
16637b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits   = ~InMask & NewMask;
1664bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1665ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
1666ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    if (NewBits == 0)
1667ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1668ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getValueType(),
1669ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getOperand(0)));
1670bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1671ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since some of the sign extended bits are demanded, we know that the sign
1672ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // bit is demanded.
16737b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InDemandedBits = InMask & NewMask;
1674ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InDemandedBits |= InSignBit;
167540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    InDemandedBits = InDemandedBits.trunc(InBits);
1676bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1677bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1678ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1679ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
168040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
168140f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1682bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1683ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known zero, convert this to a zero extend.
16847b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1685ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
1686bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                               Op.getValueType(),
1687ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1688bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1689ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known one, the top bits match.
16907b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {
1691ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  |= NewBits;
1692ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1693ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {   // Otherwise, top bits aren't known.
1694ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  &= ~NewBits;
1695ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1696ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    }
1697ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1698ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1699ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ANY_EXTEND: {
1700d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1701d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
170240f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InMask = NewMask.trunc(OperandBitWidth);
17037b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1704ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1705ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1706bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
170740f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
170840f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1709ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1710ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1711fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  case ISD::TRUNCATE: {
1712c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // Simplify the input, using demanded bit information, and compute the known
1713c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // zero/one bits live out.
1714042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman    unsigned OperandBitWidth =
1715042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
171640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt TruncMask = NewMask.zext(OperandBitWidth);
17177b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
1718fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1719fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner      return true;
172040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.trunc(BitWidth);
172140f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.trunc(BitWidth);
1722bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1723c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // If the input is only used by this truncate, see if we can shrink it based
1724c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // on the known demanded bits.
1725ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (Op.getOperand(0).getNode()->hasOneUse()) {
1726475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue In = Op.getOperand(0);
1727c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      switch (In.getOpcode()) {
1728c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      default: break;
1729c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      case ISD::SRL:
1730c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // Shrink SRL by a constant if none of the high bits shifted in are
1731c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // demanded.
1732e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (TLO.LegalTypes() &&
1733e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng            !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
1734e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1735e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // undesirable.
1736e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          break;
1737e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
1738e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (!ShAmt)
1739e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          break;
17407adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        SDValue Shift = In.getOperand(1);
17417adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        if (TLO.LegalTypes()) {
17427adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson          uint64_t ShVal = ShAmt->getZExtValue();
17437adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson          Shift =
17447adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson            TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
17457adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        }
17467adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson
1747e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1748e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                               OperandBitWidth - BitWidth);
174940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad        HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
1750e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng
1751e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1752e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // None of the shifted in bits are needed.  Add a truncate of the
1753e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // shift input, then shift it.
1754e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
1755bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                             Op.getValueType(),
1756e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                             In.getOperand(0));
1757e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1758e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                                   Op.getValueType(),
1759bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                   NewTrunc,
17607adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson                                                   Shift));
1761c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        }
1762c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        break;
1763c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      }
1764c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    }
1765bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1766bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1767fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner    break;
1768fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  }
1769ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::AssertZext: {
17707ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    // AssertZext demands all of the high bits, plus any of the low bits
17717ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    // demanded by its users.
17727ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
17737ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    APInt InMask = APInt::getLowBitsSet(BitWidth,
17747ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson                                        VT.getSizeInBits());
17757ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
1776ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1777ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1778bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1779400f75cb5ed39ab4f071f78f6a26beefbc8a46f0Dan Gohman
17807b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero |= ~InMask & NewMask;
1781ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1782ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1783bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  case ISD::BITCAST:
178457f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings    // If this is an FP->Int bitcast and if the sign bit is the only
178557f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings    // thing demanded, turn this into a FGETSIGN.
17860c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    if (!Op.getOperand(0).getValueType().isVector() &&
17870c3e67860af417febb1fa9e870ece912a16085acNadav Rotem        NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
17880c3e67860af417febb1fa9e870ece912a16085acNadav Rotem        Op.getOperand(0).getValueType().isFloatingPoint()) {
178957f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
179057f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      bool i32Legal  = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
179157f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
179257f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
17932ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // Make a FGETSIGN + SHL to move the sign bit into the appropriate
17942ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // place.  We expect the SHL to be eliminated by other optimizations.
1795090bf19de6e7c75fbf34e753f5e0ad58cc2ca15cStuart Hastings        SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
179657f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
179757f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        if (!OpVTLegal && OpVTSizeInBits > 32)
1798090bf19de6e7c75fbf34e753f5e0ad58cc2ca15cStuart Hastings          Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
179983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands        unsigned ShVal = Op.getValueType().getSizeInBits()-1;
1800bdce3726e2131f053d05f07ff135684ad6f11d80Stuart Hastings        SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
18013dfc4b12229acafee5c69c5ee1673b08027f0438Stuart Hastings        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
18023dfc4b12229acafee5c69c5ee1673b08027f0438Stuart Hastings                                                 Op.getValueType(),
18032ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner                                                 Sign, ShAmt));
18042ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner      }
18052ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    }
18062ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    break;
180797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::ADD:
180897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::MUL:
180997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::SUB: {
181097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // Add, Sub, and Mul don't demand any bits in positions beyond that
181197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // of the highest bit demanded of them.
181297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    APInt LoMask = APInt::getLowBitsSet(BitWidth,
181397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                        BitWidth - NewMask.countLeadingZeros());
181497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
181597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
181697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
181797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
181897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
181997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
182097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // See if the operation should be performed at a smaller bit width.
18214e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
182297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
182397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
182497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // FALL THROUGH
182554eed371314af403ef8307b386b3b5b46c8aa6fbDan Gohman  default:
18261482b5fc7affd691fbd8ece7808ddd4559ad20aeChris Lattner    // Just use ComputeMaskedBits to compute output bits.
18277b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
1828a6bc5a4d2138ea3cba90f5a794dd525228ec2c73Chris Lattner    break;
1829de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1830bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1831ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // If we know the value of all of the demanded bits, return this as a
1832ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // constant.
18337b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  if ((NewMask & (KnownZero|KnownOne)) == NewMask)
1834ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1835bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1836de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  return false;
1837de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman}
1838c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner
1839bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1840bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// in Mask are known to be either zero or one and return them in the
1841368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// KnownZero/KnownOne bitsets.
1842bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckvoid TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1843977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman                                                    const APInt &Mask,
1844bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                    APInt &KnownZero,
1845fd29e0eb060ea8b4d490860329234d2ae5f5952eDan Gohman                                                    APInt &KnownOne,
1846ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman                                                    const SelectionDAG &DAG,
1847368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                    unsigned Depth) const {
18481b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
18491b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
18501b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
18511b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1852c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         "Should use MaskedValueIsZero if you don't know whether Op"
1853c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         " is a target node!");
1854977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
18553a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng}
18564ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner
18575c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// ComputeNumSignBitsForTargetNode - This method can be implemented by
18585c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// targets that want to expose additional information about sign bits to the
18595c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// DAG Combiner.
1860475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanunsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
18615c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner                                                         unsigned Depth) const {
18625c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
18635c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
18645c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
18655c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
18665c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         "Should use ComputeNumSignBits if you don't know whether Op"
18675c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         " is a target node!");
18685c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  return 1;
18695c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner}
18705c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
187197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
187297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
187397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// determine which bit is set.
187497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman///
187585b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesenstatic bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
187697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // A left-shift of a constant one will have exactly one bit set, because
187797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // shifting the bit off the end is undefined.
187897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SHL)
187997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
188097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
188197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue() == 1)
188297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
188397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
188497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Similarly, a right-shift of a constant sign-bit will have exactly
188597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // one bit set.
188697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SRL)
188797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
188897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
188997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue().isSignBit())
189097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
189197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
189297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // More could be done here, though the above checks are enough
189397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // to handle some common cases.
1894e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
189597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Fall back to ComputeMaskedBits to catch other known cases.
1896e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT OpVT = Val.getValueType();
18975b870aff81da0c07413f0241087bb3722954b83dDan Gohman  unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
1898e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt Mask = APInt::getAllOnesValue(BitWidth);
1899e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt KnownZero, KnownOne;
1900e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
190185b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen  return (KnownZero.countPopulation() == BitWidth - 1) &&
190285b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen         (KnownOne.countPopulation() == 1);
1903e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman}
19045c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
1905bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1906475871a144eb604ddaf37503397ba0941442e5fbDan Gohman/// and cc. If it is unable to simplify it, return a null SDValue.
1907475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue
1908e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonTargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1909fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                              ISD::CondCode Cond, bool foldBooleans,
1910ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                              DAGCombinerInfo &DCI, DebugLoc dl) const {
1911fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  SelectionDAG &DAG = DCI.DAG;
1912fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1913fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // These setcc operations always fold.
1914fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  switch (Cond) {
1915fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  default: break;
1916fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE:
1917fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1918fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE:
1919fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE2:  return DAG.getConstant(1, VT);
1920fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
1921fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
19227a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // Ensure that the constant occurs on the RHS, and fold constant
19237a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // comparisons.
19247a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  if (isa<ConstantSDNode>(N0.getNode()))
1925b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1926362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
1927ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
19286c6cd1ccb5de1ebab7089c8ce339889f33fa2577Dan Gohman    const APInt &C1 = N1C->getAPIntValue();
1929b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1930b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1931b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // equality comparison, then we're just comparing whether X itself is
1932b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // zero.
1933b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1934b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1935b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(1).getOpcode() == ISD::Constant) {
1936347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      const APInt &ShAmt
1937347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1938b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1939b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1940b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1941b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 0  -> X != 0
1942b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 1  -> X != 0
1943b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETNE;
1944b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        } else {
1945b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 0  -> X == 0
1946b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 1  -> X == 0
1947b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETEQ;
1948fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
1949b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        SDValue Zero = DAG.getConstant(0, N0.getValueType());
1950b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1951b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                            Zero, Cond);
1952fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1953b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
195489217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen
1955d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    SDValue CTPOP = N0;
1956d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    // Look through truncs that don't change the value of a ctpop.
1957d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1958d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      CTPOP = N0.getOperand(0);
1959d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1960d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
1961c9b6a3eb90c5e0f6460632a443591068522537e2Benjamin Kramer        (N0 == CTPOP || N0.getValueType().getSizeInBits() >
1962d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer                        Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1963d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      EVT CTVT = CTPOP.getValueType();
1964d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      SDValue CTOp = CTPOP.getOperand(0);
1965d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1966d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // (ctpop x) u< 2 -> (x & x-1) == 0
1967d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // (ctpop x) u> 1 -> (x & x-1) != 0
1968d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1969d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1970d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer                                  DAG.getConstant(1, CTVT));
1971d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1972d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1973d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1974d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      }
1975d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1976d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
1977d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    }
1978d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1979e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    // (zext x) == C --> x == (trunc C)
1980e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
1981e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1982e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      unsigned MinBits = N0.getValueSizeInBits();
1983e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      SDValue PreZExt;
1984e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1985e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // ZExt
1986e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        MinBits = N0->getOperand(0).getValueSizeInBits();
1987e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        PreZExt = N0->getOperand(0);
1988e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      } else if (N0->getOpcode() == ISD::AND) {
1989e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // DAGCombine turns costly ZExts into ANDs
1990e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1991e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          if ((C->getAPIntValue()+1).isPowerOf2()) {
1992e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer            MinBits = C->getAPIntValue().countTrailingOnes();
1993e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer            PreZExt = N0->getOperand(0);
1994e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          }
1995e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
1996e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // ZEXTLOAD
1997e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1998e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          MinBits = LN0->getMemoryVT().getSizeInBits();
1999e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          PreZExt = N0;
2000e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        }
2001e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      }
2002e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer
2003e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      // Make sure we're not loosing bits from the constant.
2004e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      if (MinBits < C1.getBitWidth() && MinBits > C1.getActiveBits()) {
2005e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
2006e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
2007e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          // Will get folded away.
2008e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
2009e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
2010e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          return DAG.getSetCC(dl, VT, Trunc, C, Cond);
2011e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        }
2012e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      }
2013e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    }
2014e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer
2015b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(and load, const)', the RHS is 0,
2016b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // the test is for equality or unsigned, and all 1 bits of the const are
2017b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // in the same partial word, see if we can shorten the load.
2018b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (DCI.isBeforeLegalize() &&
2019b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOpcode() == ISD::AND && C1 == 0 &&
2020b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getNode()->hasOneUse() &&
2021b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<LoadSDNode>(N0.getOperand(0)) &&
2022b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getNode()->hasOneUse() &&
2023b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<ConstantSDNode>(N0.getOperand(1))) {
2024b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
2025347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      APInt bestMask;
2026b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned bestWidth = 0, bestOffset = 0;
2027347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      if (!Lod->isVolatile() && Lod->isUnindexed()) {
2028b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned origWidth = N0.getValueType().getSizeInBits();
2029347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        unsigned maskWidth = origWidth;
2030bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
2031b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // 8 bits, but have to be careful...
2032b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
2033b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          origWidth = Lod->getMemoryVT().getSizeInBits();
2034347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        const APInt &Mask =
2035347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2036b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        for (unsigned width = origWidth / 2; width>=8; width /= 2) {
2037347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          APInt newMask = APInt::getLowBitsSet(maskWidth, width);
2038b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          for (unsigned offset=0; offset<origWidth/width; offset++) {
2039b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            if ((newMask & Mask) == Mask) {
2040b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              if (!TD->isLittleEndian())
2041b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (origWidth/width - offset - 1) * (width/8);
2042b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              else
2043b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (uint64_t)offset * (width/8);
2044347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng              bestMask = Mask.lshr(offset * (width/8) * 8);
2045b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              bestWidth = width;
2046b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              break;
204789217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen            }
2048b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            newMask = newMask << width;
204989217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen          }
205089217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
2051b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2052b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (bestWidth) {
2053c0c7fca2fedd2259569b7b84338259c62d38802dChris Lattner        EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
2054b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (newVT.isRound()) {
2055e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT PtrType = Lod->getOperand(1).getValueType();
2056b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Ptr = Lod->getBasePtr();
2057b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (bestOffset != 0)
2058b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
2059b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(bestOffset, PtrType));
2060b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
2061b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
2062ecf42c4720aba6ee315d0166045c54187ac2de4dChris Lattner                                Lod->getPointerInfo().getWithOffset(bestOffset),
20631e559443a17d1b335f697551c6263ba60d5dd827David Greene                                        false, false, NewAlign);
2064bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          return DAG.getSetCC(dl, VT,
2065b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getNode(ISD::AND, dl, newVT, NewLoad,
2066347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                      DAG.getConstant(bestMask.trunc(bestWidth),
2067347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                                      newVT)),
2068b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(0LL, newVT), Cond);
206989217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
207089217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen      }
2071b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2072d0ab34bf8feb7633b70158b571425577670b6326Bill Wendling
2073b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2074b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2075b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
2076fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2077b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // If the comparison constant has bits in the upper part, the
2078b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // zero-extended value could never match.
2079b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
2080b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                              C1.getBitWidth() - InSize))) {
2081fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        switch (Cond) {
2082fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGT:
2083fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGE:
2084b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETEQ: return DAG.getConstant(0, VT);
2085fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETULT:
2086b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETULE:
2087b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETNE: return DAG.getConstant(1, VT);
2088b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGT:
2089b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGE:
2090b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 is set.
2091b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNegative(), VT);
2092b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLT:
2093b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLE:
2094b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 isn't set.
2095b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNonNegative(), VT);
2096fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        default:
2097b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          break;
2098fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2099fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2100b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2101b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, we can perform the comparison with the low bits.
2102b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      switch (Cond) {
2103b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETEQ:
2104b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETNE:
2105b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGT:
2106b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGE:
2107b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULT:
2108b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULE: {
2109e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT newVT = N0.getOperand(0).getValueType();
2110b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DCI.isBeforeLegalizeOps() ||
2111b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            (isOperationLegal(ISD::SETCC, newVT) &&
2112b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              getCondCodeAction(Cond, newVT)==Legal))
2113b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, N0.getOperand(0),
211440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                              DAG.getConstant(C1.trunc(InSize), newVT),
2115b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond);
2116b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;
2117b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2118b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      default:
2119b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;   // todo, be more careful with signed comparisons
2120b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2121b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
21222c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng               (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2123e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2124b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
2125e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtDstTy = N0.getValueType();
2126b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
2127b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2128ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      // If the constant doesn't fit into the number of bits for the source of
2129ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      // the sign extension, it is impossible for both sides to be equal.
2130ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      if (C1.getMinSignedBits() > ExtSrcTyBits)
2131b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getConstant(Cond == ISD::SETNE, VT);
2132bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2133b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ZextOp;
2134e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT Op0Ty = N0.getOperand(0).getValueType();
2135b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (Op0Ty == ExtSrcTy) {
2136b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = N0.getOperand(0);
2137fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else {
2138b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
2139b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
2140b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(Imm, Op0Ty));
2141fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2142b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (!DCI.isCalledByLegalizer())
2143b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        DCI.AddToWorklist(ZextOp.getNode());
2144b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, make this a use of a zext.
2145bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, ZextOp,
2146b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1 & APInt::getLowBitsSet(
2147b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                              ExtDstTyBits,
2148bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                              ExtSrcTyBits),
2149b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                          ExtDstTy),
2150b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          Cond);
2151b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
2152b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2153b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // SETCC (SETCC), [0|1], [EQ|NE]  -> SETCC
21542c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      if (N0.getOpcode() == ISD::SETCC &&
21552c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
2156347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
2157b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (TrueWhenTrue)
2158bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
2159b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // Invert the condition.
2160b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
2161bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        CC = ISD::getSetCCInverse(CC,
2162b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  N0.getOperand(0).getValueType().isInteger());
2163b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
2164fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
21652c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
2166b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((N0.getOpcode() == ISD::XOR ||
2167bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck           (N0.getOpcode() == ISD::AND &&
2168b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(0).getOpcode() == ISD::XOR &&
2169b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2170b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          isa<ConstantSDNode>(N0.getOperand(1)) &&
2171b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
2172b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // If this is (X^1) == 0/1, swap the RHS and eliminate the xor.  We
2173b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // can only do this if the top bits are known zero.
2174b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned BitWidth = N0.getValueSizeInBits();
2175b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DAG.MaskedValueIsZero(N0,
2176b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  APInt::getHighBitsSet(BitWidth,
2177b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                        BitWidth-1))) {
2178b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Okay, get the un-inverted input value.
2179b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Val;
2180b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (N0.getOpcode() == ISD::XOR)
2181b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = N0.getOperand(0);
2182b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          else {
2183bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            assert(N0.getOpcode() == ISD::AND &&
2184b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                    N0.getOperand(0).getOpcode() == ISD::XOR);
2185b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            // ((X^1)&1)^1 -> X & 1
2186b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
2187b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(0).getOperand(0),
2188b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(1));
2189b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
21902c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
2191b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, Val, N1,
2192b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2193b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        }
21942c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      } else if (N1C->getAPIntValue() == 1 &&
21952c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                 (VT == MVT::i1 ||
219628b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands                  getBooleanContents(false) == ZeroOrOneBooleanContent)) {
21972c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        SDValue Op0 = N0;
21982c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if (Op0.getOpcode() == ISD::TRUNCATE)
21992c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Op0 = Op0.getOperand(0);
22002c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
22012c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if ((Op0.getOpcode() == ISD::XOR) &&
22022c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(0).getOpcode() == ISD::SETCC &&
22032c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(1).getOpcode() == ISD::SETCC) {
22042c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
22052c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
22062c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
22072c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond);
22082c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        } else if (Op0.getOpcode() == ISD::AND &&
22092c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                isa<ConstantSDNode>(Op0.getOperand(1)) &&
22102c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
22112c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
221217458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov          if (Op0.getValueType().bitsGT(VT))
22132c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0 = DAG.getNode(ISD::AND, dl, VT,
22142c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
22152c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getConstant(1, VT));
221617458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov          else if (Op0.getValueType().bitsLT(VT))
221717458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov            Op0 = DAG.getNode(ISD::AND, dl, VT,
221817458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov                        DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
221917458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov                        DAG.getConstant(1, VT));
222017458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov
22212c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0,
22222c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              DAG.getConstant(0, Op0.getValueType()),
22232c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
22242c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        }
2225fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2226b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2227bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2228b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    APInt MinVal, MaxVal;
2229b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
2230b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (ISD::isSignedIntSetCC(Cond)) {
2231b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getSignedMinValue(OperandBitSize);
2232b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getSignedMaxValue(OperandBitSize);
2233b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else {
2234b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getMinValue(OperandBitSize);
2235b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getMaxValue(OperandBitSize);
2236b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2237fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2238b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2239b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2240b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MinVal) return DAG.getConstant(1, VT);   // X >= MIN --> true
2241b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X >= C0 --> X > (C0-1)
2242bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2243b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1-1, N1.getValueType()),
2244b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2245b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2246fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2247b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2248b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MaxVal) return DAG.getConstant(1, VT);   // X <= MAX --> true
2249b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X <= C0 --> X < (C0+1)
2250bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2251b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1+1, N1.getValueType()),
2252b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2253b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2254b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2255b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2256b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X < MIN --> false
2257b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
2258b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X >= MIN --> true
2259b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
2260b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X > MAX --> false
2261b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
2262b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X <= MAX --> true
2263b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2264b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setgt X, Min --> setne X, Min
2265b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2266b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2267b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setlt X, Max --> setne X, Max
2268b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2269b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2270b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2271b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setult X, 1, turn it into seteq X, 0
2272b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2273bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2274bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                          DAG.getConstant(MinVal, N0.getValueType()),
2275b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
2276b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setugt X, Max-1, turn it into seteq X, Max
2277b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2278bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2279b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(MaxVal, N0.getValueType()),
2280b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
2281b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2282b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have "setcc X, C0", check to see if we can shrink the immediate
2283b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // by changing cc.
2284b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2285b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETUGT X, SINTMAX  -> SETLT X, 0
2286bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (Cond == ISD::SETUGT &&
2287b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMaxValue(OperandBitSize))
2288bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2289b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(0, N1.getValueType()),
2290b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETLT);
2291b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2292b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETULT X, SINTMIN  -> SETGT X, -1
2293b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETULT &&
2294b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMinValue(OperandBitSize)) {
2295b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ConstMinusOne =
2296b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
2297b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          N1.getValueType());
2298b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
2299b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2300b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2301b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Fold bit comparisons when we can.
2302b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2303d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        (VT == N0.getValueType() ||
2304d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng         (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
2305d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        N0.getOpcode() == ISD::AND)
2306b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (ConstantSDNode *AndRHS =
2307b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                  dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2308e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT ShiftTy = DCI.isBeforeLegalize() ?
230995771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson          getPointerTy() : getShiftAmountTy(N0.getValueType());
2310b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
2311b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if the AND RHS is a single bit.
2312347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          if (AndRHS->getAPIntValue().isPowerOf2()) {
2313d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2314d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                              DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2315347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                   DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
2316b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
2317347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
2318b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (X & 8) == 8  -->  (X & 8) >> 3
2319b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if C1 is a single bit.
2320b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (C1.isPowerOf2()) {
2321d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2322d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                               DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2323d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                                      DAG.getConstant(C1.logBase2(), ShiftTy)));
2324fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2325fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2326b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2327fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2328fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2329ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (isa<ConstantFPSDNode>(N0.getNode())) {
2330fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Constant fold or commute setcc.
2331ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
2332ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (O.getNode()) return O;
2333ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
233463079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // If the RHS of an FP comparison is a constant, simplify it away in
233563079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // some cases.
233663079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (CFP->getValueAPF().isNaN()) {
233763079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      // If an operand is known to be a nan, we can fold it.
233863079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      switch (ISD::getUnorderedFlavor(Cond)) {
2339c23197a26f34f559ea9797de51e187087c039c42Torok Edwin      default: llvm_unreachable("Unknown flavor!");
234063079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 0:  // Known false.
234163079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(0, VT);
234263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 1:  // Known true.
234363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(1, VT);
23441c3e1e2ed07597111f0b1b1bb93b8080c96c526bChris Lattner      case 2:  // Undefined.
2345e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen        return DAG.getUNDEF(VT);
234663079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      }
234763079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    }
2348bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
234963079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // Otherwise, we know the RHS is not a NaN.  Simplify the node to drop the
235063079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // constant if knowing that the operand is non-nan is enough.  We prefer to
235163079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
235263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // materialize 0.0.
235363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (Cond == ISD::SETO || Cond == ISD::SETUO)
2354ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N0, Cond);
235511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman
235611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // If the condition is not legal, see if we can find an equivalent one
235711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // which is legal.
235811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    if (!isCondCodeLegal(Cond, N0.getValueType())) {
235911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // If the comparison was an awkward floating-point == or != and one of
236011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // the comparison operands is infinity or negative infinity, convert the
236111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // condition to a less-awkward <= or >=.
236211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      if (CFP->getValueAPF().isInfinity()) {
236311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        if (CFP->getValueAPF().isNegative()) {
236411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
236511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
236611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
236711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
236811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
236911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
237011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
237111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
237211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
237311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
237411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
237511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
237611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        } else {
237711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
237811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
237911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
238011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
238111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
238211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
238311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
238411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
238511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
238611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
238711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
238811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
238911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        }
239011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      }
239111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    }
2392fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2393fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2394fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if (N0 == N1) {
2395fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // We can always fold X == X for integer setcc's.
239683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (N0.getValueType().isInteger())
2397fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2398fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    unsigned UOF = ISD::getUnorderedFlavor(Cond);
2399fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == 2)   // FP operators that are undefined on NaNs.
2400fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2401fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2402fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(UOF, VT);
2403fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
2404fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // if it is not already.
2405fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2406fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (NewCond != Cond)
2407ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2408fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2409fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2410fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
241183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      N0.getValueType().isInteger()) {
2412fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2413fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N0.getOpcode() == ISD::XOR) {
2414fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Y) == (X+Z) -->  Y == Z
2415fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOpcode() == N1.getOpcode()) {
2416fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(0) == N1.getOperand(0))
2417ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2418fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(1) == N1.getOperand(1))
2419ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2420fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2421fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // If X op Y == Y op X, try other combinations.
2422fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(0) == N1.getOperand(1))
2423bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2424ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2425fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(1) == N1.getOperand(0))
2426bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2427ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2428fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2429fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2430bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2431fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2432fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2433fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X+C1) == C2 --> X == C2-C1
2434ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2435ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return DAG.getSetCC(dl, VT, N0.getOperand(0),
2436f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                DAG.getConstant(RHSC->getAPIntValue()-
2437f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                                LHSR->getAPIntValue(),
2438fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                N0.getValueType()), Cond);
2439fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2440bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2441fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2442fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOpcode() == ISD::XOR)
2443fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // If we know that all of the inverted bits are zero, don't bother
2444fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // performing the inversion.
24452e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
24462e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman              return
2447ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                DAG.getSetCC(dl, VT, N0.getOperand(0),
24482e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             DAG.getConstant(LHSR->getAPIntValue() ^
24492e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                               RHSC->getAPIntValue(),
24502e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             N0.getValueType()),
24512e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             Cond);
2452fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2453bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2454fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        // Turn (C1-X) == C2 --> X == C1-C2
2455fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2456ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
24572e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            return
2458ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen              DAG.getSetCC(dl, VT, N0.getOperand(1),
24592e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           DAG.getConstant(SUBC->getAPIntValue() -
24602e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             RHSC->getAPIntValue(),
24612e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                           N0.getValueType()),
24622e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           Cond);
2463fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2464bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        }
2465fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2466fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2467fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Z) == X -->  Z == 0
2468fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(0) == N1)
2469ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N0.getOperand(1),
2470fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N0.getValueType()), Cond);
2471fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(1) == N1) {
2472fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode()))
2473ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0),
2474fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N0.getValueType()), Cond);
2475ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        else if (N0.getNode()->hasOneUse()) {
2476fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2477fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // (Z-X) == X  --> Z == X<<1
2478ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
2479bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                     N1,
248095771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                       DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
2481fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2482ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2483ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2484fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2485fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2486fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2487fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2488fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2489fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N1.getOpcode() == ISD::XOR) {
2490fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify  X == (X+Z) -->  Z == 0
2491fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N1.getOperand(0) == N0) {
2492ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N1.getOperand(1),
2493fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N1.getValueType()), Cond);
2494fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else if (N1.getOperand(1) == N0) {
2495fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N1.getOpcode())) {
2496ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1.getOperand(0),
2497fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N1.getValueType()), Cond);
2498ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        } else if (N1.getNode()->hasOneUse()) {
2499fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2500fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // X == (Z-X)  --> X<<1 == Z
2501bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
250295771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                       DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
2503fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2504ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2505ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2506fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2507fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2508fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2509e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
25102c65c3dfe6589a630d1bdde085aec1b9cdc43ea8Dan Gohman    // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
251185b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // Note that where y is variable and is known to have at most
251285b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // one bit set (for example, if it is z&1) we cannot do this;
251385b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // the expressions are not equivalent when y==0.
2514e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N0.getOpcode() == ISD::AND)
2515e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
251685b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N1, DAG)) {
2517e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2518e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N1.getValueType());
2519ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0, Zero, Cond);
2520e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2521e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2522e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N1.getOpcode() == ISD::AND)
2523e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
252485b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N0, DAG)) {
2525e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2526e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N0.getValueType());
2527ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1, Zero, Cond);
2528e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2529e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2530fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2531fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2532fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Fold away ALL boolean setcc's.
2533475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Temp;
2534825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (N0.getValueType() == MVT::i1 && foldBooleans) {
2535fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    switch (Cond) {
2536c23197a26f34f559ea9797de51e187087c039c42Torok Edwin    default: llvm_unreachable("Unknown integer setcc!");
25374c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETEQ:  // X == Y  -> ~(X^Y)
2538825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2539825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNOT(dl, Temp, MVT::i1);
2540fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2541ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2542fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2543fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    case ISD::SETNE:  // X != Y   -->  (X^Y)
2544825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2545fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25464c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGT:  // X >s Y   -->  X == 0 & Y == 1  -->  ~X & Y
25474c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULT: // X <u Y   -->  X == 0 & Y == 1  -->  ~X & Y
2548825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2549825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2550fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2551ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2552fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25534c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLT:  // X <s Y   --> X == 1 & Y == 0  -->  ~Y & X
25544c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGT: // X >u Y   --> X == 1 & Y == 0  -->  ~Y & X
2555825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2556825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2557fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2558ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2559fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25604c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULE: // X <=u Y  --> X == 0 | Y == 1  -->  ~X | Y
25614c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGE:  // X >=s Y  --> X == 0 | Y == 1  -->  ~X | Y
2562825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2563825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2564fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2565ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2566fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25674c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGE: // X >=u Y  --> X == 1 | Y == 0  -->  ~Y | X
25684c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLE:  // X <=s Y  --> X == 1 | Y == 0  -->  ~Y | X
2569825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2570825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2571fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2572fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2573825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (VT != MVT::i1) {
2574fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2575ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(N0.getNode());
2576fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // FIXME: If running after legalize, we probably can't do this.
2577ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2578fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2579fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    return N0;
2580fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2581fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2582fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Could not fold it.
2583475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
2584fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng}
2585fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2586ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2587ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// node is a GlobalAddress + offset.
25880a9481f44fe4fc76e59109992940a76b2a3f9b3bChris Lattnerbool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
2589ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng                                    int64_t &Offset) const {
2590ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (isa<GlobalAddressSDNode>(N)) {
25919ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
25929ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GA = GASD->getGlobal();
25939ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    Offset += GASD->getOffset();
2594ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    return true;
2595ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
2596ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2597ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (N->getOpcode() == ISD::ADD) {
2598475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N1 = N->getOperand(0);
2599475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N2 = N->getOperand(1);
2600ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2601ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2602ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
26037810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2604ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2605ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2606ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2607ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2608ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
26097810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2610ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2611ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2612ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    }
2613ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
261495771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson
2615ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  return false;
2616ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng}
2617ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2618ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2619475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::
262000ffed0468ad406062b7c08c2ff46d79d2d1be4dChris LattnerPerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
262100ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner  // Default implementation: no optimization.
2622475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
262300ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner}
262400ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner
2625eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2626eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Inline Assembler Implementation Methods
2627eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2628eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
26294376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
2630eb8146b5ee4b9b66d6294f62a5ed556e332018abChris LattnerTargetLowering::ConstraintType
26314234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris LattnerTargetLowering::getConstraintType(const std::string &Constraint) const {
26324234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  if (Constraint.size() == 1) {
26334234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    switch (Constraint[0]) {
26344234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    default: break;
26354234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'r': return C_RegisterClass;
26364234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'm':    // memory
26374234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'o':    // offsetable
26384234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'V':    // not offsetable
26394234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Memory;
26404234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'i':    // Simple Integer or Relocatable Constant
26414234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'n':    // Simple Integer
264267aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'E':    // Floating Point Constant
264367aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'F':    // Floating Point Constant
26444234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 's':    // Relocatable Constant
264567aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'p':    // Address.
2646c13dd1cf4c0d83ac3ed2a6b0c36fab72e9d6c6e5Chris Lattner    case 'X':    // Allow ANY value.
26474234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'I':    // Target registers.
26484234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'J':
26494234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'K':
26504234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'L':
26514234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'M':
26524234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'N':
26534234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'O':
26544234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'P':
265567aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case '<':
265667aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case '>':
26574234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Other;
26584234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    }
2659eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
2660bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2661bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (Constraint.size() > 1 && Constraint[0] == '{' &&
2662065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner      Constraint[Constraint.size()-1] == '}')
2663065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner    return C_Register;
26644234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  return C_Unknown;
2665eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2666eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
2667ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// LowerXConstraint - try to replace an X constraint, which matches anything,
2668ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// with another that has more specific requirements based on the type of the
2669ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// corresponding operand.
2670e50ed30282bb5b4a9ed952580523f2dda16215acOwen Andersonconst char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
267183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isInteger())
26725e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "r";
267383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isFloatingPoint())
26745e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "f";      // works for many targets
26755e764233f398b6929b67701672a5e78fec20ce2eChris Lattner  return 0;
2676ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen}
2677ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen
267848884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
267948884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// vector.  If it is invalid, don't add anything to Ops.
2680475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2681100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher                                                  std::string &Constraint,
2682475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                  std::vector<SDValue> &Ops,
26835e764233f398b6929b67701672a5e78fec20ce2eChris Lattner                                                  SelectionDAG &DAG) const {
2684362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2685100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher  if (Constraint.length() > 1) return;
2686362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2687100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher  char ConstraintLetter = Constraint[0];
2688eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  switch (ConstraintLetter) {
26899ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner  default: break;
2690eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 'X':     // Allows any operand; labels (basic block) use this.
2691eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    if (Op.getOpcode() == ISD::BasicBlock) {
2692eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      Ops.push_back(Op);
2693eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      return;
2694eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    }
2695eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    // fall through
2696eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'i':    // Simple Integer or Relocatable Constant
2697eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'n':    // Simple Integer
2698eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 's': {  // Relocatable Constant
269975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // These operands are interested in values of the form (GV+C), where C may
270075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // be folded in as an offset of GV, or it may be explicitly added.  Also, it
270175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // is possible and fine if either GV or C are missing.
270275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
270375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2704bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
270575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we have "(add GV, C)", pull out GV/C
270675c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (Op.getOpcode() == ISD::ADD) {
270775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
270875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
270975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0) {
271075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
271175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
271275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
271375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0)
271475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = 0, GA = 0;
271575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
2716bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
271775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we find a valid operand, map to the TargetXXX version so that the
271875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // value itself doesn't get selected.
271975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (GA) {   // Either &GV   or   &GV+C
272075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (ConstraintLetter != 'n') {
272175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        int64_t Offs = GA->getOffset();
2722f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman        if (C) Offs += C->getZExtValue();
2723bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
272407538ad975994123317fe5b32daed4bf28683a1dDevang Patel                                                 C ? C->getDebugLoc() : DebugLoc(),
272548884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner                                                 Op.getValueType(), Offs));
272648884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
272775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
272875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
272975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (C) {   // just C, no GV.
27309ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner      // Simple constants are not allowed for 's'.
273148884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      if (ConstraintLetter != 's') {
273278e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // gcc prints these as sign extended.  Sign extend value to 64 bits
273378e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // now; without this it would get ZExt'd later in
273478e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // ScheduleDAGSDNodes::EmitNode, which is very generic.
273578e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
2736825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                                            MVT::i64));
273748884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
273848884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      }
27399ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    }
27409ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    break;
2741eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
274275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner  }
2743eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2744eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
27451efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattnerstd::pair<unsigned, const TargetRegisterClass*> TargetLowering::
27464217ca8dc175f7268a4335c8406dedd901e8e631Chris LattnergetRegForInlineAsmConstraint(const std::string &Constraint,
2747e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                             EVT VT) const {
27481efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  if (Constraint[0] != '{')
27497d9663c70b3300070298d716dba6e6f6ce2d1e3eDouglas Gregor    return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
2750a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2751a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner
2752a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  // Remove the braces from around the name.
275305872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer  StringRef RegName(Constraint.data()+1, Constraint.size()-2);
27541efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
27551efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  // Figure out which register class contains this reg.
27566f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  const TargetRegisterInfo *RI = TM.getRegisterInfo();
27576f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
27581efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner       E = RI->regclass_end(); RCI != E; ++RCI) {
27591efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    const TargetRegisterClass *RC = *RCI;
2760bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2761bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    // If none of the value types for this register class are valid, we
2762b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    // can't use it.  For example, 64-bit reg classes on 32-bit targets.
276322e8a366adf5a4c78148928ff64e7e00c1088492Jakob Stoklund Olesen    if (!isLegalRC(RC))
276422e8a366adf5a4c78148928ff64e7e00c1088492Jakob Stoklund Olesen      continue;
2765bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2766bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
27671efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner         I != E; ++I) {
276805872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer      if (RegName.equals_lower(RI->getName(*I)))
27691efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        return std::make_pair(*I, RC);
27701efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    }
27714ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner  }
2772bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
27737d9663c70b3300070298d716dba6e6f6ce2d1e3eDouglas Gregor  return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
27744ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner}
277530b37b5f29991874648d839d018aa2921b39355fEvan Cheng
277630b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
27774376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner// Constraint Selection.
27784376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
27796bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// isMatchingInputConstraint - Return true of this is an input operand that is
27806bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// a matching constraint like "4".
27816bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattnerbool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
278258f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
278358f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return isdigit(ConstraintCode[0]);
278458f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
278558f15c482a7129c78ca809792b46befa20ea337dChris Lattner
278658f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// getMatchedOperand - If this is an input matching constraint, this method
278758f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// returns the output operand it matches.
278858f15c482a7129c78ca809792b46befa20ea337dChris Lattnerunsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
278958f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
279058f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return atoi(ConstraintCode.c_str());
279158f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
279258f15c482a7129c78ca809792b46befa20ea337dChris Lattner
2793bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2794eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// ParseConstraints - Split up the constraint string from the inline
2795eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// assembly value into the specific constraints and their prefixes,
2796eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and also tie in the associated operand values.
2797eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// If this returns an empty vector, and if the constraint string itself
2798eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// isn't empty, there was an error parsing.
279944ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
2800eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ImmutableCallSite CS) const {
2801eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  /// ConstraintOperands - Information about all of the constraints.
280244ab89eb376af838d1123293a79975aede501464John Thompson  AsmOperandInfoVector ConstraintOperands;
2803eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
280467aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  unsigned maCount = 0; // Largest number of multiple alternative constraints.
2805eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2806eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Do a prepass over the constraints, canonicalizing them, and building up the
2807eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // ConstraintOperands list.
280844ab89eb376af838d1123293a79975aede501464John Thompson  InlineAsm::ConstraintInfoVector
2809eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ConstraintInfos = IA->ParseConstraints();
2810bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2811eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
2812eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  unsigned ResNo = 0;   // ResNo - The result number of the next output.
2813eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2814eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2815eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2816eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &OpInfo = ConstraintOperands.back();
2817eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
281867aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    // Update multiple alternative constraint count.
281967aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    if (OpInfo.multipleAlternatives.size() > maCount)
282067aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson      maCount = OpInfo.multipleAlternatives.size();
282167aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson
282244ab89eb376af838d1123293a79975aede501464John Thompson    OpInfo.ConstraintVT = MVT::Other;
2823eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2824eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // Compute the value type for each operand.
2825eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    switch (OpInfo.Type) {
2826eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isOutput:
2827eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Indirect outputs just consume an argument.
2828eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      if (OpInfo.isIndirect) {
2829eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2830eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        break;
2831eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2832eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2833eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // The return value of the call is this value.  As such, there is no
2834eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // corresponding argument.
2835eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      assert(!CS.getType()->isVoidTy() &&
2836eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson             "Bad inline asm!");
2837db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
283844ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = getValueType(STy->getElementType(ResNo));
2839eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      } else {
2840eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        assert(ResNo == 0 && "Asm only has one result!");
284144ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = getValueType(CS.getType());
2842eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2843eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      ++ResNo;
2844eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2845eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isInput:
2846eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2847eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2848eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isClobber:
2849eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Nothing to do.
2850eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2851eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2852bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
285344ab89eb376af838d1123293a79975aede501464John Thompson    if (OpInfo.CallOperandVal) {
2854db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
285544ab89eb376af838d1123293a79975aede501464John Thompson      if (OpInfo.isIndirect) {
2856db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner        llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
285744ab89eb376af838d1123293a79975aede501464John Thompson        if (!PtrTy)
285844ab89eb376af838d1123293a79975aede501464John Thompson          report_fatal_error("Indirect operand for inline asm not a pointer!");
285944ab89eb376af838d1123293a79975aede501464John Thompson        OpTy = PtrTy->getElementType();
286044ab89eb376af838d1123293a79975aede501464John Thompson      }
2861362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2862cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher      // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
2863db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      if (StructType *STy = dyn_cast<StructType>(OpTy))
2864cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher        if (STy->getNumElements() == 1)
2865cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher          OpTy = STy->getElementType(0);
2866cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher
286744ab89eb376af838d1123293a79975aede501464John Thompson      // If OpTy is not a single value, it may be a struct/union that we
286844ab89eb376af838d1123293a79975aede501464John Thompson      // can tile with integers.
286944ab89eb376af838d1123293a79975aede501464John Thompson      if (!OpTy->isSingleValueType() && OpTy->isSized()) {
287044ab89eb376af838d1123293a79975aede501464John Thompson        unsigned BitSize = TD->getTypeSizeInBits(OpTy);
287144ab89eb376af838d1123293a79975aede501464John Thompson        switch (BitSize) {
287244ab89eb376af838d1123293a79975aede501464John Thompson        default: break;
287344ab89eb376af838d1123293a79975aede501464John Thompson        case 1:
287444ab89eb376af838d1123293a79975aede501464John Thompson        case 8:
287544ab89eb376af838d1123293a79975aede501464John Thompson        case 16:
287644ab89eb376af838d1123293a79975aede501464John Thompson        case 32:
287744ab89eb376af838d1123293a79975aede501464John Thompson        case 64:
287844ab89eb376af838d1123293a79975aede501464John Thompson        case 128:
287971365d3774a6c02f3f198fbf08a56e4b6346bbccDale Johannesen          OpInfo.ConstraintVT =
288071365d3774a6c02f3f198fbf08a56e4b6346bbccDale Johannesen              EVT::getEVT(IntegerType::get(OpTy->getContext(), BitSize), true);
288144ab89eb376af838d1123293a79975aede501464John Thompson          break;
288244ab89eb376af838d1123293a79975aede501464John Thompson        }
288344ab89eb376af838d1123293a79975aede501464John Thompson      } else if (dyn_cast<PointerType>(OpTy)) {
288444ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = MVT::getIntegerVT(8*TD->getPointerSize());
288544ab89eb376af838d1123293a79975aede501464John Thompson      } else {
288644ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = EVT::getEVT(OpTy, true);
288744ab89eb376af838d1123293a79975aede501464John Thompson      }
288844ab89eb376af838d1123293a79975aede501464John Thompson    }
2889eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
2890eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2891eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // If we have multiple alternative constraints, select the best alternative.
2892eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  if (ConstraintInfos.size()) {
2893eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (maCount) {
2894eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      unsigned bestMAIndex = 0;
2895eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      int bestWeight = -1;
2896eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // weight:  -1 = invalid match, and 0 = so-so match to 5 = good match.
2897eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      int weight = -1;
2898eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      unsigned maIndex;
2899eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Compute the sums of the weights for each alternative, keeping track
2900eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // of the best (highest weight) one so far.
2901eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      for (maIndex = 0; maIndex < maCount; ++maIndex) {
2902eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        int weightSum = 0;
2903eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2904eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            cIndex != eIndex; ++cIndex) {
2905eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2906eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (OpInfo.Type == InlineAsm::isClobber)
2907eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            continue;
2908eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
290944ab89eb376af838d1123293a79975aede501464John Thompson          // If this is an output operand with a matching input operand,
291044ab89eb376af838d1123293a79975aede501464John Thompson          // look up the matching input. If their types mismatch, e.g. one
291144ab89eb376af838d1123293a79975aede501464John Thompson          // is an integer, the other is floating point, or their sizes are
291244ab89eb376af838d1123293a79975aede501464John Thompson          // different, flag it as an maCantMatch.
2913eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (OpInfo.hasMatchingInput()) {
2914eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
2915eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2916eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson              if ((OpInfo.ConstraintVT.isInteger() !=
2917eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                   Input.ConstraintVT.isInteger()) ||
2918eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                  (OpInfo.ConstraintVT.getSizeInBits() !=
2919eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                   Input.ConstraintVT.getSizeInBits())) {
2920eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                weightSum = -1;  // Can't match.
2921eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                break;
2922eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson              }
2923eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            }
2924eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          }
2925eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2926eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (weight == -1) {
2927eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            weightSum = -1;
2928eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            break;
2929eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          }
2930eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          weightSum += weight;
2931eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2932eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        // Update best.
2933eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if (weightSum > bestWeight) {
2934eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          bestWeight = weightSum;
2935eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          bestMAIndex = maIndex;
2936eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2937eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2938eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2939eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Now select chosen alternative in each constraint.
2940eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2941eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          cIndex != eIndex; ++cIndex) {
2942eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2943eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if (cInfo.Type == InlineAsm::isClobber)
2944eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          continue;
2945eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        cInfo.selectAlternative(bestMAIndex);
2946eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2947eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2948eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
2949eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2950eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Check and hook up tied operands, choose constraint code to use.
2951eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2952eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      cIndex != eIndex; ++cIndex) {
2953eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2954bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2955eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // If this is an output operand with a matching input operand, look up the
2956eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // matching input. If their types mismatch, e.g. one is an integer, the
2957eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // other is floating point, or their sizes are different, flag it as an
2958eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // error.
2959eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (OpInfo.hasMatchingInput()) {
2960eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
296144ab89eb376af838d1123293a79975aede501464John Thompson
2962eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      if (OpInfo.ConstraintVT != Input.ConstraintVT) {
29635427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	std::pair<unsigned, const TargetRegisterClass*> MatchRC =
29645427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	  getRegForInlineAsmConstraint(OpInfo.ConstraintCode, OpInfo.ConstraintVT);
29655427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	std::pair<unsigned, const TargetRegisterClass*> InputRC =
29665427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	  getRegForInlineAsmConstraint(Input.ConstraintCode, Input.ConstraintVT);
2967eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if ((OpInfo.ConstraintVT.isInteger() !=
2968eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson             Input.ConstraintVT.isInteger()) ||
29695427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher            (MatchRC.second != InputRC.second)) {
2970eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          report_fatal_error("Unsupported asm: input constraint"
2971eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                             " with a matching output constraint of"
2972eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                             " incompatible type!");
2973eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2974eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
297544ab89eb376af838d1123293a79975aede501464John Thompson
2976eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2977eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
2978eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2979eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return ConstraintOperands;
2980eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
2981eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
298258f15c482a7129c78ca809792b46befa20ea337dChris Lattner
29834376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// getConstraintGenerality - Return an integer indicating how general CT
29844376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is.
29854376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
29864376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  switch (CT) {
2987c23197a26f34f559ea9797de51e187087c039c42Torok Edwin  default: llvm_unreachable("Unknown constraint type!");
29884376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Other:
29894376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Unknown:
29904376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 0;
29914376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Register:
29924376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 1;
29934376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_RegisterClass:
29944376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 2;
29954376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Memory:
29964376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 3;
29974376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
29984376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
29994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
300044ab89eb376af838d1123293a79975aede501464John Thompson/// Examine constraint type and operand type and determine a weight value.
3001eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// This object must already have been set up with the operand type
3002eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and the current alternative constraint selected.
300344ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::ConstraintWeight
300444ab89eb376af838d1123293a79975aede501464John Thompson  TargetLowering::getMultipleConstraintMatchWeight(
3005eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &info, int maIndex) const {
300644ab89eb376af838d1123293a79975aede501464John Thompson  InlineAsm::ConstraintCodeVector *rCodes;
300767aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  if (maIndex >= (int)info.multipleAlternatives.size())
300867aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    rCodes = &info.Codes;
300967aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  else
301067aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    rCodes = &info.multipleAlternatives[maIndex].Codes;
301144ab89eb376af838d1123293a79975aede501464John Thompson  ConstraintWeight BestWeight = CW_Invalid;
3012eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
3013eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Loop over the options, keeping track of the most general one.
301467aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
301544ab89eb376af838d1123293a79975aede501464John Thompson    ConstraintWeight weight =
301644ab89eb376af838d1123293a79975aede501464John Thompson      getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
3017eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (weight > BestWeight)
3018eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      BestWeight = weight;
3019eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
3020eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
3021eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return BestWeight;
3022eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
3023eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
302444ab89eb376af838d1123293a79975aede501464John Thompson/// Examine constraint type and operand type and determine a weight value.
3025eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// This object must already have been set up with the operand type
3026eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and the current alternative constraint selected.
302744ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::ConstraintWeight
302844ab89eb376af838d1123293a79975aede501464John Thompson  TargetLowering::getSingleConstraintMatchWeight(
3029eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &info, const char *constraint) const {
303044ab89eb376af838d1123293a79975aede501464John Thompson  ConstraintWeight weight = CW_Invalid;
3031eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  Value *CallOperandVal = info.CallOperandVal;
3032eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // If we don't have a value, we can't do a match,
3033eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // but allow it at the lowest weight.
3034eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  if (CallOperandVal == NULL)
303544ab89eb376af838d1123293a79975aede501464John Thompson    return CW_Default;
3036eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Look at the constraint type.
3037eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  switch (*constraint) {
3038eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'i': // immediate integer.
3039eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'n': // immediate integer with a known value.
304044ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<ConstantInt>(CallOperandVal))
304144ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
3042eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
3043eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 's': // non-explicit intregal immediate.
304444ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<GlobalValue>(CallOperandVal))
304544ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
304644ab89eb376af838d1123293a79975aede501464John Thompson      break;
304744ab89eb376af838d1123293a79975aede501464John Thompson    case 'E': // immediate float if host format.
304844ab89eb376af838d1123293a79975aede501464John Thompson    case 'F': // immediate float.
304944ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<ConstantFP>(CallOperandVal))
305044ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
3051eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
305244ab89eb376af838d1123293a79975aede501464John Thompson    case '<': // memory operand with autodecrement.
305344ab89eb376af838d1123293a79975aede501464John Thompson    case '>': // memory operand with autoincrement.
3054eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'm': // memory operand.
3055eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'o': // offsettable memory operand
3056eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'V': // non-offsettable memory operand
305744ab89eb376af838d1123293a79975aede501464John Thompson      weight = CW_Memory;
3058eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
305944ab89eb376af838d1123293a79975aede501464John Thompson    case 'r': // general register.
3060eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'g': // general register, memory operand or immediate integer.
306144ab89eb376af838d1123293a79975aede501464John Thompson              // note: Clang converts "g" to "imr".
306244ab89eb376af838d1123293a79975aede501464John Thompson      if (CallOperandVal->getType()->isIntegerTy())
306344ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Register;
3064eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
306544ab89eb376af838d1123293a79975aede501464John Thompson    case 'X': // any operand.
3066eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    default:
306744ab89eb376af838d1123293a79975aede501464John Thompson      weight = CW_Default;
3068eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
3069eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
3070eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return weight;
3071eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
3072eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
30734376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ChooseConstraint - If there are multiple different constraints that we
30744376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
307524e1a9d3115a5bf4e1c21092ede0f6dfc0a810adChris Lattner/// This is somewhat tricky: constraints fall into four classes:
30764376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Other         -> immediates and magic values
30774376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Register      -> one specific register
30784376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    RegisterClass -> a group of regs
30794376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Memory        -> memory
30804376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// Ideally, we would pick the most specific constraint possible: if we have
30814376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// something that fits into a register, we would pick it.  The problem here
30824376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is that if we have something that could either be in a register or in
30834376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// memory that use of the register could cause selection of *other*
30844376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// operands to fail: they might only succeed if we pick memory.  Because of
30854376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// this the heuristic we use is:
30864376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
30874376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  1) If there is an 'other' constraint, and if the operand is valid for
30884376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     that constraint, use it.  This makes us take advantage of 'i'
30894376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     constraints when available.
30904376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  2) Otherwise, pick the most general constraint present.  This prefers
30914376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     'm' over 'r', for example.
30924376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
30934376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
30941784d160e4efa75782884d451d0788b9457e67dcDale Johannesen                             const TargetLowering &TLI,
3095475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                             SDValue Op, SelectionDAG *DAG) {
30964376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
30974376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  unsigned BestIdx = 0;
30984376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
30994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  int BestGenerality = -1;
3100a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen
31014376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Loop over the options, keeping track of the most general one.
31024376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
31034376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    TargetLowering::ConstraintType CType =
31044376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      TLI.getConstraintType(OpInfo.Codes[i]);
3105a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen
31065a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // If this is an 'other' constraint, see if the operand is valid for it.
31075a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // For example, on X86 we might have an 'rI' constraint.  If the operand
31085a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // is an integer in the range [0..31] we want to use I (saving a load
31095a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // of a register), otherwise we must use 'r'.
3110ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (CType == TargetLowering::C_Other && Op.getNode()) {
31115a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      assert(OpInfo.Codes[i].size() == 1 &&
31125a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner             "Unhandled multi-letter 'other' constraint");
3113475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      std::vector<SDValue> ResultOps;
3114100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher      TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
31155a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                       ResultOps, *DAG);
31165a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      if (!ResultOps.empty()) {
31175a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestType = CType;
31185a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestIdx = i;
31195a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        break;
31205a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      }
31215a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    }
3122bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3123a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    // Things with matching constraints can only be registers, per gcc
3124a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    // documentation.  This mainly affects "g" constraints.
3125a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
3126a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen      continue;
3127bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31284376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // This constraint letter is more general than the previous one, use it.
31294376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    int Generality = getConstraintGenerality(CType);
31304376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (Generality > BestGenerality) {
31314376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestType = CType;
31324376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestIdx = i;
31334376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestGenerality = Generality;
31344376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
31354376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
3136bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31374376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
31384376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintType = BestType;
31394376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
31404376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
31414376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ComputeConstraintToUse - Determines the constraint code and constraint
31424376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// type to use for the specific AsmOperandInfo, setting
31434376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
31445a09690446a36f94f990db7d18d9b9ac8587888aChris Lattnervoid TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
3145bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                            SDValue Op,
31465a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                            SelectionDAG *DAG) const {
31474376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
3148bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31494376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Single-letter constraints ('r') are very common.
31504376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.Codes.size() == 1) {
31514376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintCode = OpInfo.Codes[0];
31524376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
31534376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  } else {
31541784d160e4efa75782884d451d0788b9457e67dcDale Johannesen    ChooseConstraint(OpInfo, *this, Op, DAG);
31554376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
3156bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31574376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // 'X' matches anything.
31584376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
31594376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Labels and constants are handled elsewhere ('X' is the only thing
31608ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    // that matches labels).  For Functions, the type here is the type of
31615339c551b508d8231672b417d9df1629e42af89bDale Johannesen    // the result, which is not what we want to look at; leave them alone.
31625339c551b508d8231672b417d9df1629e42af89bDale Johannesen    Value *v = OpInfo.CallOperandVal;
31638ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
31648ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen      OpInfo.CallOperandVal = v;
31654376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      return;
31668ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    }
3167bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31684376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Otherwise, try to resolve it to something we know about by looking at
31694376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // the actual operand type.
31704376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
31714376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintCode = Repl;
31724376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
31734376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
31744376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
31754376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
31764376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
31774376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner//===----------------------------------------------------------------------===//
317830b37b5f29991874648d839d018aa2921b39355fEvan Cheng//  Loop Strength Reduction hooks
317930b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
318030b37b5f29991874648d839d018aa2921b39355fEvan Cheng
31811436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// isLegalAddressingMode - Return true if the addressing mode represented
31821436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// by AM is legal for this target, for a load/store of the specified type.
3183bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckbool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
3184db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                           Type *Ty) const {
31851436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // The default implementation of this implements a conservative RISCy, r+r and
31861436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // r+i addr mode.
31871436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
31881436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // Allows a sign-extended 16-bit immediate field.
31891436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
31901436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
3191bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31921436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // No global is ever allowed as a base.
31931436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseGV)
31941436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
3195bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3196bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // Only support r+r,
31971436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  switch (AM.Scale) {
31981436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 0:  // "r+i" or just "i", depending on HasBaseReg.
31991436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32001436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 1:
32011436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
32021436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
32031436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Otherwise we have r+r or r+i.
32041436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32051436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 2:
32061436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
32071436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
32081436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Allow 2*r as r+r.
32091436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32101436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  }
3211bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
32121436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  return true;
32131436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner}
32141436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
32159c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer/// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
32169c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer/// with the multiplicative inverse of the constant.
32179c64030445cbe6ac486b90c5f459f91e06770474Benjamin KramerSDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
32189c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer                                       SelectionDAG &DAG) const {
32199c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  ConstantSDNode *C = cast<ConstantSDNode>(Op2);
32209c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  APInt d = C->getAPIntValue();
32219c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  assert(d != 0 && "Division by zero!");
32229c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32239c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  // Shift the value upfront if it is even, so the LSB is one.
32249c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  unsigned ShAmt = d.countTrailingZeros();
32259c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  if (ShAmt) {
32269c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    // TODO: For UDIV use SRL instead of SRA.
32279c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
32289c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
32299c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    d = d.ashr(ShAmt);
32309c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  }
32319c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32329c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  // Calculate the multiplicative inverse, using Newton's method.
32339c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  APInt t, xn = d;
32349c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  while ((t = d*xn) != 1)
32359c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    xn *= APInt(d.getBitWidth(), 2) - t;
32369c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32379c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  Op2 = DAG.getConstant(xn, Op1.getValueType());
32389c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
32399c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer}
32409c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
3241dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3242dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
3243dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
3244dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3245bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley PeckSDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
3246475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                  std::vector<SDNode*>* Created) const {
3247e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
3248ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl= N->getDebugLoc();
3249bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3250dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
3251fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should be more aggressive here.
3252fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (!isTypeLegal(VT))
3253fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman    return SDValue();
3254bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3255fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
32564e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt::ms magics = d.magic();
3257bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3258dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
3259fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should support doing a MUL in a wider type
3260475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Q;
3261f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  if (isOperationLegalOrCustom(ISD::MULHS, VT))
3262ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
3263525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                    DAG.getConstant(magics.m, VT));
3264f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
3265ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
3266525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                              N->getOperand(0),
3267ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif                              DAG.getConstant(magics.m, VT)).getNode(), 1);
3268525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
3269475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhs or equvialent
3270dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d > 0 and m < 0, add the numerator
3271bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (d.isStrictlyPositive() && magics.m.isNegative()) {
3272ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
3273dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3274ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3275dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3276dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d < 0 and m > 0, subtract the numerator.
3277fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (d.isNegative() && magics.m.isStrictlyPositive()) {
3278ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
3279dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3280ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3281dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3282dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Shift right algebraic if shift value is nonzero
3283dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.s > 0) {
3284bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    Q = DAG.getNode(ISD::SRA, dl, VT, Q,
328595771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3286dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3287ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3288dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3289dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Extract the sign bit and add it to the quotient
3290475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue T =
3291ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
329295771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                                           getShiftAmountTy(Q.getValueType())));
3293dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
3294ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(T.getNode());
3295ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  return DAG.getNode(ISD::ADD, dl, VT, Q, T);
3296dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
3297dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
3298dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3299dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
3300dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
3301dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3302475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
3303475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                  std::vector<SDNode*>* Created) const {
3304e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
3305ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl = N->getDebugLoc();
3306201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3307dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
3308201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should be more aggressive here.
3309201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  if (!isTypeLegal(VT))
3310201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman    return SDValue();
3311201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3312201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should use a narrower constant when the upper
3313201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // bits are known to be zero.
33141c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
33151c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  APInt::mu magics = N1C.magicu();
33161c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33171c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  SDValue Q = N->getOperand(0);
33181c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33191c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  // If the divisor is even, we can avoid using the expensive fixup by shifting
33201c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  // the divided value upfront.
33211c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  if (magics.a != 0 && !N1C[0]) {
33221c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    unsigned Shift = N1C.countTrailingZeros();
33231c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = DAG.getNode(ISD::SRL, dl, VT, Q,
33241c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer                    DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
33251c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    if (Created)
33261c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer      Created->push_back(Q.getNode());
33271c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33281c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    // Get magic number for the shifted divisor.
33291c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    magics = N1C.lshr(Shift).magicu(Shift);
33301c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    assert(magics.a == 0 && "Should use cheap fixup now");
33311c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  }
3332201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3333dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
3334201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should support doing a MUL in a wider type
3335f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  if (isOperationLegalOrCustom(ISD::MULHU, VT))
33361c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
3337f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
33381c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
33391c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer                            DAG.getConstant(magics.m, VT)).getNode(), 1);
3340525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
3341475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhu or equvialent
3342dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
3343ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(Q.getNode());
3344dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
3345dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.a == 0) {
33461c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    assert(magics.s < N1C.getBitWidth() &&
3347201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman           "We shouldn't generate an undefined shift!");
3348bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    return DAG.getNode(ISD::SRL, dl, VT, Q,
334995771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3350dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  } else {
3351ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
3352dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3353ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3354bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
335595771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                      DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
3356dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3357ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3358ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
3359dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3360ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3361bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    return DAG.getNode(ISD::SRL, dl, VT, NPQ,
336295771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson             DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
3363dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3364dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
3365