1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This implements the TargetLowering class.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetLowering.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCAsmInfo.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCExpr.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetLoweringObjectFile.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetRegisterInfo.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalVariable.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/Analysis.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineJumpTableInfo.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/SelectionDAG.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/CommandLine.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MathExtras.h"
3219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <cctype>
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// We are in the process of implementing a new TypeLegalization action
3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// - the promotion of vector elements. This feature is disabled by default
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and only enabled using this flag.
3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic cl::opt<bool>
3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAllowPromoteIntElem("promote-elements", cl::Hidden,
4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  cl::desc("Allow promotion of integer vector element types"));
4119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTLSModel::Model getTLSModel(const GlobalValue *GV, Reloc::Model reloc) {
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLocal = GV->hasLocalLinkage();
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isDeclaration = GV->isDeclaration();
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: what should we do for protected and internal visibility?
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For variables, is internal different from hidden?
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isHidden = GV->hasHiddenVisibility();
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (reloc == Reloc::PIC_) {
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isLocal || isHidden)
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLSModel::LocalDynamic;
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLSModel::GeneralDynamic;
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isDeclaration || isHidden)
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLSModel::LocalExec;
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLSModel::InitialExec;
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InitLibcallNames - Set default libcall names.
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void InitLibcallNames(const char **Names) {
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SHL_I16] = "__ashlhi3";
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SHL_I32] = "__ashlsi3";
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SHL_I64] = "__ashldi3";
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SHL_I128] = "__ashlti3";
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRL_I16] = "__lshrhi3";
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRL_I32] = "__lshrsi3";
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRL_I64] = "__lshrdi3";
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRL_I128] = "__lshrti3";
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRA_I16] = "__ashrhi3";
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRA_I32] = "__ashrsi3";
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRA_I64] = "__ashrdi3";
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SRA_I128] = "__ashrti3";
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_I8] = "__mulqi3";
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_I16] = "__mulhi3";
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_I32] = "__mulsi3";
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_I64] = "__muldi3";
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_I128] = "__multi3";
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::MULO_I32] = "__mulosi4";
8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::MULO_I64] = "__mulodi4";
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::MULO_I128] = "__muloti4";
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SDIV_I8] = "__divqi3";
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SDIV_I16] = "__divhi3";
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SDIV_I32] = "__divsi3";
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SDIV_I64] = "__divdi3";
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SDIV_I128] = "__divti3";
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UDIV_I8] = "__udivqi3";
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UDIV_I16] = "__udivhi3";
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UDIV_I32] = "__udivsi3";
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UDIV_I64] = "__udivdi3";
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UDIV_I128] = "__udivti3";
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SREM_I8] = "__modqi3";
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SREM_I16] = "__modhi3";
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SREM_I32] = "__modsi3";
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SREM_I64] = "__moddi3";
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SREM_I128] = "__modti3";
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UREM_I8] = "__umodqi3";
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UREM_I16] = "__umodhi3";
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UREM_I32] = "__umodsi3";
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UREM_I64] = "__umoddi3";
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UREM_I128] = "__umodti3";
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // These are generally not available.
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SDIVREM_I8] = 0;
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SDIVREM_I16] = 0;
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SDIVREM_I32] = 0;
11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SDIVREM_I64] = 0;
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SDIVREM_I128] = 0;
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::UDIVREM_I8] = 0;
11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::UDIVREM_I16] = 0;
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::UDIVREM_I32] = 0;
11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::UDIVREM_I64] = 0;
11819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::UDIVREM_I128] = 0;
11919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEG_I32] = "__negsi2";
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEG_I64] = "__negdi2";
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::ADD_F32] = "__addsf3";
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::ADD_F64] = "__adddf3";
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::ADD_F80] = "__addxf3";
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SUB_F32] = "__subsf3";
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SUB_F64] = "__subdf3";
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SUB_F80] = "__subxf3";
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_F32] = "__mulsf3";
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_F64] = "__muldf3";
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_F80] = "__mulxf3";
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::DIV_F32] = "__divsf3";
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::DIV_F64] = "__divdf3";
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::DIV_F80] = "__divxf3";
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::REM_F32] = "fmodf";
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::REM_F64] = "fmod";
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::REM_F80] = "fmodl";
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::REM_PPCF128] = "fmodl";
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::FMA_F32] = "fmaf";
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::FMA_F64] = "fma";
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::FMA_F80] = "fmal";
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::FMA_PPCF128] = "fmal";
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POWI_F32] = "__powisf2";
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POWI_F64] = "__powidf2";
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POWI_F80] = "__powixf2";
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POWI_PPCF128] = "__powitf2";
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SQRT_F32] = "sqrtf";
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SQRT_F64] = "sqrt";
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SQRT_F80] = "sqrtl";
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SQRT_PPCF128] = "sqrtl";
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG_F32] = "logf";
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG_F64] = "log";
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG_F80] = "logl";
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG_PPCF128] = "logl";
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG2_F32] = "log2f";
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG2_F64] = "log2";
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG2_F80] = "log2l";
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG2_PPCF128] = "log2l";
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG10_F32] = "log10f";
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG10_F64] = "log10";
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG10_F80] = "log10l";
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::LOG10_PPCF128] = "log10l";
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP_F32] = "expf";
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP_F64] = "exp";
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP_F80] = "expl";
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP_PPCF128] = "expl";
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP2_F32] = "exp2f";
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP2_F64] = "exp2";
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP2_F80] = "exp2l";
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::EXP2_PPCF128] = "exp2l";
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SIN_F32] = "sinf";
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SIN_F64] = "sin";
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SIN_F80] = "sinl";
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SIN_PPCF128] = "sinl";
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COS_F32] = "cosf";
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COS_F64] = "cos";
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COS_F80] = "cosl";
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COS_PPCF128] = "cosl";
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POW_F32] = "powf";
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POW_F64] = "pow";
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POW_F80] = "powl";
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::POW_PPCF128] = "powl";
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::CEIL_F32] = "ceilf";
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::CEIL_F64] = "ceil";
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::CEIL_F80] = "ceill";
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::CEIL_PPCF128] = "ceill";
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::TRUNC_F32] = "truncf";
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::TRUNC_F64] = "trunc";
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::TRUNC_F80] = "truncl";
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::TRUNC_PPCF128] = "truncl";
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::RINT_F32] = "rintf";
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::RINT_F64] = "rint";
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::RINT_F80] = "rintl";
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::RINT_PPCF128] = "rintl";
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEARBYINT_F64] = "nearbyint";
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FLOOR_F32] = "floorf";
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FLOOR_F64] = "floor";
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FLOOR_F80] = "floorl";
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FLOOR_PPCF128] = "floorl";
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COPYSIGN_F32] = "copysignf";
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COPYSIGN_F64] = "copysign";
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COPYSIGN_F80] = "copysignl";
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::COPYSIGN_PPCF128] = "copysignl";
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPEXT_F16_F32] = "__gnu_h2f_ieee";
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_F32_F16] = "__gnu_f2h_ieee";
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfqi";
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfhi";
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F64_I8] = "__fixdfqi";
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F64_I16] = "__fixdfhi";
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfqi";
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfhi";
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F64_I8] = "__fixunsdfqi";
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F64_I16] = "__fixunsdfhi";
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OEQ_F32] = "__eqsf2";
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OEQ_F64] = "__eqdf2";
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UNE_F32] = "__nesf2";
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UNE_F64] = "__nedf2";
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OGE_F32] = "__gesf2";
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OGE_F64] = "__gedf2";
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OLT_F32] = "__ltsf2";
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OLT_F64] = "__ltdf2";
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OLE_F32] = "__lesf2";
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OLE_F64] = "__ledf2";
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OGT_F32] = "__gtsf2";
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::OGT_F64] = "__gtdf2";
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UO_F32] = "__unordsf2";
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UO_F64] = "__unorddf2";
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::O_F32] = "__unordsf2";
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::O_F64] = "__unorddf2";
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MEMCPY] = "memcpy";
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MEMMOVE] = "memmove";
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::MEMSET] = "memset";
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1";
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2";
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4";
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8";
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_1] = "__sync_lock_test_and_set_1";
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_2] = "__sync_lock_test_and_set_2";
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_4] = "__sync_lock_test_and_set_4";
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_LOCK_TEST_AND_SET_8] = "__sync_lock_test_and_set_8";
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_ADD_1] = "__sync_fetch_and_add_1";
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_ADD_2] = "__sync_fetch_and_add_2";
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_ADD_4] = "__sync_fetch_and_add_4";
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_ADD_8] = "__sync_fetch_and_add_8";
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_SUB_1] = "__sync_fetch_and_sub_1";
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_SUB_2] = "__sync_fetch_and_sub_2";
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_SUB_4] = "__sync_fetch_and_sub_4";
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_SUB_8] = "__sync_fetch_and_sub_8";
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_AND_1] = "__sync_fetch_and_and_1";
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_AND_2] = "__sync_fetch_and_and_2";
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_AND_4] = "__sync_fetch_and_and_4";
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_AND_8] = "__sync_fetch_and_and_8";
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_OR_1] = "__sync_fetch_and_or_1";
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_OR_2] = "__sync_fetch_and_or_2";
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_OR_4] = "__sync_fetch_and_or_4";
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_OR_8] = "__sync_fetch_and_or_8";
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_XOR_1] = "__sync_fetch_and_xor_1";
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_XOR_2] = "__sync_fetch_and_xor_2";
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Names[RTLIB::SYNC_FETCH_AND_XOR_4] = "__sync_fetch_and_xor_4";
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_XOR_8] = "__sync_fetch_and_xor_8";
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_NAND_1] = "__sync_fetch_and_nand_1";
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_NAND_2] = "__sync_fetch_and_nand_2";
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_NAND_4] = "__sync_fetch_and_nand_4";
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Names[RTLIB::SYNC_FETCH_AND_NAND_8] = "__sync_fetch_and_nand_8";
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InitLibcallCallingConvs - Set default libcall CallingConvs.
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void InitLibcallCallingConvs(CallingConv::ID *CCs) {
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCs[i] = CallingConv::C;
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getFPEXT - Return the FPEXT_*_* value for the given types, or
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpVT == MVT::f32) {
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f64)
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPEXT_F32_F64;
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getFPROUND - Return the FPROUND_*_* value for the given types, or
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RetVT == MVT::f32) {
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpVT == MVT::f64)
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPROUND_F64_F32;
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpVT == MVT::f80)
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPROUND_F80_F32;
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpVT == MVT::ppcf128)
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPROUND_PPCF128_F32;
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RetVT == MVT::f64) {
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpVT == MVT::f80)
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPROUND_F80_F64;
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpVT == MVT::ppcf128)
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPROUND_PPCF128_F64;
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpVT == MVT::f32) {
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i8)
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F32_I8;
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i16)
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F32_I16;
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F32_I32;
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F32_I64;
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F32_I128;
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::f64) {
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i8)
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F64_I8;
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i16)
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F64_I16;
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F64_I32;
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F64_I64;
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F64_I128;
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::f80) {
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F80_I32;
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F80_I64;
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_F80_I128;
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::ppcf128) {
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_PPCF128_I32;
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_PPCF128_I64;
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOSINT_PPCF128_I128;
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpVT == MVT::f32) {
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i8)
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F32_I8;
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i16)
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F32_I16;
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F32_I32;
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F32_I64;
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F32_I128;
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::f64) {
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i8)
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F64_I8;
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i16)
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F64_I16;
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F64_I32;
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F64_I64;
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F64_I128;
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::f80) {
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F80_I32;
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F80_I64;
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_F80_I128;
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::ppcf128) {
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i32)
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_PPCF128_I32;
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i64)
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_PPCF128_I64;
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::i128)
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return FPTOUINT_PPCF128_I128;
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpVT == MVT::i32) {
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I32_F32;
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I32_F64;
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I32_F80;
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I32_PPCF128;
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::i64) {
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I64_F32;
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I64_F64;
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I64_F80;
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I64_PPCF128;
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::i128) {
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I128_F32;
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I128_F64;
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I128_F80;
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SINTTOFP_I128_PPCF128;
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// UNKNOWN_LIBCALL if there is none.
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpVT == MVT::i32) {
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I32_F32;
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I32_F64;
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I32_F80;
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I32_PPCF128;
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::i64) {
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I64_F32;
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I64_F64;
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I64_F80;
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I64_PPCF128;
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (OpVT == MVT::i128) {
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RetVT == MVT::f32)
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I128_F32;
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f64)
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I128_F64;
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::f80)
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I128_F80;
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (RetVT == MVT::ppcf128)
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return UINTTOFP_I128_PPCF128;
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return UNKNOWN_LIBCALL;
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InitCmpLibcallCCs - Set default comparison libcall CC.
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void InitCmpLibcallCCs(ISD::CondCode *CCs) {
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::UNE_F32] = ISD::SETNE;
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::UNE_F64] = ISD::SETNE;
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OGE_F32] = ISD::SETGE;
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OGE_F64] = ISD::SETGE;
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OLT_F32] = ISD::SETLT;
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OLT_F64] = ISD::SETLT;
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OLE_F32] = ISD::SETLE;
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OLE_F64] = ISD::SETLE;
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OGT_F32] = ISD::SETGT;
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::OGT_F64] = ISD::SETGT;
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::UO_F32] = ISD::SETNE;
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::UO_F64] = ISD::SETNE;
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::O_F32] = ISD::SETEQ;
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCs[RTLIB::O_F64] = ISD::SETEQ;
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// NOTE: The constructor takes ownership of TLOF.
54419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanTargetLowering::TargetLowering(const TargetMachine &tm,
54519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               const TargetLoweringObjectFile *tlof)
54619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : TM(tm), TD(TM.getTargetData()), TLOF(*tlof),
54719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  mayPromoteElements(AllowPromoteIntElem) {
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All operations default to being supported.
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(OpActions, 0, sizeof(OpActions));
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(LoadExtActions, 0, sizeof(LoadExtActions));
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(CondCodeActions, 0, sizeof(CondCodeActions));
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Set default actions for various operations.
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Default all indexed load / store to expand.
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned IM = (unsigned)ISD::PRE_INC;
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
56319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These operations default to expand.
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Most targets ignore the @llvm.prefetch intrinsic.
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
57119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
57219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // ConstantFP nodes default to expand.  Targets can either change this to
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Legal, in which case all fp constants are legal, or use isFPImmLegal()
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to optimize expansions for certain constants.
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These library functions default to expand.
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG , MVT::f64, Expand);
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG2, MVT::f64, Expand);
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG10,MVT::f64, Expand);
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP , MVT::f64, Expand);
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP2, MVT::f64, Expand);
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG , MVT::f32, Expand);
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG2, MVT::f32, Expand);
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG10,MVT::f32, Expand);
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP , MVT::f32, Expand);
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP2, MVT::f32, Expand);
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Default ISD::TRAP to expand (which turns it into abort).
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::TRAP, MVT::Other, Expand);
59319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  IsLittleEndian = TD->isLittleEndian();
59519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  PointerTy = MVT::getIntegerVT(8*TD->getPointerSize());
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
59919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  maxStoresPerMemsetOptSize = maxStoresPerMemcpyOptSize
60019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    = maxStoresPerMemmoveOptSize = 4;
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  benefitFromCodePlacementOpt = false;
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  UseUnderscoreSetJmp = false;
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  UseUnderscoreLongJmp = false;
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SelectIsExpensive = false;
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  IntDivIsCheap = false;
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pow2DivIsCheap = false;
60719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  JumpIsExpensive = false;
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  StackPointerRegisterToSaveRestore = 0;
60919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ExceptionPointerRegister = 0;
61019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ExceptionSelectorRegister = 0;
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BooleanContents = UndefinedBooleanContent;
61219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BooleanVectorContents = UndefinedBooleanContent;
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SchedPreferenceInfo = Sched::Latency;
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpBufSize = 0;
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpBufAlignment = 0;
61619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MinFunctionAlignment = 0;
61719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  PrefFunctionAlignment = 0;
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrefLoopAlignment = 0;
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MinStackArgumentAlignment = 1;
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShouldFoldAtomicFences = false;
62119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InsertFencesForAtomic = false;
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InitLibcallNames(LibcallRoutineNames);
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InitCmpLibcallCCs(CmpLibcallCCs);
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InitLibcallCallingConvs(LibcallCallingConvs);
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::~TargetLowering() {
62919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  delete &TLOF;
63019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
63119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
63219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMVT TargetLowering::getShiftAmountTy(EVT LHSTy) const {
63319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return MVT::getIntegerVT(8*TD->getPointerSize());
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// canOpTrap - Returns true if the operation can trap for the value type.
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// VT must be a legal type.
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isTypeLegal(VT));
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op) {
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FDIV:
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FREM:
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SDIV:
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UDIV:
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SREM:
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UREM:
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          unsigned &NumIntermediates,
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          EVT &RegisterVT,
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          TargetLowering *TLI) {
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Figure out the right, legal destination reg to copy into.
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElts = VT.getVectorNumElements();
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MVT EltTy = VT.getVectorElementType();
66119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumVectorRegs = 1;
66319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
66419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // could break down into LHS/RHS like LegalizeDAG does.
666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isPowerOf2_32(NumElts)) {
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumVectorRegs = NumElts;
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumElts = 1;
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
67019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Divide the input until we get to a supported size.  This will always
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // end with a scalar if the target doesn't support vectors.
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumElts >>= 1;
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumVectorRegs <<= 1;
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NumIntermediates = NumVectorRegs;
67919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!TLI->isTypeLegal(NewVT))
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewVT = EltTy;
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  IntermediateVT = NewVT;
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
68519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NewVTSize = NewVT.getSizeInBits();
68619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
68719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Convert sizes such as i33 to i64.
68819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isPowerOf2_32(NewVTSize))
68919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    NewVTSize = NextPowerOf2(NewVTSize);
69019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DestVT = TLI->getRegisterType(NewVT);
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RegisterVT = DestVT;
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
69419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
69519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, promotion or legal types use the same number of registers as
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the vector decimated to the appropriate level.
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NumVectorRegs;
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isLegalRC - Return true if the value types that can be represented by the
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified register class are all legal.
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool TargetLowering::isLegalRC(const TargetRegisterClass *RC) const {
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I) {
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isTypeLegal(*I))
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// hasLegalSuperRegRegClasses - Return true if the specified register class
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// has one or more super-reg register classes that are legal.
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::hasLegalSuperRegRegClasses(const TargetRegisterClass *RC) const{
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (*RC->superregclasses_begin() == 0)
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = RC->superregclasses_end(); I != E; ++I) {
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetRegisterClass *RRC = *I;
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isLegalRC(RRC))
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findRepresentativeClass - Return the largest legal super-reg register class
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of the register class for the specified type and its associated "cost".
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<const TargetRegisterClass*, uint8_t>
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::findRepresentativeClass(EVT VT) const {
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *RC = RegClassForVT[VT.getSimpleVT().SimpleTy];
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!RC)
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return std::make_pair(RC, 0);
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *BestRC = RC;
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (TargetRegisterInfo::regclass_iterator I = RC->superregclasses_begin(),
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = RC->superregclasses_end(); I != E; ++I) {
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetRegisterClass *RRC = *I;
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RRC->isASubClass() || !isLegalRC(RRC))
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!hasLegalSuperRegRegClasses(RRC))
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(RRC, 1);
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BestRC = RRC;
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(BestRC, 1);
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
74719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// computeRegisterProperties - Once all of the register classes are added,
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// this allows us to compute derived properties we expose.
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid TargetLowering::computeRegisterProperties() {
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Too many value types for ValueTypeActions to hold!");
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everything defaults to needing one register.
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumRegistersForVT[i] = 1;
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ...except isVoid, which doesn't need any registers.
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NumRegistersForVT[MVT::isVoid] = 0;
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Find the largest integer register class.
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Every integer value type larger than this largest register takes twice as
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // many registers to represent as the previous ValueType.
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned ExpandedReg = LargestIntReg + 1; ; ++ExpandedReg) {
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT ExpandedVT = (MVT::SimpleValueType)ExpandedReg;
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!ExpandedVT.isInteger())
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
77619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ValueTypeActions.setTypeAction(ExpandedVT, TypeExpandInteger);
777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Inspect all of the ValueType's smaller than the largest integer
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // register to see which ones need promotion.
781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned LegalIntReg = LargestIntReg;
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned IntReg = LargestIntReg - 1;
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       IntReg >= (unsigned)MVT::i1; --IntReg) {
784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT IVT = (MVT::SimpleValueType)IntReg;
785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isTypeLegal(IVT)) {
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LegalIntReg = IntReg;
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (MVT::SimpleValueType)LegalIntReg;
79019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ppcf128 type is really two f64's.
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(MVT::ppcf128)) {
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TransformToType[MVT::ppcf128] = MVT::f64;
79919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
80019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Decide how to handle f64. If the target does not have native f64 support,
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // expand it to i64 and we will be generating soft float library calls.
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(MVT::f64)) {
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TransformToType[MVT::f64] = MVT::i64;
80819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Decide how to handle f32. If the target does not have native support for
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(MVT::f32)) {
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isTypeLegal(MVT::f64)) {
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TransformToType[MVT::f32] = MVT::f64;
81819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ValueTypeActions.setTypeAction(MVT::f32, TypePromoteInteger);
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TransformToType[MVT::f32] = MVT::i32;
82319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
82619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over all of the vector value types to see which need transformations.
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MVT VT = (MVT::SimpleValueType)i;
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isTypeLegal(VT)) continue;
83219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
83319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Determine if there is a legal wider type.  If so, we should promote to
83419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // that wider vector type.
83519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT EltVT = VT.getVectorElementType();
83619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NElts = VT.getVectorNumElements();
83719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NElts != 1) {
83819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool IsLegalWiderType = false;
83919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If we allow the promotion of vector elements using a flag,
84019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // then return TypePromoteInteger on vector elements.
84119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // First try to promote the elements of integer vectors. If no legal
84219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // promotion was found, fallback to the widen-vector method.
84319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (mayPromoteElements)
84419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
84519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT SVT = (MVT::SimpleValueType)nVT;
84619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Promote vectors of integers to vectors with the same number
84719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // of elements, with a wider element type.
84819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (SVT.getVectorElementType().getSizeInBits() > EltVT.getSizeInBits()
84919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            && SVT.getVectorNumElements() == NElts &&
85019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            isTypeLegal(SVT) && SVT.getScalarType().isInteger()) {
85119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          TransformToType[i] = SVT;
85219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          RegisterTypeForVT[i] = SVT;
85319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          NumRegistersForVT[i] = 1;
85419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
85519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          IsLegalWiderType = true;
85619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
85719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
85819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
85919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
86019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (IsLegalWiderType) continue;
86119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
86219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Try to widen the vector.
86319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
86419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT SVT = (MVT::SimpleValueType)nVT;
86519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (SVT.getVectorElementType() == EltVT &&
86619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            SVT.getVectorNumElements() > NElts &&
86719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            isTypeLegal(SVT)) {
86819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          TransformToType[i] = SVT;
86919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          RegisterTypeForVT[i] = SVT;
87019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          NumRegistersForVT[i] = 1;
87119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ValueTypeActions.setTypeAction(VT, TypeWidenVector);
87219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          IsLegalWiderType = true;
87319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
87419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
87519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
87619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (IsLegalWiderType) continue;
87719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
87819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MVT IntermediateVT;
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT RegisterVT;
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumIntermediates;
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumRegistersForVT[i] =
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                RegisterVT, this);
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegisterTypeForVT[i] = RegisterVT;
88619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
88719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT NVT = VT.getPow2VectorType();
88819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NVT == VT) {
88919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Type is already a power of 2.  The default action is to split.
89019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TransformToType[i] = MVT::Other;
89119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned NumElts = VT.getVectorNumElements();
89219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ValueTypeActions.setTypeAction(VT,
89319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumElts > 1 ? TypeSplitVector : TypeScalarizeVector);
89419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
89519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TransformToType[i] = NVT;
89619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ValueTypeActions.setTypeAction(VT, TypeWidenVector);
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Determine the 'representative' register class for each value type.
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // An representative register class is the largest (meaning one which is
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // not a sub-register class / subreg register class) legal register class for
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // a group of value types. For example, on i386, i8, i16, and i32
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // representative would be GR32; while on x86_64 it's GR64.
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetRegisterClass* RRC;
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint8_t Cost;
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    tie(RRC, Cost) =  findRepresentativeClass((MVT::SimpleValueType)i);
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RepRegClassForVT[i] = RRC;
910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RepRegClassCostForVT[i] = Cost;
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NULL;
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
91919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanEVT TargetLowering::getSetCCResultType(EVT VT) const {
92019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!VT.isVector() && "No default SetCC type for vectors!");
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return PointerTy.SimpleTy;
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MVT::i32; // return the default value
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getVectorTypeBreakdown - Vector types are broken down into some number of
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This method returns the number of registers needed, and the VT for each
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// register.  It also returns the VT and quantity of the intermediate values
935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// before they are promoted/expanded.
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                EVT &IntermediateVT,
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                unsigned &NumIntermediates,
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                EVT &RegisterVT) const {
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElts = VT.getVectorNumElements();
94219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
94319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If there is a wider vector type with the same element type as this one,
94419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // we should widen to that legal vector type.  This handles things like
94519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // <2 x float> -> <4 x float>.
94619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElts != 1 && getTypeAction(Context, VT) == TypeWidenVector) {
94719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    RegisterVT = getTypeToTransformTo(Context, VT);
94819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isTypeLegal(RegisterVT)) {
94919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      IntermediateVT = RegisterVT;
95019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      NumIntermediates = 1;
95119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return 1;
95219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
95319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
95419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
95519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Figure out the right, legal destination reg to copy into.
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltTy = VT.getVectorElementType();
95719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumVectorRegs = 1;
95919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
96019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally we
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // could break down into LHS/RHS like LegalizeDAG does.
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isPowerOf2_32(NumElts)) {
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumVectorRegs = NumElts;
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumElts = 1;
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
96619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Divide the input until we get to a supported size.  This will always
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // end with a scalar if the target doesn't support vectors.
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (NumElts > 1 && !isTypeLegal(
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   EVT::getVectorVT(Context, EltTy, NumElts))) {
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumElts >>= 1;
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumVectorRegs <<= 1;
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NumIntermediates = NumVectorRegs;
97619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(NewVT))
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewVT = EltTy;
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  IntermediateVT = NewVT;
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DestVT = getRegisterType(Context, NewVT);
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RegisterVT = DestVT;
98419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NewVTSize = NewVT.getSizeInBits();
98519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
98619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Convert sizes such as i33 to i64.
98719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isPowerOf2_32(NewVTSize))
98819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    NewVTSize = NextPowerOf2(NewVTSize);
98919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
99019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (DestVT.bitsLT(NewVT))   // Value is expanded, e.g. i64 -> i16.
99119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
99219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
99319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise, promotion or legal types use the same number of registers as
99419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the vector decimated to the appropriate level.
99519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return NumVectorRegs;
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Get the EVTs and ArgFlags collections that represent the legalized return
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// type of the given function.  This does not require a DAG or a return value,
1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// and is suitable for use before any DAGs for the function are constructed.
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// TODO: Move this out of TargetLowering.cpp.
100219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid llvm::GetReturnInfo(Type* ReturnType, Attributes attr,
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         SmallVectorImpl<ISD::OutputArg> &Outs,
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         const TargetLowering &TLI,
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         SmallVectorImpl<uint64_t> *Offsets) {
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<EVT, 4> ValueVTs;
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ComputeValueVTs(TLI, ReturnType, ValueVTs);
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumValues = ValueVTs.size();
1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumValues == 0) return;
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Offset = 0;
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned j = 0, f = NumValues; j != f; ++j) {
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VT = ValueVTs[j];
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (attr & Attribute::SExt)
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ExtendKind = ISD::SIGN_EXTEND;
1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (attr & Attribute::ZExt)
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ExtendKind = ISD::ZERO_EXTEND;
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: C calling convention requires the return type to be promoted to
1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // at least 32-bit. But this is not necessary for non-C calling
1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // conventions. The frontend should mark functions whose return values
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // require promoting with signext or zeroext attributes.
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT.bitsLT(MinVT))
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        VT = MinVT;
1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        PartVT.getTypeForEVT(ReturnType->getContext()));
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 'inreg' on function refers to return value
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (attr & Attribute::InReg)
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Flags.setInReg();
1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Propagate extension type if any
1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (attr & Attribute::SExt)
1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Flags.setSExt();
1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (attr & Attribute::ZExt)
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Flags.setZExt();
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < NumParts; ++i) {
1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Outs.push_back(ISD::OutputArg(Flags, PartVT, /*isFixed=*/true));
1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Offsets) {
1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offsets->push_back(Offset);
1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offset += PartSize;
1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// function arguments in the caller parameter area.  This is the actual
1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// alignment, not its logarithm.
106019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned TargetLowering::getByValTypeAlignment(Type *Ty) const {
1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TD->getCallFrameTypeAlignment(Ty);
1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// current function.  The returned value is a member of the
1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MachineJumpTableInfo::JTEntryKind enum.
1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned TargetLowering::getJumpTableEncoding() const {
1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In non-pic modes, just use the address of a block.
1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return MachineJumpTableInfo::EK_BlockAddress;
107119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In PIC mode, if the target supports a GPRel32 directive, use it.
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return MachineJumpTableInfo::EK_GPRel32BlockAddress;
107519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, use a label difference.
1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MachineJumpTableInfo::EK_LabelDifference32;
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 SelectionDAG &DAG) const {
1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If our PIC model is GP relative, use the global offset table as the base.
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getJumpTableEncoding() == MachineJumpTableInfo::EK_GPRel32BlockAddress)
1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Table;
1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MCExpr.
1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst MCExpr *
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             unsigned JTI,MCContext &Ctx) const{
1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The normal PIC reloc base is the label at the start of the jump table.
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Assume that everything is safe in static mode.
1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getRelocationModel() == Reloc::Static)
1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In dynamic-no-pic mode, assume that known defined values are safe.
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GA &&
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !GA->getGlobal()->isDeclaration() &&
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !GA->getGlobal()->isWeakForLinker())
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise assume nothing is safe.
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  Optimization Methods
1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ShrinkDemandedConstant - Check to see if the specified operand of the
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified instruction is a constant integer.  If so, check to see if there
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// are any bits set in the constant that are not demanded.  If so, shrink the
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constant and return true.
112319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        const APInt &Demanded) {
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: ISD::SELECT, ISD::SELECT_CC
1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::XOR:
1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AND:
1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR: {
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!C) return false;
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getOpcode() == ISD::XOR &&
1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // if we can expand it to have all bits set, do it
1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getAPIntValue().intersects(~Demanded)) {
1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = Op.getValueType();
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                DAG.getConstant(Demanded &
114519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                C->getAPIntValue(),
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                VT));
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return CombineTo(Op, New);
1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// casts are free.  This uses isZExtFree and ZERO_EXTEND for the widening
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// cast, but it could be generalized for targets with other types of
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// implicit widening casts.
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    unsigned BitWidth,
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    const APInt &Demanded,
1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    DebugLoc dl) {
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getNumOperands() == 2 &&
1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "ShrinkDemandedOp only supports binary operators!");
1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getNode()->getNumValues() == 1 &&
1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "ShrinkDemandedOp only supports nodes with one result!");
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Don't do this if the node has another user, which may require the
1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // full value.
1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Op.getNode()->hasOneUse())
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Search for the smallest integer type with free casts to and from
1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Op's type. For expedience, just check power-of-2 integer types.
1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isPowerOf2_32(SmallVTBits))
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVTBits = NextPowerOf2(SmallVTBits);
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TLI.isZExtFree(SmallVT, Op.getValueType())) {
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We found a type with free casts.
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          Op.getNode()->getOperand(0)),
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          Op.getNode()->getOperand(1)));
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return CombineTo(Op, Z);
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// DemandedMask bits of the result of Op are ever used downstream.  If we can
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// use this information to simplify Op, create a new simplified DAG node and
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// return true, returning the original and new nodes in Old and New. Otherwise,
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// analyze the expression and return a mask of KnownOne and KnownZero bits for
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the expression (used to simplify the caller).  The KnownZero/One bits may
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// only be accurate for those bits in the DemandedMask.
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool TargetLowering::SimplifyDemandedBits(SDValue Op,
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          const APInt &DemandedMask,
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          APInt &KnownZero,
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          APInt &KnownOne,
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          TargetLoweringOpt &TLO,
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          unsigned Depth) const {
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BitWidth = DemandedMask.getBitWidth();
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Mask size mismatches value type size!");
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt NewMask = DemandedMask;
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Don't know anything.
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  KnownZero = KnownOne = APInt(BitWidth, 0);
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Other users may use these bits.
122219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Op.getNode()->hasOneUse()) {
1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Depth != 0) {
122419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If not at the root, Just compute the KnownZero/KnownOne bits to
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // simplify things downstream.
1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is the root being simplified, allow it to have multiple uses,
1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // just set the NewMask to all bits.
1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMask = APInt::getAllOnesValue(BitWidth);
123219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (DemandedMask == 0) {
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Not demanding any bits from Op.
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getOpcode() != ISD::UNDEF)
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Depth == 6) {        // Limit search depth.
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::Constant:
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We know all of the bits for a constant!
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & NewMask;
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero = ~KnownOne & NewMask;
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;   // Don't fall through, will infinitely loop.
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AND:
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the RHS is a constant, check to see if the LHS would be zero without
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // using the bits from the RHS.  Below, we use knowledge about the RHS to
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // simplify the LHS, here we're using information from the LHS to simplify
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the RHS.
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt LHSZero, LHSOne;
125519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Do not increment Depth here; that can cause an infinite loop.
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TLO.DAG.ComputeMaskedBits(Op.getOperand(0), NewMask,
125719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                LHSZero, LHSOne, Depth);
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the LHS already has zeros where RHSC does, this and is dead.
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return TLO.CombineTo(Op, Op.getOperand(0));
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If any of the set bits in the RHS are known zero on the LHS, shrink
1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the constant.
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
126619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
127019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero2, KnownOne2, TLO, Depth+1))
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
127419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
127519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the demanded bits are known one on one side, return the other.
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These bits cannot contribute to the result of the 'and'.
1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(0));
1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(1));
1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the demanded bits in the inputs are known zeros, return zero.
1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the RHS is a constant, see if we can simplify it.
1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operation can be done in a smaller type, do so.
1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-1 bits are only known if set in both the LHS & RHS.
1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne &= KnownOne2;
1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-0 are known to be clear if zero in either the LHS | RHS.
1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero |= KnownZero2;
1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR:
129819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
130119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero2, KnownOne2, TLO, Depth+1))
1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
130519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
130619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the demanded bits are known zero on one side, return the other.
1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These bits cannot contribute to the result of the 'or'.
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(0));
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(1));
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the potentially set bits on one side are known to be set on
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the other side, just use the 'other' side.
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(0));
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(1));
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the RHS is a constant, see if we can simplify it.
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operation can be done in a smaller type, do so.
1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-0 bits are only known if clear in both the LHS & RHS.
1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero &= KnownZero2;
1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-1 are known to be set if set in either the LHS | RHS.
1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne |= KnownOne2;
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::XOR:
133219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
133519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne2, TLO, Depth+1))
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
133919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
134019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the demanded bits are known zero on one side, return the other.
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These bits cannot contribute to the result of the 'xor'.
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((KnownZero & NewMask) == NewMask)
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(0));
1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((KnownZero2 & NewMask) == NewMask)
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(1));
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operation can be done in a smaller type, do so.
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the unknown bits are known to be zero on one side or the other
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // (but not both) turn this into an *inclusive* or.
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               Op.getOperand(0),
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               Op.getOperand(1)));
135819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-0 bits are known if clear or set in both the LHS & RHS.
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output known-1 are known to be set if set in only one of the LHS, RHS.
1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
136319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all of the demanded bits on one side are known, and all of the set
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // bits on that side are also known to be set on the other side, turn this
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // into an AND, as we know the bits will be cleared.
1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((KnownOne & KnownOne2) == KnownOne) {
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EVT VT = Op.getValueType();
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
137219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Op.getOperand(0), ANDC));
1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
137619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the RHS is a constant, see if we can simplify it.
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // for XOR, we prefer to force bits to 1 if they will make a -1.
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // if we can't force bits, try to shrink constant
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt Expanded = C->getAPIntValue() | (~NewMask);
1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // if we can expand it to have all bits set, do it
1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Expanded.isAllOnesValue()) {
1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Expanded != C->getAPIntValue()) {
1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          EVT VT = Op.getValueType();
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          TLO.DAG.getConstant(Expanded, VT));
1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return TLO.CombineTo(Op, New);
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // if it already has all the bits set, nothing to change
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // but don't shrink either!
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero = KnownZeroOut;
1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne  = KnownOneOut;
1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SELECT:
140119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne2, TLO, Depth+1))
1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
140719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
140819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
140919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operands are constants, see if we can simplify them.
1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
141319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Only known if known in both the LHS and RHS.
1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne &= KnownOne2;
1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero &= KnownZero2;
1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SELECT_CC:
141919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne2, TLO, Depth+1))
1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
142519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
142619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
142719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operands are constants, see if we can simplify them.
1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedConstant(Op, NewMask))
1430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
143119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Only known if known in both the LHS and RHS.
1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne &= KnownOne2;
1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero &= KnownZero2;
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL:
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ShAmt = SA->getZExtValue();
1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue InOp = Op.getOperand(0);
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the shift count is an invalid immediate, don't do anything.
1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ShAmt >= BitWidth)
1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // single shift.  We can do this if the bottom bits (which are shifted
1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // out) are never demanded.
1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (InOp.getOpcode() == ISD::SRL &&
1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isa<ConstantSDNode>(InOp.getOperand(1))) {
1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned Opc = ISD::SHL;
1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          int Diff = ShAmt-C1;
1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Diff < 0) {
1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Diff = -Diff;
1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Opc = ISD::SRL;
145719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
145819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
145919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue NewSA =
1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          EVT VT = Op.getValueType();
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   InOp.getOperand(0), NewSA));
1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
146519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
146619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               KnownZero, KnownOne, TLO, Depth+1))
1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // are not demanded. This will likely allow the anyext to be folded away.
1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue InnerOp = InOp.getNode()->getOperand(0);
1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EVT InnerVT = InnerOp.getValueType();
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if ((APInt::getHighBitsSet(BitWidth,
1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   BitWidth - InnerVT.getSizeInBits()) &
1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               DemandedMask) == 0 &&
1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isTypeDesirableForOp(ISD::SHL, InnerVT)) {
148019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          EVT ShTy = getShiftAmountTy(InnerVT);
1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            ShTy = InnerVT;
1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue NarrowShl =
1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            TLO.DAG.getConstant(ShAmt, ShTy));
1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return
1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            TLO.CombineTo(Op,
1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          NarrowShl));
1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero <<= SA->getZExtValue();
1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne  <<= SA->getZExtValue();
1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // low bits known zero.
1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRL:
1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = Op.getValueType();
1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ShAmt = SA->getZExtValue();
1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned VTSize = VT.getSizeInBits();
1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue InOp = Op.getOperand(0);
150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the shift count is an invalid immediate, don't do anything.
1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ShAmt >= BitWidth)
1508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
1509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // single shift.  We can do this if the top bits (which are shifted out)
1512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // are never demanded.
1513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (InOp.getOpcode() == ISD::SHL &&
1514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isa<ConstantSDNode>(InOp.getOperand(1))) {
1515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
1516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned Opc = ISD::SRL;
1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          int Diff = ShAmt-C1;
1519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Diff < 0) {
1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Diff = -Diff;
1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Opc = ISD::SHL;
152219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
152319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue NewSA =
1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   InOp.getOperand(0), NewSA));
1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Compute the new bits that are at the top now.
1532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
1533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               KnownZero, KnownOne, TLO, Depth+1))
1534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero = KnownZero.lshr(ShAmt);
1537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne  = KnownOne.lshr(ShAmt);
1538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero |= HighBits;  // High bits known zero.
1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRA:
1544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is an arithmetic shift right and only the low-bit is set, we can
1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // always convert this into a logical shr, even if the shift amount is
1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // variable.  The low bit of the shift cannot be an input sign bit unless
1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the shift amount is >= the size of the datatype, which is undefined.
1548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DemandedMask == 1)
1549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op,
1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           Op.getOperand(0), Op.getOperand(1)));
1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = Op.getValueType();
1555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ShAmt = SA->getZExtValue();
155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the shift count is an invalid immediate, don't do anything.
1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ShAmt >= BitWidth)
1559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt InDemandedMask = (NewMask << ShAmt);
1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If any of the demanded bits are produced by the sign extension, we also
1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // demand the input sign bit.
1565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
1566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (HighBits.intersects(NewMask))
1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
156819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
1570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               KnownZero, KnownOne, TLO, Depth+1))
1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
157219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero = KnownZero.lshr(ShAmt);
1574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne  = KnownOne.lshr(ShAmt);
157519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Handle the sign bit, adjusted to where it is now in the mask.
1577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
157819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the input sign bit is known to be zero, or if none of the top bits
1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // are demanded, turn this into an unsigned shift right.
1581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
158219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
1583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Op.getOperand(0),
1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Op.getOperand(1)));
1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        KnownOne |= HighBits;
1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SIGN_EXTEND_INREG: {
1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Sign extension.  Compute the demanded bits in the result that are not
1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // present in the input.
1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt NewBits =
1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt::getHighBitsSet(BitWidth,
1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            BitWidth - EVT.getScalarType().getSizeInBits());
159819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If none of the extended bits are demanded, eliminate the sextinreg.
1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((NewBits & NewMask) == 0)
1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, Op.getOperand(0));
1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    APInt InSignBit =
160419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      APInt::getSignBit(EVT.getScalarType().getSizeInBits()).zext(BitWidth);
1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt InputDemandedBits =
1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt::getLowBitsSet(BitWidth,
1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           EVT.getScalarType().getSizeInBits()) &
1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMask;
160919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Since the sign extended bits are demanded, we know that the sign
1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // bit is demanded.
1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InputDemandedBits |= InSignBit;
1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero, KnownOne, TLO, Depth+1))
1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
161719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the sign bit of the input is known set or clear, then we know the
1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // top bits of the result.
162119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the input sign bit is known zero, convert this into a zero extension.
1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (KnownZero.intersects(InSignBit))
162419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return TLO.CombineTo(Op,
1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,EVT));
162619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (KnownOne.intersects(InSignBit)) {    // Input sign bit known set
1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne |= NewBits;
1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero &= ~NewBits;
1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {                       // Input sign bit unknown
1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero &= ~NewBits;
1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne &= ~NewBits;
1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ZERO_EXTEND: {
1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OperandBitWidth =
1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
163919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    APInt InMask = NewMask.trunc(OperandBitWidth);
164019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If none of the top bits are demanded, convert this into an any_extend.
1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt NewBits =
1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!NewBits.intersects(NewMask))
1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
164619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               Op.getValueType(),
1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               Op.getOperand(0)));
164819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero, KnownOne, TLO, Depth+1))
1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
165219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
165319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownZero = KnownZero.zext(BitWidth);
165419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownOne = KnownOne.zext(BitWidth);
1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero |= NewBits;
1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SIGN_EXTEND: {
1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT InVT = Op.getOperand(0).getValueType();
1660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned InBits = InVT.getScalarType().getSizeInBits();
1661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt InMask    = APInt::getLowBitsSet(BitWidth, InBits);
1662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
1663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt NewBits   = ~InMask & NewMask;
166419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If none of the top bits are demanded, convert this into an any_extend.
1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewBits == 0)
1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              Op.getValueType(),
1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              Op.getOperand(0)));
167019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Since some of the sign extended bits are demanded, we know that the sign
1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // bit is demanded.
1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt InDemandedBits = InMask & NewMask;
1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InDemandedBits |= InSignBit;
167519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    InDemandedBits = InDemandedBits.trunc(InBits);
167619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
167719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne, TLO, Depth+1))
1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
168019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownZero = KnownZero.zext(BitWidth);
168119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownOne = KnownOne.zext(BitWidth);
168219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the sign bit is known zero, convert this to a zero extend.
1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (KnownZero.intersects(InSignBit))
1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
168619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               Op.getValueType(),
1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               Op.getOperand(0)));
168819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the sign bit is known one, the top bits match.
1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (KnownOne.intersects(InSignBit)) {
1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne  |= NewBits;
1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero &= ~NewBits;
1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {   // Otherwise, top bits aren't known.
1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownOne  &= ~NewBits;
1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      KnownZero &= ~NewBits;
1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ANY_EXTEND: {
1700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OperandBitWidth =
1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
170219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    APInt InMask = NewMask.trunc(OperandBitWidth);
1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), InMask,
1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero, KnownOne, TLO, Depth+1))
1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
170619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
170719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownZero = KnownZero.zext(BitWidth);
170819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownOne = KnownOne.zext(BitWidth);
1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::TRUNCATE: {
1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Simplify the input, using demanded bit information, and compute the known
1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // zero/one bits live out.
1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OperandBitWidth =
1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
171619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    APInt TruncMask = NewMask.zext(OperandBitWidth);
1717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownZero, KnownOne, TLO, Depth+1))
1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
172019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownZero = KnownZero.trunc(BitWidth);
172119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    KnownOne = KnownOne.trunc(BitWidth);
172219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the input is only used by this truncate, see if we can shrink it based
1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // on the known demanded bits.
1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getOperand(0).getNode()->hasOneUse()) {
1726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue In = Op.getOperand(0);
1727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (In.getOpcode()) {
1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SRL:
1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Shrink SRL by a constant if none of the high bits shifted in are
1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // demanded.
1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (TLO.LegalTypes() &&
1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // undesirable.
1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!ShAmt)
1739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
174019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Shift = In.getOperand(1);
174119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (TLO.LegalTypes()) {
174219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          uint64_t ShVal = ShAmt->getZExtValue();
174319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Shift =
174419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
174519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
174619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               OperandBitWidth - BitWidth);
174919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // None of the shifted in bits are needed.  Add a truncate of the
1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // shift input, then shift it.
1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
175519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             Op.getValueType(),
1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             In.getOperand(0));
1757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   Op.getValueType(),
175919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                   NewTrunc,
176019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                   Shift));
1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
176519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
176619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AssertZext: {
177019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // AssertZext demands all of the high bits, plus any of the low bits
177119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // demanded by its users.
1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt InMask = APInt::getLowBitsSet(BitWidth,
1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        VT.getSizeInBits());
177519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
177619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             KnownZero, KnownOne, TLO, Depth+1))
177719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return true;
177819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
177919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero |= ~InMask & NewMask;
1781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
178319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::BITCAST:
178419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If this is an FP->Int bitcast and if the sign bit is the only
178519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // thing demanded, turn this into a FGETSIGN.
178619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Op.getOperand(0).getValueType().isVector() &&
178719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
178819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Op.getOperand(0).getValueType().isFloatingPoint()) {
178919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
179019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool i32Legal  = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
179119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
179219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
1793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // place.  We expect the SHL to be eliminated by other optimizations.
179519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
179619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
179719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!OpVTLegal && OpVTSizeInBits > 32)
179819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
1799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned ShVal = Op.getValueType().getSizeInBits()-1;
180019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
180119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
180219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                 Op.getValueType(),
1803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Sign, ShAmt));
1804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ADD:
1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::MUL:
1809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SUB: {
1810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Add, Sub, and Mul don't demand any bits in positions beyond that
1811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // of the highest bit demanded of them.
1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt LoMask = APInt::getLowBitsSet(BitWidth,
1813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        BitWidth - NewMask.countLeadingZeros());
1814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne2, TLO, Depth+1))
1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             KnownOne2, TLO, Depth+1))
1819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // See if the operation should be performed at a smaller bit width.
1821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
1822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
1823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FALL THROUGH
1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
1826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Just use ComputeMaskedBits to compute output bits.
1827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TLO.DAG.ComputeMaskedBits(Op, NewMask, KnownZero, KnownOne, Depth);
1828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
183019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we know the value of all of the demanded bits, return this as a
1832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // constant.
1833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((NewMask & (KnownZero|KnownOne)) == NewMask)
1834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
183519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// computeMaskedBitsForTargetNode - Determine which of the bits specified
184019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// in Mask are known to be either zero or one and return them in the
1841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// KnownZero/KnownOne bitsets.
184219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    const APInt &Mask,
184419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                    APInt &KnownZero,
1845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    APInt &KnownOne,
1846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    const SelectionDAG &DAG,
1847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    unsigned Depth) const {
1848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Should use MaskedValueIsZero if you don't know whether Op"
1853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         " is a target node!");
1854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
1855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// targets that want to expose additional information about sign bits to the
1859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// DAG Combiner.
1860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
1861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         unsigned Depth) const {
1862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Should use ComputeNumSignBits if you don't know whether Op"
1867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         " is a target node!");
1868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 1;
1869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// determine which bit is set.
1874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
1876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // A left-shift of a constant one will have exactly one bit set, because
1877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // shifting the bit off the end is undefined.
1878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val.getOpcode() == ISD::SHL)
1879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C =
1880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getAPIntValue() == 1)
1882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
1883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Similarly, a right-shift of a constant sign-bit will have exactly
1885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // one bit set.
1886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val.getOpcode() == ISD::SRL)
1887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C =
1888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getAPIntValue().isSignBit())
1890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
1891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // More could be done here, though the above checks are enough
1893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to handle some common cases.
1894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fall back to ComputeMaskedBits to catch other known cases.
1896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT OpVT = Val.getValueType();
1897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
1898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt Mask = APInt::getAllOnesValue(BitWidth);
1899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt KnownZero, KnownOne;
1900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DAG.ComputeMaskedBits(Val, Mask, KnownZero, KnownOne);
1901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (KnownZero.countPopulation() == BitWidth - 1) &&
1902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         (KnownOne.countPopulation() == 1);
1903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
190519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// SimplifySetCC - Try to simplify a setcc built with the specified operands
1906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// and cc. If it is unable to simplify it, return a null SDValue.
1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              ISD::CondCode Cond, bool foldBooleans,
1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAGCombinerInfo &DCI, DebugLoc dl) const {
1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SelectionDAG &DAG = DCI.DAG;
1912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These setcc operations always fold.
1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Cond) {
1915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETFALSE:
1917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETTRUE:
1919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETTRUE2:  return DAG.getConstant(1, VT);
1920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
192219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Ensure that the constant occurs on the RHS, and fold constant
192319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // comparisons.
192419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<ConstantSDNode>(N0.getNode()))
1925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
1928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const APInt &C1 = N1C->getAPIntValue();
1929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // equality comparison, then we're just comparing whether X itself is
1932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // zero.
1933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOperand(1).getOpcode() == ISD::Constant) {
1936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      const APInt &ShAmt
1937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (srl (ctlz x), 5) == 0  -> X != 0
1942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (srl (ctlz x), 5) != 1  -> X != 0
1943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = ISD::SETNE;
1944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else {
1945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (srl (ctlz x), 5) != 0  -> X == 0
1946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (srl (ctlz x), 5) == 1  -> X == 0
1947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = ISD::SETEQ;
1948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Zero = DAG.getConstant(0, N0.getValueType());
1950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            Zero, Cond);
1952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
195519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CTPOP = N0;
195619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Look through truncs that don't change the value of a ctpop.
195719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
195819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      CTPOP = N0.getOperand(0);
195919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
196019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
196119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (N0 == CTPOP || N0.getValueType().getSizeInBits() >
196219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
196319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EVT CTVT = CTPOP.getValueType();
196419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue CTOp = CTPOP.getOperand(0);
196519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
196619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // (ctpop x) u< 2 -> (x & x-1) == 0
196719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // (ctpop x) u> 1 -> (x & x-1) != 0
196819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
196919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
197019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DAG.getConstant(1, CTVT));
197119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
197219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
197319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
197419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
197519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
197619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
197719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
197819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
197919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // (zext x) == C --> x == (trunc C)
198019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
198119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
198219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned MinBits = N0.getValueSizeInBits();
198319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue PreZExt;
198419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (N0->getOpcode() == ISD::ZERO_EXTEND) {
198519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // ZExt
198619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MinBits = N0->getOperand(0).getValueSizeInBits();
198719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        PreZExt = N0->getOperand(0);
198819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (N0->getOpcode() == ISD::AND) {
198919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // DAGCombine turns costly ZExts into ANDs
199019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
199119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if ((C->getAPIntValue()+1).isPowerOf2()) {
199219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            MinBits = C->getAPIntValue().countTrailingOnes();
199319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            PreZExt = N0->getOperand(0);
199419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
199519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
199619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // ZEXTLOAD
199719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
199819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MinBits = LN0->getMemoryVT().getSizeInBits();
199919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          PreZExt = N0;
200019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
200119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
200219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
200319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Make sure we're not loosing bits from the constant.
200419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (MinBits < C1.getBitWidth() && MinBits > C1.getActiveBits()) {
200519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
200619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
200719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // Will get folded away.
200819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
200919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
201019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return DAG.getSetCC(dl, VT, Trunc, C, Cond);
201119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
201219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
201319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
201419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the LHS is '(and load, const)', the RHS is 0,
2016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the test is for equality or unsigned, and all 1 bits of the const are
2017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // in the same partial word, see if we can shorten the load.
2018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DCI.isBeforeLegalize() &&
2019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOpcode() == ISD::AND && C1 == 0 &&
2020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getNode()->hasOneUse() &&
2021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        isa<LoadSDNode>(N0.getOperand(0)) &&
2022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOperand(0).getNode()->hasOneUse() &&
2023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        isa<ConstantSDNode>(N0.getOperand(1))) {
2024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
2025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt bestMask;
2026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned bestWidth = 0, bestOffset = 0;
2027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!Lod->isVolatile() && Lod->isUnindexed()) {
2028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned origWidth = N0.getValueType().getSizeInBits();
2029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned maskWidth = origWidth;
203019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
2031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // 8 bits, but have to be careful...
2032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
2033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          origWidth = Lod->getMemoryVT().getSizeInBits();
2034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        const APInt &Mask =
2035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned width = origWidth / 2; width>=8; width /= 2) {
2037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          APInt newMask = APInt::getLowBitsSet(maskWidth, width);
2038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          for (unsigned offset=0; offset<origWidth/width; offset++) {
2039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if ((newMask & Mask) == Mask) {
2040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              if (!TD->isLittleEndian())
2041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                bestOffset = (origWidth/width - offset - 1) * (width/8);
2042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              else
2043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                bestOffset = (uint64_t)offset * (width/8);
2044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              bestMask = Mask.lshr(offset * (width/8) * 8);
2045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              bestWidth = width;
2046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              break;
2047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            }
2048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            newMask = newMask << width;
2049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
2050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (bestWidth) {
205319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
2054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (newVT.isRound()) {
2055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          EVT PtrType = Lod->getOperand(1).getValueType();
2056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Ptr = Lod->getBasePtr();
2057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (bestOffset != 0)
2058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
2059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(bestOffset, PtrType));
2060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
2061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
206219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                Lod->getPointerInfo().getWithOffset(bestOffset),
2063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        false, false, NewAlign);
206419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return DAG.getSetCC(dl, VT,
2065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getNode(ISD::AND, dl, newVT, NewLoad,
2066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      DAG.getConstant(bestMask.trunc(bestWidth),
2067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                      newVT)),
2068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(0LL, newVT), Cond);
2069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
2076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the comparison constant has bits in the upper part, the
2078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // zero-extended value could never match.
2079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
2080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              C1.getBitWidth() - InSize))) {
2081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (Cond) {
2082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETUGT:
2083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETUGE:
2084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETEQ: return DAG.getConstant(0, VT);
2085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETULT:
2086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETULE:
2087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETNE: return DAG.getConstant(1, VT);
2088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETGT:
2089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETGE:
2090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // True if the sign bit of C1 is set.
2091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getConstant(C1.isNegative(), VT);
2092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETLT:
2093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ISD::SETLE:
2094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // True if the sign bit of C1 isn't set.
2095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getConstant(C1.isNonNegative(), VT);
2096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default:
2097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
2098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Otherwise, we can perform the comparison with the low bits.
2102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (Cond) {
2103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETEQ:
2104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETNE:
2105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGT:
2106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGE:
2107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULT:
2108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULE: {
2109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EVT newVT = N0.getOperand(0).getValueType();
2110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (DCI.isBeforeLegalizeOps() ||
2111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (isOperationLegal(ISD::SETCC, newVT) &&
2112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              getCondCodeAction(Cond, newVT)==Legal))
2113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0.getOperand(0),
211419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG.getConstant(C1.trunc(InSize), newVT),
2115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              Cond);
2116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
2117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default:
2119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;   // todo, be more careful with signed comparisons
2120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
2125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT ExtDstTy = N0.getValueType();
2126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
2127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the constant doesn't fit into the number of bits for the source of
2129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the sign extension, it is impossible for both sides to be equal.
2130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C1.getMinSignedBits() > ExtSrcTyBits)
2131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getConstant(Cond == ISD::SETNE, VT);
213219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue ZextOp;
2134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT Op0Ty = N0.getOperand(0).getValueType();
2135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Op0Ty == ExtSrcTy) {
2136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ZextOp = N0.getOperand(0);
2137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
2138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
2139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
2140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(Imm, Op0Ty));
2141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(ZextOp.getNode());
2144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Otherwise, make this a use of a zext.
214519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, ZextOp,
2146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(C1 & APInt::getLowBitsSet(
2147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                              ExtDstTyBits,
214819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                              ExtSrcTyBits),
2149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          ExtDstTy),
2150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          Cond);
2151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
2152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // SETCC (SETCC), [0|1], [EQ|NE]  -> SETCC
2154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N0.getOpcode() == ISD::SETCC &&
2155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
2156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
2157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (TrueWhenTrue)
215819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
2159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Invert the condition.
2160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
216119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        CC = ISD::getSetCCInverse(CC,
2162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  N0.getOperand(0).getValueType().isInteger());
2163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
2164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((N0.getOpcode() == ISD::XOR ||
216719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           (N0.getOpcode() == ISD::AND &&
2168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            N0.getOperand(0).getOpcode() == ISD::XOR &&
2169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isa<ConstantSDNode>(N0.getOperand(1)) &&
2171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
2172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If this is (X^1) == 0/1, swap the RHS and eliminate the xor.  We
2173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // can only do this if the top bits are known zero.
2174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned BitWidth = N0.getValueSizeInBits();
2175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (DAG.MaskedValueIsZero(N0,
2176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  APInt::getHighBitsSet(BitWidth,
2177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        BitWidth-1))) {
2178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Okay, get the un-inverted input value.
2179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Val;
2180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOpcode() == ISD::XOR)
2181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Val = N0.getOperand(0);
2182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          else {
218319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            assert(N0.getOpcode() == ISD::AND &&
2184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    N0.getOperand(0).getOpcode() == ISD::XOR);
2185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // ((X^1)&1)^1 -> X & 1
2186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
2187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              N0.getOperand(0).getOperand(0),
2188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              N0.getOperand(1));
2189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
2190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, Val, N1,
2192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (N1C->getAPIntValue() == 1 &&
2195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 (VT == MVT::i1 ||
219619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  getBooleanContents(false) == ZeroOrOneBooleanContent)) {
2197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Op0 = N0;
2198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Op0.getOpcode() == ISD::TRUNCATE)
2199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op0 = Op0.getOperand(0);
2200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if ((Op0.getOpcode() == ISD::XOR) &&
2202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Op0.getOperand(0).getOpcode() == ISD::SETCC &&
2203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Op0.getOperand(1).getOpcode() == ISD::SETCC) {
2204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
2205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
2206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
2207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              Cond);
2208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else if (Op0.getOpcode() == ISD::AND &&
2209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                isa<ConstantSDNode>(Op0.getOperand(1)) &&
2210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
2211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
2212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Op0.getValueType().bitsGT(VT))
2213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Op0 = DAG.getNode(ISD::AND, dl, VT,
2214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
2215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(1, VT));
2216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          else if (Op0.getValueType().bitsLT(VT))
2217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Op0 = DAG.getNode(ISD::AND, dl, VT,
2218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
2219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getConstant(1, VT));
2220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, Op0,
2222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(0, Op0.getValueType()),
2223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
222719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt MinVal, MaxVal;
2229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
2230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ISD::isSignedIntSetCC(Cond)) {
2231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MinVal = APInt::getSignedMinValue(OperandBitSize);
2232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxVal = APInt::getSignedMaxValue(OperandBitSize);
2233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
2234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MinVal = APInt::getMinValue(OperandBitSize);
2235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxVal = APInt::getMaxValue(OperandBitSize);
2236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C1 == MinVal) return DAG.getConstant(1, VT);   // X >= MIN --> true
2241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // X >= C0 --> X > (C0-1)
224219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, N0,
2243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(C1-1, N1.getValueType()),
2244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C1 == MaxVal) return DAG.getConstant(1, VT);   // X <= MAX --> true
2249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // X <= C0 --> X < (C0+1)
225019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, N0,
2251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(C1+1, N1.getValueType()),
2252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(0, VT);      // X < MIN --> false
2257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
2258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(1, VT);      // X >= MIN --> true
2259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
2260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(0, VT);      // X > MAX --> false
2261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
2262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(1, VT);      // X <= MAX --> true
2263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Canonicalize setgt X, Min --> setne X, Min
2265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Canonicalize setlt X, Max --> setne X, Max
2268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
2270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have setult X, 1, turn it into seteq X, 0
2272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
227319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, N0,
227419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          DAG.getConstant(MinVal, N0.getValueType()),
2275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          ISD::SETEQ);
2276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have setugt X, Max-1, turn it into seteq X, Max
2277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
227819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, N0,
2279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(MaxVal, N0.getValueType()),
2280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          ISD::SETEQ);
2281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have "setcc X, C0", check to see if we can shrink the immediate
2283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // by changing cc.
2284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // SETUGT X, SINTMAX  -> SETLT X, 0
228619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Cond == ISD::SETUGT &&
2287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        C1 == APInt::getSignedMaxValue(OperandBitSize))
228819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getSetCC(dl, VT, N0,
2289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(0, N1.getValueType()),
2290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          ISD::SETLT);
2291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // SETULT X, SINTMIN  -> SETGT X, -1
2293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond == ISD::SETULT &&
2294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        C1 == APInt::getSignedMinValue(OperandBitSize)) {
2295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue ConstMinusOne =
2296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
2297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          N1.getValueType());
2298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
2299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Fold bit comparisons when we can.
2302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (VT == N0.getValueType() ||
2304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
2305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOpcode() == ISD::AND)
2306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstantSDNode *AndRHS =
2307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EVT ShiftTy = DCI.isBeforeLegalize() ?
230919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          getPointerTy() : getShiftAmountTy(N0.getValueType());
2310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
2311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Perform the xform if the AND RHS is a single bit.
2312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (AndRHS->getAPIntValue().isPowerOf2()) {
2313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
2316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
2317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
2318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (X & 8) == 8  -->  (X & 8) >> 3
2319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Perform the xform if C1 is a single bit.
2320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (C1.isPowerOf2()) {
2321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getNode(ISD::TRUNCATE, dl, VT,
2322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
2323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      DAG.getConstant(C1.logBase2(), ShiftTy)));
2324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
2325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<ConstantFPSDNode>(N0.getNode())) {
2330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Constant fold or commute setcc.
2331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
2332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (O.getNode()) return O;
2333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
2334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the RHS of an FP comparison is a constant, simplify it away in
2335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // some cases.
2336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CFP->getValueAPF().isNaN()) {
2337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If an operand is known to be a nan, we can fold it.
2338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (ISD::getUnorderedFlavor(Cond)) {
2339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: llvm_unreachable("Unknown flavor!");
2340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case 0:  // Known false.
2341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getConstant(0, VT);
2342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case 1:  // Known true.
2343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getConstant(1, VT);
2344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case 2:  // Undefined.
2345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getUNDEF(VT);
2346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
234819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, we know the RHS is not a NaN.  Simplify the node to drop the
2350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // constant if knowing that the operand is non-nan is enough.  We prefer to
2351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
2352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // materialize 0.0.
2353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond == ISD::SETO || Cond == ISD::SETUO)
2354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getSetCC(dl, VT, N0, N0, Cond);
2355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the condition is not legal, see if we can find an equivalent one
2357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // which is legal.
2358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isCondCodeLegal(Cond, N0.getValueType())) {
2359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the comparison was an awkward floating-point == or != and one of
2360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the comparison operands is infinity or negative infinity, convert the
2361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // condition to a less-awkward <= or >=.
2362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CFP->getValueAPF().isInfinity()) {
2363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (CFP->getValueAPF().isNegative()) {
2364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETOEQ &&
2365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
2366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
2367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETUEQ &&
2368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
2369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
2370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETUNE &&
2371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
2372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
2373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETONE &&
2374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
2375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
2376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else {
2377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETOEQ &&
2378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
2379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
2380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETUEQ &&
2381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
2382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
2383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETUNE &&
2384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
2385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
2386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Cond == ISD::SETONE &&
2387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isCondCodeLegal(ISD::SETULT, N0.getValueType()))
2388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
2389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N0 == N1) {
2395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We can always fold X == X for integer setcc's.
2396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N0.getValueType().isInteger())
2397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned UOF = ISD::getUnorderedFlavor(Cond);
2399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (UOF == 2)   // FP operators that are undefined on NaNs.
2400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getConstant(UOF, VT);
2403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
2404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // if it is not already.
2405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
2406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewCond != Cond)
2407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getSetCC(dl, VT, N0, N1, NewCond);
2408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0.getValueType().isInteger()) {
2412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N0.getOpcode() == ISD::XOR) {
2414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Simplify (X+Y) == (X+Z) -->  Y == Z
2415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N0.getOpcode() == N1.getOpcode()) {
2416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N0.getOperand(0) == N1.getOperand(0))
2417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
2418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N0.getOperand(1) == N1.getOperand(1))
2419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
2420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (DAG.isCommutativeBinOp(N0.getOpcode())) {
2421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // If X op Y == Y op X, try other combinations.
2422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOperand(0) == N1.getOperand(1))
242319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
2424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                Cond);
2425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOperand(1) == N1.getOperand(0))
242619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
2427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                Cond);
2428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
243019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Turn (X+C1) == C2 --> X == C2-C1
2434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
2435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DAG.getSetCC(dl, VT, N0.getOperand(0),
2436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                DAG.getConstant(RHSC->getAPIntValue()-
2437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                LHSR->getAPIntValue(),
2438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                N0.getValueType()), Cond);
2439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
244019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOpcode() == ISD::XOR)
2443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // If we know that all of the inverted bits are zero, don't bother
2444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // performing the inversion.
2445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
2446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              return
2447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getSetCC(dl, VT, N0.getOperand(0),
2448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(LHSR->getAPIntValue() ^
2449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               RHSC->getAPIntValue(),
2450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             N0.getValueType()),
2451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Cond);
2452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
245319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Turn (C1-X) == C2 --> X == C1-C2
2455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
2457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return
2458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              DAG.getSetCC(dl, VT, N0.getOperand(1),
2459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(SUBC->getAPIntValue() -
2460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             RHSC->getAPIntValue(),
2461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           N0.getValueType()),
2462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Cond);
2463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
246419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
2465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Simplify (X+Z) == X -->  Z == 0
2468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N0.getOperand(0) == N1)
2469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getSetCC(dl, VT, N0.getOperand(1),
2470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getConstant(0, N0.getValueType()), Cond);
2471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N0.getOperand(1) == N1) {
2472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (DAG.isCommutativeBinOp(N0.getOpcode()))
2473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0.getOperand(0),
2474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(0, N0.getValueType()), Cond);
2475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        else if (N0.getNode()->hasOneUse()) {
2476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // (Z-X) == X  --> Z == X<<1
2478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(),
247919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     N1,
248019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
2481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!DCI.isCalledByLegalizer())
2482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            DCI.AddToWorklist(SH.getNode());
2483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
2484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        N1.getOpcode() == ISD::XOR) {
2490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Simplify  X == (X+Z) -->  Z == 0
2491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N1.getOperand(0) == N0) {
2492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getSetCC(dl, VT, N1.getOperand(1),
2493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getConstant(0, N1.getValueType()), Cond);
2494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (N1.getOperand(1) == N0) {
2495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (DAG.isCommutativeBinOp(N1.getOpcode())) {
2496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N1.getOperand(0),
2497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(0, N1.getValueType()), Cond);
2498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else if (N1.getNode()->hasOneUse()) {
2499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // X == (Z-X)  --> X<<1 == Z
250119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
250219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
2503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!DCI.isCalledByLegalizer())
2504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            DCI.AddToWorklist(SH.getNode());
2505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
2506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
2511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Note that where y is variable and is known to have at most
2512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // one bit set (for example, if it is z&1) we cannot do this;
2513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the expressions are not equivalent when y==0.
2514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N0.getOpcode() == ISD::AND)
2515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
2516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ValueHasExactlyOneBitSet(N1, DAG)) {
2517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Zero = DAG.getConstant(0, N1.getValueType());
2519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N0, Zero, Cond);
2520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N1.getOpcode() == ISD::AND)
2523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
2524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ValueHasExactlyOneBitSet(N0, DAG)) {
2525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Zero = DAG.getConstant(0, N0.getValueType());
2527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getSetCC(dl, VT, N1, Zero, Cond);
2528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fold away ALL boolean setcc's.
2533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Temp;
2534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N0.getValueType() == MVT::i1 && foldBooleans) {
2535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Cond) {
2536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("Unknown integer setcc!");
2537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETEQ:  // X == Y  -> ~(X^Y)
2538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNOT(dl, Temp, MVT::i1);
2540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(Temp.getNode());
2542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETNE:  // X != Y   -->  (X^Y)
2544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGT:  // X >s Y   -->  X == 0 & Y == 1  -->  ~X & Y
2547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULT: // X <u Y   -->  X == 0 & Y == 1  -->  ~X & Y
2548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Temp = DAG.getNOT(dl, N0, MVT::i1);
2549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
2550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(Temp.getNode());
2552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLT:  // X <s Y   --> X == 1 & Y == 0  -->  ~Y & X
2554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGT: // X >u Y   --> X == 1 & Y == 0  -->  ~Y & X
2555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Temp = DAG.getNOT(dl, N1, MVT::i1);
2556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
2557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(Temp.getNode());
2559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULE: // X <=u Y  --> X == 0 | Y == 1  -->  ~X | Y
2561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGE:  // X >=s Y  --> X == 0 | Y == 1  -->  ~X | Y
2562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Temp = DAG.getNOT(dl, N0, MVT::i1);
2563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
2564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(Temp.getNode());
2566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGE: // X >=u Y  --> X == 1 | Y == 0  -->  ~Y | X
2568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLE:  // X <=s Y  --> X == 1 | Y == 0  -->  ~Y | X
2569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Temp = DAG.getNOT(dl, N1, MVT::i1);
2570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
2571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT != MVT::i1) {
2574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!DCI.isCalledByLegalizer())
2575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DCI.AddToWorklist(N0.getNode());
2576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: If running after legalize, we probably can't do this.
2577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
2578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N0;
2580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Could not fold it.
2583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
2584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// node is a GlobalAddress + offset.
258819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
2589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    int64_t &Offset) const {
2590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<GlobalAddressSDNode>(N)) {
2591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
2592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GA = GASD->getGlobal();
2593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset += GASD->getOffset();
2594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
2595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N->getOpcode() == ISD::ADD) {
2598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N1 = N->getOperand(0);
2599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N2 = N->getOperand(1);
2600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
2601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (V) {
2603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offset += V->getSExtValue();
2604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
2605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
2607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (V) {
2609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offset += V->getSExtValue();
2610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
2611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
261419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
2616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue TargetLowering::
2620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
2621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Default implementation: no optimization.
2622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
2623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  Inline Assembler Implementation Methods
2627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::ConstraintType
2631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTargetLowering::getConstraintType(const std::string &Constraint) const {
2632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint.size() == 1) {
2633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Constraint[0]) {
2634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: break;
2635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'r': return C_RegisterClass;
2636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'm':    // memory
2637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'o':    // offsetable
2638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'V':    // not offsetable
2639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return C_Memory;
2640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'i':    // Simple Integer or Relocatable Constant
2641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'n':    // Simple Integer
264219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'E':    // Floating Point Constant
264319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'F':    // Floating Point Constant
2644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 's':    // Relocatable Constant
264519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'p':    // Address.
2646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'X':    // Allow ANY value.
2647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'I':    // Target registers.
2648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'J':
2649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'K':
2650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'L':
2651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'M':
2652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'N':
2653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'O':
2654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'P':
265519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case '<':
265619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case '>':
2657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return C_Other;
2658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
266019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
266119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constraint.size() > 1 && Constraint[0] == '{' &&
2662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constraint[Constraint.size()-1] == '}')
2663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return C_Register;
2664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C_Unknown;
2665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerXConstraint - try to replace an X constraint, which matches anything,
2668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// with another that has more specific requirements based on the type of the
2669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// corresponding operand.
2670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
2671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstraintVT.isInteger())
2672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return "r";
2673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstraintVT.isFloatingPoint())
2674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return "f";      // works for many targets
2675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
2676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// vector.  If it is invalid, don't add anything to Ops.
2680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
268119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  std::string &Constraint,
2682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                  std::vector<SDValue> &Ops,
2683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                  SelectionDAG &DAG) const {
268419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
268519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constraint.length() > 1) return;
268619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
268719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  char ConstraintLetter = Constraint[0];
2688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (ConstraintLetter) {
2689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
2690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'X':     // Allows any operand; labels (basic block) use this.
2691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getOpcode() == ISD::BasicBlock) {
2692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ops.push_back(Op);
2693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
2694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // fall through
2696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'i':    // Simple Integer or Relocatable Constant
2697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'n':    // Simple Integer
2698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 's': {  // Relocatable Constant
2699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These operands are interested in values of the form (GV+C), where C may
2700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // be folded in as an offset of GV, or it may be explicitly added.  Also, it
2701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // is possible and fine if either GV or C are missing.
2702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
270419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have "(add GV, C)", pull out GV/C
2706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getOpcode() == ISD::ADD) {
2707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C == 0 || GA == 0) {
2710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C == 0 || GA == 0)
2714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        C = 0, GA = 0;
2715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
271619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we find a valid operand, map to the TargetXXX version so that the
2718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // value itself doesn't get selected.
2719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GA) {   // Either &GV   or   &GV+C
2720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstraintLetter != 'n') {
2721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        int64_t Offs = GA->getOffset();
2722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (C) Offs += C->getZExtValue();
272319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
2724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 C ? C->getDebugLoc() : DebugLoc(),
2725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Op.getValueType(), Offs));
2726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return;
2727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C) {   // just C, no GV.
2730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Simple constants are not allowed for 's'.
2731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstraintLetter != 's') {
2732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // gcc prints these as sign extended.  Sign extend value to 64 bits
2733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // now; without this it would get ZExt'd later in
2734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // ScheduleDAGSDNodes::EmitNode, which is very generic.
2735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
2736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            MVT::i64));
2737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return;
2738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
2741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<unsigned, const TargetRegisterClass*> TargetLowering::
2746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetRegForInlineAsmConstraint(const std::string &Constraint,
2747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             EVT VT) const {
2748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint[0] != '{')
2749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
2750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Remove the braces from around the name.
2753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  StringRef RegName(Constraint.data()+1, Constraint.size()-2);
2754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Figure out which register class contains this reg.
2756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterInfo *RI = TM.getRegisterInfo();
2757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
2758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       E = RI->regclass_end(); RCI != E; ++RCI) {
2759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetRegisterClass *RC = *RCI;
276019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
276119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If none of the value types for this register class are valid, we
2762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // can't use it.  For example, 64-bit reg classes on 32-bit targets.
276319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isLegalRC(RC))
276419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
276519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
276619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
2767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
2768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RegName.equals_lower(RI->getName(*I)))
2769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(*I, RC);
2770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
277219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
2774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Constraint Selection.
2778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMatchingInputConstraint - Return true of this is an input operand that is
2780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a matching constraint like "4".
2781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
2782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!ConstraintCode.empty() && "No known constraint!");
2783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isdigit(ConstraintCode[0]);
2784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getMatchedOperand - If this is an input matching constraint, this method
2787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// returns the output operand it matches.
2788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!ConstraintCode.empty() && "No known constraint!");
2790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return atoi(ConstraintCode.c_str());
2791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
279419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ParseConstraints - Split up the constraint string from the inline
279519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// assembly value into the specific constraints and their prefixes,
279619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and also tie in the associated operand values.
279719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// If this returns an empty vector, and if the constraint string itself
279819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isn't empty, there was an error parsing.
279919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanTargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
280019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ImmutableCallSite CS) const {
280119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// ConstraintOperands - Information about all of the constraints.
280219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AsmOperandInfoVector ConstraintOperands;
280319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
280419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned maCount = 0; // Largest number of multiple alternative constraints.
280519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
280619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Do a prepass over the constraints, canonicalizing them, and building up the
280719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // ConstraintOperands list.
280819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InlineAsm::ConstraintInfoVector
280919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintInfos = IA->ParseConstraints();
281019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
281119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
281219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ResNo = 0;   // ResNo - The result number of the next output.
281319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
281419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
281519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
281619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AsmOperandInfo &OpInfo = ConstraintOperands.back();
281719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
281819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Update multiple alternative constraint count.
281919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (OpInfo.multipleAlternatives.size() > maCount)
282019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      maCount = OpInfo.multipleAlternatives.size();
282119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
282219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OpInfo.ConstraintVT = MVT::Other;
282319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
282419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Compute the value type for each operand.
282519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (OpInfo.Type) {
282619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case InlineAsm::isOutput:
282719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Indirect outputs just consume an argument.
282819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (OpInfo.isIndirect) {
282919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
283019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        break;
283119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
283219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
283319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // The return value of the call is this value.  As such, there is no
283419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // corresponding argument.
283519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(!CS.getType()->isVoidTy() &&
283619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             "Bad inline asm!");
283719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
283819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpInfo.ConstraintVT = getValueType(STy->getElementType(ResNo));
283919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else {
284019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(ResNo == 0 && "Asm only has one result!");
284119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpInfo.ConstraintVT = getValueType(CS.getType());
284219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
284319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ++ResNo;
284419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
284519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case InlineAsm::isInput:
284619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
284719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
284819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case InlineAsm::isClobber:
284919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Nothing to do.
285019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
285119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
285219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
285319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (OpInfo.CallOperandVal) {
285419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
285519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (OpInfo.isIndirect) {
285619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
285719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!PtrTy)
285819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          report_fatal_error("Indirect operand for inline asm not a pointer!");
285919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpTy = PtrTy->getElementType();
286019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
286119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
286219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
286319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (StructType *STy = dyn_cast<StructType>(OpTy))
286419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (STy->getNumElements() == 1)
286519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          OpTy = STy->getElementType(0);
286619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
286719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If OpTy is not a single value, it may be a struct/union that we
286819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // can tile with integers.
286919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!OpTy->isSingleValueType() && OpTy->isSized()) {
287019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned BitSize = TD->getTypeSizeInBits(OpTy);
287119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        switch (BitSize) {
287219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        default: break;
287319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 1:
287419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 8:
287519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 16:
287619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 32:
287719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 64:
287819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case 128:
287919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          OpInfo.ConstraintVT =
288019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              EVT::getEVT(IntegerType::get(OpTy->getContext(), BitSize), true);
288119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
288219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
288319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (dyn_cast<PointerType>(OpTy)) {
288419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpInfo.ConstraintVT = MVT::getIntegerVT(8*TD->getPointerSize());
288519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else {
288619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpInfo.ConstraintVT = EVT::getEVT(OpTy, true);
288719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
288819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
288919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
289019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
289119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we have multiple alternative constraints, select the best alternative.
289219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ConstraintInfos.size()) {
289319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (maCount) {
289419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned bestMAIndex = 0;
289519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int bestWeight = -1;
289619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // weight:  -1 = invalid match, and 0 = so-so match to 5 = good match.
289719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int weight = -1;
289819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned maIndex;
289919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Compute the sums of the weights for each alternative, keeping track
290019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // of the best (highest weight) one so far.
290119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (maIndex = 0; maIndex < maCount; ++maIndex) {
290219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        int weightSum = 0;
290319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
290419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            cIndex != eIndex; ++cIndex) {
290519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
290619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (OpInfo.Type == InlineAsm::isClobber)
290719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            continue;
290819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
290919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // If this is an output operand with a matching input operand,
291019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // look up the matching input. If their types mismatch, e.g. one
291119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // is an integer, the other is floating point, or their sizes are
291219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // different, flag it as an maCantMatch.
291319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (OpInfo.hasMatchingInput()) {
291419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
291519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if (OpInfo.ConstraintVT != Input.ConstraintVT) {
291619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              if ((OpInfo.ConstraintVT.isInteger() !=
291719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   Input.ConstraintVT.isInteger()) ||
291819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  (OpInfo.ConstraintVT.getSizeInBits() !=
291919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   Input.ConstraintVT.getSizeInBits())) {
292019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                weightSum = -1;  // Can't match.
292119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                break;
292219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              }
292319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            }
292419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
292519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
292619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (weight == -1) {
292719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            weightSum = -1;
292819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            break;
292919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
293019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          weightSum += weight;
293119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
293219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Update best.
293319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (weightSum > bestWeight) {
293419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          bestWeight = weightSum;
293519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          bestMAIndex = maIndex;
293619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
293719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
293819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
293919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Now select chosen alternative in each constraint.
294019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
294119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          cIndex != eIndex; ++cIndex) {
294219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
294319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (cInfo.Type == InlineAsm::isClobber)
294419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
294519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        cInfo.selectAlternative(bestMAIndex);
294619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
294719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
294819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
294919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
295019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check and hook up tied operands, choose constraint code to use.
295119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
295219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      cIndex != eIndex; ++cIndex) {
295319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
295419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
295519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If this is an output operand with a matching input operand, look up the
295619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // matching input. If their types mismatch, e.g. one is an integer, the
295719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // other is floating point, or their sizes are different, flag it as an
295819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // error.
295919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (OpInfo.hasMatchingInput()) {
296019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
296119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
296219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (OpInfo.ConstraintVT != Input.ConstraintVT) {
296319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	std::pair<unsigned, const TargetRegisterClass*> MatchRC =
296419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  getRegForInlineAsmConstraint(OpInfo.ConstraintCode, OpInfo.ConstraintVT);
296519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	std::pair<unsigned, const TargetRegisterClass*> InputRC =
296619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  getRegForInlineAsmConstraint(Input.ConstraintCode, Input.ConstraintVT);
296719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if ((OpInfo.ConstraintVT.isInteger() !=
296819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             Input.ConstraintVT.isInteger()) ||
296919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            (MatchRC.second != InputRC.second)) {
297019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          report_fatal_error("Unsupported asm: input constraint"
297119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             " with a matching output constraint of"
297219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             " incompatible type!");
297319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
297419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
297519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
297619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
297719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
297819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
297919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ConstraintOperands;
298019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
298119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
298219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getConstraintGenerality - Return an integer indicating how general CT
2984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is.
2985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (CT) {
2987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown constraint type!");
2988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case TargetLowering::C_Other:
2989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case TargetLowering::C_Unknown:
2990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
2991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case TargetLowering::C_Register:
2992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 1;
2993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case TargetLowering::C_RegisterClass:
2994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 2;
2995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case TargetLowering::C_Memory:
2996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 3;
2997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
300019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Examine constraint type and operand type and determine a weight value.
300119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This object must already have been set up with the operand type
300219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and the current alternative constraint selected.
300319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanTargetLowering::ConstraintWeight
300419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TargetLowering::getMultipleConstraintMatchWeight(
300519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AsmOperandInfo &info, int maIndex) const {
300619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InlineAsm::ConstraintCodeVector *rCodes;
300719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (maIndex >= (int)info.multipleAlternatives.size())
300819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    rCodes = &info.Codes;
300919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
301019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    rCodes = &info.multipleAlternatives[maIndex].Codes;
301119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstraintWeight BestWeight = CW_Invalid;
301219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
301319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Loop over the options, keeping track of the most general one.
301419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
301519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstraintWeight weight =
301619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
301719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (weight > BestWeight)
301819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      BestWeight = weight;
301919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
302019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
302119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return BestWeight;
302219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
302319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
302419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Examine constraint type and operand type and determine a weight value.
302519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This object must already have been set up with the operand type
302619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and the current alternative constraint selected.
302719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanTargetLowering::ConstraintWeight
302819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TargetLowering::getSingleConstraintMatchWeight(
302919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AsmOperandInfo &info, const char *constraint) const {
303019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstraintWeight weight = CW_Invalid;
303119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value *CallOperandVal = info.CallOperandVal;
303219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If we don't have a value, we can't do a match,
303319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // but allow it at the lowest weight.
303419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CallOperandVal == NULL)
303519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CW_Default;
303619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look at the constraint type.
303719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (*constraint) {
303819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'i': // immediate integer.
303919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'n': // immediate integer with a known value.
304019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isa<ConstantInt>(CallOperandVal))
304119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
304219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
304319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 's': // non-explicit intregal immediate.
304419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isa<GlobalValue>(CallOperandVal))
304519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
304619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
304719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'E': // immediate float if host format.
304819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'F': // immediate float.
304919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isa<ConstantFP>(CallOperandVal))
305019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
305119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
305219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case '<': // memory operand with autodecrement.
305319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case '>': // memory operand with autoincrement.
305419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'm': // memory operand.
305519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'o': // offsettable memory operand
305619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'V': // non-offsettable memory operand
305719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      weight = CW_Memory;
305819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
305919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'r': // general register.
306019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'g': // general register, memory operand or immediate integer.
306119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              // note: Clang converts "g" to "imr".
306219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (CallOperandVal->getType()->isIntegerTy())
306319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Register;
306419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
306519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'X': // any operand.
306619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default:
306719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      weight = CW_Default;
306819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
306919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
307019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return weight;
307119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
307219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ChooseConstraint - If there are multiple different constraints that we
3074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
3075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is somewhat tricky: constraints fall into four classes:
3076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    Other         -> immediates and magic values
3077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    Register      -> one specific register
3078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    RegisterClass -> a group of regs
3079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///    Memory        -> memory
3080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Ideally, we would pick the most specific constraint possible: if we have
3081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// something that fits into a register, we would pick it.  The problem here
3082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is that if we have something that could either be in a register or in
3083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// memory that use of the register could cause selection of *other*
3084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// operands to fail: they might only succeed if we pick memory.  Because of
3085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// this the heuristic we use is:
3086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
3087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///  1) If there is an 'other' constraint, and if the operand is valid for
3088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     that constraint, use it.  This makes us take advantage of 'i'
3089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     constraints when available.
3090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///  2) Otherwise, pick the most general constraint present.  This prefers
3091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     'm' over 'r', for example.
3092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
3093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
3094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const TargetLowering &TLI,
3095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             SDValue Op, SelectionDAG *DAG) {
3096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
3097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BestIdx = 0;
3098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
3099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int BestGenerality = -1;
3100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over the options, keeping track of the most general one.
3102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
3103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TargetLowering::ConstraintType CType =
3104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TLI.getConstraintType(OpInfo.Codes[i]);
3105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is an 'other' constraint, see if the operand is valid for it.
3107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // For example, on X86 we might have an 'rI' constraint.  If the operand
3108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // is an integer in the range [0..31] we want to use I (saving a load
3109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // of a register), otherwise we must use 'r'.
3110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CType == TargetLowering::C_Other && Op.getNode()) {
3111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(OpInfo.Codes[i].size() == 1 &&
3112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "Unhandled multi-letter 'other' constraint");
3113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::vector<SDValue> ResultOps;
311419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
3115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       ResultOps, *DAG);
3116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!ResultOps.empty()) {
3117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BestType = CType;
3118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BestIdx = i;
3119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
3120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
3121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
312219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Things with matching constraints can only be registers, per gcc
3124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // documentation.  This mainly affects "g" constraints.
3125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
3126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
312719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This constraint letter is more general than the previous one, use it.
3129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Generality = getConstraintGenerality(CType);
3130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Generality > BestGenerality) {
3131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestType = CType;
3132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestIdx = i;
3133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestGenerality = Generality;
3134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
3135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
313619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
3138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OpInfo.ConstraintType = BestType;
3139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ComputeConstraintToUse - Determines the constraint code and constraint
3142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// type to use for the specific AsmOperandInfo, setting
3143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
3144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
314519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            SDValue Op,
3146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            SelectionDAG *DAG) const {
3147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
314819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Single-letter constraints ('r') are very common.
3150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpInfo.Codes.size() == 1) {
3151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpInfo.ConstraintCode = OpInfo.Codes[0];
3152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
3153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
3154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ChooseConstraint(OpInfo, *this, Op, DAG);
3155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
315619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 'X' matches anything.
3158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
3159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Labels and constants are handled elsewhere ('X' is the only thing
3160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // that matches labels).  For Functions, the type here is the type of
3161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the result, which is not what we want to look at; leave them alone.
3162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Value *v = OpInfo.CallOperandVal;
3163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
3164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpInfo.CallOperandVal = v;
3165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
3166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
316719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, try to resolve it to something we know about by looking at
3169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the actual operand type.
3170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
3171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpInfo.ConstraintCode = Repl;
3172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
3173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
3174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
3178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  Loop Strength Reduction hooks
3179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
3180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isLegalAddressingMode - Return true if the addressing mode represented
3182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// by AM is legal for this target, for a load/store of the specified type.
318319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
318419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           Type *Ty) const {
3185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The default implementation of this implements a conservative RISCy, r+r and
3186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // r+i addr mode.
3187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Allows a sign-extended 16-bit immediate field.
3189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
3190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
319119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // No global is ever allowed as a base.
3193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AM.BaseGV)
3194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
319519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
319619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Only support r+r,
3197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (AM.Scale) {
3198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 0:  // "r+i" or just "i", depending on HasBaseReg.
3199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
3200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 1:
3201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
3202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise we have r+r or r+i.
3204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
3205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 2:
3206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
3207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Allow 2*r as r+r.
3209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
3210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
321119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
321519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
321619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// with the multiplicative inverse of the constant.
321719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
321819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       SelectionDAG &DAG) const {
321919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstantSDNode *C = cast<ConstantSDNode>(Op2);
322019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  APInt d = C->getAPIntValue();
322119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(d != 0 && "Division by zero!");
322219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
322319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Shift the value upfront if it is even, so the LSB is one.
322419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ShAmt = d.countTrailingZeros();
322519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ShAmt) {
322619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // TODO: For UDIV use SRL instead of SRA.
322719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
322819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
322919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    d = d.ashr(ShAmt);
323019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
323119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
323219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Calculate the multiplicative inverse, using Newton's method.
323319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  APInt t, xn = d;
323419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  while ((t = d*xn) != 1)
323519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    xn *= APInt(d.getBitWidth(), 2) - t;
323619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
323719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Op2 = DAG.getConstant(xn, Op1.getValueType());
323819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
323919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
324019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// return a DAG expression to select that will generate the same value by
3243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// multiplying by a magic number.  See:
3244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
324519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
3246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  std::vector<SDNode*>* Created) const {
3247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
3248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl= N->getDebugLoc();
324919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check to see if we can do this.
3251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We should be more aggressive here.
3252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(VT))
3253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
325419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
3256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt::ms magics = d.magic();
325719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Multiply the numerator (operand 0) by the magic value
3259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We should support doing a MUL in a wider type
3260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Q;
3261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isOperationLegalOrCustom(ISD::MULHS, VT))
3262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
3263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getConstant(magics.m, VT));
3264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
3265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
3266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              N->getOperand(0),
3267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(magics.m, VT)).getNode(), 1);
3268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
3269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();       // No mulhs or equvialent
3270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If d > 0 and m < 0, add the numerator
327119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (d.isStrictlyPositive() && magics.m.isNegative()) {
3272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
3273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(Q.getNode());
3275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If d < 0 and m > 0, subtract the numerator.
3277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (d.isNegative() && magics.m.isStrictlyPositive()) {
3278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
3279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(Q.getNode());
3281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Shift right algebraic if shift value is nonzero
3283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (magics.s > 0) {
328419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Q = DAG.getNode(ISD::SRA, dl, VT, Q,
328519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(Q.getNode());
3288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Extract the sign bit and add it to the quotient
3290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue T =
3291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
329219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           getShiftAmountTy(Q.getValueType())));
3293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Created)
3294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Created->push_back(T.getNode());
3295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(ISD::ADD, dl, VT, Q, T);
3296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// return a DAG expression to select that will generate the same value by
3300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// multiplying by a magic number.  See:
3301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
3303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  std::vector<SDNode*>* Created) const {
3304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
3305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = N->getDebugLoc();
3306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check to see if we can do this.
3308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We should be more aggressive here.
3309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(VT))
3310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
3311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We should use a narrower constant when the upper
3313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // bits are known to be zero.
331419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
331519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  APInt::mu magics = N1C.magicu();
331619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
331719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Q = N->getOperand(0);
331819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
331919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the divisor is even, we can avoid using the expensive fixup by shifting
332019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the divided value upfront.
332119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (magics.a != 0 && !N1C[0]) {
332219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Shift = N1C.countTrailingZeros();
332319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Q = DAG.getNode(ISD::SRL, dl, VT, Q,
332419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
332519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Created)
332619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Created->push_back(Q.getNode());
332719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
332819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Get magic number for the shifted divisor.
332919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    magics = N1C.lshr(Shift).magicu(Shift);
333019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(magics.a == 0 && "Should use cheap fixup now");
333119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
3332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Multiply the numerator (operand 0) by the magic value
3334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We should support doing a MUL in a wider type
3335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isOperationLegalOrCustom(ISD::MULHU, VT))
333619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
3337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
333819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
333919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            DAG.getConstant(magics.m, VT)).getNode(), 1);
3340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
3341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();       // No mulhu or equvialent
3342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Created)
3343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Created->push_back(Q.getNode());
3344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (magics.a == 0) {
334619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(magics.s < N1C.getBitWidth() &&
3347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "We shouldn't generate an undefined shift!");
334819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::SRL, dl, VT, Q,
334919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
3350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
3351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
3352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(NPQ.getNode());
335419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
335519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
3356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(NPQ.getNode());
3358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
3359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Created)
3360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Created->push_back(NPQ.getNode());
336119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::SRL, dl, VT, NPQ,
336219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
3363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3365