TargetLowering.cpp revision e3376ecd504300ae529c902135f51baffbdc2824
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.
575e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
576825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
577825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
578825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
579310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
5800bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen  // These library functions default to expand.
581e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG ,  MVT::f16, Expand);
582e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG2,  MVT::f16, Expand);
583e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG10, MVT::f16, Expand);
584e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FEXP ,  MVT::f16, Expand);
585e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FEXP2,  MVT::f16, Expand);
586e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FFLOOR, MVT::f16, Expand);
587e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FNEARBYINT, MVT::f16, Expand);
588e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FCEIL,  MVT::f16, Expand);
589e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FRINT,  MVT::f16, Expand);
590e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FTRUNC, MVT::f16, Expand);
5914a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FLOG ,  MVT::f32, Expand);
5924a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FLOG2,  MVT::f32, Expand);
5934a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FLOG10, MVT::f32, Expand);
5944a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FEXP ,  MVT::f32, Expand);
5954a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FEXP2,  MVT::f32, Expand);
5964a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FFLOOR, MVT::f32, Expand);
5974a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FNEARBYINT, MVT::f32, Expand);
5984a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FCEIL,  MVT::f32, Expand);
5994a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FRINT,  MVT::f32, Expand);
6004a4fdf3476473021f62d5b02e8ee8802f5b25e5aOwen Anderson  setOperationAction(ISD::FTRUNC, MVT::f32, Expand);
601e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG ,  MVT::f64, Expand);
602e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG2,  MVT::f64, Expand);
603e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FLOG10, MVT::f64, Expand);
604e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FEXP ,  MVT::f64, Expand);
605e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FEXP2,  MVT::f64, Expand);
606e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
607e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
608e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FCEIL,  MVT::f64, Expand);
609e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FRINT,  MVT::f64, Expand);
610e3376ecd504300ae529c902135f51baffbdc2824Dan Gohman  setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
6110bb41608e94adfe9884bc188457c4f6ae47ea43cDale Johannesen
61241bab0beac2600701af99f63e23a6b65f71468acChris Lattner  // Default ISD::TRAP to expand (which turns it into abort).
613825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  setOperationAction(ISD::TRAP, MVT::Other, Expand);
614bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
615a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson  IsLittleEndian = TD->isLittleEndian();
61695771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson  PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
617825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
618718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson  memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
619a03a5dc7ce876dac4b3b91bae14216de4233bacdEvan Cheng  maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
6200521928ae7cc492f3f45ef0e0cedc349102489c5Evan Cheng  maxStoresPerMemsetOptSize = maxStoresPerMemcpyOptSize
6210521928ae7cc492f3f45ef0e0cedc349102489c5Evan Cheng    = maxStoresPerMemmoveOptSize = 4;
6226ebf7bc7405ee79d27d50b70f0c1a474cbea820dEvan Cheng  benefitFromCodePlacementOpt = false;
623d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreSetJmp = false;
624d27a258d2d7691db3731135a448b7654d260cc07Anton Korobeynikov  UseUnderscoreLongJmp = false;
6256618039f9f1f2e41fe2742d83007c68bdebc3656Chris Lattner  SelectIsExpensive = false;
626405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  IntDivIsCheap = false;
627405e3ecb563f21e7b4ee30f0de57821f3eb91219Nate Begeman  Pow2DivIsCheap = false;
628de189be53f5831737f38e720f10dbcdcce6876c6Chris Lattner  JumpIsExpensive = false;
629ee4a76563a84839453588104e94d4891fc44d625Chris Lattner  StackPointerRegisterToSaveRestore = 0;
6309bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionPointerRegister = 0;
6319bb3c93af271449dcbfce9f7fb7442dd11d93813Jim Laskey  ExceptionSelectorRegister = 0;
63203228089d5235f8c90477f88809139464e9c6ea5Duncan Sands  BooleanContents = UndefinedBooleanContent;
63328b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands  BooleanVectorContents = UndefinedBooleanContent;
6348c2d270ae8b460912633883f24346c0763373a56Dan Gohman  SchedPreferenceInfo = Sched::ILP;
6357acf5f39fea463df5ad196affbef0176f3b25e0fChris Lattner  JumpBufSize = 0;
6360c9e0ff24900aa2bcae9518f8b1bdc3fd41dc2edDuraid Madina  JumpBufAlignment = 0;
637fc5d305597ea6336d75bd7f3b741e8d57d6a5105Eli Friedman  MinFunctionAlignment = 0;
638fc5d305597ea6336d75bd7f3b741e8d57d6a5105Eli Friedman  PrefFunctionAlignment = 0;
639fb8075d03f5c87bd57dcc9c5f2304f6b13c55aadEvan Cheng  PrefLoopAlignment = 0;
640cbeeae23c31d32b833c9c7c3e8984e4cbcf22f45Rafael Espindola  MinStackArgumentAlignment = 1;
6419a526495e0c06c4014d7500788cad1929fd244d3Jim Grosbach  ShouldFoldAtomicFences = false;
64226689ac37ebec3b358588089415509285e558de9Eli Friedman  InsertFencesForAtomic = false;
64356966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng
64456966225d1eed9f9a6951d2167bfbbec9628c8d6Evan Cheng  InitLibcallNames(LibcallRoutineNames);
645d385fd62cb43435b3ad70d789198d34bf148e579Evan Cheng  InitCmpLibcallCCs(CmpLibcallCCs);
64672977a45a8ad9d9524c9b49399e89fb9a3a676edAnton Korobeynikov  InitLibcallCallingConvs(LibcallCallingConvs);
647310968cbbb564c4141d4bd418a746e8103560222Chris Lattner}
648310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
649f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLowering::~TargetLowering() {
650f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  delete &TLOF;
651f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
652cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
65395771afbfd604ad003fa3723cac66c9370fed55dOwen AndersonMVT TargetLowering::getShiftAmountTy(EVT LHSTy) const {
65495771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson  return MVT::getIntegerVT(8*TD->getPointerSize());
65595771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson}
65695771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson
657f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// canOpTrap - Returns true if the operation can trap for the value type.
658f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang/// VT must be a legal type.
659f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wangbool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
660f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  assert(isTypeLegal(VT));
661f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  switch (Op) {
662f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  default:
663f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return false;
664f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FDIV:
665f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::FREM:
666f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SDIV:
667f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UDIV:
668f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::SREM:
669f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  case ISD::UREM:
670f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang    return true;
671f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang  }
672f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang}
673f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
674f7ea6c3ee89e605c8d0bb7cdb0ade79706c750e8Mon P Wang
67523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonstatic unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
676598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          unsigned &NumIntermediates,
677598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          EVT &RegisterVT,
678598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                          TargetLowering *TLI) {
67923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Figure out the right, legal destination reg to copy into.
68023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumElts = VT.getVectorNumElements();
68123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT EltTy = VT.getVectorElementType();
682bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
68323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  unsigned NumVectorRegs = 1;
684bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
685bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
68623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // could break down into LHS/RHS like LegalizeDAG does.
68723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!isPowerOf2_32(NumElts)) {
68823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs = NumElts;
68923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts = 1;
69023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
691bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
69223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // Divide the input until we get to a supported size.  This will always
69323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  // end with a scalar if the target doesn't support vectors.
69423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
69523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumElts >>= 1;
69623b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NumVectorRegs <<= 1;
69723b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  }
69823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
69923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  NumIntermediates = NumVectorRegs;
700bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
70123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
70223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  if (!TLI->isTypeLegal(NewVT))
70323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    NewVT = EltTy;
70423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  IntermediateVT = NewVT;
70523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
7060c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  unsigned NewVTSize = NewVT.getSizeInBits();
7070c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
7080c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  // Convert sizes such as i33 to i64.
7090c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  if (!isPowerOf2_32(NewVTSize))
7100c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    NewVTSize = NextPowerOf2(NewVTSize);
7110c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
71223b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = TLI->getRegisterType(NewVT);
71323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  RegisterVT = DestVT;
71432b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
7150c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
716bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
71732b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  // Otherwise, promotion or legal types use the same number of registers as
71832b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  // the vector decimated to the appropriate level.
71932b4b5aea6dc38ac5aaff56fc717aebf188e39c9Chris Lattner  return NumVectorRegs;
72023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson}
72123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson
72246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// isLegalRC - Return true if the value types that can be represented by the
72346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// specified register class are all legal.
72446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Chengbool TargetLowering::isLegalRC(const TargetRegisterClass *RC) const {
72546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
72646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng       I != E; ++I) {
72746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (isTypeLegal(*I))
72846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      return true;
72946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
73046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  return false;
73146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
73246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
73346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// hasLegalSuperRegRegClasses - Return true if the specified register class
73446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// has one or more super-reg register classes that are legal.
735d70f57b254114841892425a40944268d38ae0bcdEvan Chengbool
736d70f57b254114841892425a40944268d38ae0bcdEvan ChengTargetLowering::hasLegalSuperRegRegClasses(const TargetRegisterClass *RC) const{
73746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  if (*RC->superregclasses_begin() == 0)
73846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    return false;
73946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
74046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng         E = RC->superregclasses_end(); I != E; ++I) {
74146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    const TargetRegisterClass *RRC = *I;
74246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (isLegalRC(RRC))
74346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      return true;
74446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
74546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  return false;
74646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
74746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
74846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng/// findRepresentativeClass - Return the largest legal super-reg register class
7494f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng/// of the register class for the specified type and its associated "cost".
7504f6b4674be5473319ac5e70c76fd5cb964da2128Evan Chengstd::pair<const TargetRegisterClass*, uint8_t>
7514f6b4674be5473319ac5e70c76fd5cb964da2128Evan ChengTargetLowering::findRepresentativeClass(EVT VT) const {
7524f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  const TargetRegisterClass *RC = RegClassForVT[VT.getSimpleVT().SimpleTy];
7534f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  if (!RC)
7544f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    return std::make_pair(RC, 0);
75546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  const TargetRegisterClass *BestRC = RC;
75646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
75746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng         E = RC->superregclasses_end(); I != E; ++I) {
75846dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    const TargetRegisterClass *RRC = *I;
75946dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (RRC->isASubClass() || !isLegalRC(RRC))
76046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng      continue;
76146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    if (!hasLegalSuperRegRegClasses(RRC))
7624f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng      return std::make_pair(RRC, 1);
76346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng    BestRC = RRC;
76446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  }
7654f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng  return std::make_pair(BestRC, 1);
76646dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng}
76746dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
768e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner
769310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// computeRegisterProperties - Once all of the register classes are added,
770310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// this allows us to compute derived properties we expose.
771310968cbbb564c4141d4bd418a746e8103560222Chris Lattnervoid TargetLowering::computeRegisterProperties() {
772825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
773bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner         "Too many value types for ValueTypeActions to hold!");
774bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
775b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Everything defaults to needing one register.
776825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
777b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[i] = 1;
778825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
779b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
780b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // ...except isVoid, which doesn't need any registers.
781825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  NumRegistersForVT[MVT::isVoid] = 0;
782f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
783310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Find the largest integer register class.
784825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
785310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
786825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
787310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
788310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Every integer value type larger than this largest register takes twice as
789310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // many registers to represent as the previous ValueType.
79083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
7918a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
7928a55ce4a392f07ac1f3c183100ac591b7ad7c693Dan Gohman    if (!ExpandedVT.isInteger())
79383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      break;
794b9f10196961ebe5b5573a5705048a5a8a6b56bb3Dan Gohman    NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
795825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
796825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
797b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(ExpandedVT, TypeExpandInteger);
798b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
799310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
800b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Inspect all of the ValueType's smaller than the largest integer
801b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // register to see which ones need promotion.
80283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned LegalIntReg = LargestIntReg;
80383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  for (unsigned IntReg = LargestIntReg - 1;
804825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       IntReg >= (unsigned)MVT::i1; --IntReg) {
805825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    EVT IVT = (MVT::SimpleValueType)IntReg;
80683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (isTypeLegal(IVT)) {
807b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman      LegalIntReg = IntReg;
808b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
80983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
810825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        (MVT::SimpleValueType)LegalIntReg;
811b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
812b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
813b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  }
814f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
815161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen  // ppcf128 type is really two f64's.
816825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::ppcf128)) {
817825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
818825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
819825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::ppcf128] = MVT::f64;
820b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
821bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  }
822161e897b0fb35d156b2fe720fadabd975b0d6723Dale Johannesen
823b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f64. If the target does not have native f64 support,
824b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // expand it to i64 and we will be generating soft float library calls.
825825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f64)) {
826825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
827825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
828825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    TransformToType[MVT::f64] = MVT::i64;
829b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem    ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
8301a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
831b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
832b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Decide how to handle f32. If the target does not have native support for
833b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
834825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (!isTypeLegal(MVT::f32)) {
835825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (isTypeLegal(MVT::f64)) {
836825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
837825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
838825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::f64;
839b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(MVT::f32, TypePromoteInteger);
840b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    } else {
841825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
842825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
843825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      TransformToType[MVT::f32] = MVT::i32;
844b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
845b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman    }
8461a8f1fe676f1d83e0da7336e744ebcdcc14a88c4Evan Cheng  }
847bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
848b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  // Loop over all of the vector value types to see which need transformations.
849825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
850825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson       i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
85123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    MVT VT = (MVT::SimpleValueType)i;
852598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    if (isTypeLegal(VT)) continue;
853bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
854e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    // Determine if there is a legal wider type.  If so, we should promote to
855e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    // that wider vector type.
856e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    EVT EltVT = VT.getVectorElementType();
857e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    unsigned NElts = VT.getVectorNumElements();
858e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (NElts != 1) {
859e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      bool IsLegalWiderType = false;
860f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // If we allow the promotion of vector elements using a flag,
861f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // then return TypePromoteInteger on vector elements.
862f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // First try to promote the elements of integer vectors. If no legal
863f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // promotion was found, fallback to the widen-vector method.
864f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      if (mayPromoteElements)
865e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
866e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        EVT SVT = (MVT::SimpleValueType)nVT;
867f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        // Promote vectors of integers to vectors with the same number
868f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        // of elements, with a wider element type.
869f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        if (SVT.getVectorElementType().getSizeInBits() > EltVT.getSizeInBits()
870f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem            && SVT.getVectorNumElements() == NElts &&
871f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem            isTypeLegal(SVT) && SVT.getScalarType().isInteger()) {
872f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          TransformToType[i] = SVT;
873f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          RegisterTypeForVT[i] = SVT;
874f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          NumRegistersForVT[i] = 1;
875f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
876f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          IsLegalWiderType = true;
877f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem          break;
878b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem        }
879f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      }
880f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem
881f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      if (IsLegalWiderType) continue;
882b6fbec3a546fd04bb2e79040db2436b0bd629162Nadav Rotem
883f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      // Try to widen the vector.
884f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
885f1c025d1d106905df3d38d2482fde68c8f81b107Nadav Rotem        EVT SVT = (MVT::SimpleValueType)nVT;
886e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        if (SVT.getVectorElementType() == EltVT &&
887bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            SVT.getVectorNumElements() > NElts &&
888e93d99cf0742eebab859022e4cfdcf03cb9d5dfaDale Johannesen            isTypeLegal(SVT)) {
889e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          TransformToType[i] = SVT;
890e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          RegisterTypeForVT[i] = SVT;
891e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          NumRegistersForVT[i] = 1;
892b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem          ValueTypeActions.setTypeAction(VT, TypeWidenVector);
893e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          IsLegalWiderType = true;
894e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner          break;
895e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner        }
896e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      }
897e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      if (IsLegalWiderType) continue;
898e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    }
899bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
900598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    MVT IntermediateVT;
901598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    EVT RegisterVT;
902598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    unsigned NumIntermediates;
903598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    NumRegistersForVT[i] =
904598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner      getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
905598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner                                RegisterVT, this);
906598751ed2544291ba623e013b4e0b61bf56ca9c4Chris Lattner    RegisterTypeForVT[i] = RegisterVT;
907bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
908e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    EVT NVT = VT.getPow2VectorType();
909e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (NVT == VT) {
910e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      // Type is already a power of 2.  The default action is to split.
911e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      TransformToType[i] = MVT::Other;
912b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      unsigned NumElts = VT.getVectorNumElements();
913b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(VT,
914b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem            NumElts > 1 ? TypeSplitVector : TypeScalarizeVector);
915e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    } else {
916e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      TransformToType[i] = NVT;
917b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem      ValueTypeActions.setTypeAction(VT, TypeWidenVector);
9187f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    }
9193a59358499f527ad9a8d1d4ed7d80b6bf0f1c12dChris Lattner  }
92046dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng
92146dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // Determine the 'representative' register class for each value type.
92246dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // An representative register class is the largest (meaning one which is
92346dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // not a sub-register class / subreg register class) legal register class for
92446dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // a group of value types. For example, on i386, i8, i16, and i32
92546dcb57e18884099ca6ad2aebb81fd7e1513c1f6Evan Cheng  // representative would be GR32; while on x86_64 it's GR64.
926d70f57b254114841892425a40944268d38ae0bcdEvan Cheng  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
9274f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    const TargetRegisterClass* RRC;
9284f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    uint8_t Cost;
9294f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    tie(RRC, Cost) =  findRepresentativeClass((MVT::SimpleValueType)i);
9304f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    RepRegClassForVT[i] = RRC;
9314f6b4674be5473319ac5e70c76fd5cb964da2128Evan Cheng    RepRegClassCostForVT[i] = Cost;
932d70f57b254114841892425a40944268d38ae0bcdEvan Cheng  }
933bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner}
934cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
9357226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Chengconst char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
9367226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng  return NULL;
9377226158d7e3986e55b58214a749aa4eabb3fb6d5Evan Cheng}
9383a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng
9395b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
94028b77e968d2b01fc9da724762bd8ddcd80650e32Duncan SandsEVT TargetLowering::getSetCCResultType(EVT VT) const {
94128b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands  assert(!VT.isVector() && "No default SetCC type for vectors!");
9421d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  return PointerTy.SimpleTy;
9435b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel}
9445b8f82e35b51bf007de07a7ca9347d804084ddf8Scott Michel
9458f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv GuptaMVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
9468f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta  return MVT::i32; // return the default value
9478f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta}
9488f17a36d3107bdc4ffed53ce782c1724ef6460e7Sanjiv Gupta
9497f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// getVectorTypeBreakdown - Vector types are broken down into some number of
950825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
951825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
952825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
953dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
9547f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman/// This method returns the number of registers needed, and the VT for each
955b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// register.  It also returns the VT and quantity of the intermediate values
956b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman/// before they are promoted/expanded.
957dc879296090a738c66968f5eec77db65d7e03623Chris Lattner///
95823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Andersonunsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
959e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                                                EVT &IntermediateVT,
960b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman                                                unsigned &NumIntermediates,
96123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                                EVT &RegisterVT) const {
96283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  unsigned NumElts = VT.getVectorNumElements();
963bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
964e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // If there is a wider vector type with the same element type as this one,
965e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // we should widen to that legal vector type.  This handles things like
966e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // <2 x float> -> <4 x float>.
967b6aacae9413adde66d9686cd9e561eb836b3ee34Nadav Rotem  if (NumElts != 1 && getTypeAction(Context, VT) == TypeWidenVector) {
968e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    RegisterVT = getTypeToTransformTo(Context, VT);
969e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    if (isTypeLegal(RegisterVT)) {
970e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      IntermediateVT = RegisterVT;
971e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      NumIntermediates = 1;
972e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner      return 1;
973e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner    }
974e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  }
975bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
976e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // Figure out the right, legal destination reg to copy into.
977e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT EltTy = VT.getVectorElementType();
978bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
979dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  unsigned NumVectorRegs = 1;
980bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
981bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
982d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  // could break down into LHS/RHS like LegalizeDAG does.
983d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  if (!isPowerOf2_32(NumElts)) {
984d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumVectorRegs = NumElts;
985d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman    NumElts = 1;
986d73ab8884f5c95d2704be3c00af4cc3dba963da6Nate Begeman  }
987bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
988dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // Divide the input until we get to a supported size.  This will always
989dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  // end with a scalar if the target doesn't support vectors.
99023b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  while (NumElts > 1 && !isTypeLegal(
99123b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson                                   EVT::getVectorVT(Context, EltTy, NumElts))) {
992dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumElts >>= 1;
993dc879296090a738c66968f5eec77db65d7e03623Chris Lattner    NumVectorRegs <<= 1;
994dc879296090a738c66968f5eec77db65d7e03623Chris Lattner  }
995b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman
996b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  NumIntermediates = NumVectorRegs;
997bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
99823b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
9997f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman  if (!isTypeLegal(NewVT))
10007f32156bb9c017b71971c52fac892fa7b9b06dd2Dan Gohman    NewVT = EltTy;
1001b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  IntermediateVT = NewVT;
1002dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
100323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson  EVT DestVT = getRegisterType(Context, NewVT);
1004b6f5b00c3bad3415d3f2ee1a6d5ee5a6f66a4540Dan Gohman  RegisterVT = DestVT;
10050c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  unsigned NewVTSize = NewVT.getSizeInBits();
10060c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
10070c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  // Convert sizes such as i33 to i64.
10080c3e67860af417febb1fa9e870ece912a16085acNadav Rotem  if (!isPowerOf2_32(NewVTSize))
10090c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    NewVTSize = NextPowerOf2(NewVTSize);
10100c3e67860af417febb1fa9e870ece912a16085acNadav Rotem
1011e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  if (DestVT.bitsLT(NewVT))   // Value is expanded, e.g. i64 -> i16.
10120c3e67860af417febb1fa9e870ece912a16085acNadav Rotem    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1013bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1014e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // Otherwise, promotion or legal types use the same number of registers as
1015e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  // the vector decimated to the appropriate level.
1016e6f7c267df11a44679c35dec79787fbc276839fbChris Lattner  return NumVectorRegs;
1017dc879296090a738c66968f5eec77db65d7e03623Chris Lattner}
1018dc879296090a738c66968f5eec77db65d7e03623Chris Lattner
1019bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// Get the EVTs and ArgFlags collections that represent the legalized return
102084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// type of the given function.  This does not require a DAG or a return value,
102184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// and is suitable for use before any DAGs for the function are constructed.
102284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman/// TODO: Move this out of TargetLowering.cpp.
1023db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnervoid llvm::GetReturnInfo(Type* ReturnType, Attributes attr,
102484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         SmallVectorImpl<ISD::OutputArg> &Outs,
102584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         const TargetLowering &TLI,
102684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                         SmallVectorImpl<uint64_t> *Offsets) {
102784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  SmallVector<EVT, 4> ValueVTs;
102884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  ComputeValueVTs(TLI, ReturnType, ValueVTs);
102984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  unsigned NumValues = ValueVTs.size();
103084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  if (NumValues == 0) return;
103184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  unsigned Offset = 0;
103284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
103384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  for (unsigned j = 0, f = NumValues; j != f; ++j) {
103484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    EVT VT = ValueVTs[j];
103584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
103684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
103784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::SExt)
103884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      ExtendKind = ISD::SIGN_EXTEND;
103984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    else if (attr & Attribute::ZExt)
104084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      ExtendKind = ISD::ZERO_EXTEND;
104184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
104284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // FIXME: C calling convention requires the return type to be promoted to
104384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // at least 32-bit. But this is not necessary for non-C calling
104484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // conventions. The frontend should mark functions whose return values
104584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // require promoting with signext or zeroext attributes.
104684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
104784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
104884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      if (VT.bitsLT(MinVT))
104984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        VT = MinVT;
105084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    }
105184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
105284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
105384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
105484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
105584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman                        PartVT.getTypeForEVT(ReturnType->getContext()));
105684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
105784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // 'inreg' on function refers to return value
105884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
105984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::InReg)
106084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setInReg();
106184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
106284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    // Propagate extension type if any
106384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    if (attr & Attribute::SExt)
106484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setSExt();
106584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    else if (attr & Attribute::ZExt)
106684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Flags.setZExt();
106784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
106884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    for (unsigned i = 0; i < NumParts; ++i) {
106984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      Outs.push_back(ISD::OutputArg(Flags, PartVT, /*isFixed=*/true));
107084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      if (Offsets) {
107184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        Offsets->push_back(Offset);
107284023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        Offset += PartSize;
107384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman      }
107484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    }
107584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  }
107684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman}
107784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
10783ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
107928d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// function arguments in the caller parameter area.  This is the actual
108028d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen/// alignment, not its logarithm.
1081db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerunsigned TargetLowering::getByValTypeAlignment(Type *Ty) const {
108228d08fdb9f6572cafd5aae95c7caffa3cd136d8eDale Johannesen  return TD->getCallFrameTypeAlignment(Ty);
10833ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng}
10843ae054385cfe9f2fcef2d77f26839615b1d3e48bEvan Cheng
1085071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1086071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// current function.  The returned value is a member of the
1087071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner/// MachineJumpTableInfo::JTEntryKind enum.
1088071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattnerunsigned TargetLowering::getJumpTableEncoding() const {
1089071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In non-pic modes, just use the address of a block.
1090071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
1091071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_BlockAddress;
1092bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1093071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // In PIC mode, if the target supports a GPRel32 directive, use it.
1094071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
1095071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner    return MachineJumpTableInfo::EK_GPRel32BlockAddress;
1096bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1097071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  // Otherwise, use a label difference.
1098071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner  return MachineJumpTableInfo::EK_LabelDifference32;
1099071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner}
1100071c62fad0b25ad4131e7f984173a796c1e63f61Chris Lattner
1101475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
1102475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                 SelectionDAG &DAG) const {
1103f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  // If our PIC model is GP relative, use the global offset table as the base.
1104f1214cbf3c2d151d3a2353d82143da186313a42aChris Lattner  if (getJumpTableEncoding() == MachineJumpTableInfo::EK_GPRel32BlockAddress)
1105b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen    return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
1106cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng  return Table;
1107cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng}
1108cc41586b9d79532172b37e1f44a9077da4b73fc9Evan Cheng
110913e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
111013e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
111113e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner/// MCExpr.
111213e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattnerconst MCExpr *
1113589c6f620e8dcf3d59af1ae0e15372c934647c82Chris LattnerTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
1114589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner                                             unsigned JTI,MCContext &Ctx) const{
1115beeb93e6ba48af2661eabc4872d8b159fb43e5dbChris Lattner  // The normal PIC reloc base is the label at the start of the jump table.
1116589c6f620e8dcf3d59af1ae0e15372c934647c82Chris Lattner  return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
111713e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner}
111813e97a29d9dfa5602f93a8c546f112c5d029e8f7Chris Lattner
11196520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohmanbool
11206520e20e4fb31f2e65e25c38b372b19d33a83df4Dan GohmanTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11216520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Assume that everything is safe in static mode.
11226520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::Static)
11236520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
11246520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
11256520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // In dynamic-no-pic mode, assume that known defined values are safe.
11266520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
11276520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      GA &&
11286520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman      !GA->getGlobal()->isDeclaration() &&
1129667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands      !GA->getGlobal()->isWeakForLinker())
11306520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman    return true;
11316520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
11326520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  // Otherwise assume nothing is safe.
11336520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman  return false;
11346520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman}
11356520e20e4fb31f2e65e25c38b372b19d33a83df4Dan Gohman
1136eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
1137eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Optimization Methods
1138eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
1139eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
1140bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// ShrinkDemandedConstant - Check to see if the specified operand of the
1141368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// specified instruction is a constant integer.  If so, check to see if there
1142368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// are any bits set in the constant that are not demanded.  If so, shrink the
1143368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// constant and return true.
1144bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckbool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
11457b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                                        const APInt &Demanded) {
1146de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen  DebugLoc dl = Op.getDebugLoc();
114736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1148ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // FIXME: ISD::SELECT, ISD::SELECT_CC
1149e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  switch (Op.getOpcode()) {
1150368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  default: break;
1151368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
115236ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::AND:
115336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  case ISD::OR: {
115436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
115536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (!C) return false;
115636ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
115736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (Op.getOpcode() == ISD::XOR &&
115836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling        (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
115936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return false;
116036ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
116136ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    // if we can expand it to have all bits set, do it
116236ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    if (C->getAPIntValue().intersects(~Demanded)) {
1163e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
116436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
116536ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                DAG.getConstant(Demanded &
1166bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                C->getAPIntValue(),
116736ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling                                                VT));
116836ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling      return CombineTo(Op, New);
116936ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling    }
117036ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1171368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1172368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
117336ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling  }
117436ae6c182759871a4af71d9af85f970b29a669f1Bill Wendling
1175368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  return false;
1176368e18d56a87308045d341e85584597bfe7426e9Nate Begeman}
1177368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
117897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
117997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// casts are free.  This uses isZExtFree and ZERO_EXTEND for the widening
118097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// cast, but it could be generalized for targets with other types of
118197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman/// implicit widening casts.
118297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohmanbool
118397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan GohmanTargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
118497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    unsigned BitWidth,
118597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    const APInt &Demanded,
118697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                                    DebugLoc dl) {
118797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNumOperands() == 2 &&
118897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports binary operators!");
118997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  assert(Op.getNode()->getNumValues() == 1 &&
119097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman         "ShrinkDemandedOp only supports nodes with one result!");
119197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
119297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Don't do this if the node has another user, which may require the
119397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // full value.
119497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!Op.getNode()->hasOneUse())
119597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    return false;
119697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
119797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Search for the smallest integer type with free casts to and from
119897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // Op's type. For expedience, just check power-of-2 integer types.
119997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
120097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
120197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  if (!isPowerOf2_32(SmallVTBits))
120297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    SmallVTBits = NextPowerOf2(SmallVTBits);
120397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
120423b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
120597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
120697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman        TLI.isZExtFree(SmallVT, Op.getValueType())) {
120797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      // We found a type with free casts.
120897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
120997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
121097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(0)),
121197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
121297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                          Op.getNode()->getOperand(1)));
121397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
121497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return CombineTo(Op, Z);
121597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    }
121697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
121797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  return false;
121897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman}
121997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1220368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
12218c1ec5a0a21dcb3364aace8174f29f209ff3224eChad Rosier/// DemandedMask bits of the result of Op are ever used downstream.  If we can
1222368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// use this information to simplify Op, create a new simplified DAG node and
1223368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// return true, returning the original and new nodes in Old and New. Otherwise,
1224368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// analyze the expression and return a mask of KnownOne and KnownZero bits for
1225368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// the expression (used to simplify the caller).  The KnownZero/One bits may
1226368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// only be accurate for those bits in the DemandedMask.
1227475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanbool TargetLowering::SimplifyDemandedBits(SDValue Op,
12287b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          const APInt &DemandedMask,
12297b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownZero,
12307b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman                                          APInt &KnownOne,
1231368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          TargetLoweringOpt &TLO,
1232368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                          unsigned Depth) const {
12337b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  unsigned BitWidth = DemandedMask.getBitWidth();
123487862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman  assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
12357b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman         "Mask size mismatches value type size!");
12367b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt NewMask = DemandedMask;
12376f38cb61a94b3abab70f0ee463bdcf55d86d334eDale Johannesen  DebugLoc dl = Op.getDebugLoc();
12387b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
12397b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  // Don't know anything.
12407b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  KnownZero = KnownOne = APInt(BitWidth, 0);
12413fc5b01d38ddccbe53ff0b32cc0100351d698c66Chris Lattner
1242368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  // Other users may use these bits.
1243bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (!Op.getNode()->hasOneUse()) {
1244368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Depth != 0) {
1245bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      // If not at the root, Just compute the KnownZero/KnownOne bits to
1246368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // simplify things downstream.
1247ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman      TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
1248368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return false;
1249368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1250368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If this is the root being simplified, allow it to have multiple uses,
12517b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    // just set the NewMask to all bits.
12527b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    NewMask = APInt::getAllOnesValue(BitWidth);
1253bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  } else if (DemandedMask == 0) {
1254368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Not demanding any bits from Op.
1255368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (Op.getOpcode() != ISD::UNDEF)
1256e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
1257de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    return false;
1258368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  } else if (Depth == 6) {        // Limit search depth.
1259368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    return false;
1260368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  }
1261368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
12627b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
1263de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  switch (Op.getOpcode()) {
1264368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::Constant:
1265368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // We know all of the bits for a constant!
12667b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
12677b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero = ~KnownOne & NewMask;
1268ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return false;   // Don't fall through, will infinitely loop.
1269de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::AND:
127081cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // If the RHS is a constant, check to see if the LHS would be zero without
127181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // using the bits from the RHS.  Below, we use knowledge about the RHS to
127281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // simplify the LHS, here we're using information from the LHS to simplify
127381cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    // the RHS.
127481cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12757b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt LHSZero, LHSOne;
127697fd9a58de3c99a74b0890a908430fa3ab894bedDale Johannesen      // Do not increment Depth here; that can cause an infinite loop.
12777b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
127897fd9a58de3c99a74b0890a908430fa3ab894bedDale Johannesen                                LHSZero, LHSOne, Depth);
127981cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If the LHS already has zeros where RHSC does, this and is dead.
12807b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
128181cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return TLO.CombineTo(Op, Op.getOperand(0));
128281cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // If any of the set bits in the RHS are known zero on the LHS, shrink
128381cd35586f5b675faf5391e1f597908bdda5338eChris Lattner      // the constant.
12847b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
128581cd35586f5b675faf5391e1f597908bdda5338eChris Lattner        return true;
128681cd35586f5b675faf5391e1f597908bdda5338eChris Lattner    }
1287bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
12887b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1289368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1290368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1291bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
12927b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
1293368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1294368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1295bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1296bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1297368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known one on one side, return the other.
1298368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'and'.
12997b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1300368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13017b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1302368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1303368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits in the inputs are known zeros, return zero.
13047b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
1305368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1306368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
13077b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
1308368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
130997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
13104e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
131197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
131297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1313368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 bits are only known if set in both the LHS & RHS.
1314368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1315368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 are known to be clear if zero in either the LHS | RHS.
1316368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero |= KnownZero2;
1317368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1318368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::OR:
1319bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1320368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1321368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1322bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13237b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
1324368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero2, KnownOne2, TLO, Depth+1))
1325368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1326bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1327bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1328368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1329368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'or'.
13307b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
1331368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13327b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
1333368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1334368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the potentially set bits on one side are known to be set on
1335368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // the other side, just use the 'other' side.
13367b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1337368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13387b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1339368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
1340368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
13417b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1342368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
134397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
13444e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
134597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
134697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
1347368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are only known if clear in both the LHS & RHS.
1348368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1349368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in either the LHS | RHS.
1350368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne |= KnownOne2;
1351368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1352368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::XOR:
1353bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1354368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1355368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1356bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
13577b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
1358368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1359368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1360bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1361bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1362368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits are known zero on one side, return the other.
1363368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // These bits cannot contribute to the result of the 'xor'.
13647b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero & NewMask) == NewMask)
1365368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(0));
13667b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((KnownZero2 & NewMask) == NewMask)
1367368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return TLO.CombineTo(Op, Op.getOperand(1));
136897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // If the operation can be done in a smaller type, do so.
13694e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
137097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
137197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman
13723687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // If all of the unknown bits are known to be zero on one side or the other
13733687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    // (but not both) turn this into an *inclusive* or.
13743687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner    //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
13757b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
1376de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
13773687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(0),
13783687c1a4d3ecbe6709fddd77d79dc6b128d31431Chris Lattner                                               Op.getOperand(1)));
1379bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1380368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-0 bits are known if clear or set in both the LHS & RHS.
1381368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1382368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Output known-1 are known to be set if set in only one of the LHS, RHS.
1383368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1384bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1385368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If all of the demanded bits on one side are known, and all of the set
1386368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bits on that side are also known to be set on the other side, turn this
1387368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // into an AND, as we know the bits will be cleared.
1388368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
13897b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
1390368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      if ((KnownOne & KnownOne2) == KnownOne) {
1391e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT VT = Op.getValueType();
1392475871a144eb604ddaf37503397ba0941442e5fbDan Gohman        SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
1393bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1394ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0), ANDC));
1395368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1396de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1397bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1398368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the RHS is a constant, see if we can simplify it.
13994fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // for XOR, we prefer to force bits to 1 if they will make a -1.
14004fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    // if we can't force bits, try to shrink constant
14014fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14024fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      APInt Expanded = C->getAPIntValue() | (~NewMask);
14034fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      // if we can expand it to have all bits set, do it
14044fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      if (Expanded.isAllOnesValue()) {
14054fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        if (Expanded != C->getAPIntValue()) {
1406e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1407ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
14084fea2e982d79132715711dfcfdc46abf15239217Torok Edwin                                          TLO.DAG.getConstant(Expanded, VT));
14094fea2e982d79132715711dfcfdc46abf15239217Torok Edwin          return TLO.CombineTo(Op, New);
14104fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        }
14114fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // if it already has all the bits set, nothing to change
14124fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        // but don't shrink either!
14134fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
14144fea2e982d79132715711dfcfdc46abf15239217Torok Edwin        return true;
14154fea2e982d79132715711dfcfdc46abf15239217Torok Edwin      }
14164fea2e982d79132715711dfcfdc46abf15239217Torok Edwin    }
14174fea2e982d79132715711dfcfdc46abf15239217Torok Edwin
1418368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero = KnownZeroOut;
1419368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne  = KnownOneOut;
1420368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1421368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SELECT:
1422bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
1423368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne, TLO, Depth+1))
1424368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
14257b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
1426368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownOne2, TLO, Depth+1))
1427368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1428bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1429bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1430bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1431368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the operands are constants, see if we can simplify them.
14327b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1433368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      return true;
1434bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1435368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // Only known if known in both the LHS and RHS.
1436368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownOne &= KnownOne2;
1437368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    KnownZero &= KnownZero2;
1438de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1439ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SELECT_CC:
1440bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
1441ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1442ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
14437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
1444ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne2, TLO, Depth+1))
1445ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1446bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1447bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1448bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1449ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the operands are constants, see if we can simplify them.
14507b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1451ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1452bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1453ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Only known if known in both the LHS and RHS.
1454ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownOne &= KnownOne2;
1455ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero &= KnownZero2;
1456ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1457de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SHL:
1458368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1459f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1460475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1461895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner
14627b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
14637b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
14647b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
14657b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1466895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1467895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the bottom bits (which are shifted
1468895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // out) are never demanded.
1469895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SRL &&
1470895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
14717b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
1472f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1473895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SHL;
1474895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1475895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1476895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1477895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SRL;
1478bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          }
1479bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1480bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          SDValue NewSA =
14814e7e6cd13ab8cab410ad11375101878865579325Chris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1482e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT VT = Op.getValueType();
1483ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1484895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1485895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1486bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      }
1487bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1488a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
1489368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1490368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1491a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman
1492a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
1493a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      // are not demanded. This will likely allow the anyext to be folded away.
1494a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
1495a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        SDValue InnerOp = InOp.getNode()->getOperand(0);
1496a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        EVT InnerVT = InnerOp.getValueType();
14972dd0353fec61b412e521013049d6a03de29aa8c7Eli Friedman        unsigned InnerBits = InnerVT.getSizeInBits();
14982dd0353fec61b412e521013049d6a03de29aa8c7Eli Friedman        if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
1499a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            isTypeDesirableForOp(ISD::SHL, InnerVT)) {
150095771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson          EVT ShTy = getShiftAmountTy(InnerVT);
1501cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman          if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
1502cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman            ShTy = InnerVT;
1503a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman          SDValue NarrowShl =
1504a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
1505cd20c6fb18230f399fc99276fbdee14ce9faf6a1Dan Gohman                            TLO.DAG.getConstant(ShAmt, ShTy));
1506a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman          return
1507a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman            TLO.CombineTo(Op,
1508a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman                          TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
1509a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman                                          NarrowShl));
1510a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman        }
1511a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman      }
1512a4f4d699ec627e798357929295aecd3d55b8d703Dan Gohman
1513f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero <<= SA->getZExtValue();
1514f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownOne  <<= SA->getZExtValue();
15157b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // low bits known zero.
1516f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
1517368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1518368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1519368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRL:
1520368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1521e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1522f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
152383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      unsigned VTSize = VT.getSizeInBits();
1524475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue InOp = Op.getOperand(0);
1525bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15267b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
15277b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
15287b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
15297b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
1530895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1531895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // single shift.  We can do this if the top bits (which are shifted out)
1532895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      // are never demanded.
1533895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner      if (InOp.getOpcode() == ISD::SHL &&
1534895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          isa<ConstantSDNode>(InOp.getOperand(1))) {
15357b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
1536f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1537895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          unsigned Opc = ISD::SRL;
1538895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          int Diff = ShAmt-C1;
1539895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner          if (Diff < 0) {
1540895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Diff = -Diff;
1541895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner            Opc = ISD::SHL;
1542bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          }
1543bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1544475871a144eb604ddaf37503397ba0941442e5fbDan Gohman          SDValue NewSA =
15458c7d2d56bf5aa2186e2d08ed1dc37c2f392b4aaeChris Lattner            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1546ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1547895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner                                                   InOp.getOperand(0), NewSA));
1548895c4ab564c145d16a585201ea49b91541d806b6Chris Lattner        }
1549bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      }
1550bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1551368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // Compute the new bits that are at the top now.
15527b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
1553368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1554368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1555bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
15567b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
15577b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1558c4fa603347e798c94f54430227df8265736ef183Chris Lattner
15597b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1560c4fa603347e798c94f54430227df8265736ef183Chris Lattner      KnownZero |= HighBits;  // High bits known zero.
1561368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    }
1562368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    break;
1563368e18d56a87308045d341e85584597bfe7426e9Nate Begeman  case ISD::SRA:
1564e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // If this is an arithmetic shift right and only the low-bit is set, we can
1565e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // always convert this into a logical shr, even if the shift amount is
1566e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // variable.  The low bit of the shift cannot be an input sign bit unless
1567e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    // the shift amount is >= the size of the datatype, which is undefined.
15682dd0353fec61b412e521013049d6a03de29aa8c7Eli Friedman    if (NewMask == 1)
1569e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng      return TLO.CombineTo(Op,
1570e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                           TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
1571e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                           Op.getOperand(0), Op.getOperand(1)));
1572e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
1573368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1574e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT VT = Op.getValueType();
1575f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman      unsigned ShAmt = SA->getZExtValue();
1576bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15777b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // If the shift count is an invalid immediate, don't do anything.
15787b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (ShAmt >= BitWidth)
15797b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman        break;
15807b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman
15817b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt InDemandedMask = (NewMask << ShAmt);
15821b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner
15831b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // If any of the demanded bits are produced by the sign extension, we also
15841b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      // demand the input sign bit.
15857b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
15867b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (HighBits.intersects(NewMask))
158787862e77bbf90cf1b68c9eea1f3641ad81435e38Dan Gohman        InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
1588bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15891b7371331fef3db77999b9c4ca62c2ddbaf1d804Chris Lattner      if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
1590368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                               KnownZero, KnownOne, TLO, Depth+1))
1591368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        return true;
1592bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
15937b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownZero = KnownZero.lshr(ShAmt);
15947b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      KnownOne  = KnownOne.lshr(ShAmt);
1595bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
15967b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      // Handle the sign bit, adjusted to where it is now in the mask.
15977b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
1598bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1599368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // If the input sign bit is known to be zero, or if none of the top bits
1600368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      // are demanded, turn this into an unsigned shift right.
16017b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
1602bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1603ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                                 Op.getOperand(0),
1604368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                 Op.getOperand(1)));
16057b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
1606368e18d56a87308045d341e85584597bfe7426e9Nate Begeman        KnownOne |= HighBits;
1607368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      }
1608de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1609de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1610de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  case ISD::SIGN_EXTEND_INREG: {
1611e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1612368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1613bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    // Sign extension.  Compute the demanded bits in the result that are not
1614368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // present in the input.
1615d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt NewBits =
1616d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getHighBitsSet(BitWidth,
16171d17d199a4cd6190fdf82e35c42b617eaae2cd35Eli Friedman                            BitWidth - EVT.getScalarType().getSizeInBits());
1618bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1619ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the extended bits are demanded, eliminate the sextinreg.
16201d17d199a4cd6190fdf82e35c42b617eaae2cd35Eli Friedman    if ((NewBits & NewMask) == 0)
1621ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return TLO.CombineTo(Op, Op.getOperand(0));
1622ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner
162340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InSignBit =
162440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad      APInt::getSignBit(EVT.getScalarType().getSizeInBits()).zext(BitWidth);
1625d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    APInt InputDemandedBits =
1626d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      APInt::getLowBitsSet(BitWidth,
1627d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman                           EVT.getScalarType().getSizeInBits()) &
1628d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      NewMask;
1629bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1630ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since the sign extended bits are demanded, we know that the sign
1631368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // bit is demanded.
1632ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InputDemandedBits |= InSignBit;
1633368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1634368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1635368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                             KnownZero, KnownOne, TLO, Depth+1))
1636de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman      return true;
1637bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1638368e18d56a87308045d341e85584597bfe7426e9Nate Begeman
1639368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // If the sign bit of the input is known set or clear, then we know the
1640368e18d56a87308045d341e85584597bfe7426e9Nate Begeman    // top bits of the result.
1641bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1642ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the input sign bit is known zero, convert this into a zero extension.
16437b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1644bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return TLO.CombineTo(Op,
1645ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                           TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
1646bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
16477b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {    // Input sign bit known set
1648368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne |= NewBits;
1649368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1650ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {                       // Input sign bit unknown
1651368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownZero &= ~NewBits;
1652368e18d56a87308045d341e85584597bfe7426e9Nate Begeman      KnownOne &= ~NewBits;
1653de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    }
1654de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman    break;
1655de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1656ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ZERO_EXTEND: {
1657d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1658d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
165940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InMask = NewMask.trunc(OperandBitWidth);
1660bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1661ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
16627b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits =
16637b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman      APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
16647b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (!NewBits.intersects(NewMask))
1665ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1666bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                               Op.getValueType(),
1667ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1668bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
16697b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1670ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1671ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1672bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
167340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
167440f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1675ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    KnownZero |= NewBits;
1676ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1677ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1678ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::SIGN_EXTEND: {
1679e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT InVT = Op.getOperand(0).getValueType();
1680d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned InBits = InVT.getScalarType().getSizeInBits();
16817b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InMask    = APInt::getLowBitsSet(BitWidth, InBits);
16829736028d84de3a72dd8db5f49cfaa07280154a0eDan Gohman    APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
16837b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt NewBits   = ~InMask & NewMask;
1684bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1685ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If none of the top bits are demanded, convert this into an any_extend.
1686ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    if (NewBits == 0)
1687ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1688ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getValueType(),
1689ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                              Op.getOperand(0)));
1690bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1691ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // Since some of the sign extended bits are demanded, we know that the sign
1692ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // bit is demanded.
16937b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    APInt InDemandedBits = InMask & NewMask;
1694ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    InDemandedBits |= InSignBit;
169540f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    InDemandedBits = InDemandedBits.trunc(InBits);
1696bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1697bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1698ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownOne, TLO, Depth+1))
1699ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
170040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
170140f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1702bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1703ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known zero, convert this to a zero extend.
17047b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownZero.intersects(InSignBit))
1705ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
1706bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                               Op.getValueType(),
1707ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                                               Op.getOperand(0)));
1708bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1709ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    // If the sign bit is known one, the top bits match.
17107b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (KnownOne.intersects(InSignBit)) {
1711ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  |= NewBits;
1712ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1713ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    } else {   // Otherwise, top bits aren't known.
1714ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownOne  &= ~NewBits;
1715ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      KnownZero &= ~NewBits;
1716ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    }
1717ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1718ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1719ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::ANY_EXTEND: {
1720d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman    unsigned OperandBitWidth =
1721d1996360399ad6dbe75ee185b661b16c83146373Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
172240f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt InMask = NewMask.trunc(OperandBitWidth);
17237b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1724ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1725ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1726bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
172740f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.zext(BitWidth);
172840f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.zext(BitWidth);
1729ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1730ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1731fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  case ISD::TRUNCATE: {
1732c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // Simplify the input, using demanded bit information, and compute the known
1733c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // zero/one bits live out.
1734042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman    unsigned OperandBitWidth =
1735042919c0c5f87f937e1a01ca548811af2f297e83Dan Gohman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
173640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    APInt TruncMask = NewMask.zext(OperandBitWidth);
17377b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
1738fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1739fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner      return true;
174040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownZero = KnownZero.trunc(BitWidth);
174140f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad    KnownOne = KnownOne.trunc(BitWidth);
1742bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1743c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // If the input is only used by this truncate, see if we can shrink it based
1744c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    // on the known demanded bits.
1745ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (Op.getOperand(0).getNode()->hasOneUse()) {
1746475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue In = Op.getOperand(0);
1747c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      switch (In.getOpcode()) {
1748c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      default: break;
1749c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      case ISD::SRL:
1750c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // Shrink SRL by a constant if none of the high bits shifted in are
1751c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        // demanded.
1752e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (TLO.LegalTypes() &&
1753e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng            !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
1754e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1755e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // undesirable.
1756e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          break;
1757e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
1758e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (!ShAmt)
1759e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          break;
17607adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        SDValue Shift = In.getOperand(1);
17617adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        if (TLO.LegalTypes()) {
17627adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson          uint64_t ShVal = ShAmt->getZExtValue();
17637adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson          Shift =
17647adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson            TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
17657adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson        }
17667adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson
1767e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1768e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                               OperandBitWidth - BitWidth);
176940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad        HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
1770e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng
1771e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng        if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1772e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // None of the shifted in bits are needed.  Add a truncate of the
1773e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          // shift input, then shift it.
1774e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
1775bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                             Op.getValueType(),
1776e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                             In.getOperand(0));
1777e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng          return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1778e5b51ac7708402473f0a558f4aac74fab63d4f7eEvan Cheng                                                   Op.getValueType(),
1779bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                   NewTrunc,
17807adf862eb24f7c6cbf12fbc9e3e8229f6f863b8aOwen Anderson                                                   Shift));
1781c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        }
1782c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner        break;
1783c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner      }
1784c93dfda9056a83a81b07ad25ed567cc303893990Chris Lattner    }
1785bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1786bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1787fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner    break;
1788fe8babf689c624c53d03f5629324b049b5327a6eChris Lattner  }
1789ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  case ISD::AssertZext: {
17907ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    // AssertZext demands all of the high bits, plus any of the low bits
17917ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    // demanded by its users.
17927ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
17937ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    APInt InMask = APInt::getLowBitsSet(BitWidth,
17947ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson                                        VT.getSizeInBits());
17957ab15f6d4b9b9fd03c8ab6327ac2b1b3c4b4d04bOwen Anderson    if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
1796ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner                             KnownZero, KnownOne, TLO, Depth+1))
1797ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner      return true;
1798bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1799400f75cb5ed39ab4f071f78f6a26beefbc8a46f0Dan Gohman
18007b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    KnownZero |= ~InMask & NewMask;
1801ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    break;
1802ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  }
1803bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  case ISD::BITCAST:
180457f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings    // If this is an FP->Int bitcast and if the sign bit is the only
180557f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings    // thing demanded, turn this into a FGETSIGN.
1806ca072a39778933df4264ae393bd0ca797db59df6Eli Friedman    if (!TLO.LegalOperations() &&
1807ca072a39778933df4264ae393bd0ca797db59df6Eli Friedman        !Op.getValueType().isVector() &&
18080948f0acca745eef8db6922edfd8836f717396b4Eli Friedman        !Op.getOperand(0).getValueType().isVector() &&
18090c3e67860af417febb1fa9e870ece912a16085acNadav Rotem        NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
18100c3e67860af417febb1fa9e870ece912a16085acNadav Rotem        Op.getOperand(0).getValueType().isFloatingPoint()) {
181157f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
181257f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      bool i32Legal  = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
181357f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings      if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
181457f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
18152ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // Make a FGETSIGN + SHL to move the sign bit into the appropriate
18162ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner        // place.  We expect the SHL to be eliminated by other optimizations.
1817090bf19de6e7c75fbf34e753f5e0ad58cc2ca15cStuart Hastings        SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
181857f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
181957f1fde7fcda84a7e1d7490163fbad18c3755d40Stuart Hastings        if (!OpVTLegal && OpVTSizeInBits > 32)
1820090bf19de6e7c75fbf34e753f5e0ad58cc2ca15cStuart Hastings          Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
182183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands        unsigned ShVal = Op.getValueType().getSizeInBits()-1;
1822bdce3726e2131f053d05f07ff135684ad6f11d80Stuart Hastings        SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
18233dfc4b12229acafee5c69c5ee1673b08027f0438Stuart Hastings        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
18243dfc4b12229acafee5c69c5ee1673b08027f0438Stuart Hastings                                                 Op.getValueType(),
18252ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner                                                 Sign, ShAmt));
18262ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner      }
18272ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    }
18282ceb2cfa8e8580a7d080f5c6235ab133b97fb8fbChris Lattner    break;
182997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::ADD:
183097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::MUL:
183197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  case ISD::SUB: {
183297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // Add, Sub, and Mul don't demand any bits in positions beyond that
183397121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // of the highest bit demanded of them.
183497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    APInt LoMask = APInt::getLowBitsSet(BitWidth,
183597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                                        BitWidth - NewMask.countLeadingZeros());
183697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
183797121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
183897121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
183997121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
184097121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman                             KnownOne2, TLO, Depth+1))
184197121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
184297121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman    // See if the operation should be performed at a smaller bit width.
18434e39e9da0f3a435445261d0f796bb0913f3c2bf0Dan Gohman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
184497121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman      return true;
184597121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  }
184697121ba2afb8d566ff1bf5c4e8fc5d4077940a7fDan Gohman  // FALL THROUGH
184754eed371314af403ef8307b386b3b5b46c8aa6fbDan Gohman  default:
18481482b5fc7affd691fbd8ece7808ddd4559ad20aeChris Lattner    // Just use ComputeMaskedBits to compute output bits.
18497b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman    TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
1850a6bc5a4d2138ea3cba90f5a794dd525228ec2c73Chris Lattner    break;
1851de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  }
1852bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1853ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // If we know the value of all of the demanded bits, return this as a
1854ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner  // constant.
18557b8d4a9eef4eb02e561227b50c9d119cea4e8860Dan Gohman  if ((NewMask & (KnownZero|KnownOne)) == NewMask)
1856ec665151b891bf59d7c7c6cab180a978e2c20265Chris Lattner    return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
1857bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
1858de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman  return false;
1859de99629e2ae8cd7cc731328d2ad6ed6b8e759f2cNate Begeman}
1860c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner
1861bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1862bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// in Mask are known to be either zero or one and return them in the
1863368e18d56a87308045d341e85584597bfe7426e9Nate Begeman/// KnownZero/KnownOne bitsets.
1864bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckvoid TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1865977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman                                                    const APInt &Mask,
1866bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                    APInt &KnownZero,
1867fd29e0eb060ea8b4d490860329234d2ae5f5952eDan Gohman                                                    APInt &KnownOne,
1868ea859be53ca13a1547c4675549946b74dc3c6f41Dan Gohman                                                    const SelectionDAG &DAG,
1869368e18d56a87308045d341e85584597bfe7426e9Nate Begeman                                                    unsigned Depth) const {
18701b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
18711b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
18721b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
18731b5232a93767eac1424c67fb86580deef97e21e0Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1874c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         "Should use MaskedValueIsZero if you don't know whether Op"
1875c6fd6cd65c88ef1f11da43c11be0152cb69013a7Chris Lattner         " is a target node!");
1876977a76fbb6ea1b87dfd7fbbe2ae2afb63e982ff3Dan Gohman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
18773a03ebb37747c2b3fd9b4f8b44f1124f53727894Evan Cheng}
18784ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner
18795c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// ComputeNumSignBitsForTargetNode - This method can be implemented by
18805c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// targets that want to expose additional information about sign bits to the
18815c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner/// DAG Combiner.
1882475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanunsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
18835c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner                                                         unsigned Depth) const {
18845c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
18855c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
18865c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
18875c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
18885c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         "Should use ComputeNumSignBits if you don't know whether Op"
18895c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner         " is a target node!");
18905c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner  return 1;
18915c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner}
18925c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
189397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
189497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
189597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman/// determine which bit is set.
189697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman///
189785b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesenstatic bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
189897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // A left-shift of a constant one will have exactly one bit set, because
189997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // shifting the bit off the end is undefined.
190097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SHL)
190197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
190297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
190397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue() == 1)
190497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
190597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
190697d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Similarly, a right-shift of a constant sign-bit will have exactly
190797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // one bit set.
190897d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  if (Val.getOpcode() == ISD::SRL)
190997d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman    if (ConstantSDNode *C =
191097d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
191197d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman      if (C->getAPIntValue().isSignBit())
191297d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman        return true;
191397d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman
191497d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // More could be done here, though the above checks are enough
191597d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // to handle some common cases.
1916e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
191797d116346cdf47ce0c8536a69ff6c606309cb4c0Dan Gohman  // Fall back to ComputeMaskedBits to catch other known cases.
1918e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT OpVT = Val.getValueType();
19195b870aff81da0c07413f0241087bb3722954b83dDan Gohman  unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
1920e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt Mask = APInt::getAllOnesValue(BitWidth);
1921e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  APInt KnownZero, KnownOne;
1922e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman  DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
192385b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen  return (KnownZero.countPopulation() == BitWidth - 1) &&
192485b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen         (KnownOne.countPopulation() == 1);
1925e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman}
19265c3e21d68726dbfc313d8af3d320b0a6b3eeac36Chris Lattner
1927bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1928475871a144eb604ddaf37503397ba0941442e5fbDan Gohman/// and cc. If it is unable to simplify it, return a null SDValue.
1929475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue
1930e50ed30282bb5b4a9ed952580523f2dda16215acOwen AndersonTargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1931fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                              ISD::CondCode Cond, bool foldBooleans,
1932ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                              DAGCombinerInfo &DCI, DebugLoc dl) const {
1933fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  SelectionDAG &DAG = DCI.DAG;
1934fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
1935fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // These setcc operations always fold.
1936fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  switch (Cond) {
1937fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  default: break;
1938fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE:
1939fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1940fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE:
1941fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  case ISD::SETTRUE2:  return DAG.getConstant(1, VT);
1942fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
1943fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
19447a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // Ensure that the constant occurs on the RHS, and fold constant
19457a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // comparisons.
19467a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  if (isa<ConstantSDNode>(N0.getNode()))
1947b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1948362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
1949ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
19506c6cd1ccb5de1ebab7089c8ce339889f33fa2577Dan Gohman    const APInt &C1 = N1C->getAPIntValue();
1951b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
1952b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1953b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // equality comparison, then we're just comparing whether X itself is
1954b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // zero.
1955b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1956b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1957b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(1).getOpcode() == ISD::Constant) {
1958347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      const APInt &ShAmt
1959347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1960b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1961b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1962b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1963b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 0  -> X != 0
1964b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 1  -> X != 0
1965b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETNE;
1966b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        } else {
1967b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) != 0  -> X == 0
1968b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (srl (ctlz x), 5) == 1  -> X == 0
1969b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          Cond = ISD::SETEQ;
1970fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
1971b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        SDValue Zero = DAG.getConstant(0, N0.getValueType());
1972b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1973b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                            Zero, Cond);
1974fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
1975b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
197689217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen
1977d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    SDValue CTPOP = N0;
1978d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    // Look through truncs that don't change the value of a ctpop.
1979d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1980d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      CTPOP = N0.getOperand(0);
1981d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1982d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
1983c9b6a3eb90c5e0f6460632a443591068522537e2Benjamin Kramer        (N0 == CTPOP || N0.getValueType().getSizeInBits() >
1984d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer                        Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1985d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      EVT CTVT = CTPOP.getValueType();
1986d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      SDValue CTOp = CTPOP.getOperand(0);
1987d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1988d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // (ctpop x) u< 2 -> (x & x-1) == 0
1989d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // (ctpop x) u> 1 -> (x & x-1) != 0
1990d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1991d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1992d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer                                  DAG.getConstant(1, CTVT));
1993d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1994d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1995d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer        return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1996d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      }
1997d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
1998d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer      // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
1999d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer    }
2000d8228924556d3c465da5b858c620b29fd1cf298eBenjamin Kramer
2001e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    // (zext x) == C --> x == (trunc C)
2002e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
2003e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2004e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      unsigned MinBits = N0.getValueSizeInBits();
2005e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      SDValue PreZExt;
2006e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      if (N0->getOpcode() == ISD::ZERO_EXTEND) {
2007e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // ZExt
2008e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        MinBits = N0->getOperand(0).getValueSizeInBits();
2009e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        PreZExt = N0->getOperand(0);
2010e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      } else if (N0->getOpcode() == ISD::AND) {
2011e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // DAGCombine turns costly ZExts into ANDs
2012e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
2013e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          if ((C->getAPIntValue()+1).isPowerOf2()) {
2014e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer            MinBits = C->getAPIntValue().countTrailingOnes();
2015e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer            PreZExt = N0->getOperand(0);
2016e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          }
2017e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
2018e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        // ZEXTLOAD
2019e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
2020e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          MinBits = LN0->getMemoryVT().getSizeInBits();
2021e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          PreZExt = N0;
2022e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        }
2023e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      }
2024e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer
2025e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      // Make sure we're not loosing bits from the constant.
2026e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      if (MinBits < C1.getBitWidth() && MinBits > C1.getActiveBits()) {
2027e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
2028e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
2029e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          // Will get folded away.
2030e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
2031e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
2032e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer          return DAG.getSetCC(dl, VT, Trunc, C, Cond);
2033e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer        }
2034e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer      }
2035e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer    }
2036e7cf062537e898f830565db5dbf99ae9c928399eBenjamin Kramer
2037b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is '(and load, const)', the RHS is 0,
2038b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // the test is for equality or unsigned, and all 1 bits of the const are
2039b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // in the same partial word, see if we can shorten the load.
2040b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (DCI.isBeforeLegalize() &&
2041b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOpcode() == ISD::AND && C1 == 0 &&
2042b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getNode()->hasOneUse() &&
2043b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<LoadSDNode>(N0.getOperand(0)) &&
2044b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        N0.getOperand(0).getNode()->hasOneUse() &&
2045b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        isa<ConstantSDNode>(N0.getOperand(1))) {
2046b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
2047347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      APInt bestMask;
2048b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned bestWidth = 0, bestOffset = 0;
2049347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng      if (!Lod->isVolatile() && Lod->isUnindexed()) {
2050b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned origWidth = N0.getValueType().getSizeInBits();
2051347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        unsigned maskWidth = origWidth;
2052bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
2053b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // 8 bits, but have to be careful...
2054b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
2055b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          origWidth = Lod->getMemoryVT().getSizeInBits();
2056347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        const APInt &Mask =
2057347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2058b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        for (unsigned width = origWidth / 2; width>=8; width /= 2) {
2059347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          APInt newMask = APInt::getLowBitsSet(maskWidth, width);
2060b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          for (unsigned offset=0; offset<origWidth/width; offset++) {
2061b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            if ((newMask & Mask) == Mask) {
2062b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              if (!TD->isLittleEndian())
2063b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (origWidth/width - offset - 1) * (width/8);
2064b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              else
2065b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                bestOffset = (uint64_t)offset * (width/8);
2066347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng              bestMask = Mask.lshr(offset * (width/8) * 8);
2067b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              bestWidth = width;
2068b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              break;
206989217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen            }
2070b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            newMask = newMask << width;
207189217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen          }
207289217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
2073b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2074b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (bestWidth) {
2075c0c7fca2fedd2259569b7b84338259c62d38802dChris Lattner        EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
2076b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (newVT.isRound()) {
2077e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson          EVT PtrType = Lod->getOperand(1).getValueType();
2078b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Ptr = Lod->getBasePtr();
2079b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (bestOffset != 0)
2080b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
2081b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(bestOffset, PtrType));
2082b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
2083b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
2084ecf42c4720aba6ee315d0166045c54187ac2de4dChris Lattner                                Lod->getPointerInfo().getWithOffset(bestOffset),
2085d752e0f7e64585839cb3a458ef52456eaebbea3cPete Cooper                                        false, false, false, NewAlign);
2086bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          return DAG.getSetCC(dl, VT,
2087b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getNode(ISD::AND, dl, newVT, NewLoad,
2088347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                      DAG.getConstant(bestMask.trunc(bestWidth),
2089347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                                                      newVT)),
2090b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(0LL, newVT), Cond);
209189217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen        }
209289217a6f1852e764e58c489872b2d155dc2b7b8bDale Johannesen      }
2093b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2094d0ab34bf8feb7633b70158b571425577670b6326Bill Wendling
2095b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2096b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2097b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
2098fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2099b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // If the comparison constant has bits in the upper part, the
2100b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // zero-extended value could never match.
2101b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
2102b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                              C1.getBitWidth() - InSize))) {
2103fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        switch (Cond) {
2104fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGT:
2105fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETUGE:
2106b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETEQ: return DAG.getConstant(0, VT);
2107fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        case ISD::SETULT:
2108b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETULE:
2109b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETNE: return DAG.getConstant(1, VT);
2110b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGT:
2111b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETGE:
2112b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 is set.
2113b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNegative(), VT);
2114b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLT:
2115b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        case ISD::SETLE:
2116b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // True if the sign bit of C1 isn't set.
2117b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getConstant(C1.isNonNegative(), VT);
2118fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        default:
2119b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          break;
2120fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2121fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2122b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2123b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, we can perform the comparison with the low bits.
2124b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      switch (Cond) {
2125b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETEQ:
2126b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETNE:
2127b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGT:
2128b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETUGE:
2129b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULT:
2130b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      case ISD::SETULE: {
2131e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT newVT = N0.getOperand(0).getValueType();
2132b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DCI.isBeforeLegalizeOps() ||
2133b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            (isOperationLegal(ISD::SETCC, newVT) &&
2134b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman              getCondCodeAction(Cond, newVT)==Legal))
2135b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, N0.getOperand(0),
213640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad                              DAG.getConstant(C1.trunc(InSize), newVT),
2137b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond);
2138b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;
2139b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2140b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      default:
2141b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        break;   // todo, be more careful with signed comparisons
2142b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2143b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
21442c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng               (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2145e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2146b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
2147e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT ExtDstTy = N0.getValueType();
2148b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
2149b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2150ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      // If the constant doesn't fit into the number of bits for the source of
2151ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      // the sign extension, it is impossible for both sides to be equal.
2152ad78a88711979aa84a89a32576b615ff13b3de15Eli Friedman      if (C1.getMinSignedBits() > ExtSrcTyBits)
2153b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getConstant(Cond == ISD::SETNE, VT);
2154bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2155b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ZextOp;
2156e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT Op0Ty = N0.getOperand(0).getValueType();
2157b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (Op0Ty == ExtSrcTy) {
2158b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = N0.getOperand(0);
2159fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else {
2160b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
2161b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
2162b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              DAG.getConstant(Imm, Op0Ty));
2163fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2164b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (!DCI.isCalledByLegalizer())
2165b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        DCI.AddToWorklist(ZextOp.getNode());
2166b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // Otherwise, make this a use of a zext.
2167bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, ZextOp,
2168b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1 & APInt::getLowBitsSet(
2169b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                              ExtDstTyBits,
2170bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                                              ExtSrcTyBits),
2171b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                          ExtDstTy),
2172b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          Cond);
2173b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
2174b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2175b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // SETCC (SETCC), [0|1], [EQ|NE]  -> SETCC
21762c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      if (N0.getOpcode() == ISD::SETCC &&
21772c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
2178347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
2179b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (TrueWhenTrue)
2180bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
2181b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // Invert the condition.
2182b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
2183bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        CC = ISD::getSetCCInverse(CC,
2184b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  N0.getOperand(0).getValueType().isInteger());
2185b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
2186fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
21872c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
2188b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if ((N0.getOpcode() == ISD::XOR ||
2189bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck           (N0.getOpcode() == ISD::AND &&
2190b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(0).getOpcode() == ISD::XOR &&
2191b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2192b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          isa<ConstantSDNode>(N0.getOperand(1)) &&
2193b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
2194b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // If this is (X^1) == 0/1, swap the RHS and eliminate the xor.  We
2195b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        // can only do this if the top bits are known zero.
2196b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        unsigned BitWidth = N0.getValueSizeInBits();
2197b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (DAG.MaskedValueIsZero(N0,
2198b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                  APInt::getHighBitsSet(BitWidth,
2199b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                                                        BitWidth-1))) {
2200b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Okay, get the un-inverted input value.
2201b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          SDValue Val;
2202b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (N0.getOpcode() == ISD::XOR)
2203b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = N0.getOperand(0);
2204b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          else {
2205bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            assert(N0.getOpcode() == ISD::AND &&
2206b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                    N0.getOperand(0).getOpcode() == ISD::XOR);
2207b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            // ((X^1)&1)^1 -> X & 1
2208b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman            Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
2209b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(0).getOperand(0),
2210b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              N0.getOperand(1));
2211b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
22122c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
2213b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          return DAG.getSetCC(dl, VT, Val, N1,
2214b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2215b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        }
22162c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng      } else if (N1C->getAPIntValue() == 1 &&
22172c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                 (VT == MVT::i1 ||
221828b77e968d2b01fc9da724762bd8ddcd80650e32Duncan Sands                  getBooleanContents(false) == ZeroOrOneBooleanContent)) {
22192c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        SDValue Op0 = N0;
22202c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if (Op0.getOpcode() == ISD::TRUNCATE)
22212c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Op0 = Op0.getOperand(0);
22222c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng
22232c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        if ((Op0.getOpcode() == ISD::XOR) &&
22242c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(0).getOpcode() == ISD::SETCC &&
22252c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0.getOperand(1).getOpcode() == ISD::SETCC) {
22262c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
22272c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
22282c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
22292c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond);
22302c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        } else if (Op0.getOpcode() == ISD::AND &&
22312c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                isa<ConstantSDNode>(Op0.getOperand(1)) &&
22322c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
22332c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
223417458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov          if (Op0.getValueType().bitsGT(VT))
22352c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng            Op0 = DAG.getNode(ISD::AND, dl, VT,
22362c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
22372c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                          DAG.getConstant(1, VT));
223817458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov          else if (Op0.getValueType().bitsLT(VT))
223917458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov            Op0 = DAG.getNode(ISD::AND, dl, VT,
224017458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov                        DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
224117458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov                        DAG.getConstant(1, VT));
224217458a786e4d3eeff9c66fc0ec284b763df53642Anton Korobeynikov
22432c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng          return DAG.getSetCC(dl, VT, Op0,
22442c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              DAG.getConstant(0, Op0.getValueType()),
22452c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
22462c755ba12a79e0bb2899c0bde00b2f7ea2c975a0Evan Cheng        }
2247fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2248b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2249bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2250b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    APInt MinVal, MaxVal;
2251b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
2252b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (ISD::isSignedIntSetCC(Cond)) {
2253b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getSignedMinValue(OperandBitSize);
2254b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getSignedMaxValue(OperandBitSize);
2255b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    } else {
2256b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MinVal = APInt::getMinValue(OperandBitSize);
2257b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      MaxVal = APInt::getMaxValue(OperandBitSize);
2258b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2259fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2260b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2261b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2262b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MinVal) return DAG.getConstant(1, VT);   // X >= MIN --> true
2263b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X >= C0 --> X > (C0-1)
2264bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2265b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1-1, N1.getValueType()),
2266b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2267b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2268fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2269b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2270b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (C1 == MaxVal) return DAG.getConstant(1, VT);   // X <= MAX --> true
2271b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      // X <= C0 --> X < (C0+1)
2272bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2273b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(C1+1, N1.getValueType()),
2274b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2275b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2276b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2277b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2278b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X < MIN --> false
2279b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
2280b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X >= MIN --> true
2281b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
2282b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(0, VT);      // X > MAX --> false
2283b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
2284b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getConstant(1, VT);      // X <= MAX --> true
2285b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2286b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setgt X, Min --> setne X, Min
2287b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2288b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2289b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Canonicalize setlt X, Max --> setne X, Max
2290b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2291b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2292b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2293b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setult X, 1, turn it into seteq X, 0
2294b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2295bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2296bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                          DAG.getConstant(MinVal, N0.getValueType()),
2297b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
2298b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have setugt X, Max-1, turn it into seteq X, Max
2299b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2300bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2301b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(MaxVal, N0.getValueType()),
2302b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETEQ);
2303b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2304b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // If we have "setcc X, C0", check to see if we can shrink the immediate
2305b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // by changing cc.
2306b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2307b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETUGT X, SINTMAX  -> SETLT X, 0
2308bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    if (Cond == ISD::SETUGT &&
2309b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMaxValue(OperandBitSize))
2310bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck      return DAG.getSetCC(dl, VT, N0,
2311b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          DAG.getConstant(0, N1.getValueType()),
2312b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          ISD::SETLT);
2313b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2314b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // SETULT X, SINTMIN  -> SETGT X, -1
2315b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if (Cond == ISD::SETULT &&
2316b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        C1 == APInt::getSignedMinValue(OperandBitSize)) {
2317b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      SDValue ConstMinusOne =
2318b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
2319b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                          N1.getValueType());
2320b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
2321b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    }
2322b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman
2323b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    // Fold bit comparisons when we can.
2324b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman    if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2325d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        (VT == N0.getValueType() ||
2326d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng         (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
2327d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng        N0.getOpcode() == ISD::AND)
2328b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      if (ConstantSDNode *AndRHS =
2329b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman                  dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2330e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT ShiftTy = DCI.isBeforeLegalize() ?
233195771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson          getPointerTy() : getShiftAmountTy(N0.getValueType());
2332b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman        if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
2333b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if the AND RHS is a single bit.
2334347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng          if (AndRHS->getAPIntValue().isPowerOf2()) {
2335d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2336d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                              DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2337347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng                   DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
2338b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          }
2339347a9cb5070be6473a5f881dd29997b46081d23bEvan Cheng        } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
2340b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // (X & 8) == 8  -->  (X & 8) >> 3
2341b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          // Perform the xform if C1 is a single bit.
2342b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman          if (C1.isPowerOf2()) {
2343d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2344d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                               DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2345d40d03e1bd1d51857fc2f9f9230e334c3a32b249Evan Cheng                                      DAG.getConstant(C1.logBase2(), ShiftTy)));
2346fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2347fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2348b101b0bdbd22fa1bef3502fd7521da60038ac333Eli Friedman      }
2349fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2350fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2351ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (isa<ConstantFPSDNode>(N0.getNode())) {
2352fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Constant fold or commute setcc.
2353ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
2354ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (O.getNode()) return O;
2355ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
235663079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // If the RHS of an FP comparison is a constant, simplify it away in
235763079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // some cases.
235863079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (CFP->getValueAPF().isNaN()) {
235963079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      // If an operand is known to be a nan, we can fold it.
236063079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      switch (ISD::getUnorderedFlavor(Cond)) {
2361c23197a26f34f559ea9797de51e187087c039c42Torok Edwin      default: llvm_unreachable("Unknown flavor!");
236263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 0:  // Known false.
236363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(0, VT);
236463079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      case 1:  // Known true.
236563079f0757785c5c461bafdd3101ee40aeb717feChris Lattner        return DAG.getConstant(1, VT);
23661c3e1e2ed07597111f0b1b1bb93b8080c96c526bChris Lattner      case 2:  // Undefined.
2367e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen        return DAG.getUNDEF(VT);
236863079f0757785c5c461bafdd3101ee40aeb717feChris Lattner      }
236963079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    }
2370bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
237163079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // Otherwise, we know the RHS is not a NaN.  Simplify the node to drop the
237263079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // constant if knowing that the operand is non-nan is enough.  We prefer to
237363079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
237463079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    // materialize 0.0.
237563079f0757785c5c461bafdd3101ee40aeb717feChris Lattner    if (Cond == ISD::SETO || Cond == ISD::SETUO)
2376ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N0, Cond);
237711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman
237811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // If the condition is not legal, see if we can find an equivalent one
237911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    // which is legal.
238011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    if (!isCondCodeLegal(Cond, N0.getValueType())) {
238111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // If the comparison was an awkward floating-point == or != and one of
238211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // the comparison operands is infinity or negative infinity, convert the
238311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      // condition to a less-awkward <= or >=.
238411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      if (CFP->getValueAPF().isInfinity()) {
238511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        if (CFP->getValueAPF().isNegative()) {
238611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
238711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
238811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
238911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
239011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
239111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
239211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
239311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
239411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
239511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
239611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
239711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
239811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        } else {
239911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETOEQ &&
240011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
240111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
240211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUEQ &&
240311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
240411eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
240511eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETUNE &&
240611eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
240711eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
240811eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman          if (Cond == ISD::SETONE &&
240911eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
241011eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
241111eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman        }
241211eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman      }
241311eab02b770086c119a18aae0fe214fbe5eed0d0Dan Gohman    }
2414fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2415fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2416fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if (N0 == N1) {
2417fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // We can always fold X == X for integer setcc's.
241883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (N0.getValueType().isInteger())
2419fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2420fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    unsigned UOF = ISD::getUnorderedFlavor(Cond);
2421fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == 2)   // FP operators that are undefined on NaNs.
2422fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2423fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2424fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      return DAG.getConstant(UOF, VT);
2425fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
2426fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    // if it is not already.
2427fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2428fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (NewCond != Cond)
2429ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2430fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2431fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2432fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
243383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      N0.getValueType().isInteger()) {
2434fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2435fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N0.getOpcode() == ISD::XOR) {
2436fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Y) == (X+Z) -->  Y == Z
2437fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOpcode() == N1.getOpcode()) {
2438fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(0) == N1.getOperand(0))
2439ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2440fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (N0.getOperand(1) == N1.getOperand(1))
2441ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2442fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2443fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // If X op Y == Y op X, try other combinations.
2444fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(0) == N1.getOperand(1))
2445bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2446ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2447fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOperand(1) == N1.getOperand(0))
2448bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck            return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2449ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                                Cond);
2450fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2451fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2452bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2453fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2454fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2455fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X+C1) == C2 --> X == C2-C1
2456ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2457ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen            return DAG.getSetCC(dl, VT, N0.getOperand(0),
2458f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                DAG.getConstant(RHSC->getAPIntValue()-
2459f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                                                LHSR->getAPIntValue(),
2460fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                                N0.getValueType()), Cond);
2461fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2462bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2463fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2464fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (N0.getOpcode() == ISD::XOR)
2465fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // If we know that all of the inverted bits are zero, don't bother
2466fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng            // performing the inversion.
24672e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
24682e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman              return
2469ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                DAG.getSetCC(dl, VT, N0.getOperand(0),
24702e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             DAG.getConstant(LHSR->getAPIntValue() ^
24712e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                               RHSC->getAPIntValue(),
24722e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             N0.getValueType()),
24732e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                             Cond);
2474fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2475bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2476fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        // Turn (C1-X) == C2 --> X == C1-C2
2477fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2478ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif          if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
24792e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman            return
2480ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen              DAG.getSetCC(dl, VT, N0.getOperand(1),
24812e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           DAG.getConstant(SUBC->getAPIntValue() -
24822e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                             RHSC->getAPIntValue(),
24832e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                                           N0.getValueType()),
24842e68b6f52d0979575b2f02ed29717d907ba0684cDan Gohman                           Cond);
2485fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          }
2486bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        }
2487fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2488fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2489fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify (X+Z) == X -->  Z == 0
2490fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(0) == N1)
2491ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N0.getOperand(1),
2492fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N0.getValueType()), Cond);
2493fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N0.getOperand(1) == N1) {
2494fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N0.getOpcode()))
2495ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0),
2496fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N0.getValueType()), Cond);
2497ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        else if (N0.getNode()->hasOneUse()) {
2498fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2499fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // (Z-X) == X  --> Z == X<<1
2500ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
2501bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                     N1,
250295771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                       DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
2503fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2504ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2505ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2506fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2507fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2508fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2509fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2510fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2511fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        N1.getOpcode() == ISD::XOR) {
2512fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // Simplify  X == (X+Z) -->  Z == 0
2513fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (N1.getOperand(0) == N0) {
2514ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen        return DAG.getSetCC(dl, VT, N1.getOperand(1),
2515fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                        DAG.getConstant(0, N1.getValueType()), Cond);
2516fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      } else if (N1.getOperand(1) == N0) {
2517fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        if (DAG.isCommutativeBinOp(N1.getOpcode())) {
2518ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1.getOperand(0),
2519fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng                          DAG.getConstant(0, N1.getValueType()), Cond);
2520ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        } else if (N1.getNode()->hasOneUse()) {
2521fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2522fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          // X == (Z-X)  --> X<<1 == Z
2523bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
252495771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                       DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
2525fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng          if (!DCI.isCalledByLegalizer())
2526ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif            DCI.AddToWorklist(SH.getNode());
2527ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2528fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng        }
2529fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      }
2530fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2531e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman
25322c65c3dfe6589a630d1bdde085aec1b9cdc43ea8Dan Gohman    // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
253385b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // Note that where y is variable and is known to have at most
253485b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // one bit set (for example, if it is z&1) we cannot do this;
253585b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen    // the expressions are not equivalent when y==0.
2536e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N0.getOpcode() == ISD::AND)
2537e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
253885b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N1, DAG)) {
2539e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2540e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N1.getValueType());
2541ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N0, Zero, Cond);
2542e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2543e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2544e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman    if (N1.getOpcode() == ISD::AND)
2545e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
254685b0edec4672f6ac6c15f757b119d04232a61861Dale Johannesen        if (ValueHasExactlyOneBitSet(N0, DAG)) {
2547e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2548e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman          SDValue Zero = DAG.getConstant(0, N0.getValueType());
2549ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen          return DAG.getSetCC(dl, VT, N1, Zero, Cond);
2550e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman        }
2551e5af2d3a224d4b38760a26d237cde040cb6e14ebDan Gohman      }
2552fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2553fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2554fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Fold away ALL boolean setcc's.
2555475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Temp;
2556825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (N0.getValueType() == MVT::i1 && foldBooleans) {
2557fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    switch (Cond) {
2558c23197a26f34f559ea9797de51e187087c039c42Torok Edwin    default: llvm_unreachable("Unknown integer setcc!");
25594c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETEQ:  // X == Y  -> ~(X^Y)
2560825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2561825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNOT(dl, Temp, MVT::i1);
2562fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2563ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2564fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2565fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    case ISD::SETNE:  // X != Y   -->  (X^Y)
2566825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2567fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25684c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGT:  // X >s Y   -->  X == 0 & Y == 1  -->  ~X & Y
25694c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULT: // X <u Y   -->  X == 0 & Y == 1  -->  ~X & Y
2570825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2571825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2572fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2573ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2574fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25754c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLT:  // X <s Y   --> X == 1 & Y == 0  -->  ~Y & X
25764c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGT: // X >u Y   --> X == 1 & Y == 0  -->  ~Y & X
2577825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2578825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2579fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2580ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2581fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25824c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETULE: // X <=u Y  --> X == 0 | Y == 1  -->  ~X | Y
25834c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETGE:  // X >=s Y  --> X == 0 | Y == 1  -->  ~X | Y
2584825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N0, MVT::i1);
2585825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2586fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2587ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(Temp.getNode());
2588fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
25894c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETUGE: // X >=u Y  --> X == 1 | Y == 0  -->  ~Y | X
25904c2454623841f05c6c665659b34c214950d12d7eBob Wilson    case ISD::SETLE:  // X <=s Y  --> X == 1 | Y == 0  -->  ~Y | X
2591825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      Temp = DAG.getNOT(dl, N1, MVT::i1);
2592825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2593fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      break;
2594fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2595825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (VT != MVT::i1) {
2596fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      if (!DCI.isCalledByLegalizer())
2597ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        DCI.AddToWorklist(N0.getNode());
2598fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng      // FIXME: If running after legalize, we probably can't do this.
2599ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen      N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2600fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    }
2601fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng    return N0;
2602fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  }
2603fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2604fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng  // Could not fold it.
2605475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
2606fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng}
2607fa1eb27b76ab1e0f78574bf52a432c84a4c1a520Evan Cheng
2608ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2609ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng/// node is a GlobalAddress + offset.
26100a9481f44fe4fc76e59109992940a76b2a3f9b3bChris Lattnerbool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
2611ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng                                    int64_t &Offset) const {
2612ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (isa<GlobalAddressSDNode>(N)) {
26139ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
26149ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    GA = GASD->getGlobal();
26159ea3f56d07875ecb4ae2cd5bc14a9563b9742553Dan Gohman    Offset += GASD->getOffset();
2616ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    return true;
2617ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
2618ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2619ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  if (N->getOpcode() == ISD::ADD) {
2620475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N1 = N->getOperand(0);
2621475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue N2 = N->getOperand(1);
2622ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2623ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2624ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
26257810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2626ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2627ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2628ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2629ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2630ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      if (V) {
26317810bfed5570c192e0714a8fd0e5130a0c38dd2eDan Gohman        Offset += V->getSExtValue();
2632ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng        return true;
2633ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng      }
2634ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng    }
2635ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  }
263695771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson
2637ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng  return false;
2638ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng}
2639ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2640ad4196b44ae714a6b95e238d9d96303df74b0429Evan Cheng
2641475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue TargetLowering::
264200ffed0468ad406062b7c08c2ff46d79d2d1be4dChris LattnerPerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
264300ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner  // Default implementation: no optimization.
2644475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return SDValue();
264500ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner}
264600ffed0468ad406062b7c08c2ff46d79d2d1be4dChris Lattner
2647eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2648eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//  Inline Assembler Implementation Methods
2649eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner//===----------------------------------------------------------------------===//
2650eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
26514376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
2652eb8146b5ee4b9b66d6294f62a5ed556e332018abChris LattnerTargetLowering::ConstraintType
26534234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris LattnerTargetLowering::getConstraintType(const std::string &Constraint) const {
26544234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  if (Constraint.size() == 1) {
26554234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    switch (Constraint[0]) {
26564234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    default: break;
26574234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'r': return C_RegisterClass;
26584234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'm':    // memory
26594234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'o':    // offsetable
26604234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'V':    // not offsetable
26614234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Memory;
26624234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'i':    // Simple Integer or Relocatable Constant
26634234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'n':    // Simple Integer
266467aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'E':    // Floating Point Constant
266567aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'F':    // Floating Point Constant
26664234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 's':    // Relocatable Constant
266767aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case 'p':    // Address.
2668c13dd1cf4c0d83ac3ed2a6b0c36fab72e9d6c6e5Chris Lattner    case 'X':    // Allow ANY value.
26694234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'I':    // Target registers.
26704234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'J':
26714234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'K':
26724234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'L':
26734234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'M':
26744234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'N':
26754234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'O':
26764234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    case 'P':
267767aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case '<':
267867aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    case '>':
26794234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner      return C_Other;
26804234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner    }
2681eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
2682bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2683bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (Constraint.size() > 1 && Constraint[0] == '{' &&
2684065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner      Constraint[Constraint.size()-1] == '}')
2685065421f99fc155c3910a77c3a47de99f3f6af4d0Chris Lattner    return C_Register;
26864234f57fa02b1f04a9f52a7b3c2aa22d32ac521cChris Lattner  return C_Unknown;
2687eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2688eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
2689ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// LowerXConstraint - try to replace an X constraint, which matches anything,
2690ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// with another that has more specific requirements based on the type of the
2691ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen/// corresponding operand.
2692e50ed30282bb5b4a9ed952580523f2dda16215acOwen Andersonconst char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
269383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isInteger())
26945e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "r";
269583ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  if (ConstraintVT.isFloatingPoint())
26965e764233f398b6929b67701672a5e78fec20ce2eChris Lattner    return "f";      // works for many targets
26975e764233f398b6929b67701672a5e78fec20ce2eChris Lattner  return 0;
2698ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen}
2699ba2a0b960ea4c73d0f81557f63ae2ea126e08905Dale Johannesen
270048884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
270148884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner/// vector.  If it is invalid, don't add anything to Ops.
2702475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2703100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher                                                  std::string &Constraint,
2704475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                  std::vector<SDValue> &Ops,
27055e764233f398b6929b67701672a5e78fec20ce2eChris Lattner                                                  SelectionDAG &DAG) const {
2706362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2707100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher  if (Constraint.length() > 1) return;
2708362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2709100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher  char ConstraintLetter = Constraint[0];
2710eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  switch (ConstraintLetter) {
27119ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner  default: break;
2712eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 'X':     // Allows any operand; labels (basic block) use this.
2713eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    if (Op.getOpcode() == ISD::BasicBlock) {
2714eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      Ops.push_back(Op);
2715eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen      return;
2716eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    }
2717eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen    // fall through
2718eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'i':    // Simple Integer or Relocatable Constant
2719eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  case 'n':    // Simple Integer
2720eb57ea7ea2378b77bc995371c1888193b960cd03Dale Johannesen  case 's': {  // Relocatable Constant
272175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // These operands are interested in values of the form (GV+C), where C may
272275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // be folded in as an offset of GV, or it may be explicitly added.  Also, it
272375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // is possible and fine if either GV or C are missing.
272475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
272575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2726bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
272775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we have "(add GV, C)", pull out GV/C
272875c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (Op.getOpcode() == ISD::ADD) {
272975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
273075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
273175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0) {
273275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
273375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
273475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
273575c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (C == 0 || GA == 0)
273675c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        C = 0, GA = 0;
273775c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
2738bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
273975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // If we find a valid operand, map to the TargetXXX version so that the
274075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    // value itself doesn't get selected.
274175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (GA) {   // Either &GV   or   &GV+C
274275c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      if (ConstraintLetter != 'n') {
274375c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner        int64_t Offs = GA->getOffset();
2744f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman        if (C) Offs += C->getZExtValue();
2745bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck        Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
274607538ad975994123317fe5b32daed4bf28683a1dDevang Patel                                                 C ? C->getDebugLoc() : DebugLoc(),
274748884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner                                                 Op.getValueType(), Offs));
274848884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
274975c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner      }
275075c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    }
275175c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner    if (C) {   // just C, no GV.
27529ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner      // Simple constants are not allowed for 's'.
275348884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      if (ConstraintLetter != 's') {
275478e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // gcc prints these as sign extended.  Sign extend value to 64 bits
275578e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // now; without this it would get ZExt'd later in
275678e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        // ScheduleDAGSDNodes::EmitNode, which is very generic.
275778e3e521cd90e5f6382c14b6e4d809fb59610ed6Dale Johannesen        Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
2758825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                                            MVT::i64));
275948884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner        return;
276048884cd80b52be1528618f2e9b3425ac24e7b5caChris Lattner      }
27619ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    }
27629ff6ee85fe281c5686e0885eaa0c37ad5378ed52Chris Lattner    break;
2763eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner  }
276475c7d2bd551acd1ad4f0f58b763ec8840f1d9c34Chris Lattner  }
2765eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner}
2766eb8146b5ee4b9b66d6294f62a5ed556e332018abChris Lattner
27671efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattnerstd::pair<unsigned, const TargetRegisterClass*> TargetLowering::
27684217ca8dc175f7268a4335c8406dedd901e8e631Chris LattnergetRegForInlineAsmConstraint(const std::string &Constraint,
2769e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                             EVT VT) const {
27701efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  if (Constraint[0] != '{')
27717d9663c70b3300070298d716dba6e6f6ce2d1e3eDouglas Gregor    return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
2772a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2773a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner
2774a55079a5ccdf0cdb4d482fb47a3fb21825f56713Chris Lattner  // Remove the braces from around the name.
277505872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer  StringRef RegName(Constraint.data()+1, Constraint.size()-2);
27761efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
27771efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  // Figure out which register class contains this reg.
27786f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  const TargetRegisterInfo *RI = TM.getRegisterInfo();
27796f0d024a534af18d9e60b3ea757376cd8a3a980eDan Gohman  for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
27801efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner       E = RI->regclass_end(); RCI != E; ++RCI) {
27811efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    const TargetRegisterClass *RC = *RCI;
2782bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2783bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    // If none of the value types for this register class are valid, we
2784b3befd41b4b5aa882bed9796bbb097df29b505acChris Lattner    // can't use it.  For example, 64-bit reg classes on 32-bit targets.
278522e8a366adf5a4c78148928ff64e7e00c1088492Jakob Stoklund Olesen    if (!isLegalRC(RC))
278622e8a366adf5a4c78148928ff64e7e00c1088492Jakob Stoklund Olesen      continue;
2787bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2788bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
27891efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner         I != E; ++I) {
279005872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer      if (RegName.equals_lower(RI->getName(*I)))
27911efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        return std::make_pair(*I, RC);
27921efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    }
27934ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner  }
2794bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
27957d9663c70b3300070298d716dba6e6f6ce2d1e3eDouglas Gregor  return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
27964ccb070f158b0f331c68de800c6bab8c31c2ecb6Chris Lattner}
279730b37b5f29991874648d839d018aa2921b39355fEvan Cheng
279830b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
27994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner// Constraint Selection.
28004376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
28016bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// isMatchingInputConstraint - Return true of this is an input operand that is
28026bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattner/// a matching constraint like "4".
28036bdcda3d3e30003fb6cef1d4e2fd3a5d5b40d3fcChris Lattnerbool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
280458f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
280558f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return isdigit(ConstraintCode[0]);
280658f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
280758f15c482a7129c78ca809792b46befa20ea337dChris Lattner
280858f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// getMatchedOperand - If this is an input matching constraint, this method
280958f15c482a7129c78ca809792b46befa20ea337dChris Lattner/// returns the output operand it matches.
281058f15c482a7129c78ca809792b46befa20ea337dChris Lattnerunsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
281158f15c482a7129c78ca809792b46befa20ea337dChris Lattner  assert(!ConstraintCode.empty() && "No known constraint!");
281258f15c482a7129c78ca809792b46befa20ea337dChris Lattner  return atoi(ConstraintCode.c_str());
281358f15c482a7129c78ca809792b46befa20ea337dChris Lattner}
281458f15c482a7129c78ca809792b46befa20ea337dChris Lattner
2815bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2816eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// ParseConstraints - Split up the constraint string from the inline
2817eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// assembly value into the specific constraints and their prefixes,
2818eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and also tie in the associated operand values.
2819eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// If this returns an empty vector, and if the constraint string itself
2820eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// isn't empty, there was an error parsing.
282144ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
2822eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ImmutableCallSite CS) const {
2823eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  /// ConstraintOperands - Information about all of the constraints.
282444ab89eb376af838d1123293a79975aede501464John Thompson  AsmOperandInfoVector ConstraintOperands;
2825eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
282667aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  unsigned maCount = 0; // Largest number of multiple alternative constraints.
2827eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2828eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Do a prepass over the constraints, canonicalizing them, and building up the
2829eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // ConstraintOperands list.
283044ab89eb376af838d1123293a79975aede501464John Thompson  InlineAsm::ConstraintInfoVector
2831eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ConstraintInfos = IA->ParseConstraints();
2832bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2833eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
2834eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  unsigned ResNo = 0;   // ResNo - The result number of the next output.
2835eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2836eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2837eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2838eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &OpInfo = ConstraintOperands.back();
2839eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
284067aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    // Update multiple alternative constraint count.
284167aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    if (OpInfo.multipleAlternatives.size() > maCount)
284267aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson      maCount = OpInfo.multipleAlternatives.size();
284367aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson
284444ab89eb376af838d1123293a79975aede501464John Thompson    OpInfo.ConstraintVT = MVT::Other;
2845eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2846eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // Compute the value type for each operand.
2847eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    switch (OpInfo.Type) {
2848eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isOutput:
2849eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Indirect outputs just consume an argument.
2850eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      if (OpInfo.isIndirect) {
2851eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2852eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        break;
2853eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2854eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2855eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // The return value of the call is this value.  As such, there is no
2856eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // corresponding argument.
2857eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      assert(!CS.getType()->isVoidTy() &&
2858eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson             "Bad inline asm!");
2859db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
286044ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = getValueType(STy->getElementType(ResNo));
2861eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      } else {
2862eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        assert(ResNo == 0 && "Asm only has one result!");
286344ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = getValueType(CS.getType());
2864eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2865eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      ++ResNo;
2866eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2867eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isInput:
2868eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2869eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2870eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case InlineAsm::isClobber:
2871eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Nothing to do.
2872eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
2873eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2874bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
287544ab89eb376af838d1123293a79975aede501464John Thompson    if (OpInfo.CallOperandVal) {
2876db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
287744ab89eb376af838d1123293a79975aede501464John Thompson      if (OpInfo.isIndirect) {
2878db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner        llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
287944ab89eb376af838d1123293a79975aede501464John Thompson        if (!PtrTy)
288044ab89eb376af838d1123293a79975aede501464John Thompson          report_fatal_error("Indirect operand for inline asm not a pointer!");
288144ab89eb376af838d1123293a79975aede501464John Thompson        OpTy = PtrTy->getElementType();
288244ab89eb376af838d1123293a79975aede501464John Thompson      }
2883362fee90b9a1d64ac091755466caf6a94ade22ebEric Christopher
2884cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher      // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
2885db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner      if (StructType *STy = dyn_cast<StructType>(OpTy))
2886cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher        if (STy->getNumElements() == 1)
2887cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher          OpTy = STy->getElementType(0);
2888cef81b7e519764c6a008a689bc4c6dfc9119f8e9Eric Christopher
288944ab89eb376af838d1123293a79975aede501464John Thompson      // If OpTy is not a single value, it may be a struct/union that we
289044ab89eb376af838d1123293a79975aede501464John Thompson      // can tile with integers.
289144ab89eb376af838d1123293a79975aede501464John Thompson      if (!OpTy->isSingleValueType() && OpTy->isSized()) {
289244ab89eb376af838d1123293a79975aede501464John Thompson        unsigned BitSize = TD->getTypeSizeInBits(OpTy);
289344ab89eb376af838d1123293a79975aede501464John Thompson        switch (BitSize) {
289444ab89eb376af838d1123293a79975aede501464John Thompson        default: break;
289544ab89eb376af838d1123293a79975aede501464John Thompson        case 1:
289644ab89eb376af838d1123293a79975aede501464John Thompson        case 8:
289744ab89eb376af838d1123293a79975aede501464John Thompson        case 16:
289844ab89eb376af838d1123293a79975aede501464John Thompson        case 32:
289944ab89eb376af838d1123293a79975aede501464John Thompson        case 64:
290044ab89eb376af838d1123293a79975aede501464John Thompson        case 128:
290171365d3774a6c02f3f198fbf08a56e4b6346bbccDale Johannesen          OpInfo.ConstraintVT =
290271365d3774a6c02f3f198fbf08a56e4b6346bbccDale Johannesen              EVT::getEVT(IntegerType::get(OpTy->getContext(), BitSize), true);
290344ab89eb376af838d1123293a79975aede501464John Thompson          break;
290444ab89eb376af838d1123293a79975aede501464John Thompson        }
290544ab89eb376af838d1123293a79975aede501464John Thompson      } else if (dyn_cast<PointerType>(OpTy)) {
290644ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = MVT::getIntegerVT(8*TD->getPointerSize());
290744ab89eb376af838d1123293a79975aede501464John Thompson      } else {
290844ab89eb376af838d1123293a79975aede501464John Thompson        OpInfo.ConstraintVT = EVT::getEVT(OpTy, true);
290944ab89eb376af838d1123293a79975aede501464John Thompson      }
291044ab89eb376af838d1123293a79975aede501464John Thompson    }
2911eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
2912eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2913eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // If we have multiple alternative constraints, select the best alternative.
2914eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  if (ConstraintInfos.size()) {
2915eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (maCount) {
2916eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      unsigned bestMAIndex = 0;
2917eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      int bestWeight = -1;
2918eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // weight:  -1 = invalid match, and 0 = so-so match to 5 = good match.
2919eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      int weight = -1;
2920eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      unsigned maIndex;
2921eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Compute the sums of the weights for each alternative, keeping track
2922eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // of the best (highest weight) one so far.
2923eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      for (maIndex = 0; maIndex < maCount; ++maIndex) {
2924eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        int weightSum = 0;
2925eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2926eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            cIndex != eIndex; ++cIndex) {
2927eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2928eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (OpInfo.Type == InlineAsm::isClobber)
2929eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            continue;
2930eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
293144ab89eb376af838d1123293a79975aede501464John Thompson          // If this is an output operand with a matching input operand,
293244ab89eb376af838d1123293a79975aede501464John Thompson          // look up the matching input. If their types mismatch, e.g. one
293344ab89eb376af838d1123293a79975aede501464John Thompson          // is an integer, the other is floating point, or their sizes are
293444ab89eb376af838d1123293a79975aede501464John Thompson          // different, flag it as an maCantMatch.
2935eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (OpInfo.hasMatchingInput()) {
2936eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
2937eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2938eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson              if ((OpInfo.ConstraintVT.isInteger() !=
2939eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                   Input.ConstraintVT.isInteger()) ||
2940eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                  (OpInfo.ConstraintVT.getSizeInBits() !=
2941eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                   Input.ConstraintVT.getSizeInBits())) {
2942eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                weightSum = -1;  // Can't match.
2943eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                break;
2944eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson              }
2945eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            }
2946eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          }
2947eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2948eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          if (weight == -1) {
2949eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            weightSum = -1;
2950eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson            break;
2951eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          }
2952eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          weightSum += weight;
2953eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2954eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        // Update best.
2955eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if (weightSum > bestWeight) {
2956eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          bestWeight = weightSum;
2957eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          bestMAIndex = maIndex;
2958eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2959eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2960eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2961eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      // Now select chosen alternative in each constraint.
2962eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2963eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          cIndex != eIndex; ++cIndex) {
2964eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2965eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if (cInfo.Type == InlineAsm::isClobber)
2966eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          continue;
2967eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        cInfo.selectAlternative(bestMAIndex);
2968eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
2969eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2970eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
2971eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
2972eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Check and hook up tied operands, choose constraint code to use.
2973eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2974eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      cIndex != eIndex; ++cIndex) {
2975eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2976bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
2977eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // If this is an output operand with a matching input operand, look up the
2978eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // matching input. If their types mismatch, e.g. one is an integer, the
2979eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // other is floating point, or their sizes are different, flag it as an
2980eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // error.
2981eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (OpInfo.hasMatchingInput()) {
2982eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
298344ab89eb376af838d1123293a79975aede501464John Thompson
2984eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      if (OpInfo.ConstraintVT != Input.ConstraintVT) {
29855427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	std::pair<unsigned, const TargetRegisterClass*> MatchRC =
29865427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	  getRegForInlineAsmConstraint(OpInfo.ConstraintCode, OpInfo.ConstraintVT);
29875427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	std::pair<unsigned, const TargetRegisterClass*> InputRC =
29885427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher	  getRegForInlineAsmConstraint(Input.ConstraintCode, Input.ConstraintVT);
2989eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        if ((OpInfo.ConstraintVT.isInteger() !=
2990eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson             Input.ConstraintVT.isInteger()) ||
29915427edeb68d653ced860ed14f83848ebbb01b64bEric Christopher            (MatchRC.second != InputRC.second)) {
2992eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson          report_fatal_error("Unsupported asm: input constraint"
2993eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                             " with a matching output constraint of"
2994eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson                             " incompatible type!");
2995eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson        }
2996eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      }
299744ab89eb376af838d1123293a79975aede501464John Thompson
2998eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    }
2999eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
3000eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
3001eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return ConstraintOperands;
3002eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
3003eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
300458f15c482a7129c78ca809792b46befa20ea337dChris Lattner
30054376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// getConstraintGenerality - Return an integer indicating how general CT
30064376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is.
30074376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
30084376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  switch (CT) {
3009c23197a26f34f559ea9797de51e187087c039c42Torok Edwin  default: llvm_unreachable("Unknown constraint type!");
30104376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Other:
30114376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Unknown:
30124376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 0;
30134376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Register:
30144376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 1;
30154376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_RegisterClass:
30164376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 2;
30174376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  case TargetLowering::C_Memory:
30184376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    return 3;
30194376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
30204376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
30214376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
302244ab89eb376af838d1123293a79975aede501464John Thompson/// Examine constraint type and operand type and determine a weight value.
3023eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// This object must already have been set up with the operand type
3024eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and the current alternative constraint selected.
302544ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::ConstraintWeight
302644ab89eb376af838d1123293a79975aede501464John Thompson  TargetLowering::getMultipleConstraintMatchWeight(
3027eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &info, int maIndex) const {
302844ab89eb376af838d1123293a79975aede501464John Thompson  InlineAsm::ConstraintCodeVector *rCodes;
302967aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  if (maIndex >= (int)info.multipleAlternatives.size())
303067aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    rCodes = &info.Codes;
303167aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  else
303267aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson    rCodes = &info.multipleAlternatives[maIndex].Codes;
303344ab89eb376af838d1123293a79975aede501464John Thompson  ConstraintWeight BestWeight = CW_Invalid;
3034eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
3035eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Loop over the options, keeping track of the most general one.
303667aff164c039765e3ec19e5a31659250c8427dfbJohn Thompson  for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
303744ab89eb376af838d1123293a79975aede501464John Thompson    ConstraintWeight weight =
303844ab89eb376af838d1123293a79975aede501464John Thompson      getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
3039eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    if (weight > BestWeight)
3040eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      BestWeight = weight;
3041eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
3042eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
3043eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return BestWeight;
3044eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
3045eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
304644ab89eb376af838d1123293a79975aede501464John Thompson/// Examine constraint type and operand type and determine a weight value.
3047eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// This object must already have been set up with the operand type
3048eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson/// and the current alternative constraint selected.
304944ab89eb376af838d1123293a79975aede501464John ThompsonTargetLowering::ConstraintWeight
305044ab89eb376af838d1123293a79975aede501464John Thompson  TargetLowering::getSingleConstraintMatchWeight(
3051eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    AsmOperandInfo &info, const char *constraint) const {
305244ab89eb376af838d1123293a79975aede501464John Thompson  ConstraintWeight weight = CW_Invalid;
3053eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  Value *CallOperandVal = info.CallOperandVal;
3054eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // If we don't have a value, we can't do a match,
3055eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    // but allow it at the lowest weight.
3056eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  if (CallOperandVal == NULL)
305744ab89eb376af838d1123293a79975aede501464John Thompson    return CW_Default;
3058eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  // Look at the constraint type.
3059eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  switch (*constraint) {
3060eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'i': // immediate integer.
3061eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'n': // immediate integer with a known value.
306244ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<ConstantInt>(CallOperandVal))
306344ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
3064eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
3065eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 's': // non-explicit intregal immediate.
306644ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<GlobalValue>(CallOperandVal))
306744ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
306844ab89eb376af838d1123293a79975aede501464John Thompson      break;
306944ab89eb376af838d1123293a79975aede501464John Thompson    case 'E': // immediate float if host format.
307044ab89eb376af838d1123293a79975aede501464John Thompson    case 'F': // immediate float.
307144ab89eb376af838d1123293a79975aede501464John Thompson      if (isa<ConstantFP>(CallOperandVal))
307244ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Constant;
3073eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
307444ab89eb376af838d1123293a79975aede501464John Thompson    case '<': // memory operand with autodecrement.
307544ab89eb376af838d1123293a79975aede501464John Thompson    case '>': // memory operand with autoincrement.
3076eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'm': // memory operand.
3077eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'o': // offsettable memory operand
3078eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'V': // non-offsettable memory operand
307944ab89eb376af838d1123293a79975aede501464John Thompson      weight = CW_Memory;
3080eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
308144ab89eb376af838d1123293a79975aede501464John Thompson    case 'r': // general register.
3082eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    case 'g': // general register, memory operand or immediate integer.
308344ab89eb376af838d1123293a79975aede501464John Thompson              // note: Clang converts "g" to "imr".
308444ab89eb376af838d1123293a79975aede501464John Thompson      if (CallOperandVal->getType()->isIntegerTy())
308544ab89eb376af838d1123293a79975aede501464John Thompson        weight = CW_Register;
3086eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
308744ab89eb376af838d1123293a79975aede501464John Thompson    case 'X': // any operand.
3088eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson    default:
308944ab89eb376af838d1123293a79975aede501464John Thompson      weight = CW_Default;
3090eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson      break;
3091eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  }
3092eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson  return weight;
3093eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson}
3094eac6e1d0c748afc3d1496be0753ffbe5f5a4279bJohn Thompson
30954376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ChooseConstraint - If there are multiple different constraints that we
30964376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
309724e1a9d3115a5bf4e1c21092ede0f6dfc0a810adChris Lattner/// This is somewhat tricky: constraints fall into four classes:
30984376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Other         -> immediates and magic values
30994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Register      -> one specific register
31004376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    RegisterClass -> a group of regs
31014376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///    Memory        -> memory
31024376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// Ideally, we would pick the most specific constraint possible: if we have
31034376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// something that fits into a register, we would pick it.  The problem here
31044376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// is that if we have something that could either be in a register or in
31054376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// memory that use of the register could cause selection of *other*
31064376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// operands to fail: they might only succeed if we pick memory.  Because of
31074376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// this the heuristic we use is:
31084376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
31094376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  1) If there is an 'other' constraint, and if the operand is valid for
31104376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     that constraint, use it.  This makes us take advantage of 'i'
31114376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     constraints when available.
31124376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///  2) Otherwise, pick the most general constraint present.  This prefers
31134376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///     'm' over 'r', for example.
31144376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner///
31154376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattnerstatic void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
31161784d160e4efa75782884d451d0788b9457e67dcDale Johannesen                             const TargetLowering &TLI,
3117475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                             SDValue Op, SelectionDAG *DAG) {
31184376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
31194376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  unsigned BestIdx = 0;
31204376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
31214376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  int BestGenerality = -1;
3122a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen
31234376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Loop over the options, keeping track of the most general one.
31244376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
31254376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    TargetLowering::ConstraintType CType =
31264376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      TLI.getConstraintType(OpInfo.Codes[i]);
3127a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen
31285a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // If this is an 'other' constraint, see if the operand is valid for it.
31295a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // For example, on X86 we might have an 'rI' constraint.  If the operand
31305a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // is an integer in the range [0..31] we want to use I (saving a load
31315a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    // of a register), otherwise we must use 'r'.
3132ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    if (CType == TargetLowering::C_Other && Op.getNode()) {
31335a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      assert(OpInfo.Codes[i].size() == 1 &&
31345a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner             "Unhandled multi-letter 'other' constraint");
3135475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      std::vector<SDValue> ResultOps;
3136100c83341676d8aae8fc34b5452563ed08b14f3eEric Christopher      TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
31375a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                       ResultOps, *DAG);
31385a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      if (!ResultOps.empty()) {
31395a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestType = CType;
31405a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        BestIdx = i;
31415a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner        break;
31425a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner      }
31435a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner    }
3144bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3145a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    // Things with matching constraints can only be registers, per gcc
3146a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    // documentation.  This mainly affects "g" constraints.
3147a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen    if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
3148a5989f8e222f6e2ad67704d7e8cc67c86c4d0697Dale Johannesen      continue;
3149bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31504376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // This constraint letter is more general than the previous one, use it.
31514376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    int Generality = getConstraintGenerality(CType);
31524376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (Generality > BestGenerality) {
31534376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestType = CType;
31544376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestIdx = i;
31554376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      BestGenerality = Generality;
31564376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
31574376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
3158bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31594376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
31604376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  OpInfo.ConstraintType = BestType;
31614376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
31624376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
31634376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// ComputeConstraintToUse - Determines the constraint code and constraint
31644376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// type to use for the specific AsmOperandInfo, setting
31654376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
31665a09690446a36f94f990db7d18d9b9ac8587888aChris Lattnervoid TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
3167bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck                                            SDValue Op,
31685a09690446a36f94f990db7d18d9b9ac8587888aChris Lattner                                            SelectionDAG *DAG) const {
31694376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
3170bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31714376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // Single-letter constraints ('r') are very common.
31724376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.Codes.size() == 1) {
31734376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintCode = OpInfo.Codes[0];
31744376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
31754376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  } else {
31761784d160e4efa75782884d451d0788b9457e67dcDale Johannesen    ChooseConstraint(OpInfo, *this, Op, DAG);
31774376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
3178bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31794376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  // 'X' matches anything.
31804376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
31814376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Labels and constants are handled elsewhere ('X' is the only thing
31828ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    // that matches labels).  For Functions, the type here is the type of
31835339c551b508d8231672b417d9df1629e42af89bDale Johannesen    // the result, which is not what we want to look at; leave them alone.
31845339c551b508d8231672b417d9df1629e42af89bDale Johannesen    Value *v = OpInfo.CallOperandVal;
31858ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
31868ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen      OpInfo.CallOperandVal = v;
31874376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      return;
31888ea5ec681bd4838c84e545f8a1226a62d3b2f089Dale Johannesen    }
3189bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
31904376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // Otherwise, try to resolve it to something we know about by looking at
31914376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    // the actual operand type.
31924376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
31934376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintCode = Repl;
31944376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner      OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
31954376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner    }
31964376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner  }
31974376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner}
31984376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner
31994376fea6631d41fff3f2a7c6186faed9eff59619Chris Lattner//===----------------------------------------------------------------------===//
320030b37b5f29991874648d839d018aa2921b39355fEvan Cheng//  Loop Strength Reduction hooks
320130b37b5f29991874648d839d018aa2921b39355fEvan Cheng//===----------------------------------------------------------------------===//
320230b37b5f29991874648d839d018aa2921b39355fEvan Cheng
32031436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// isLegalAddressingMode - Return true if the addressing mode represented
32041436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner/// by AM is legal for this target, for a load/store of the specified type.
3205bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peckbool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
3206db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                           Type *Ty) const {
32071436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // The default implementation of this implements a conservative RISCy, r+r and
32081436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // r+i addr mode.
32091436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
32101436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // Allows a sign-extended 16-bit immediate field.
32111436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
32121436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
3213bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
32141436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  // No global is ever allowed as a base.
32151436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  if (AM.BaseGV)
32161436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    return false;
3217bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3218bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  // Only support r+r,
32191436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  switch (AM.Scale) {
32201436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 0:  // "r+i" or just "i", depending on HasBaseReg.
32211436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32221436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 1:
32231436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
32241436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
32251436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Otherwise we have r+r or r+i.
32261436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32271436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  case 2:
32281436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
32291436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner      return false;
32301436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    // Allow 2*r as r+r.
32311436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner    break;
32321436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  }
3233bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
32341436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner  return true;
32351436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner}
32361436bb657d22b01fd9a526ee7f9b2cb880c064a7Chris Lattner
32379c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer/// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
32389c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer/// with the multiplicative inverse of the constant.
32399c64030445cbe6ac486b90c5f459f91e06770474Benjamin KramerSDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
32409c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer                                       SelectionDAG &DAG) const {
32419c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  ConstantSDNode *C = cast<ConstantSDNode>(Op2);
32429c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  APInt d = C->getAPIntValue();
32439c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  assert(d != 0 && "Division by zero!");
32449c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32459c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  // Shift the value upfront if it is even, so the LSB is one.
32469c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  unsigned ShAmt = d.countTrailingZeros();
32479c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  if (ShAmt) {
32489c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    // TODO: For UDIV use SRL instead of SRA.
32499c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
32509c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
32519c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    d = d.ashr(ShAmt);
32529c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  }
32539c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32549c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  // Calculate the multiplicative inverse, using Newton's method.
32559c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  APInt t, xn = d;
32569c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  while ((t = d*xn) != 1)
32579c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer    xn *= APInt(d.getBitWidth(), 2) - t;
32589c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
32599c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  Op2 = DAG.getConstant(xn, Op1.getValueType());
32609c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer  return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
32619c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer}
32629c64030445cbe6ac486b90c5f459f91e06770474Benjamin Kramer
3263dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3264dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
3265dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
3266dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
326719a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard OsborneSDValue TargetLowering::
326819a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard OsborneBuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
326919a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne          std::vector<SDNode*>* Created) const {
3270e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
3271ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl= N->getDebugLoc();
3272bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3273dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
3274fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should be more aggressive here.
3275fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (!isTypeLegal(VT))
3276fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman    return SDValue();
3277bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3278fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
32794e5ea553d055512b0b8aa098e363ae17bafda957Jay Foad  APInt::ms magics = d.magic();
3280bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck
3281dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
3282fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  // FIXME: We should support doing a MUL in a wider type
3283475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Q;
328419a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne  if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
328519a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne                            isOperationLegalOrCustom(ISD::MULHS, VT))
3286ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
3287525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                    DAG.getConstant(magics.m, VT));
328819a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne  else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
328919a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne                                 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
3290ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
3291525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman                              N->getOperand(0),
3292ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif                              DAG.getConstant(magics.m, VT)).getNode(), 1);
3293525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
3294475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhs or equvialent
3295dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d > 0 and m < 0, add the numerator
3296bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck  if (d.isStrictlyPositive() && magics.m.isNegative()) {
3297ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
3298dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3299ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3300dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3301dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // If d < 0 and m > 0, subtract the numerator.
3302fc69cb475f3df50fb47c0cf8df90c2a77b7ff36aEli Friedman  if (d.isNegative() && magics.m.isStrictlyPositive()) {
3303ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
3304dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3305ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3306dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3307dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Shift right algebraic if shift value is nonzero
3308dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.s > 0) {
3309bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    Q = DAG.getNode(ISD::SRA, dl, VT, Q,
331095771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3311dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3312ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(Q.getNode());
3313dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3314dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Extract the sign bit and add it to the quotient
3315475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue T =
3316ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
331795771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                                           getShiftAmountTy(Q.getValueType())));
3318dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
3319ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(T.getNode());
3320ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  return DAG.getNode(ISD::ADD, dl, VT, Q, T);
3321dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
3322dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
3323dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3324dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// return a DAG expression to select that will generate the same value by
3325dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// multiplying by a magic number.  See:
3326dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
332719a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard OsborneSDValue TargetLowering::
332819a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard OsborneBuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
332919a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne          std::vector<SDNode*>* Created) const {
3330e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = N->getValueType(0);
3331ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen  DebugLoc dl = N->getDebugLoc();
3332201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3333dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Check to see if we can do this.
3334201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should be more aggressive here.
3335201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  if (!isTypeLegal(VT))
3336201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman    return SDValue();
3337201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3338201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should use a narrower constant when the upper
3339201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // bits are known to be zero.
33401c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
33411c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  APInt::mu magics = N1C.magicu();
33421c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33431c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  SDValue Q = N->getOperand(0);
33441c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33451c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  // If the divisor is even, we can avoid using the expensive fixup by shifting
33461c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  // the divided value upfront.
33471c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  if (magics.a != 0 && !N1C[0]) {
33481c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    unsigned Shift = N1C.countTrailingZeros();
33491c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = DAG.getNode(ISD::SRL, dl, VT, Q,
33501c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer                    DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
33511c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    if (Created)
33521c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer      Created->push_back(Q.getNode());
33531c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer
33541c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    // Get magic number for the shifted divisor.
33551c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    magics = N1C.lshr(Shift).magicu(Shift);
33561c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    assert(magics.a == 0 && "Should use cheap fixup now");
33571c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer  }
3358201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman
3359dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  // Multiply the numerator (operand 0) by the magic value
3360201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman  // FIXME: We should support doing a MUL in a wider type
336119a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne  if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
336219a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne                            isOperationLegalOrCustom(ISD::MULHU, VT))
33631c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
336419a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne  else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
336519a4daff9bbe18dab2620e25ac6cbf0635639ec6Richard Osborne                                 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
33661c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
33671c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer                            DAG.getConstant(magics.m, VT)).getNode(), 1);
3368525178cdbf00720ea8bce297a7d65b0cca0ab439Dan Gohman  else
3369475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return SDValue();       // No mulhu or equvialent
3370dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (Created)
3371ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif    Created->push_back(Q.getNode());
3372dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth
3373dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  if (magics.a == 0) {
33741c10b8de46ce8e39178eeded4786b1e53722f099Benjamin Kramer    assert(magics.s < N1C.getBitWidth() &&
3375201c9776bd4197569b71fef0519f98a28d1db989Eli Friedman           "We shouldn't generate an undefined shift!");
3376bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    return DAG.getNode(ISD::SRL, dl, VT, Q,
337795771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3378dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  } else {
3379ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
3380dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3381ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3382bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
338395771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson                      DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
3384dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3385ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3386ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen    NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
3387dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth    if (Created)
3388ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif      Created->push_back(NPQ.getNode());
3389bf17cfa3f904e488e898ac2e3af706fd1a892f08Wesley Peck    return DAG.getNode(ISD::SRL, dl, VT, NPQ,
339095771afbfd604ad003fa3723cac66c9370fed55dOwen Anderson             DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
3391dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth  }
3392dae9cbe8d4fcd8f182a99403d67cae906bdb3175Andrew Lenharth}
3393