blob: 2e248e94511d39d4852237712100f82d80f97cc6 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetLowering.h"
Jakob Stoklund Olesene3ee49f2012-05-04 02:19:22 +000015#include "llvm/ADT/BitVector.h"
Owen Anderson718cb662007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/CodeGen/Analysis.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineJumpTableInfo.h"
21#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/GlobalVariable.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCExpr.h"
Nadav Rotemb6fbec32011-06-01 12:51:46 +000027#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000029#include "llvm/Support/MathExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetRegisterInfo.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000033#include <cctype>
Chris Lattner310968c2005-01-07 07:44:53 +000034using namespace llvm;
35
Duncan Sandsb2ff8852008-07-17 02:36:29 +000036/// getFPEXT - Return the FPEXT_*_* value for the given types, or
37/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +000038RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000039 if (OpVT == MVT::f32) {
40 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000041 return FPEXT_F32_F64;
Tim Northover24d315d2013-01-08 17:09:59 +000042 if (RetVT == MVT::f128)
43 return FPEXT_F32_F128;
44 } else if (OpVT == MVT::f64) {
45 if (RetVT == MVT::f128)
46 return FPEXT_F64_F128;
Duncan Sandsb2ff8852008-07-17 02:36:29 +000047 }
Anton Korobeynikov927411b2010-03-14 18:42:24 +000048
Duncan Sandsb2ff8852008-07-17 02:36:29 +000049 return UNKNOWN_LIBCALL;
50}
51
52/// getFPROUND - Return the FPROUND_*_* value for the given types, or
53/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +000054RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000055 if (RetVT == MVT::f32) {
56 if (OpVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000057 return FPROUND_F64_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +000058 if (OpVT == MVT::f80)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +000059 return FPROUND_F80_F32;
Tim Northover24d315d2013-01-08 17:09:59 +000060 if (OpVT == MVT::f128)
61 return FPROUND_F128_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +000062 if (OpVT == MVT::ppcf128)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +000063 return FPROUND_PPCF128_F32;
Owen Anderson825b72b2009-08-11 20:47:22 +000064 } else if (RetVT == MVT::f64) {
65 if (OpVT == MVT::f80)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +000066 return FPROUND_F80_F64;
Tim Northover24d315d2013-01-08 17:09:59 +000067 if (OpVT == MVT::f128)
68 return FPROUND_F128_F64;
Owen Anderson825b72b2009-08-11 20:47:22 +000069 if (OpVT == MVT::ppcf128)
Bruno Cardoso Lopese36bfe62008-08-07 19:01:24 +000070 return FPROUND_PPCF128_F64;
Duncan Sandsb2ff8852008-07-17 02:36:29 +000071 }
Anton Korobeynikov927411b2010-03-14 18:42:24 +000072
Duncan Sandsb2ff8852008-07-17 02:36:29 +000073 return UNKNOWN_LIBCALL;
74}
75
76/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
77/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +000078RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000079 if (OpVT == MVT::f32) {
80 if (RetVT == MVT::i8)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +000081 return FPTOSINT_F32_I8;
Owen Anderson825b72b2009-08-11 20:47:22 +000082 if (RetVT == MVT::i16)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +000083 return FPTOSINT_F32_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +000084 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000085 return FPTOSINT_F32_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +000086 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000087 return FPTOSINT_F32_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000089 return FPTOSINT_F32_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +000090 } else if (OpVT == MVT::f64) {
Anton Korobeynikovde0118c2010-03-26 21:32:14 +000091 if (RetVT == MVT::i8)
92 return FPTOSINT_F64_I8;
93 if (RetVT == MVT::i16)
94 return FPTOSINT_F64_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +000095 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000096 return FPTOSINT_F64_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +000097 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +000098 return FPTOSINT_F64_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +000099 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000100 return FPTOSINT_F64_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 } else if (OpVT == MVT::f80) {
102 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000103 return FPTOSINT_F80_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000105 return FPTOSINT_F80_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000107 return FPTOSINT_F80_I128;
Tim Northover24d315d2013-01-08 17:09:59 +0000108 } else if (OpVT == MVT::f128) {
109 if (RetVT == MVT::i32)
110 return FPTOSINT_F128_I32;
111 if (RetVT == MVT::i64)
112 return FPTOSINT_F128_I64;
113 if (RetVT == MVT::i128)
114 return FPTOSINT_F128_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 } else if (OpVT == MVT::ppcf128) {
116 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000117 return FPTOSINT_PPCF128_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000119 return FPTOSINT_PPCF128_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000121 return FPTOSINT_PPCF128_I128;
122 }
123 return UNKNOWN_LIBCALL;
124}
125
126/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
127/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000128RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 if (OpVT == MVT::f32) {
130 if (RetVT == MVT::i8)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000131 return FPTOUINT_F32_I8;
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 if (RetVT == MVT::i16)
Sanjiv Gupta8aa207e2009-06-16 09:03:58 +0000133 return FPTOUINT_F32_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000135 return FPTOUINT_F32_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000137 return FPTOUINT_F32_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000139 return FPTOUINT_F32_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 } else if (OpVT == MVT::f64) {
Anton Korobeynikovde0118c2010-03-26 21:32:14 +0000141 if (RetVT == MVT::i8)
142 return FPTOUINT_F64_I8;
143 if (RetVT == MVT::i16)
144 return FPTOUINT_F64_I16;
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000146 return FPTOUINT_F64_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000148 return FPTOUINT_F64_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000150 return FPTOUINT_F64_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 } else if (OpVT == MVT::f80) {
152 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000153 return FPTOUINT_F80_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000155 return FPTOUINT_F80_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000157 return FPTOUINT_F80_I128;
Tim Northover24d315d2013-01-08 17:09:59 +0000158 } else if (OpVT == MVT::f128) {
159 if (RetVT == MVT::i32)
160 return FPTOUINT_F128_I32;
161 if (RetVT == MVT::i64)
162 return FPTOUINT_F128_I64;
163 if (RetVT == MVT::i128)
164 return FPTOUINT_F128_I128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 } else if (OpVT == MVT::ppcf128) {
166 if (RetVT == MVT::i32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000167 return FPTOUINT_PPCF128_I32;
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 if (RetVT == MVT::i64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000169 return FPTOUINT_PPCF128_I64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 if (RetVT == MVT::i128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000171 return FPTOUINT_PPCF128_I128;
172 }
173 return UNKNOWN_LIBCALL;
174}
175
176/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
177/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000178RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 if (OpVT == MVT::i32) {
180 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000181 return SINTTOFP_I32_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000182 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000183 return SINTTOFP_I32_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000184 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000185 return SINTTOFP_I32_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000186 if (RetVT == MVT::f128)
187 return SINTTOFP_I32_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000188 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000189 return SINTTOFP_I32_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 } else if (OpVT == MVT::i64) {
191 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000192 return SINTTOFP_I64_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000193 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000194 return SINTTOFP_I64_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000195 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000196 return SINTTOFP_I64_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000197 if (RetVT == MVT::f128)
198 return SINTTOFP_I64_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000199 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000200 return SINTTOFP_I64_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 } else if (OpVT == MVT::i128) {
202 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000203 return SINTTOFP_I128_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000204 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000205 return SINTTOFP_I128_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000206 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000207 return SINTTOFP_I128_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000208 if (RetVT == MVT::f128)
209 return SINTTOFP_I128_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000210 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000211 return SINTTOFP_I128_PPCF128;
212 }
213 return UNKNOWN_LIBCALL;
214}
215
216/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
217/// UNKNOWN_LIBCALL if there is none.
Owen Andersone50ed302009-08-10 22:56:29 +0000218RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 if (OpVT == MVT::i32) {
220 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000221 return UINTTOFP_I32_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000222 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000223 return UINTTOFP_I32_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000224 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000225 return UINTTOFP_I32_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000226 if (RetVT == MVT::f128)
227 return UINTTOFP_I32_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000228 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000229 return UINTTOFP_I32_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 } else if (OpVT == MVT::i64) {
231 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000232 return UINTTOFP_I64_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000233 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000234 return UINTTOFP_I64_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000235 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000236 return UINTTOFP_I64_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000237 if (RetVT == MVT::f128)
238 return UINTTOFP_I64_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000239 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000240 return UINTTOFP_I64_PPCF128;
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 } else if (OpVT == MVT::i128) {
242 if (RetVT == MVT::f32)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000243 return UINTTOFP_I128_F32;
Craig Toppera1b3c032012-12-19 06:39:17 +0000244 if (RetVT == MVT::f64)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000245 return UINTTOFP_I128_F64;
Craig Toppera1b3c032012-12-19 06:39:17 +0000246 if (RetVT == MVT::f80)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000247 return UINTTOFP_I128_F80;
Tim Northover24d315d2013-01-08 17:09:59 +0000248 if (RetVT == MVT::f128)
249 return UINTTOFP_I128_F128;
Craig Toppera1b3c032012-12-19 06:39:17 +0000250 if (RetVT == MVT::ppcf128)
Duncan Sandsb2ff8852008-07-17 02:36:29 +0000251 return UINTTOFP_I128_PPCF128;
252 }
253 return UNKNOWN_LIBCALL;
254}
255
Chris Lattnerf0144122009-07-28 03:13:23 +0000256/// NOTE: The constructor takes ownership of TLOF.
Dan Gohmanf0757b02010-04-21 01:34:56 +0000257TargetLowering::TargetLowering(const TargetMachine &tm,
258 const TargetLoweringObjectFile *tlof)
Benjamin Kramer69e42db2013-01-11 20:05:37 +0000259 : TargetLoweringBase(tm, tlof) {}
Chris Lattnercba82f92005-01-16 07:28:11 +0000260
Evan Cheng72261582005-12-20 06:22:03 +0000261const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
262 return NULL;
263}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000264
Tim Northover2c8cf4b2013-01-09 13:18:15 +0000265/// Check whether a given call node is in tail position within its function. If
266/// so, it sets Chain to the input chain of the tail call.
267bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
268 SDValue &Chain) const {
269 const Function *F = DAG.getMachineFunction().getFunction();
270
271 // Conservatively require the attributes of the call to match those of
272 // the return. Ignore noalias because it doesn't affect the call sequence.
273 Attribute CallerRetAttr = F->getAttributes().getRetAttributes();
274 if (AttrBuilder(CallerRetAttr)
275 .removeAttribute(Attribute::NoAlias).hasAttributes())
276 return false;
277
278 // It's not safe to eliminate the sign / zero extension of the return value.
279 if (CallerRetAttr.hasAttribute(Attribute::ZExt) ||
280 CallerRetAttr.hasAttribute(Attribute::SExt))
281 return false;
282
283 // Check if the only use is a function return node.
284 return isUsedByReturnOnly(Node, Chain);
285}
286
287
288/// Generate a libcall taking the given operands as arguments and returning a
289/// result of type RetVT.
290SDValue TargetLowering::makeLibCall(SelectionDAG &DAG,
291 RTLIB::Libcall LC, EVT RetVT,
292 const SDValue *Ops, unsigned NumOps,
293 bool isSigned, DebugLoc dl) const {
294 TargetLowering::ArgListTy Args;
295 Args.reserve(NumOps);
296
297 TargetLowering::ArgListEntry Entry;
298 for (unsigned i = 0; i != NumOps; ++i) {
299 Entry.Node = Ops[i];
300 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
301 Entry.isSExt = isSigned;
302 Entry.isZExt = !isSigned;
303 Args.push_back(Entry);
304 }
305 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), getPointerTy());
306
307 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
308 TargetLowering::
309 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
310 false, 0, getLibcallCallingConv(LC),
311 /*isTailCall=*/false,
312 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
313 Callee, Args, DAG, dl);
314 std::pair<SDValue,SDValue> CallInfo = LowerCallTo(CLI);
315
316 return CallInfo.first;
317}
318
319
320/// SoftenSetCCOperands - Soften the operands of a comparison. This code is
321/// shared among BR_CC, SELECT_CC, and SETCC handlers.
322void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
323 SDValue &NewLHS, SDValue &NewRHS,
324 ISD::CondCode &CCCode,
325 DebugLoc dl) const {
326 assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
327 && "Unsupported setcc type!");
328
329 // Expand into one or more soft-fp libcall(s).
330 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
331 switch (CCCode) {
332 case ISD::SETEQ:
333 case ISD::SETOEQ:
334 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
335 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
336 break;
337 case ISD::SETNE:
338 case ISD::SETUNE:
339 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
340 (VT == MVT::f64) ? RTLIB::UNE_F64 : RTLIB::UNE_F128;
341 break;
342 case ISD::SETGE:
343 case ISD::SETOGE:
344 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
345 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
346 break;
347 case ISD::SETLT:
348 case ISD::SETOLT:
349 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
350 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
351 break;
352 case ISD::SETLE:
353 case ISD::SETOLE:
354 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
355 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
356 break;
357 case ISD::SETGT:
358 case ISD::SETOGT:
359 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
360 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
361 break;
362 case ISD::SETUO:
363 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
364 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
365 break;
366 case ISD::SETO:
367 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
368 (VT == MVT::f64) ? RTLIB::O_F64 : RTLIB::O_F128;
369 break;
370 default:
371 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
372 (VT == MVT::f64) ? RTLIB::UO_F64 : RTLIB::UO_F128;
373 switch (CCCode) {
374 case ISD::SETONE:
375 // SETONE = SETOLT | SETOGT
376 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
377 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
378 // Fallthrough
379 case ISD::SETUGT:
380 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
381 (VT == MVT::f64) ? RTLIB::OGT_F64 : RTLIB::OGT_F128;
382 break;
383 case ISD::SETUGE:
384 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
385 (VT == MVT::f64) ? RTLIB::OGE_F64 : RTLIB::OGE_F128;
386 break;
387 case ISD::SETULT:
388 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
389 (VT == MVT::f64) ? RTLIB::OLT_F64 : RTLIB::OLT_F128;
390 break;
391 case ISD::SETULE:
392 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
393 (VT == MVT::f64) ? RTLIB::OLE_F64 : RTLIB::OLE_F128;
394 break;
395 case ISD::SETUEQ:
396 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
397 (VT == MVT::f64) ? RTLIB::OEQ_F64 : RTLIB::OEQ_F128;
398 break;
399 default: llvm_unreachable("Do not know how to soften this setcc!");
400 }
401 }
402
403 // Use the target specific return value for comparions lib calls.
404 EVT RetVT = getCmpLibcallReturnType();
405 SDValue Ops[2] = { NewLHS, NewRHS };
406 NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
407 NewRHS = DAG.getConstant(0, RetVT);
408 CCCode = getCmpLibcallCC(LC1);
409 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
410 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, getSetCCResultType(RetVT),
411 NewLHS, NewRHS, DAG.getCondCode(CCCode));
412 NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
413 NewLHS = DAG.getNode(ISD::SETCC, dl, getSetCCResultType(RetVT), NewLHS,
414 NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
415 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
416 NewRHS = SDValue();
417 }
418}
419
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000420/// Get the EVTs and ArgFlags collections that represent the legalized return
Dan Gohman84023e02010-07-10 09:00:22 +0000421/// type of the given function. This does not require a DAG or a return value,
422/// and is suitable for use before any DAGs for the function are constructed.
423/// TODO: Move this out of TargetLowering.cpp.
Bill Wendling8b62abd2012-12-30 13:01:51 +0000424void llvm::GetReturnInfo(Type* ReturnType, AttributeSet attr,
Dan Gohman84023e02010-07-10 09:00:22 +0000425 SmallVectorImpl<ISD::OutputArg> &Outs,
Eli Friedman2db0e9e2012-05-25 00:09:29 +0000426 const TargetLowering &TLI) {
Dan Gohman84023e02010-07-10 09:00:22 +0000427 SmallVector<EVT, 4> ValueVTs;
428 ComputeValueVTs(TLI, ReturnType, ValueVTs);
429 unsigned NumValues = ValueVTs.size();
430 if (NumValues == 0) return;
Dan Gohman84023e02010-07-10 09:00:22 +0000431
432 for (unsigned j = 0, f = NumValues; j != f; ++j) {
433 EVT VT = ValueVTs[j];
434 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
435
Bill Wendling8b62abd2012-12-30 13:01:51 +0000436 if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Dan Gohman84023e02010-07-10 09:00:22 +0000437 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000438 else if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt))
Dan Gohman84023e02010-07-10 09:00:22 +0000439 ExtendKind = ISD::ZERO_EXTEND;
440
441 // FIXME: C calling convention requires the return type to be promoted to
442 // at least 32-bit. But this is not necessary for non-C calling
443 // conventions. The frontend should mark functions whose return values
444 // require promoting with signext or zeroext attributes.
445 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000446 MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Dan Gohman84023e02010-07-10 09:00:22 +0000447 if (VT.bitsLT(MinVT))
448 VT = MinVT;
449 }
450
451 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000452 MVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Dan Gohman84023e02010-07-10 09:00:22 +0000453
454 // 'inreg' on function refers to return value
455 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +0000456 if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::InReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000457 Flags.setInReg();
458
459 // Propagate extension type if any
Bill Wendling8b62abd2012-12-30 13:01:51 +0000460 if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Dan Gohman84023e02010-07-10 09:00:22 +0000461 Flags.setSExt();
Bill Wendling8b62abd2012-12-30 13:01:51 +0000462 else if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt))
Dan Gohman84023e02010-07-10 09:00:22 +0000463 Flags.setZExt();
464
Bill Wendlinge853d2e2012-09-19 23:35:21 +0000465 for (unsigned i = 0; i < NumParts; ++i)
Manman Ren0a1544d2012-11-01 23:49:58 +0000466 Outs.push_back(ISD::OutputArg(Flags, PartVT, /*isFixed=*/true, 0, 0));
Dan Gohman84023e02010-07-10 09:00:22 +0000467 }
468}
469
Chris Lattner071c62f2010-01-25 23:26:13 +0000470/// getJumpTableEncoding - Return the entry encoding for a jump table in the
471/// current function. The returned value is a member of the
472/// MachineJumpTableInfo::JTEntryKind enum.
473unsigned TargetLowering::getJumpTableEncoding() const {
474 // In non-pic modes, just use the address of a block.
475 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
476 return MachineJumpTableInfo::EK_BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000477
Chris Lattner071c62f2010-01-25 23:26:13 +0000478 // In PIC mode, if the target supports a GPRel32 directive, use it.
479 if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
480 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000481
Chris Lattner071c62f2010-01-25 23:26:13 +0000482 // Otherwise, use a label difference.
483 return MachineJumpTableInfo::EK_LabelDifference32;
484}
485
Dan Gohman475871a2008-07-27 21:46:04 +0000486SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
487 SelectionDAG &DAG) const {
Chris Lattnerf1214cb2010-01-26 06:53:37 +0000488 // If our PIC model is GP relative, use the global offset table as the base.
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000489 unsigned JTEncoding = getJumpTableEncoding();
490
491 if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
492 (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
Micah Villmow7d661462012-10-09 16:06:12 +0000493 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(0));
Akira Hatanaka787c3fd2012-04-09 20:32:12 +0000494
Evan Chengcc415862007-11-09 01:32:10 +0000495 return Table;
496}
497
Chris Lattner13e97a22010-01-26 05:30:30 +0000498/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
499/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
500/// MCExpr.
501const MCExpr *
Chris Lattner589c6f62010-01-26 06:28:43 +0000502TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
503 unsigned JTI,MCContext &Ctx) const{
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000504 // The normal PIC reloc base is the label at the start of the jump table.
Chris Lattner589c6f62010-01-26 06:28:43 +0000505 return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
Chris Lattner13e97a22010-01-26 05:30:30 +0000506}
507
Dan Gohman6520e202008-10-18 02:06:02 +0000508bool
509TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
510 // Assume that everything is safe in static mode.
511 if (getTargetMachine().getRelocationModel() == Reloc::Static)
512 return true;
513
514 // In dynamic-no-pic mode, assume that known defined values are safe.
515 if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
516 GA &&
517 !GA->getGlobal()->isDeclaration() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000518 !GA->getGlobal()->isWeakForLinker())
Dan Gohman6520e202008-10-18 02:06:02 +0000519 return true;
520
521 // Otherwise assume nothing is safe.
522 return false;
523}
524
Chris Lattnereb8146b2006-02-04 02:13:02 +0000525//===----------------------------------------------------------------------===//
526// Optimization Methods
527//===----------------------------------------------------------------------===//
528
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000529/// ShrinkDemandedConstant - Check to see if the specified operand of the
Nate Begeman368e18d2006-02-16 21:11:51 +0000530/// specified instruction is a constant integer. If so, check to see if there
531/// are any bits set in the constant that are not demanded. If so, shrink the
532/// constant and return true.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000533bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000534 const APInt &Demanded) {
Dale Johannesende064702009-02-06 21:50:26 +0000535 DebugLoc dl = Op.getDebugLoc();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000536
Chris Lattnerec665152006-02-26 23:36:02 +0000537 // FIXME: ISD::SELECT, ISD::SELECT_CC
Dan Gohmane5af2d32009-01-29 01:59:02 +0000538 switch (Op.getOpcode()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000539 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000540 case ISD::XOR:
Bill Wendling36ae6c12009-03-04 00:18:06 +0000541 case ISD::AND:
542 case ISD::OR: {
543 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
544 if (!C) return false;
545
546 if (Op.getOpcode() == ISD::XOR &&
547 (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
548 return false;
549
550 // if we can expand it to have all bits set, do it
551 if (C->getAPIntValue().intersects(~Demanded)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000552 EVT VT = Op.getValueType();
Bill Wendling36ae6c12009-03-04 00:18:06 +0000553 SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
554 DAG.getConstant(Demanded &
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000555 C->getAPIntValue(),
Bill Wendling36ae6c12009-03-04 00:18:06 +0000556 VT));
557 return CombineTo(Op, New);
558 }
559
Nate Begemande996292006-02-03 22:24:05 +0000560 break;
561 }
Bill Wendling36ae6c12009-03-04 00:18:06 +0000562 }
563
Nate Begemande996292006-02-03 22:24:05 +0000564 return false;
565}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000566
Dan Gohman97121ba2009-04-08 00:15:30 +0000567/// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
568/// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
569/// cast, but it could be generalized for targets with other types of
570/// implicit widening casts.
571bool
572TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
573 unsigned BitWidth,
574 const APInt &Demanded,
575 DebugLoc dl) {
576 assert(Op.getNumOperands() == 2 &&
577 "ShrinkDemandedOp only supports binary operators!");
578 assert(Op.getNode()->getNumValues() == 1 &&
579 "ShrinkDemandedOp only supports nodes with one result!");
580
581 // Don't do this if the node has another user, which may require the
582 // full value.
583 if (!Op.getNode()->hasOneUse())
584 return false;
585
586 // Search for the smallest integer type with free casts to and from
587 // Op's type. For expedience, just check power-of-2 integer types.
588 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000589 unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros();
590 unsigned SmallVTBits = DemandedSize;
Dan Gohman97121ba2009-04-08 00:15:30 +0000591 if (!isPowerOf2_32(SmallVTBits))
592 SmallVTBits = NextPowerOf2(SmallVTBits);
593 for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000594 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
Dan Gohman97121ba2009-04-08 00:15:30 +0000595 if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
596 TLI.isZExtFree(SmallVT, Op.getValueType())) {
597 // We found a type with free casts.
598 SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
599 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
600 Op.getNode()->getOperand(0)),
601 DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
602 Op.getNode()->getOperand(1)));
Nadav Rotembf5a2c62012-12-19 07:39:08 +0000603 bool NeedZext = DemandedSize > SmallVTBits;
604 SDValue Z = DAG.getNode(NeedZext ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND,
605 dl, Op.getValueType(), X);
Dan Gohman97121ba2009-04-08 00:15:30 +0000606 return CombineTo(Op, Z);
607 }
608 }
609 return false;
610}
611
Nate Begeman368e18d2006-02-16 21:11:51 +0000612/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
Chad Rosier8c1ec5a2011-06-11 02:27:46 +0000613/// DemandedMask bits of the result of Op are ever used downstream. If we can
Nate Begeman368e18d2006-02-16 21:11:51 +0000614/// use this information to simplify Op, create a new simplified DAG node and
615/// return true, returning the original and new nodes in Old and New. Otherwise,
616/// analyze the expression and return a mask of KnownOne and KnownZero bits for
617/// the expression (used to simplify the caller). The KnownZero/One bits may
618/// only be accurate for those bits in the DemandedMask.
Dan Gohman475871a2008-07-27 21:46:04 +0000619bool TargetLowering::SimplifyDemandedBits(SDValue Op,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000620 const APInt &DemandedMask,
621 APInt &KnownZero,
622 APInt &KnownOne,
Nate Begeman368e18d2006-02-16 21:11:51 +0000623 TargetLoweringOpt &TLO,
624 unsigned Depth) const {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000625 unsigned BitWidth = DemandedMask.getBitWidth();
Dan Gohman87862e72009-12-11 21:31:27 +0000626 assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000627 "Mask size mismatches value type size!");
628 APInt NewMask = DemandedMask;
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000629 DebugLoc dl = Op.getDebugLoc();
Chris Lattner3fc5b012007-05-17 18:19:23 +0000630
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000631 // Don't know anything.
632 KnownZero = KnownOne = APInt(BitWidth, 0);
633
Nate Begeman368e18d2006-02-16 21:11:51 +0000634 // Other users may use these bits.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000635 if (!Op.getNode()->hasOneUse()) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000636 if (Depth != 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000637 // If not at the root, Just compute the KnownZero/KnownOne bits to
Nate Begeman368e18d2006-02-16 21:11:51 +0000638 // simplify things downstream.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000639 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000640 return false;
641 }
642 // If this is the root being simplified, allow it to have multiple uses,
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000643 // just set the NewMask to all bits.
644 NewMask = APInt::getAllOnesValue(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000645 } else if (DemandedMask == 0) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000646 // Not demanding any bits from Op.
647 if (Op.getOpcode() != ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +0000648 return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
Nate Begeman368e18d2006-02-16 21:11:51 +0000649 return false;
650 } else if (Depth == 6) { // Limit search depth.
651 return false;
652 }
653
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000654 APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000655 switch (Op.getOpcode()) {
656 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000657 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000658 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
659 KnownZero = ~KnownOne;
Chris Lattnerec665152006-02-26 23:36:02 +0000660 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000661 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000662 // If the RHS is a constant, check to see if the LHS would be zero without
663 // using the bits from the RHS. Below, we use knowledge about the RHS to
664 // simplify the LHS, here we're using information from the LHS to simplify
665 // the RHS.
666 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000667 APInt LHSZero, LHSOne;
Dale Johannesen97fd9a52011-01-10 21:53:07 +0000668 // Do not increment Depth here; that can cause an infinite loop.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000669 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
Chris Lattner81cd3552006-02-27 00:36:27 +0000670 // If the LHS already has zeros where RHSC does, this and is dead.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000671 if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000672 return TLO.CombineTo(Op, Op.getOperand(0));
673 // If any of the set bits in the RHS are known zero on the LHS, shrink
674 // the constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000675 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
Chris Lattner81cd3552006-02-27 00:36:27 +0000676 return true;
677 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000678
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000679 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000680 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000681 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000683 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000684 KnownZero2, KnownOne2, TLO, Depth+1))
685 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
687
Nate Begeman368e18d2006-02-16 21:11:51 +0000688 // If all of the demanded bits are known one on one side, return the other.
689 // These bits cannot contribute to the result of the 'and'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000690 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000691 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000692 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000693 return TLO.CombineTo(Op, Op.getOperand(1));
694 // If all of the demanded bits in the inputs are known zeros, return zero.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000695 if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000696 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
697 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000698 if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000699 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000700 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000701 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000702 return true;
703
Nate Begeman368e18d2006-02-16 21:11:51 +0000704 // Output known-1 bits are only known if set in both the LHS & RHS.
705 KnownOne &= KnownOne2;
706 // Output known-0 are known to be clear if zero in either the LHS | RHS.
707 KnownZero |= KnownZero2;
708 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000709 case ISD::OR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000710 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000711 KnownOne, TLO, Depth+1))
712 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000713 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000714 if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000715 KnownZero2, KnownOne2, TLO, Depth+1))
716 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
718
Nate Begeman368e18d2006-02-16 21:11:51 +0000719 // If all of the demanded bits are known zero on one side, return the other.
720 // These bits cannot contribute to the result of the 'or'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000721 if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000722 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000723 if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000724 return TLO.CombineTo(Op, Op.getOperand(1));
725 // If all of the potentially set bits on one side are known to be set on
726 // the other side, just use the 'other' side.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000727 if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000728 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000729 if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000730 return TLO.CombineTo(Op, Op.getOperand(1));
731 // If the RHS is a constant, see if we can simplify it.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000732 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000733 return true;
Dan Gohman97121ba2009-04-08 00:15:30 +0000734 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000735 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000736 return true;
737
Nate Begeman368e18d2006-02-16 21:11:51 +0000738 // Output known-0 bits are only known if clear in both the LHS & RHS.
739 KnownZero &= KnownZero2;
740 // Output known-1 are known to be set if set in either the LHS | RHS.
741 KnownOne |= KnownOne2;
742 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000743 case ISD::XOR:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000744 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000745 KnownOne, TLO, Depth+1))
746 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000747 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000748 if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000749 KnownOne2, TLO, Depth+1))
750 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000751 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
752
Nate Begeman368e18d2006-02-16 21:11:51 +0000753 // If all of the demanded bits are known zero on one side, return the other.
754 // These bits cannot contribute to the result of the 'xor'.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000755 if ((KnownZero & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000756 return TLO.CombineTo(Op, Op.getOperand(0));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000757 if ((KnownZero2 & NewMask) == NewMask)
Nate Begeman368e18d2006-02-16 21:11:51 +0000758 return TLO.CombineTo(Op, Op.getOperand(1));
Dan Gohman97121ba2009-04-08 00:15:30 +0000759 // If the operation can be done in a smaller type, do so.
Dan Gohman4e39e9d2010-06-24 14:30:44 +0000760 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +0000761 return true;
762
Chris Lattner3687c1a2006-11-27 21:50:02 +0000763 // If all of the unknown bits are known to be zero on one side or the other
764 // (but not both) turn this into an *inclusive* or.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000765 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000766 if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
Dale Johannesende064702009-02-06 21:50:26 +0000767 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
Chris Lattner3687c1a2006-11-27 21:50:02 +0000768 Op.getOperand(0),
769 Op.getOperand(1)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000770
Nate Begeman368e18d2006-02-16 21:11:51 +0000771 // Output known-0 bits are known if clear or set in both the LHS & RHS.
772 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
773 // Output known-1 are known to be set if set in only one of the LHS, RHS.
774 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775
Nate Begeman368e18d2006-02-16 21:11:51 +0000776 // If all of the demanded bits on one side are known, and all of the set
777 // bits on that side are also known to be set on the other side, turn this
778 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000779 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Joel Jonesd16ce172012-04-17 22:23:10 +0000780 // NB: it is okay if more bits are known than are requested
781 if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
782 if (KnownOne == KnownOne2) { // set bits are the same on both sides
Owen Andersone50ed302009-08-10 22:56:29 +0000783 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000784 SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000785 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000786 Op.getOperand(0), ANDC));
Nate Begeman368e18d2006-02-16 21:11:51 +0000787 }
788 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000789
Nate Begeman368e18d2006-02-16 21:11:51 +0000790 // If the RHS is a constant, see if we can simplify it.
Torok Edwin4fea2e92008-04-06 21:23:02 +0000791 // for XOR, we prefer to force bits to 1 if they will make a -1.
792 // if we can't force bits, try to shrink constant
793 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
794 APInt Expanded = C->getAPIntValue() | (~NewMask);
795 // if we can expand it to have all bits set, do it
796 if (Expanded.isAllOnesValue()) {
797 if (Expanded != C->getAPIntValue()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000798 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000799 SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
Torok Edwin4fea2e92008-04-06 21:23:02 +0000800 TLO.DAG.getConstant(Expanded, VT));
801 return TLO.CombineTo(Op, New);
802 }
803 // if it already has all the bits set, nothing to change
804 // but don't shrink either!
805 } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
806 return true;
807 }
808 }
809
Nate Begeman368e18d2006-02-16 21:11:51 +0000810 KnownZero = KnownZeroOut;
811 KnownOne = KnownOneOut;
812 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000813 case ISD::SELECT:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000814 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +0000815 KnownOne, TLO, Depth+1))
816 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000817 if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
Nate Begeman368e18d2006-02-16 21:11:51 +0000818 KnownOne2, TLO, Depth+1))
819 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000820 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
821 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
822
Nate Begeman368e18d2006-02-16 21:11:51 +0000823 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000824 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Nate Begeman368e18d2006-02-16 21:11:51 +0000825 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000826
Nate Begeman368e18d2006-02-16 21:11:51 +0000827 // Only known if known in both the LHS and RHS.
828 KnownOne &= KnownOne2;
829 KnownZero &= KnownZero2;
830 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000831 case ISD::SELECT_CC:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000832 if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +0000833 KnownOne, TLO, Depth+1))
834 return true;
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000835 if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
Chris Lattnerec665152006-02-26 23:36:02 +0000836 KnownOne2, TLO, Depth+1))
837 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000838 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
839 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
840
Chris Lattnerec665152006-02-26 23:36:02 +0000841 // If the operands are constants, see if we can simplify them.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000842 if (TLO.ShrinkDemandedConstant(Op, NewMask))
Chris Lattnerec665152006-02-26 23:36:02 +0000843 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000844
Chris Lattnerec665152006-02-26 23:36:02 +0000845 // Only known if known in both the LHS and RHS.
846 KnownOne &= KnownOne2;
847 KnownZero &= KnownZero2;
848 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000849 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000850 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000851 unsigned ShAmt = SA->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SDValue InOp = Op.getOperand(0);
Chris Lattner895c4ab2007-04-17 21:14:16 +0000853
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000854 // If the shift count is an invalid immediate, don't do anything.
855 if (ShAmt >= BitWidth)
856 break;
857
Chris Lattner895c4ab2007-04-17 21:14:16 +0000858 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
859 // single shift. We can do this if the bottom bits (which are shifted
860 // out) are never demanded.
861 if (InOp.getOpcode() == ISD::SRL &&
862 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000863 if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000864 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000865 unsigned Opc = ISD::SHL;
866 int Diff = ShAmt-C1;
867 if (Diff < 0) {
868 Diff = -Diff;
869 Opc = ISD::SRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000870 }
871
872 SDValue NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000873 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +0000874 EVT VT = Op.getValueType();
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000875 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000876 InOp.getOperand(0), NewSA));
877 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000878 }
879
Dan Gohmana4f4d692010-07-23 18:03:30 +0000880 if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000881 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000882 return true;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000883
884 // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
885 // are not demanded. This will likely allow the anyext to be folded away.
886 if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
887 SDValue InnerOp = InOp.getNode()->getOperand(0);
888 EVT InnerVT = InnerOp.getValueType();
Eli Friedman2dd03532011-12-09 01:16:26 +0000889 unsigned InnerBits = InnerVT.getSizeInBits();
890 if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
Dan Gohmana4f4d692010-07-23 18:03:30 +0000891 isTypeDesirableForOp(ISD::SHL, InnerVT)) {
Owen Anderson95771af2011-02-25 21:41:48 +0000892 EVT ShTy = getShiftAmountTy(InnerVT);
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000893 if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
894 ShTy = InnerVT;
Dan Gohmana4f4d692010-07-23 18:03:30 +0000895 SDValue NarrowShl =
896 TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
Dan Gohmancd20c6f2010-07-23 21:08:12 +0000897 TLO.DAG.getConstant(ShAmt, ShTy));
Dan Gohmana4f4d692010-07-23 18:03:30 +0000898 return
899 TLO.CombineTo(Op,
900 TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
901 NarrowShl));
902 }
903 }
904
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000905 KnownZero <<= SA->getZExtValue();
906 KnownOne <<= SA->getZExtValue();
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000907 // low bits known zero.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000908 KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000909 }
910 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000911 case ISD::SRL:
912 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000913 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000914 unsigned ShAmt = SA->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000915 unsigned VTSize = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +0000916 SDValue InOp = Op.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000917
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000918 // If the shift count is an invalid immediate, don't do anything.
919 if (ShAmt >= BitWidth)
920 break;
921
Chris Lattner895c4ab2007-04-17 21:14:16 +0000922 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
923 // single shift. We can do this if the top bits (which are shifted out)
924 // are never demanded.
925 if (InOp.getOpcode() == ISD::SHL &&
926 isa<ConstantSDNode>(InOp.getOperand(1))) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000927 if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000928 unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000929 unsigned Opc = ISD::SRL;
930 int Diff = ShAmt-C1;
931 if (Diff < 0) {
932 Diff = -Diff;
933 Opc = ISD::SHL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000934 }
935
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000937 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000938 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000939 InOp.getOperand(0), NewSA));
940 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000941 }
942
Nate Begeman368e18d2006-02-16 21:11:51 +0000943 // Compute the new bits that are at the top now.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000944 if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
Nate Begeman368e18d2006-02-16 21:11:51 +0000945 KnownZero, KnownOne, TLO, Depth+1))
946 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000947 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000948 KnownZero = KnownZero.lshr(ShAmt);
949 KnownOne = KnownOne.lshr(ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000950
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000951 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000952 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000953 }
954 break;
955 case ISD::SRA:
Dan Gohmane5af2d32009-01-29 01:59:02 +0000956 // If this is an arithmetic shift right and only the low-bit is set, we can
957 // always convert this into a logical shr, even if the shift amount is
958 // variable. The low bit of the shift cannot be an input sign bit unless
959 // the shift amount is >= the size of the datatype, which is undefined.
Eli Friedman2dd03532011-12-09 01:16:26 +0000960 if (NewMask == 1)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000961 return TLO.CombineTo(Op,
962 TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
963 Op.getOperand(0), Op.getOperand(1)));
Dan Gohmane5af2d32009-01-29 01:59:02 +0000964
Nate Begeman368e18d2006-02-16 21:11:51 +0000965 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000966 EVT VT = Op.getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000967 unsigned ShAmt = SA->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000968
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000969 // If the shift count is an invalid immediate, don't do anything.
970 if (ShAmt >= BitWidth)
971 break;
972
973 APInt InDemandedMask = (NewMask << ShAmt);
Chris Lattner1b737132006-05-08 17:22:53 +0000974
975 // If any of the demanded bits are produced by the sign extension, we also
976 // demand the input sign bit.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000977 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
978 if (HighBits.intersects(NewMask))
Dan Gohman87862e72009-12-11 21:31:27 +0000979 InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000980
Chris Lattner1b737132006-05-08 17:22:53 +0000981 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000982 KnownZero, KnownOne, TLO, Depth+1))
983 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000984 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000985 KnownZero = KnownZero.lshr(ShAmt);
986 KnownOne = KnownOne.lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000987
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000988 // Handle the sign bit, adjusted to where it is now in the mask.
989 APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000990
Nate Begeman368e18d2006-02-16 21:11:51 +0000991 // If the input sign bit is known to be zero, or if none of the top bits
992 // are demanded, turn this into an unsigned shift right.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000993 if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000994 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000995 Op.getOperand(0),
Nate Begeman368e18d2006-02-16 21:11:51 +0000996 Op.getOperand(1)));
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000997 } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
Nate Begeman368e18d2006-02-16 21:11:51 +0000998 KnownOne |= HighBits;
999 }
1000 }
1001 break;
1002 case ISD::SIGN_EXTEND_INREG: {
Nadav Rotemcc616562012-01-15 19:27:55 +00001003 EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1004
1005 APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1);
1006 // If we only care about the highest bit, don't bother shifting right.
Eli Friedmand49db362012-01-31 01:08:03 +00001007 if (MsbMask == DemandedMask) {
Nadav Rotemcc616562012-01-15 19:27:55 +00001008 unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
1009 SDValue InOp = Op.getOperand(0);
Eli Friedmand49db362012-01-31 01:08:03 +00001010
1011 // Compute the correct shift amount type, which must be getShiftAmountTy
1012 // for scalar types after legalization.
1013 EVT ShiftAmtTy = Op.getValueType();
1014 if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
1015 ShiftAmtTy = getShiftAmountTy(ShiftAmtTy);
1016
1017 SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, ShiftAmtTy);
Nadav Rotemcc616562012-01-15 19:27:55 +00001018 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1019 Op.getValueType(), InOp, ShiftAmt));
1020 }
Nate Begeman368e18d2006-02-16 21:11:51 +00001021
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001022 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +00001023 // present in the input.
Dan Gohmand1996362010-01-09 02:13:55 +00001024 APInt NewBits =
1025 APInt::getHighBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +00001026 BitWidth - ExVT.getScalarType().getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001027
Chris Lattnerec665152006-02-26 23:36:02 +00001028 // If none of the extended bits are demanded, eliminate the sextinreg.
Eli Friedman1d17d192010-08-02 04:42:25 +00001029 if ((NewBits & NewMask) == 0)
Chris Lattnerec665152006-02-26 23:36:02 +00001030 return TLO.CombineTo(Op, Op.getOperand(0));
1031
Jay Foad40f8f622010-12-07 08:25:19 +00001032 APInt InSignBit =
Nadav Rotemcc616562012-01-15 19:27:55 +00001033 APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
Dan Gohmand1996362010-01-09 02:13:55 +00001034 APInt InputDemandedBits =
1035 APInt::getLowBitsSet(BitWidth,
Nadav Rotemcc616562012-01-15 19:27:55 +00001036 ExVT.getScalarType().getSizeInBits()) &
Dan Gohmand1996362010-01-09 02:13:55 +00001037 NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001038
Chris Lattnerec665152006-02-26 23:36:02 +00001039 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +00001040 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +00001041 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +00001042
1043 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
1044 KnownZero, KnownOne, TLO, Depth+1))
1045 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001046 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +00001047
1048 // If the sign bit of the input is known set or clear, then we know the
1049 // top bits of the result.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001050
Chris Lattnerec665152006-02-26 23:36:02 +00001051 // If the input sign bit is known zero, convert this into a zero extension.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001052 if (KnownZero.intersects(InSignBit))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001053 return TLO.CombineTo(Op,
Nadav Rotemcc616562012-01-15 19:27:55 +00001054 TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001055
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001056 if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +00001057 KnownOne |= NewBits;
1058 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +00001059 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +00001060 KnownZero &= ~NewBits;
1061 KnownOne &= ~NewBits;
1062 }
1063 break;
1064 }
Chris Lattnerec665152006-02-26 23:36:02 +00001065 case ISD::ZERO_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +00001066 unsigned OperandBitWidth =
1067 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001068 APInt InMask = NewMask.trunc(OperandBitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001069
Chris Lattnerec665152006-02-26 23:36:02 +00001070 // If none of the top bits are demanded, convert this into an any_extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001071 APInt NewBits =
1072 APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
1073 if (!NewBits.intersects(NewMask))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001074 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001075 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +00001076 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001077
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001078 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001079 KnownZero, KnownOne, TLO, Depth+1))
1080 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001082 KnownZero = KnownZero.zext(BitWidth);
1083 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001084 KnownZero |= NewBits;
1085 break;
1086 }
1087 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001088 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohmand1996362010-01-09 02:13:55 +00001089 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001090 APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
Dan Gohman97360282008-03-11 21:29:43 +00001091 APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001092 APInt NewBits = ~InMask & NewMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001093
Chris Lattnerec665152006-02-26 23:36:02 +00001094 // If none of the top bits are demanded, convert this into an any_extend.
1095 if (NewBits == 0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001096 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
1097 Op.getValueType(),
1098 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001099
Chris Lattnerec665152006-02-26 23:36:02 +00001100 // Since some of the sign extended bits are demanded, we know that the sign
1101 // bit is demanded.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001102 APInt InDemandedBits = InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001103 InDemandedBits |= InSignBit;
Jay Foad40f8f622010-12-07 08:25:19 +00001104 InDemandedBits = InDemandedBits.trunc(InBits);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001105
1106 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
Chris Lattnerec665152006-02-26 23:36:02 +00001107 KnownOne, TLO, Depth+1))
1108 return true;
Jay Foad40f8f622010-12-07 08:25:19 +00001109 KnownZero = KnownZero.zext(BitWidth);
1110 KnownOne = KnownOne.zext(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001111
Chris Lattnerec665152006-02-26 23:36:02 +00001112 // If the sign bit is known zero, convert this to a zero extend.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001113 if (KnownZero.intersects(InSignBit))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001114 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001115 Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +00001116 Op.getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001117
Chris Lattnerec665152006-02-26 23:36:02 +00001118 // If the sign bit is known one, the top bits match.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001119 if (KnownOne.intersects(InSignBit)) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001120 KnownOne |= NewBits;
1121 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +00001122 } else { // Otherwise, top bits aren't known.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001123 assert((KnownOne & NewBits) == 0);
1124 assert((KnownZero & NewBits) == 0);
Chris Lattnerec665152006-02-26 23:36:02 +00001125 }
1126 break;
1127 }
1128 case ISD::ANY_EXTEND: {
Dan Gohmand1996362010-01-09 02:13:55 +00001129 unsigned OperandBitWidth =
1130 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001131 APInt InMask = NewMask.trunc(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001132 if (SimplifyDemandedBits(Op.getOperand(0), InMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001133 KnownZero, KnownOne, TLO, Depth+1))
1134 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001135 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001136 KnownZero = KnownZero.zext(BitWidth);
1137 KnownOne = KnownOne.zext(BitWidth);
Chris Lattnerec665152006-02-26 23:36:02 +00001138 break;
1139 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001140 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001141 // Simplify the input, using demanded bit information, and compute the known
1142 // zero/one bits live out.
Dan Gohman042919c2010-03-01 17:59:21 +00001143 unsigned OperandBitWidth =
1144 Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001145 APInt TruncMask = NewMask.zext(OperandBitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001146 if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001147 KnownZero, KnownOne, TLO, Depth+1))
1148 return true;
Jay Foad40f8f622010-12-07 08:25:19 +00001149 KnownZero = KnownZero.trunc(BitWidth);
1150 KnownOne = KnownOne.trunc(BitWidth);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001151
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001152 // If the input is only used by this truncate, see if we can shrink it based
1153 // on the known demanded bits.
Gabor Greifba36cb52008-08-28 21:40:38 +00001154 if (Op.getOperand(0).getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001155 SDValue In = Op.getOperand(0);
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001156 switch (In.getOpcode()) {
1157 default: break;
1158 case ISD::SRL:
1159 // Shrink SRL by a constant if none of the high bits shifted in are
1160 // demanded.
Evan Chenge5b51ac2010-04-17 06:13:15 +00001161 if (TLO.LegalTypes() &&
1162 !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
1163 // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
1164 // undesirable.
1165 break;
1166 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
1167 if (!ShAmt)
1168 break;
Owen Anderson7adf8622011-04-13 23:22:23 +00001169 SDValue Shift = In.getOperand(1);
1170 if (TLO.LegalTypes()) {
1171 uint64_t ShVal = ShAmt->getZExtValue();
1172 Shift =
1173 TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
1174 }
1175
Evan Chenge5b51ac2010-04-17 06:13:15 +00001176 APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
1177 OperandBitWidth - BitWidth);
Jay Foad40f8f622010-12-07 08:25:19 +00001178 HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
Evan Chenge5b51ac2010-04-17 06:13:15 +00001179
1180 if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
1181 // None of the shifted in bits are needed. Add a truncate of the
1182 // shift input, then shift it.
1183 SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001184 Op.getValueType(),
Evan Chenge5b51ac2010-04-17 06:13:15 +00001185 In.getOperand(0));
1186 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
1187 Op.getValueType(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001188 NewTrunc,
Owen Anderson7adf8622011-04-13 23:22:23 +00001189 Shift));
Chris Lattnerc93dfda2006-05-06 00:11:52 +00001190 }
1191 break;
1192 }
1193 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194
1195 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerfe8babf2006-05-05 22:32:12 +00001196 break;
1197 }
Chris Lattnerec665152006-02-26 23:36:02 +00001198 case ISD::AssertZext: {
Owen Anderson7ab15f62011-09-03 00:26:49 +00001199 // AssertZext demands all of the high bits, plus any of the low bits
1200 // demanded by its users.
1201 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1202 APInt InMask = APInt::getLowBitsSet(BitWidth,
1203 VT.getSizeInBits());
1204 if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
Chris Lattnerec665152006-02-26 23:36:02 +00001205 KnownZero, KnownOne, TLO, Depth+1))
1206 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman400f75c2010-06-03 20:21:33 +00001208
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001209 KnownZero |= ~InMask & NewMask;
Chris Lattnerec665152006-02-26 23:36:02 +00001210 break;
1211 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001212 case ISD::BITCAST:
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001213 // If this is an FP->Int bitcast and if the sign bit is the only
1214 // thing demanded, turn this into a FGETSIGN.
Eli Friedmanca072a32011-12-15 02:07:20 +00001215 if (!TLO.LegalOperations() &&
1216 !Op.getValueType().isVector() &&
Eli Friedman0948f0a2011-11-09 22:25:12 +00001217 !Op.getOperand(0).getValueType().isVector() &&
Nadav Rotem0c3e6782011-06-12 14:56:55 +00001218 NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
1219 Op.getOperand(0).getValueType().isFloatingPoint()) {
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001220 bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
1221 bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
1222 if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
1223 EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001224 // Make a FGETSIGN + SHL to move the sign bit into the appropriate
1225 // place. We expect the SHL to be eliminated by other optimizations.
Stuart Hastings090bf192011-06-01 18:32:25 +00001226 SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
Stuart Hastings57f1fde2011-06-06 16:44:31 +00001227 unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
1228 if (!OpVTLegal && OpVTSizeInBits > 32)
Stuart Hastings090bf192011-06-01 18:32:25 +00001229 Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001230 unsigned ShVal = Op.getValueType().getSizeInBits()-1;
Stuart Hastingsbdce3722011-06-01 14:04:17 +00001231 SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
Stuart Hastings3dfc4b122011-05-19 18:48:20 +00001232 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
1233 Op.getValueType(),
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001234 Sign, ShAmt));
1235 }
1236 }
Chris Lattner2ceb2cf2007-12-22 21:35:38 +00001237 break;
Dan Gohman97121ba2009-04-08 00:15:30 +00001238 case ISD::ADD:
1239 case ISD::MUL:
1240 case ISD::SUB: {
1241 // Add, Sub, and Mul don't demand any bits in positions beyond that
1242 // of the highest bit demanded of them.
1243 APInt LoMask = APInt::getLowBitsSet(BitWidth,
1244 BitWidth - NewMask.countLeadingZeros());
1245 if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
1246 KnownOne2, TLO, Depth+1))
1247 return true;
1248 if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
1249 KnownOne2, TLO, Depth+1))
1250 return true;
1251 // See if the operation should be performed at a smaller bit width.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00001252 if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
Dan Gohman97121ba2009-04-08 00:15:30 +00001253 return true;
1254 }
1255 // FALL THROUGH
Dan Gohman54eed372008-05-06 00:53:29 +00001256 default:
Chris Lattner1482b5f2006-04-02 06:15:09 +00001257 // Just use ComputeMaskedBits to compute output bits.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001258 TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001259 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00001260 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001261
Chris Lattnerec665152006-02-26 23:36:02 +00001262 // If we know the value of all of the demanded bits, return this as a
1263 // constant.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00001264 if ((NewMask & (KnownZero|KnownOne)) == NewMask)
Chris Lattnerec665152006-02-26 23:36:02 +00001265 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001266
Nate Begeman368e18d2006-02-16 21:11:51 +00001267 return false;
1268}
1269
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001270/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1271/// in Mask are known to be either zero or one and return them in the
Nate Begeman368e18d2006-02-16 21:11:51 +00001272/// KnownZero/KnownOne bitsets.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001273void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001275 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001276 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00001277 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001278 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1279 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1280 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1281 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001282 "Should use MaskedValueIsZero if you don't know whether Op"
1283 " is a target node!");
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001284 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001285}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001286
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001287/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1288/// targets that want to expose additional information about sign bits to the
1289/// DAG Combiner.
Dan Gohman475871a2008-07-27 21:46:04 +00001290unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001291 unsigned Depth) const {
1292 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1293 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1294 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1295 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1296 "Should use ComputeNumSignBits if you don't know whether Op"
1297 " is a target node!");
1298 return 1;
1299}
1300
Dan Gohman97d11632009-02-15 23:59:32 +00001301/// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
1302/// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
1303/// determine which bit is set.
1304///
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001305static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
Dan Gohman97d11632009-02-15 23:59:32 +00001306 // A left-shift of a constant one will have exactly one bit set, because
1307 // shifting the bit off the end is undefined.
1308 if (Val.getOpcode() == ISD::SHL)
1309 if (ConstantSDNode *C =
1310 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1311 if (C->getAPIntValue() == 1)
1312 return true;
Dan Gohmane5af2d32009-01-29 01:59:02 +00001313
Dan Gohman97d11632009-02-15 23:59:32 +00001314 // Similarly, a right-shift of a constant sign-bit will have exactly
1315 // one bit set.
1316 if (Val.getOpcode() == ISD::SRL)
1317 if (ConstantSDNode *C =
1318 dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
1319 if (C->getAPIntValue().isSignBit())
1320 return true;
1321
1322 // More could be done here, though the above checks are enough
1323 // to handle some common cases.
1324
1325 // Fall back to ComputeMaskedBits to catch other known cases.
Owen Andersone50ed302009-08-10 22:56:29 +00001326 EVT OpVT = Val.getValueType();
Dan Gohman5b870af2010-03-02 02:14:38 +00001327 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
Dan Gohmane5af2d32009-01-29 01:59:02 +00001328 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001329 DAG.ComputeMaskedBits(Val, KnownZero, KnownOne);
Dale Johannesen85b0ede2009-02-11 19:19:41 +00001330 return (KnownZero.countPopulation() == BitWidth - 1) &&
1331 (KnownOne.countPopulation() == 1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00001332}
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001333
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001334/// SimplifySetCC - Try to simplify a setcc built with the specified operands
Dan Gohman475871a2008-07-27 21:46:04 +00001335/// and cc. If it is unable to simplify it, return a null SDValue.
1336SDValue
Owen Andersone50ed302009-08-10 22:56:29 +00001337TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
Evan Chengfa1eb272007-02-08 22:13:59 +00001338 ISD::CondCode Cond, bool foldBooleans,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001339 DAGCombinerInfo &DCI, DebugLoc dl) const {
Evan Chengfa1eb272007-02-08 22:13:59 +00001340 SelectionDAG &DAG = DCI.DAG;
1341
1342 // These setcc operations always fold.
1343 switch (Cond) {
1344 default: break;
1345 case ISD::SETFALSE:
1346 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1347 case ISD::SETTRUE:
1348 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1349 }
1350
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001351 // Ensure that the constant occurs on the RHS, and fold constant
1352 // comparisons.
1353 if (isa<ConstantSDNode>(N0.getNode()))
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001354 return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
Eric Christopher362fee92011-06-17 20:41:29 +00001355
Gabor Greifba36cb52008-08-28 21:40:38 +00001356 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c6cd1c2008-03-03 22:22:56 +00001357 const APInt &C1 = N1C->getAPIntValue();
Dale Johannesen89217a62008-11-07 01:28:02 +00001358
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001359 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
1360 // equality comparison, then we're just comparing whether X itself is
1361 // zero.
1362 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
1363 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
1364 N0.getOperand(1).getOpcode() == ISD::Constant) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001365 const APInt &ShAmt
1366 = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001367 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1368 ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
1369 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
1370 // (srl (ctlz x), 5) == 0 -> X != 0
1371 // (srl (ctlz x), 5) != 1 -> X != 0
1372 Cond = ISD::SETNE;
1373 } else {
1374 // (srl (ctlz x), 5) != 0 -> X == 0
1375 // (srl (ctlz x), 5) == 1 -> X == 0
1376 Cond = ISD::SETEQ;
1377 }
1378 SDValue Zero = DAG.getConstant(0, N0.getValueType());
1379 return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
1380 Zero, Cond);
1381 }
1382 }
1383
Benjamin Kramerd8228922011-01-17 12:04:57 +00001384 SDValue CTPOP = N0;
1385 // Look through truncs that don't change the value of a ctpop.
1386 if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
1387 CTPOP = N0.getOperand(0);
1388
1389 if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
Benjamin Kramerc9b6a3e2011-01-17 18:00:28 +00001390 (N0 == CTPOP || N0.getValueType().getSizeInBits() >
Benjamin Kramerd8228922011-01-17 12:04:57 +00001391 Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
1392 EVT CTVT = CTPOP.getValueType();
1393 SDValue CTOp = CTPOP.getOperand(0);
1394
1395 // (ctpop x) u< 2 -> (x & x-1) == 0
1396 // (ctpop x) u> 1 -> (x & x-1) != 0
1397 if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
1398 SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
1399 DAG.getConstant(1, CTVT));
1400 SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
1401 ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
1402 return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
1403 }
1404
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001405 // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
Benjamin Kramerd8228922011-01-17 12:04:57 +00001406 }
1407
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001408 // (zext x) == C --> x == (trunc C)
1409 if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
1410 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1411 unsigned MinBits = N0.getValueSizeInBits();
1412 SDValue PreZExt;
1413 if (N0->getOpcode() == ISD::ZERO_EXTEND) {
1414 // ZExt
1415 MinBits = N0->getOperand(0).getValueSizeInBits();
1416 PreZExt = N0->getOperand(0);
1417 } else if (N0->getOpcode() == ISD::AND) {
1418 // DAGCombine turns costly ZExts into ANDs
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
1420 if ((C->getAPIntValue()+1).isPowerOf2()) {
1421 MinBits = C->getAPIntValue().countTrailingOnes();
1422 PreZExt = N0->getOperand(0);
1423 }
1424 } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
1425 // ZEXTLOAD
1426 if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
1427 MinBits = LN0->getMemoryVT().getSizeInBits();
1428 PreZExt = N0;
1429 }
1430 }
1431
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001432 // Make sure we're not losing bits from the constant.
Benjamin Kramere7cf0622011-04-22 18:47:44 +00001433 if (MinBits < C1.getBitWidth() && MinBits > C1.getActiveBits()) {
1434 EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
1435 if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
1436 // Will get folded away.
1437 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
1438 SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
1439 return DAG.getSetCC(dl, VT, Trunc, C, Cond);
1440 }
1441 }
1442 }
1443
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001444 // If the LHS is '(and load, const)', the RHS is 0,
1445 // the test is for equality or unsigned, and all 1 bits of the const are
1446 // in the same partial word, see if we can shorten the load.
1447 if (DCI.isBeforeLegalize() &&
1448 N0.getOpcode() == ISD::AND && C1 == 0 &&
1449 N0.getNode()->hasOneUse() &&
1450 isa<LoadSDNode>(N0.getOperand(0)) &&
1451 N0.getOperand(0).getNode()->hasOneUse() &&
1452 isa<ConstantSDNode>(N0.getOperand(1))) {
1453 LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
Evan Cheng347a9cb2010-01-07 20:58:44 +00001454 APInt bestMask;
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001455 unsigned bestWidth = 0, bestOffset = 0;
Evan Cheng347a9cb2010-01-07 20:58:44 +00001456 if (!Lod->isVolatile() && Lod->isUnindexed()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001457 unsigned origWidth = N0.getValueType().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001458 unsigned maskWidth = origWidth;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001459 // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001460 // 8 bits, but have to be careful...
1461 if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
1462 origWidth = Lod->getMemoryVT().getSizeInBits();
Evan Cheng347a9cb2010-01-07 20:58:44 +00001463 const APInt &Mask =
1464 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001465 for (unsigned width = origWidth / 2; width>=8; width /= 2) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001466 APInt newMask = APInt::getLowBitsSet(maskWidth, width);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001467 for (unsigned offset=0; offset<origWidth/width; offset++) {
1468 if ((newMask & Mask) == Mask) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00001469 if (!getDataLayout()->isLittleEndian())
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001470 bestOffset = (origWidth/width - offset - 1) * (width/8);
1471 else
1472 bestOffset = (uint64_t)offset * (width/8);
Evan Cheng347a9cb2010-01-07 20:58:44 +00001473 bestMask = Mask.lshr(offset * (width/8) * 8);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001474 bestWidth = width;
1475 break;
Dale Johannesen89217a62008-11-07 01:28:02 +00001476 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001477 newMask = newMask << width;
Dale Johannesen89217a62008-11-07 01:28:02 +00001478 }
1479 }
1480 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001481 if (bestWidth) {
Chris Lattnerc0c7fca2011-04-14 04:12:47 +00001482 EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001483 if (newVT.isRound()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001484 EVT PtrType = Lod->getOperand(1).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001485 SDValue Ptr = Lod->getBasePtr();
1486 if (bestOffset != 0)
1487 Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
1488 DAG.getConstant(bestOffset, PtrType));
1489 unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
1490 SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001491 Lod->getPointerInfo().getWithOffset(bestOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001492 false, false, false, NewAlign);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001493 return DAG.getSetCC(dl, VT,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001494 DAG.getNode(ISD::AND, dl, newVT, NewLoad,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001495 DAG.getConstant(bestMask.trunc(bestWidth),
1496 newVT)),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001497 DAG.getConstant(0LL, newVT), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001498 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001499 }
1500 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001501
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001502 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1503 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1504 unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
1505
1506 // If the comparison constant has bits in the upper part, the
1507 // zero-extended value could never match.
1508 if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
1509 C1.getBitWidth() - InSize))) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001510 switch (Cond) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001511 case ISD::SETUGT:
1512 case ISD::SETUGE:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001513 case ISD::SETEQ: return DAG.getConstant(0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001514 case ISD::SETULT:
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001515 case ISD::SETULE:
1516 case ISD::SETNE: return DAG.getConstant(1, VT);
1517 case ISD::SETGT:
1518 case ISD::SETGE:
1519 // True if the sign bit of C1 is set.
1520 return DAG.getConstant(C1.isNegative(), VT);
1521 case ISD::SETLT:
1522 case ISD::SETLE:
1523 // True if the sign bit of C1 isn't set.
1524 return DAG.getConstant(C1.isNonNegative(), VT);
1525 default:
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001526 break;
1527 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001528 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001529
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001530 // Otherwise, we can perform the comparison with the low bits.
1531 switch (Cond) {
1532 case ISD::SETEQ:
1533 case ISD::SETNE:
1534 case ISD::SETUGT:
1535 case ISD::SETUGE:
1536 case ISD::SETULT:
1537 case ISD::SETULE: {
Patrik Hagglund34525f92012-12-11 11:14:33 +00001538 EVT newVT = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001539 if (DCI.isBeforeLegalizeOps() ||
1540 (isOperationLegal(ISD::SETCC, newVT) &&
Patrik Hagglund9c5ab932012-12-19 10:09:26 +00001541 getCondCodeAction(Cond, newVT.getSimpleVT())==Legal))
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001542 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Jay Foad40f8f622010-12-07 08:25:19 +00001543 DAG.getConstant(C1.trunc(InSize), newVT),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001544 Cond);
1545 break;
1546 }
1547 default:
1548 break; // todo, be more careful with signed comparisons
1549 }
1550 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00001551 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001552 EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001553 unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00001554 EVT ExtDstTy = N0.getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001555 unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
1556
Eli Friedmanad78a882010-07-30 06:44:31 +00001557 // If the constant doesn't fit into the number of bits for the source of
1558 // the sign extension, it is impossible for both sides to be equal.
1559 if (C1.getMinSignedBits() > ExtSrcTyBits)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001560 return DAG.getConstant(Cond == ISD::SETNE, VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001561
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001562 SDValue ZextOp;
Owen Andersone50ed302009-08-10 22:56:29 +00001563 EVT Op0Ty = N0.getOperand(0).getValueType();
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001564 if (Op0Ty == ExtSrcTy) {
1565 ZextOp = N0.getOperand(0);
1566 } else {
1567 APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
1568 ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
1569 DAG.getConstant(Imm, Op0Ty));
1570 }
1571 if (!DCI.isCalledByLegalizer())
1572 DCI.AddToWorklist(ZextOp.getNode());
1573 // Otherwise, make this a use of a zext.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001574 return DAG.getSetCC(dl, VT, ZextOp,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001575 DAG.getConstant(C1 & APInt::getLowBitsSet(
1576 ExtDstTyBits,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001577 ExtSrcTyBits),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001578 ExtDstTy),
1579 Cond);
1580 } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
1581 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001582 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00001583 if (N0.getOpcode() == ISD::SETCC &&
1584 isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
Evan Cheng347a9cb2010-01-07 20:58:44 +00001585 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001586 if (TrueWhenTrue)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001587 return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001588 // Invert the condition.
1589 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001590 CC = ISD::getSetCCInverse(CC,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001591 N0.getOperand(0).getValueType().isInteger());
1592 return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
Evan Chengfa1eb272007-02-08 22:13:59 +00001593 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001594
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001595 if ((N0.getOpcode() == ISD::XOR ||
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001596 (N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001597 N0.getOperand(0).getOpcode() == ISD::XOR &&
1598 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1599 isa<ConstantSDNode>(N0.getOperand(1)) &&
1600 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
1601 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1602 // can only do this if the top bits are known zero.
1603 unsigned BitWidth = N0.getValueSizeInBits();
1604 if (DAG.MaskedValueIsZero(N0,
1605 APInt::getHighBitsSet(BitWidth,
1606 BitWidth-1))) {
1607 // Okay, get the un-inverted input value.
1608 SDValue Val;
1609 if (N0.getOpcode() == ISD::XOR)
1610 Val = N0.getOperand(0);
1611 else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001612 assert(N0.getOpcode() == ISD::AND &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001613 N0.getOperand(0).getOpcode() == ISD::XOR);
1614 // ((X^1)&1)^1 -> X & 1
1615 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
1616 N0.getOperand(0).getOperand(0),
1617 N0.getOperand(1));
1618 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001619
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001620 return DAG.getSetCC(dl, VT, Val, N1,
1621 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1622 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00001623 } else if (N1C->getAPIntValue() == 1 &&
1624 (VT == MVT::i1 ||
Duncan Sands28b77e92011-09-06 19:07:46 +00001625 getBooleanContents(false) == ZeroOrOneBooleanContent)) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001626 SDValue Op0 = N0;
1627 if (Op0.getOpcode() == ISD::TRUNCATE)
1628 Op0 = Op0.getOperand(0);
1629
1630 if ((Op0.getOpcode() == ISD::XOR) &&
1631 Op0.getOperand(0).getOpcode() == ISD::SETCC &&
1632 Op0.getOperand(1).getOpcode() == ISD::SETCC) {
1633 // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
1634 Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
1635 return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
1636 Cond);
Craig Topper40b4a812012-12-19 06:12:28 +00001637 }
1638 if (Op0.getOpcode() == ISD::AND &&
1639 isa<ConstantSDNode>(Op0.getOperand(1)) &&
1640 cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00001641 // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001642 if (Op0.getValueType().bitsGT(VT))
Evan Cheng2c755ba2010-02-27 07:36:59 +00001643 Op0 = DAG.getNode(ISD::AND, dl, VT,
1644 DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
1645 DAG.getConstant(1, VT));
Anton Korobeynikov17458a72010-05-01 12:52:34 +00001646 else if (Op0.getValueType().bitsLT(VT))
1647 Op0 = DAG.getNode(ISD::AND, dl, VT,
1648 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
1649 DAG.getConstant(1, VT));
1650
Evan Cheng2c755ba2010-02-27 07:36:59 +00001651 return DAG.getSetCC(dl, VT, Op0,
1652 DAG.getConstant(0, Op0.getValueType()),
1653 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1654 }
Craig Topper40b4a812012-12-19 06:12:28 +00001655 if (Op0.getOpcode() == ISD::AssertZext &&
1656 cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
1657 return DAG.getSetCC(dl, VT, Op0,
1658 DAG.getConstant(0, Op0.getValueType()),
1659 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001660 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001661 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001662
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001663 APInt MinVal, MaxVal;
1664 unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
1665 if (ISD::isSignedIntSetCC(Cond)) {
1666 MinVal = APInt::getSignedMinValue(OperandBitSize);
1667 MaxVal = APInt::getSignedMaxValue(OperandBitSize);
1668 } else {
1669 MinVal = APInt::getMinValue(OperandBitSize);
1670 MaxVal = APInt::getMaxValue(OperandBitSize);
1671 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001672
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001673 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1674 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1675 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1676 // X >= C0 --> X > (C0-1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001677 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001678 DAG.getConstant(C1-1, N1.getValueType()),
1679 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1680 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001681
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001682 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1683 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1684 // X <= C0 --> X < (C0+1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001685 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001686 DAG.getConstant(C1+1, N1.getValueType()),
1687 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1688 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001689
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001690 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1691 return DAG.getConstant(0, VT); // X < MIN --> false
1692 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1693 return DAG.getConstant(1, VT); // X >= MIN --> true
1694 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1695 return DAG.getConstant(0, VT); // X > MAX --> false
1696 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1697 return DAG.getConstant(1, VT); // X <= MAX --> true
Evan Chengfa1eb272007-02-08 22:13:59 +00001698
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001699 // Canonicalize setgt X, Min --> setne X, Min
1700 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1701 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
1702 // Canonicalize setlt X, Max --> setne X, Max
1703 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1704 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
Evan Chengfa1eb272007-02-08 22:13:59 +00001705
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001706 // If we have setult X, 1, turn it into seteq X, 0
1707 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708 return DAG.getSetCC(dl, VT, N0,
1709 DAG.getConstant(MinVal, N0.getValueType()),
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001710 ISD::SETEQ);
1711 // If we have setugt X, Max-1, turn it into seteq X, Max
Craig Topper85022562012-12-19 06:43:58 +00001712 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001713 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001714 DAG.getConstant(MaxVal, N0.getValueType()),
1715 ISD::SETEQ);
Evan Chengfa1eb272007-02-08 22:13:59 +00001716
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001717 // If we have "setcc X, C0", check to see if we can shrink the immediate
1718 // by changing cc.
Evan Chengfa1eb272007-02-08 22:13:59 +00001719
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001720 // SETUGT X, SINTMAX -> SETLT X, 0
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001721 if (Cond == ISD::SETUGT &&
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001722 C1 == APInt::getSignedMaxValue(OperandBitSize))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001723 return DAG.getSetCC(dl, VT, N0,
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001724 DAG.getConstant(0, N1.getValueType()),
1725 ISD::SETLT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001726
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001727 // SETULT X, SINTMIN -> SETGT X, -1
1728 if (Cond == ISD::SETULT &&
1729 C1 == APInt::getSignedMinValue(OperandBitSize)) {
1730 SDValue ConstMinusOne =
1731 DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
1732 N1.getValueType());
1733 return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
1734 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001735
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001736 // Fold bit comparisons when we can.
1737 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001738 (VT == N0.getValueType() ||
1739 (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
1740 N0.getOpcode() == ISD::AND)
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001741 if (ConstantSDNode *AndRHS =
1742 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001743 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Owen Anderson95771af2011-02-25 21:41:48 +00001744 getPointerTy() : getShiftAmountTy(N0.getValueType());
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001745 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1746 // Perform the xform if the AND RHS is a single bit.
Evan Cheng347a9cb2010-01-07 20:58:44 +00001747 if (AndRHS->getAPIntValue().isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001748 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1749 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
Evan Cheng347a9cb2010-01-07 20:58:44 +00001750 DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001751 }
Evan Cheng347a9cb2010-01-07 20:58:44 +00001752 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001753 // (X & 8) == 8 --> (X & 8) >> 3
1754 // Perform the xform if C1 is a single bit.
1755 if (C1.isPowerOf2()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00001756 return DAG.getNode(ISD::TRUNCATE, dl, VT,
1757 DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
1758 DAG.getConstant(C1.logBase2(), ShiftTy)));
Evan Chengfa1eb272007-02-08 22:13:59 +00001759 }
1760 }
Eli Friedmanb101b0b2009-07-26 23:47:17 +00001761 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001762
Evan Chengb4d49592012-07-17 07:47:50 +00001763 if (C1.getMinSignedBits() <= 64 &&
1764 !isLegalICmpImmediate(C1.getSExtValue())) {
Evan Cheng70e10d32012-07-17 06:53:39 +00001765 // (X & -256) == 256 -> (X >> 8) == 1
1766 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1767 N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
1768 if (ConstantSDNode *AndRHS =
1769 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1770 const APInt &AndRHSC = AndRHS->getAPIntValue();
1771 if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
1772 unsigned ShiftBits = AndRHSC.countTrailingZeros();
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001773 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Evan Cheng70e10d32012-07-17 06:53:39 +00001774 getPointerTy() : getShiftAmountTy(N0.getValueType());
1775 EVT CmpTy = N0.getValueType();
1776 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
1777 DAG.getConstant(ShiftBits, ShiftTy));
1778 SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), CmpTy);
1779 return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
1780 }
1781 }
Evan Chengf5c05392012-07-17 08:31:11 +00001782 } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
1783 Cond == ISD::SETULE || Cond == ISD::SETUGT) {
1784 bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
1785 // X < 0x100000000 -> (X >> 32) < 1
1786 // X >= 0x100000000 -> (X >> 32) >= 1
1787 // X <= 0x0ffffffff -> (X >> 32) < 1
1788 // X > 0x0ffffffff -> (X >> 32) >= 1
1789 unsigned ShiftBits;
1790 APInt NewC = C1;
1791 ISD::CondCode NewCond = Cond;
1792 if (AdjOne) {
1793 ShiftBits = C1.countTrailingOnes();
1794 NewC = NewC + 1;
1795 NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1796 } else {
1797 ShiftBits = C1.countTrailingZeros();
1798 }
1799 NewC = NewC.lshr(ShiftBits);
1800 if (ShiftBits && isLegalICmpImmediate(NewC.getSExtValue())) {
Benjamin Kramerb97cebd2012-08-17 15:54:21 +00001801 EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
Evan Chengf5c05392012-07-17 08:31:11 +00001802 getPointerTy() : getShiftAmountTy(N0.getValueType());
1803 EVT CmpTy = N0.getValueType();
1804 SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
1805 DAG.getConstant(ShiftBits, ShiftTy));
1806 SDValue CmpRHS = DAG.getConstant(NewC, CmpTy);
1807 return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
1808 }
Evan Cheng70e10d32012-07-17 06:53:39 +00001809 }
1810 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001811 }
1812
Gabor Greifba36cb52008-08-28 21:40:38 +00001813 if (isa<ConstantFPSDNode>(N0.getNode())) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001814 // Constant fold or commute setcc.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001815 SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00001816 if (O.getNode()) return O;
1817 } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
Chris Lattner63079f02007-12-29 08:37:08 +00001818 // If the RHS of an FP comparison is a constant, simplify it away in
1819 // some cases.
1820 if (CFP->getValueAPF().isNaN()) {
1821 // If an operand is known to be a nan, we can fold it.
1822 switch (ISD::getUnorderedFlavor(Cond)) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001823 default: llvm_unreachable("Unknown flavor!");
Chris Lattner63079f02007-12-29 08:37:08 +00001824 case 0: // Known false.
1825 return DAG.getConstant(0, VT);
1826 case 1: // Known true.
1827 return DAG.getConstant(1, VT);
Chris Lattner1c3e1e22007-12-30 21:21:10 +00001828 case 2: // Undefined.
Dale Johannesene8d72302009-02-06 23:05:02 +00001829 return DAG.getUNDEF(VT);
Chris Lattner63079f02007-12-29 08:37:08 +00001830 }
1831 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001832
Chris Lattner63079f02007-12-29 08:37:08 +00001833 // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
1834 // constant if knowing that the operand is non-nan is enough. We prefer to
1835 // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
1836 // materialize 0.0.
1837 if (Cond == ISD::SETO || Cond == ISD::SETUO)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001838 return DAG.getSetCC(dl, VT, N0, N0, Cond);
Dan Gohman11eab022009-09-26 15:24:17 +00001839
1840 // If the condition is not legal, see if we can find an equivalent one
1841 // which is legal.
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001842 if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
Dan Gohman11eab022009-09-26 15:24:17 +00001843 // If the comparison was an awkward floating-point == or != and one of
1844 // the comparison operands is infinity or negative infinity, convert the
1845 // condition to a less-awkward <= or >=.
1846 if (CFP->getValueAPF().isInfinity()) {
1847 if (CFP->getValueAPF().isNegative()) {
1848 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001849 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001850 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
1851 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001852 isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001853 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
1854 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001855 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001856 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
1857 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001858 isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001859 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
1860 } else {
1861 if (Cond == ISD::SETOEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001862 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001863 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
1864 if (Cond == ISD::SETUEQ &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001865 isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001866 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
1867 if (Cond == ISD::SETUNE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001868 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001869 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
1870 if (Cond == ISD::SETONE &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00001871 isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
Dan Gohman11eab022009-09-26 15:24:17 +00001872 return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
1873 }
1874 }
1875 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001876 }
1877
1878 if (N0 == N1) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001879 // The sext(setcc()) => setcc() optimization relies on the appropriate
1880 // constant being emitted.
Nadav Roteme7576402012-09-06 11:13:55 +00001881 uint64_t EqVal = 0;
Duncan Sandse7de3b22012-07-05 09:32:46 +00001882 switch (getBooleanContents(N0.getValueType().isVector())) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001883 case UndefinedBooleanContent:
1884 case ZeroOrOneBooleanContent:
1885 EqVal = ISD::isTrueWhenEqual(Cond);
1886 break;
1887 case ZeroOrNegativeOneBooleanContent:
1888 EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
1889 break;
1890 }
1891
Evan Chengfa1eb272007-02-08 22:13:59 +00001892 // We can always fold X == X for integer setcc's.
Chad Rosier9dbb0182012-04-03 20:11:24 +00001893 if (N0.getValueType().isInteger()) {
Duncan Sandse7de3b22012-07-05 09:32:46 +00001894 return DAG.getConstant(EqVal, VT);
Chad Rosier9dbb0182012-04-03 20:11:24 +00001895 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001896 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1897 if (UOF == 2) // FP operators that are undefined on NaNs.
Duncan Sandse7de3b22012-07-05 09:32:46 +00001898 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001899 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Duncan Sandse7de3b22012-07-05 09:32:46 +00001900 return DAG.getConstant(EqVal, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +00001901 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1902 // if it is not already.
1903 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Micah Villmow8c574be2012-07-31 18:07:43 +00001904 if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
Patrik Hagglund9c5ab932012-12-19 10:09:26 +00001905 getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001906 return DAG.getSetCC(dl, VT, N0, N1, NewCond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001907 }
1908
1909 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001910 N0.getValueType().isInteger()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001911 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1912 N0.getOpcode() == ISD::XOR) {
1913 // Simplify (X+Y) == (X+Z) --> Y == Z
1914 if (N0.getOpcode() == N1.getOpcode()) {
1915 if (N0.getOperand(0) == N1.getOperand(0))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001916 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001917 if (N0.getOperand(1) == N1.getOperand(1))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001918 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001919 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1920 // If X op Y == Y op X, try other combinations.
1921 if (N0.getOperand(0) == N1.getOperand(1))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001922 return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001923 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001924 if (N0.getOperand(1) == N1.getOperand(0))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001925 return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001926 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001927 }
1928 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001929
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001930 // If RHS is a legal immediate value for a compare instruction, we need
1931 // to be careful about increasing register pressure needlessly.
1932 bool LegalRHSImm = false;
1933
Evan Chengfa1eb272007-02-08 22:13:59 +00001934 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1935 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1936 // Turn (X+C1) == C2 --> X == C2-C1
Gabor Greifba36cb52008-08-28 21:40:38 +00001937 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001938 return DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001939 DAG.getConstant(RHSC->getAPIntValue()-
1940 LHSR->getAPIntValue(),
Evan Chengfa1eb272007-02-08 22:13:59 +00001941 N0.getValueType()), Cond);
1942 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001943
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001944 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
Evan Chengfa1eb272007-02-08 22:13:59 +00001945 if (N0.getOpcode() == ISD::XOR)
1946 // If we know that all of the inverted bits are zero, don't bother
1947 // performing the inversion.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001948 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
1949 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001950 DAG.getSetCC(dl, VT, N0.getOperand(0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001951 DAG.getConstant(LHSR->getAPIntValue() ^
1952 RHSC->getAPIntValue(),
1953 N0.getValueType()),
1954 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001955 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001956
Evan Chengfa1eb272007-02-08 22:13:59 +00001957 // Turn (C1-X) == C2 --> X == C1-C2
1958 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001959 if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001960 return
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001961 DAG.getSetCC(dl, VT, N0.getOperand(1),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001962 DAG.getConstant(SUBC->getAPIntValue() -
1963 RHSC->getAPIntValue(),
1964 N0.getValueType()),
1965 Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00001966 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001967 }
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001968
1969 // Could RHSC fold directly into a compare?
1970 if (RHSC->getValueType(0).getSizeInBits() <= 64)
1971 LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
Evan Chengfa1eb272007-02-08 22:13:59 +00001972 }
1973
1974 // Simplify (X+Z) == X --> Z == 0
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001975 // Don't do this if X is an immediate that can fold into a cmp
1976 // instruction and X+Z has other uses. It could be an induction variable
1977 // chain, and the transform would increase register pressure.
1978 if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
1979 if (N0.getOperand(0) == N1)
1980 return DAG.getSetCC(dl, VT, N0.getOperand(1),
1981 DAG.getConstant(0, N0.getValueType()), Cond);
1982 if (N0.getOperand(1) == N1) {
1983 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1984 return DAG.getSetCC(dl, VT, N0.getOperand(0),
1985 DAG.getConstant(0, N0.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00001986 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001987 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1988 // (Z-X) == X --> Z == X<<1
1989 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N1,
Owen Anderson95771af2011-02-25 21:41:48 +00001990 DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
Jakob Stoklund Olesen740cd652012-04-05 20:30:20 +00001991 if (!DCI.isCalledByLegalizer())
1992 DCI.AddToWorklist(SH.getNode());
1993 return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
1994 }
Evan Chengfa1eb272007-02-08 22:13:59 +00001995 }
1996 }
1997 }
1998
1999 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2000 N1.getOpcode() == ISD::XOR) {
2001 // Simplify X == (X+Z) --> Z == 0
Craig Topper85022562012-12-19 06:43:58 +00002002 if (N1.getOperand(0) == N0)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002003 return DAG.getSetCC(dl, VT, N1.getOperand(1),
Evan Chengfa1eb272007-02-08 22:13:59 +00002004 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00002005 if (N1.getOperand(1) == N0) {
2006 if (DAG.isCommutativeBinOp(N1.getOpcode()))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002007 return DAG.getSetCC(dl, VT, N1.getOperand(0),
Evan Chengfa1eb272007-02-08 22:13:59 +00002008 DAG.getConstant(0, N1.getValueType()), Cond);
Craig Topper85022562012-12-19 06:43:58 +00002009 if (N1.getNode()->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002010 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2011 // X == (Z-X) --> X<<1 == Z
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002012 SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
Owen Anderson95771af2011-02-25 21:41:48 +00002013 DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
Evan Chengfa1eb272007-02-08 22:13:59 +00002014 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002015 DCI.AddToWorklist(SH.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002016 return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
Evan Chengfa1eb272007-02-08 22:13:59 +00002017 }
2018 }
2019 }
Dan Gohmane5af2d32009-01-29 01:59:02 +00002020
Dan Gohman2c65c3d2009-01-29 16:18:12 +00002021 // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002022 // Note that where y is variable and is known to have at most
2023 // one bit set (for example, if it is z&1) we cannot do this;
2024 // the expressions are not equivalent when y==0.
Dan Gohmane5af2d32009-01-29 01:59:02 +00002025 if (N0.getOpcode() == ISD::AND)
2026 if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002027 if (ValueHasExactlyOneBitSet(N1, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00002028 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2029 SDValue Zero = DAG.getConstant(0, N1.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002030 return DAG.getSetCC(dl, VT, N0, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00002031 }
2032 }
2033 if (N1.getOpcode() == ISD::AND)
2034 if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
Dale Johannesen85b0ede2009-02-11 19:19:41 +00002035 if (ValueHasExactlyOneBitSet(N0, DAG)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00002036 Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
2037 SDValue Zero = DAG.getConstant(0, N0.getValueType());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002038 return DAG.getSetCC(dl, VT, N1, Zero, Cond);
Dan Gohmane5af2d32009-01-29 01:59:02 +00002039 }
2040 }
Evan Chengfa1eb272007-02-08 22:13:59 +00002041 }
2042
2043 // Fold away ALL boolean setcc's.
Dan Gohman475871a2008-07-27 21:46:04 +00002044 SDValue Temp;
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002046 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002047 default: llvm_unreachable("Unknown integer setcc!");
Bob Wilson4c245462009-01-22 17:39:32 +00002048 case ISD::SETEQ: // X == Y -> ~(X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
2050 N0 = DAG.getNOT(dl, Temp, MVT::i1);
Evan Chengfa1eb272007-02-08 22:13:59 +00002051 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002052 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002053 break;
2054 case ISD::SETNE: // X != Y --> (X^Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002055 N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
Evan Chengfa1eb272007-02-08 22:13:59 +00002056 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002057 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
2058 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 Temp = DAG.getNOT(dl, N0, MVT::i1);
2060 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002061 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002062 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002063 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002064 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
2065 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 Temp = DAG.getNOT(dl, N1, MVT::i1);
2067 N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002068 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002069 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002070 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002071 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
2072 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 Temp = DAG.getNOT(dl, N0, MVT::i1);
2074 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002075 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002076 DCI.AddToWorklist(Temp.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002077 break;
Bob Wilson4c245462009-01-22 17:39:32 +00002078 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
2079 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Temp = DAG.getNOT(dl, N1, MVT::i1);
2081 N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
Evan Chengfa1eb272007-02-08 22:13:59 +00002082 break;
2083 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002084 if (VT != MVT::i1) {
Evan Chengfa1eb272007-02-08 22:13:59 +00002085 if (!DCI.isCalledByLegalizer())
Gabor Greifba36cb52008-08-28 21:40:38 +00002086 DCI.AddToWorklist(N0.getNode());
Evan Chengfa1eb272007-02-08 22:13:59 +00002087 // FIXME: If running after legalize, we probably can't do this.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002088 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
Evan Chengfa1eb272007-02-08 22:13:59 +00002089 }
2090 return N0;
2091 }
2092
2093 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00002094 return SDValue();
Evan Chengfa1eb272007-02-08 22:13:59 +00002095}
2096
Evan Chengad4196b2008-05-12 19:56:52 +00002097/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
2098/// node is a GlobalAddress + offset.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002099bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
Evan Chengad4196b2008-05-12 19:56:52 +00002100 int64_t &Offset) const {
2101 if (isa<GlobalAddressSDNode>(N)) {
Dan Gohman9ea3f562008-06-09 22:05:52 +00002102 GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
2103 GA = GASD->getGlobal();
2104 Offset += GASD->getOffset();
Evan Chengad4196b2008-05-12 19:56:52 +00002105 return true;
2106 }
2107
2108 if (N->getOpcode() == ISD::ADD) {
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SDValue N1 = N->getOperand(0);
2110 SDValue N2 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002111 if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002112 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
2113 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002114 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002115 return true;
2116 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002117 } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
Evan Chengad4196b2008-05-12 19:56:52 +00002118 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
2119 if (V) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00002120 Offset += V->getSExtValue();
Evan Chengad4196b2008-05-12 19:56:52 +00002121 return true;
2122 }
2123 }
2124 }
Owen Anderson95771af2011-02-25 21:41:48 +00002125
Evan Chengad4196b2008-05-12 19:56:52 +00002126 return false;
2127}
2128
2129
Dan Gohman475871a2008-07-27 21:46:04 +00002130SDValue TargetLowering::
Chris Lattner00ffed02006-03-01 04:52:55 +00002131PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
2132 // Default implementation: no optimization.
Dan Gohman475871a2008-07-27 21:46:04 +00002133 return SDValue();
Chris Lattner00ffed02006-03-01 04:52:55 +00002134}
2135
Chris Lattnereb8146b2006-02-04 02:13:02 +00002136//===----------------------------------------------------------------------===//
2137// Inline Assembler Implementation Methods
2138//===----------------------------------------------------------------------===//
2139
Chris Lattner4376fea2008-04-27 00:09:47 +00002140
Chris Lattnereb8146b2006-02-04 02:13:02 +00002141TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002142TargetLowering::getConstraintType(const std::string &Constraint) const {
Eric Christopherfffe3632013-01-11 18:12:39 +00002143 unsigned S = Constraint.size();
2144
2145 if (S == 1) {
Chris Lattner4234f572007-03-25 02:14:49 +00002146 switch (Constraint[0]) {
2147 default: break;
2148 case 'r': return C_RegisterClass;
2149 case 'm': // memory
2150 case 'o': // offsetable
2151 case 'V': // not offsetable
2152 return C_Memory;
2153 case 'i': // Simple Integer or Relocatable Constant
2154 case 'n': // Simple Integer
John Thompson67aff162010-09-21 22:04:54 +00002155 case 'E': // Floating Point Constant
2156 case 'F': // Floating Point Constant
Chris Lattner4234f572007-03-25 02:14:49 +00002157 case 's': // Relocatable Constant
John Thompson67aff162010-09-21 22:04:54 +00002158 case 'p': // Address.
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00002159 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00002160 case 'I': // Target registers.
2161 case 'J':
2162 case 'K':
2163 case 'L':
2164 case 'M':
2165 case 'N':
2166 case 'O':
2167 case 'P':
John Thompson67aff162010-09-21 22:04:54 +00002168 case '<':
2169 case '>':
Chris Lattner4234f572007-03-25 02:14:49 +00002170 return C_Other;
2171 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002172 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002173
Eric Christopherfffe3632013-01-11 18:12:39 +00002174 if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
2175 if (S == 8 && !Constraint.compare(1, 6, "memory", 6)) // "{memory}"
2176 return C_Memory;
Chris Lattner065421f2007-03-25 02:18:14 +00002177 return C_Register;
Eric Christopherfffe3632013-01-11 18:12:39 +00002178 }
Chris Lattner4234f572007-03-25 02:14:49 +00002179 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002180}
2181
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002182/// LowerXConstraint - try to replace an X constraint, which matches anything,
2183/// with another that has more specific requirements based on the type of the
2184/// corresponding operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002185const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00002186 if (ConstraintVT.isInteger())
Chris Lattner5e764232008-04-26 23:02:14 +00002187 return "r";
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 if (ConstraintVT.isFloatingPoint())
Chris Lattner5e764232008-04-26 23:02:14 +00002189 return "f"; // works for many targets
2190 return 0;
Dale Johannesenba2a0b92008-01-29 02:21:21 +00002191}
2192
Chris Lattner48884cd2007-08-25 00:47:38 +00002193/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2194/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00002195void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00002196 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00002197 std::vector<SDValue> &Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00002198 SelectionDAG &DAG) const {
Eric Christopher362fee92011-06-17 20:41:29 +00002199
Eric Christopher100c8332011-06-02 23:16:42 +00002200 if (Constraint.length() > 1) return;
Eric Christopher362fee92011-06-17 20:41:29 +00002201
Eric Christopher100c8332011-06-02 23:16:42 +00002202 char ConstraintLetter = Constraint[0];
Chris Lattnereb8146b2006-02-04 02:13:02 +00002203 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002204 default: break;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002205 case 'X': // Allows any operand; labels (basic block) use this.
2206 if (Op.getOpcode() == ISD::BasicBlock) {
2207 Ops.push_back(Op);
2208 return;
2209 }
2210 // fall through
Chris Lattnereb8146b2006-02-04 02:13:02 +00002211 case 'i': // Simple Integer or Relocatable Constant
2212 case 'n': // Simple Integer
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002213 case 's': { // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002214 // These operands are interested in values of the form (GV+C), where C may
2215 // be folded in as an offset of GV, or it may be explicitly added. Also, it
2216 // is possible and fine if either GV or C are missing.
2217 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2218 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002219
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002220 // If we have "(add GV, C)", pull out GV/C
2221 if (Op.getOpcode() == ISD::ADD) {
2222 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2223 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
2224 if (C == 0 || GA == 0) {
2225 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
2226 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
2227 }
2228 if (C == 0 || GA == 0)
2229 C = 0, GA = 0;
2230 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002231
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002232 // If we find a valid operand, map to the TargetXXX version so that the
2233 // value itself doesn't get selected.
2234 if (GA) { // Either &GV or &GV+C
2235 if (ConstraintLetter != 'n') {
2236 int64_t Offs = GA->getOffset();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002237 if (C) Offs += C->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002238 Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
Devang Patel07538ad2010-07-15 18:45:27 +00002239 C ? C->getDebugLoc() : DebugLoc(),
Chris Lattner48884cd2007-08-25 00:47:38 +00002240 Op.getValueType(), Offs));
2241 return;
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002242 }
2243 }
2244 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002245 // Simple constants are not allowed for 's'.
Chris Lattner48884cd2007-08-25 00:47:38 +00002246 if (ConstraintLetter != 's') {
Dale Johannesen78e3e522009-02-12 20:58:09 +00002247 // gcc prints these as sign extended. Sign extend value to 64 bits
2248 // now; without this it would get ZExt'd later in
2249 // ScheduleDAGSDNodes::EmitNode, which is very generic.
2250 Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002251 MVT::i64));
Chris Lattner48884cd2007-08-25 00:47:38 +00002252 return;
2253 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002254 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00002255 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00002256 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00002257 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00002258}
2259
Chris Lattner1efa40f2006-02-22 00:56:39 +00002260std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00002261getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00002262 EVT VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00002263 if (Constraint[0] != '{')
Douglas Gregor7d9663c2010-05-11 06:17:44 +00002264 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
Chris Lattnera55079a2006-02-01 01:29:47 +00002265 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
2266
2267 // Remove the braces from around the name.
Benjamin Kramer05872ea2009-11-12 20:36:59 +00002268 StringRef RegName(Constraint.data()+1, Constraint.size()-2);
Chris Lattner1efa40f2006-02-22 00:56:39 +00002269
Hal Finkelca2dd362012-12-18 17:50:58 +00002270 std::pair<unsigned, const TargetRegisterClass*> R =
2271 std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
2272
Chris Lattner1efa40f2006-02-22 00:56:39 +00002273 // Figure out which register class contains this reg.
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002274 const TargetRegisterInfo *RI = getTargetMachine().getRegisterInfo();
Dan Gohman6f0d0242008-02-10 18:45:23 +00002275 for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
Chris Lattner1efa40f2006-02-22 00:56:39 +00002276 E = RI->regclass_end(); RCI != E; ++RCI) {
2277 const TargetRegisterClass *RC = *RCI;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002278
2279 // If none of the value types for this register class are valid, we
Chris Lattnerb3befd42006-02-22 23:00:51 +00002280 // can't use it. For example, 64-bit reg classes on 32-bit targets.
Jakob Stoklund Olesen22e8a362011-10-12 01:24:51 +00002281 if (!isLegalRC(RC))
2282 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002283
2284 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
Chris Lattner1efa40f2006-02-22 00:56:39 +00002285 I != E; ++I) {
Hal Finkelca2dd362012-12-18 17:50:58 +00002286 if (RegName.equals_lower(RI->getName(*I))) {
2287 std::pair<unsigned, const TargetRegisterClass*> S =
2288 std::make_pair(*I, RC);
2289
2290 // If this register class has the requested value type, return it,
2291 // otherwise keep searching and return the first class found
2292 // if no other is found which explicitly has the requested type.
2293 if (RC->hasType(VT))
2294 return S;
2295 else if (!R.second)
2296 R = S;
2297 }
Chris Lattner1efa40f2006-02-22 00:56:39 +00002298 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00002299 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002300
Hal Finkelca2dd362012-12-18 17:50:58 +00002301 return R;
Chris Lattner4ccb0702006-01-26 20:37:03 +00002302}
Evan Cheng30b37b52006-03-13 23:18:16 +00002303
2304//===----------------------------------------------------------------------===//
Chris Lattner4376fea2008-04-27 00:09:47 +00002305// Constraint Selection.
2306
Chris Lattner6bdcda32008-10-17 16:47:46 +00002307/// isMatchingInputConstraint - Return true of this is an input operand that is
2308/// a matching constraint like "4".
2309bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
Chris Lattner58f15c42008-10-17 16:21:11 +00002310 assert(!ConstraintCode.empty() && "No known constraint!");
2311 return isdigit(ConstraintCode[0]);
2312}
2313
2314/// getMatchedOperand - If this is an input matching constraint, this method
2315/// returns the output operand it matches.
2316unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
2317 assert(!ConstraintCode.empty() && "No known constraint!");
2318 return atoi(ConstraintCode.c_str());
2319}
2320
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002321
John Thompsoneac6e1d2010-09-13 18:15:37 +00002322/// ParseConstraints - Split up the constraint string from the inline
2323/// assembly value into the specific constraints and their prefixes,
2324/// and also tie in the associated operand values.
2325/// If this returns an empty vector, and if the constraint string itself
2326/// isn't empty, there was an error parsing.
John Thompson44ab89e2010-10-29 17:29:13 +00002327TargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002328 ImmutableCallSite CS) const {
2329 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00002330 AsmOperandInfoVector ConstraintOperands;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002331 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
John Thompson67aff162010-09-21 22:04:54 +00002332 unsigned maCount = 0; // Largest number of multiple alternative constraints.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002333
2334 // Do a prepass over the constraints, canonicalizing them, and building up the
2335 // ConstraintOperands list.
John Thompson44ab89e2010-10-29 17:29:13 +00002336 InlineAsm::ConstraintInfoVector
John Thompsoneac6e1d2010-09-13 18:15:37 +00002337 ConstraintInfos = IA->ParseConstraints();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002338
John Thompsoneac6e1d2010-09-13 18:15:37 +00002339 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
2340 unsigned ResNo = 0; // ResNo - The result number of the next output.
2341
2342 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
2343 ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
2344 AsmOperandInfo &OpInfo = ConstraintOperands.back();
2345
John Thompson67aff162010-09-21 22:04:54 +00002346 // Update multiple alternative constraint count.
2347 if (OpInfo.multipleAlternatives.size() > maCount)
2348 maCount = OpInfo.multipleAlternatives.size();
2349
John Thompson44ab89e2010-10-29 17:29:13 +00002350 OpInfo.ConstraintVT = MVT::Other;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002351
2352 // Compute the value type for each operand.
2353 switch (OpInfo.Type) {
2354 case InlineAsm::isOutput:
2355 // Indirect outputs just consume an argument.
2356 if (OpInfo.isIndirect) {
2357 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2358 break;
2359 }
2360
2361 // The return value of the call is this value. As such, there is no
2362 // corresponding argument.
2363 assert(!CS.getType()->isVoidTy() &&
2364 "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002365 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002366 OpInfo.ConstraintVT = getSimpleValueType(STy->getElementType(ResNo));
John Thompsoneac6e1d2010-09-13 18:15:37 +00002367 } else {
2368 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002369 OpInfo.ConstraintVT = getSimpleValueType(CS.getType());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002370 }
2371 ++ResNo;
2372 break;
2373 case InlineAsm::isInput:
2374 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
2375 break;
2376 case InlineAsm::isClobber:
2377 // Nothing to do.
2378 break;
2379 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002380
John Thompson44ab89e2010-10-29 17:29:13 +00002381 if (OpInfo.CallOperandVal) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002382 llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002383 if (OpInfo.isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002384 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002385 if (!PtrTy)
2386 report_fatal_error("Indirect operand for inline asm not a pointer!");
2387 OpTy = PtrTy->getElementType();
2388 }
Eric Christopher362fee92011-06-17 20:41:29 +00002389
Eric Christophercef81b72011-05-09 20:04:43 +00002390 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002391 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00002392 if (STy->getNumElements() == 1)
2393 OpTy = STy->getElementType(0);
2394
John Thompson44ab89e2010-10-29 17:29:13 +00002395 // If OpTy is not a single value, it may be a struct/union that we
2396 // can tile with integers.
2397 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002398 unsigned BitSize = getDataLayout()->getTypeSizeInBits(OpTy);
John Thompson44ab89e2010-10-29 17:29:13 +00002399 switch (BitSize) {
2400 default: break;
2401 case 1:
2402 case 8:
2403 case 16:
2404 case 32:
2405 case 64:
2406 case 128:
Dale Johannesen71365d32010-11-09 01:15:07 +00002407 OpInfo.ConstraintVT =
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002408 MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
John Thompson44ab89e2010-10-29 17:29:13 +00002409 break;
2410 }
Micah Villmow7d661462012-10-09 16:06:12 +00002411 } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
2412 OpInfo.ConstraintVT = MVT::getIntegerVT(
Benjamin Kramer69e42db2013-01-11 20:05:37 +00002413 8*getDataLayout()->getPointerSize(PT->getAddressSpace()));
John Thompson44ab89e2010-10-29 17:29:13 +00002414 } else {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00002415 OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
John Thompson44ab89e2010-10-29 17:29:13 +00002416 }
2417 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002418 }
2419
2420 // If we have multiple alternative constraints, select the best alternative.
2421 if (ConstraintInfos.size()) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002422 if (maCount) {
2423 unsigned bestMAIndex = 0;
2424 int bestWeight = -1;
2425 // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
2426 int weight = -1;
2427 unsigned maIndex;
2428 // Compute the sums of the weights for each alternative, keeping track
2429 // of the best (highest weight) one so far.
2430 for (maIndex = 0; maIndex < maCount; ++maIndex) {
2431 int weightSum = 0;
2432 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2433 cIndex != eIndex; ++cIndex) {
2434 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
2435 if (OpInfo.Type == InlineAsm::isClobber)
2436 continue;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002437
John Thompson44ab89e2010-10-29 17:29:13 +00002438 // If this is an output operand with a matching input operand,
2439 // look up the matching input. If their types mismatch, e.g. one
2440 // is an integer, the other is floating point, or their sizes are
2441 // different, flag it as an maCantMatch.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002442 if (OpInfo.hasMatchingInput()) {
2443 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompsoneac6e1d2010-09-13 18:15:37 +00002444 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
2445 if ((OpInfo.ConstraintVT.isInteger() !=
2446 Input.ConstraintVT.isInteger()) ||
2447 (OpInfo.ConstraintVT.getSizeInBits() !=
2448 Input.ConstraintVT.getSizeInBits())) {
2449 weightSum = -1; // Can't match.
2450 break;
2451 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002452 }
2453 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002454 weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
2455 if (weight == -1) {
2456 weightSum = -1;
2457 break;
2458 }
2459 weightSum += weight;
2460 }
2461 // Update best.
2462 if (weightSum > bestWeight) {
2463 bestWeight = weightSum;
2464 bestMAIndex = maIndex;
2465 }
2466 }
2467
2468 // Now select chosen alternative in each constraint.
2469 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2470 cIndex != eIndex; ++cIndex) {
2471 AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
2472 if (cInfo.Type == InlineAsm::isClobber)
2473 continue;
2474 cInfo.selectAlternative(bestMAIndex);
2475 }
2476 }
2477 }
2478
2479 // Check and hook up tied operands, choose constraint code to use.
2480 for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
2481 cIndex != eIndex; ++cIndex) {
2482 AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002483
John Thompsoneac6e1d2010-09-13 18:15:37 +00002484 // If this is an output operand with a matching input operand, look up the
2485 // matching input. If their types mismatch, e.g. one is an integer, the
2486 // other is floating point, or their sizes are different, flag it as an
2487 // error.
2488 if (OpInfo.hasMatchingInput()) {
2489 AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
John Thompson44ab89e2010-10-29 17:29:13 +00002490
John Thompsoneac6e1d2010-09-13 18:15:37 +00002491 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00002492 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
2493 getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
2494 OpInfo.ConstraintVT);
2495 std::pair<unsigned, const TargetRegisterClass*> InputRC =
2496 getRegForInlineAsmConstraint(Input.ConstraintCode,
2497 Input.ConstraintVT);
John Thompsoneac6e1d2010-09-13 18:15:37 +00002498 if ((OpInfo.ConstraintVT.isInteger() !=
2499 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00002500 (MatchRC.second != InputRC.second)) {
John Thompsoneac6e1d2010-09-13 18:15:37 +00002501 report_fatal_error("Unsupported asm: input constraint"
2502 " with a matching output constraint of"
2503 " incompatible type!");
2504 }
John Thompsoneac6e1d2010-09-13 18:15:37 +00002505 }
John Thompson44ab89e2010-10-29 17:29:13 +00002506
John Thompsoneac6e1d2010-09-13 18:15:37 +00002507 }
2508 }
2509
2510 return ConstraintOperands;
2511}
2512
Chris Lattner58f15c42008-10-17 16:21:11 +00002513
Chris Lattner4376fea2008-04-27 00:09:47 +00002514/// getConstraintGenerality - Return an integer indicating how general CT
2515/// is.
2516static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
2517 switch (CT) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002518 case TargetLowering::C_Other:
2519 case TargetLowering::C_Unknown:
2520 return 0;
2521 case TargetLowering::C_Register:
2522 return 1;
2523 case TargetLowering::C_RegisterClass:
2524 return 2;
2525 case TargetLowering::C_Memory:
2526 return 3;
2527 }
Chandler Carruth732f05c2012-01-10 18:08:01 +00002528 llvm_unreachable("Invalid constraint type");
Chris Lattner4376fea2008-04-27 00:09:47 +00002529}
2530
John Thompson44ab89e2010-10-29 17:29:13 +00002531/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002532/// This object must already have been set up with the operand type
2533/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002534TargetLowering::ConstraintWeight
2535 TargetLowering::getMultipleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002536 AsmOperandInfo &info, int maIndex) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002537 InlineAsm::ConstraintCodeVector *rCodes;
John Thompson67aff162010-09-21 22:04:54 +00002538 if (maIndex >= (int)info.multipleAlternatives.size())
2539 rCodes = &info.Codes;
2540 else
2541 rCodes = &info.multipleAlternatives[maIndex].Codes;
John Thompson44ab89e2010-10-29 17:29:13 +00002542 ConstraintWeight BestWeight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002543
2544 // Loop over the options, keeping track of the most general one.
John Thompson67aff162010-09-21 22:04:54 +00002545 for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
John Thompson44ab89e2010-10-29 17:29:13 +00002546 ConstraintWeight weight =
2547 getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
John Thompsoneac6e1d2010-09-13 18:15:37 +00002548 if (weight > BestWeight)
2549 BestWeight = weight;
2550 }
2551
2552 return BestWeight;
2553}
2554
John Thompson44ab89e2010-10-29 17:29:13 +00002555/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002556/// This object must already have been set up with the operand type
2557/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +00002558TargetLowering::ConstraintWeight
2559 TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +00002560 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +00002561 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002562 Value *CallOperandVal = info.CallOperandVal;
2563 // If we don't have a value, we can't do a match,
2564 // but allow it at the lowest weight.
2565 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +00002566 return CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002567 // Look at the constraint type.
2568 switch (*constraint) {
2569 case 'i': // immediate integer.
2570 case 'n': // immediate integer with a known value.
John Thompson44ab89e2010-10-29 17:29:13 +00002571 if (isa<ConstantInt>(CallOperandVal))
2572 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002573 break;
2574 case 's': // non-explicit intregal immediate.
John Thompson44ab89e2010-10-29 17:29:13 +00002575 if (isa<GlobalValue>(CallOperandVal))
2576 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002577 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002578 case 'E': // immediate float if host format.
2579 case 'F': // immediate float.
2580 if (isa<ConstantFP>(CallOperandVal))
2581 weight = CW_Constant;
2582 break;
2583 case '<': // memory operand with autodecrement.
2584 case '>': // memory operand with autoincrement.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002585 case 'm': // memory operand.
2586 case 'o': // offsettable memory operand
2587 case 'V': // non-offsettable memory operand
John Thompson44ab89e2010-10-29 17:29:13 +00002588 weight = CW_Memory;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002589 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002590 case 'r': // general register.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002591 case 'g': // general register, memory operand or immediate integer.
John Thompson44ab89e2010-10-29 17:29:13 +00002592 // note: Clang converts "g" to "imr".
2593 if (CallOperandVal->getType()->isIntegerTy())
2594 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002595 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002596 case 'X': // any operand.
John Thompsoneac6e1d2010-09-13 18:15:37 +00002597 default:
John Thompson44ab89e2010-10-29 17:29:13 +00002598 weight = CW_Default;
John Thompsoneac6e1d2010-09-13 18:15:37 +00002599 break;
2600 }
2601 return weight;
2602}
2603
Chris Lattner4376fea2008-04-27 00:09:47 +00002604/// ChooseConstraint - If there are multiple different constraints that we
2605/// could pick for this operand (e.g. "imr") try to pick the 'best' one.
Chris Lattner24e1a9d2008-04-27 01:49:46 +00002606/// This is somewhat tricky: constraints fall into four classes:
Chris Lattner4376fea2008-04-27 00:09:47 +00002607/// Other -> immediates and magic values
2608/// Register -> one specific register
2609/// RegisterClass -> a group of regs
2610/// Memory -> memory
2611/// Ideally, we would pick the most specific constraint possible: if we have
2612/// something that fits into a register, we would pick it. The problem here
2613/// is that if we have something that could either be in a register or in
2614/// memory that use of the register could cause selection of *other*
2615/// operands to fail: they might only succeed if we pick memory. Because of
2616/// this the heuristic we use is:
2617///
2618/// 1) If there is an 'other' constraint, and if the operand is valid for
2619/// that constraint, use it. This makes us take advantage of 'i'
2620/// constraints when available.
2621/// 2) Otherwise, pick the most general constraint present. This prefers
2622/// 'm' over 'r', for example.
2623///
2624static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
Dale Johannesen1784d162010-06-25 21:55:36 +00002625 const TargetLowering &TLI,
Dan Gohman475871a2008-07-27 21:46:04 +00002626 SDValue Op, SelectionDAG *DAG) {
Chris Lattner4376fea2008-04-27 00:09:47 +00002627 assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
2628 unsigned BestIdx = 0;
2629 TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
2630 int BestGenerality = -1;
Dale Johannesena5989f82010-06-28 22:09:45 +00002631
Chris Lattner4376fea2008-04-27 00:09:47 +00002632 // Loop over the options, keeping track of the most general one.
2633 for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
2634 TargetLowering::ConstraintType CType =
2635 TLI.getConstraintType(OpInfo.Codes[i]);
Dale Johannesena5989f82010-06-28 22:09:45 +00002636
Chris Lattner5a096902008-04-27 00:37:18 +00002637 // If this is an 'other' constraint, see if the operand is valid for it.
2638 // For example, on X86 we might have an 'rI' constraint. If the operand
2639 // is an integer in the range [0..31] we want to use I (saving a load
2640 // of a register), otherwise we must use 'r'.
Gabor Greifba36cb52008-08-28 21:40:38 +00002641 if (CType == TargetLowering::C_Other && Op.getNode()) {
Chris Lattner5a096902008-04-27 00:37:18 +00002642 assert(OpInfo.Codes[i].size() == 1 &&
2643 "Unhandled multi-letter 'other' constraint");
Dan Gohman475871a2008-07-27 21:46:04 +00002644 std::vector<SDValue> ResultOps;
Eric Christopher100c8332011-06-02 23:16:42 +00002645 TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
Chris Lattner5a096902008-04-27 00:37:18 +00002646 ResultOps, *DAG);
2647 if (!ResultOps.empty()) {
2648 BestType = CType;
2649 BestIdx = i;
2650 break;
2651 }
2652 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002653
Dale Johannesena5989f82010-06-28 22:09:45 +00002654 // Things with matching constraints can only be registers, per gcc
2655 // documentation. This mainly affects "g" constraints.
2656 if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
2657 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002658
Chris Lattner4376fea2008-04-27 00:09:47 +00002659 // This constraint letter is more general than the previous one, use it.
2660 int Generality = getConstraintGenerality(CType);
2661 if (Generality > BestGenerality) {
2662 BestType = CType;
2663 BestIdx = i;
2664 BestGenerality = Generality;
2665 }
2666 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002667
Chris Lattner4376fea2008-04-27 00:09:47 +00002668 OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
2669 OpInfo.ConstraintType = BestType;
2670}
2671
2672/// ComputeConstraintToUse - Determines the constraint code and constraint
2673/// type to use for the specific AsmOperandInfo, setting
2674/// OpInfo.ConstraintCode and OpInfo.ConstraintType.
Chris Lattner5a096902008-04-27 00:37:18 +00002675void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002676 SDValue Op,
Chris Lattner5a096902008-04-27 00:37:18 +00002677 SelectionDAG *DAG) const {
Chris Lattner4376fea2008-04-27 00:09:47 +00002678 assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002679
Chris Lattner4376fea2008-04-27 00:09:47 +00002680 // Single-letter constraints ('r') are very common.
2681 if (OpInfo.Codes.size() == 1) {
2682 OpInfo.ConstraintCode = OpInfo.Codes[0];
2683 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2684 } else {
Dale Johannesen1784d162010-06-25 21:55:36 +00002685 ChooseConstraint(OpInfo, *this, Op, DAG);
Chris Lattner4376fea2008-04-27 00:09:47 +00002686 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002687
Chris Lattner4376fea2008-04-27 00:09:47 +00002688 // 'X' matches anything.
2689 if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
2690 // Labels and constants are handled elsewhere ('X' is the only thing
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002691 // that matches labels). For Functions, the type here is the type of
Dale Johannesen5339c552009-07-20 23:27:39 +00002692 // the result, which is not what we want to look at; leave them alone.
2693 Value *v = OpInfo.CallOperandVal;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002694 if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
2695 OpInfo.CallOperandVal = v;
Chris Lattner4376fea2008-04-27 00:09:47 +00002696 return;
Dale Johannesen8ea5ec62009-07-07 23:26:33 +00002697 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002698
Chris Lattner4376fea2008-04-27 00:09:47 +00002699 // Otherwise, try to resolve it to something we know about by looking at
2700 // the actual operand type.
2701 if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
2702 OpInfo.ConstraintCode = Repl;
2703 OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
2704 }
2705 }
2706}
2707
Benjamin Kramer9c640302011-07-08 10:31:30 +00002708/// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
2709/// with the multiplicative inverse of the constant.
2710SDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
2711 SelectionDAG &DAG) const {
2712 ConstantSDNode *C = cast<ConstantSDNode>(Op2);
2713 APInt d = C->getAPIntValue();
2714 assert(d != 0 && "Division by zero!");
2715
2716 // Shift the value upfront if it is even, so the LSB is one.
2717 unsigned ShAmt = d.countTrailingZeros();
2718 if (ShAmt) {
2719 // TODO: For UDIV use SRL instead of SRA.
2720 SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
2721 Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
2722 d = d.ashr(ShAmt);
2723 }
2724
2725 // Calculate the multiplicative inverse, using Newton's method.
2726 APInt t, xn = d;
2727 while ((t = d*xn) != 1)
2728 xn *= APInt(d.getBitWidth(), 2) - t;
2729
2730 Op2 = DAG.getConstant(xn, Op1.getValueType());
2731 return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
2732}
2733
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002734/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2735/// return a DAG expression to select that will generate the same value by
2736/// multiplying by a magic number. See:
2737/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne19a4daf2011-11-07 17:09:05 +00002738SDValue TargetLowering::
2739BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher9171fb92012-12-10 22:00:20 +00002740 std::vector<SDNode*> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002741 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002742 DebugLoc dl= N->getDebugLoc();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002743
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002744 // Check to see if we can do this.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002745 // FIXME: We should be more aggressive here.
2746 if (!isTypeLegal(VT))
2747 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002748
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002749 APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
Jay Foad4e5ea552009-04-30 10:15:35 +00002750 APInt::ms magics = d.magic();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002751
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002752 // Multiply the numerator (operand 0) by the magic value
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002753 // FIXME: We should support doing a MUL in a wider type
Dan Gohman475871a2008-07-27 21:46:04 +00002754 SDValue Q;
Richard Osborne19a4daf2011-11-07 17:09:05 +00002755 if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
2756 isOperationLegalOrCustom(ISD::MULHS, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002757 Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
Dan Gohman525178c2007-10-08 18:33:35 +00002758 DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002759 else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
2760 isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002761 Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
Dan Gohman525178c2007-10-08 18:33:35 +00002762 N->getOperand(0),
Gabor Greifba36cb52008-08-28 21:40:38 +00002763 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002764 else
Dan Gohman475871a2008-07-27 21:46:04 +00002765 return SDValue(); // No mulhs or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002766 // If d > 0 and m < 0, add the numerator
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002767 if (d.isStrictlyPositive() && magics.m.isNegative()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002768 Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002769 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002770 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002771 }
2772 // If d < 0 and m > 0, subtract the numerator.
Eli Friedmanfc69cb42008-11-30 06:35:39 +00002773 if (d.isNegative() && magics.m.isStrictlyPositive()) {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002774 Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002775 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002776 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002777 }
2778 // Shift right algebraic if shift value is nonzero
2779 if (magics.s > 0) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002780 Q = DAG.getNode(ISD::SRA, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002781 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002782 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002783 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002784 }
2785 // Extract the sign bit and add it to the quotient
Dan Gohman475871a2008-07-27 21:46:04 +00002786 SDValue T =
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002787 DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00002788 getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002789 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002790 Created->push_back(T.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002791 return DAG.getNode(ISD::ADD, dl, VT, Q, T);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002792}
2793
2794/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2795/// return a DAG expression to select that will generate the same value by
2796/// multiplying by a magic number. See:
2797/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Richard Osborne19a4daf2011-11-07 17:09:05 +00002798SDValue TargetLowering::
2799BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
Eric Christopher9171fb92012-12-10 22:00:20 +00002800 std::vector<SDNode*> *Created) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002801 EVT VT = N->getValueType(0);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002802 DebugLoc dl = N->getDebugLoc();
Eli Friedman201c9772008-11-30 06:02:26 +00002803
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002804 // Check to see if we can do this.
Eli Friedman201c9772008-11-30 06:02:26 +00002805 // FIXME: We should be more aggressive here.
2806 if (!isTypeLegal(VT))
2807 return SDValue();
2808
2809 // FIXME: We should use a narrower constant when the upper
2810 // bits are known to be zero.
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002811 const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
2812 APInt::mu magics = N1C.magicu();
2813
2814 SDValue Q = N->getOperand(0);
2815
2816 // If the divisor is even, we can avoid using the expensive fixup by shifting
2817 // the divided value upfront.
2818 if (magics.a != 0 && !N1C[0]) {
2819 unsigned Shift = N1C.countTrailingZeros();
2820 Q = DAG.getNode(ISD::SRL, dl, VT, Q,
2821 DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
2822 if (Created)
2823 Created->push_back(Q.getNode());
2824
2825 // Get magic number for the shifted divisor.
2826 magics = N1C.lshr(Shift).magicu(Shift);
2827 assert(magics.a == 0 && "Should use cheap fixup now");
2828 }
Eli Friedman201c9772008-11-30 06:02:26 +00002829
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002830 // Multiply the numerator (operand 0) by the magic value
Eli Friedman201c9772008-11-30 06:02:26 +00002831 // FIXME: We should support doing a MUL in a wider type
Richard Osborne19a4daf2011-11-07 17:09:05 +00002832 if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
2833 isOperationLegalOrCustom(ISD::MULHU, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002834 Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
Richard Osborne19a4daf2011-11-07 17:09:05 +00002835 else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
2836 isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002837 Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
2838 DAG.getConstant(magics.m, VT)).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002839 else
Dan Gohman475871a2008-07-27 21:46:04 +00002840 return SDValue(); // No mulhu or equvialent
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002841 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002842 Created->push_back(Q.getNode());
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002843
2844 if (magics.a == 0) {
Benjamin Kramer1c10b8d2011-03-17 20:39:14 +00002845 assert(magics.s < N1C.getBitWidth() &&
Eli Friedman201c9772008-11-30 06:02:26 +00002846 "We shouldn't generate an undefined shift!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002847 return DAG.getNode(ISD::SRL, dl, VT, Q,
Owen Anderson95771af2011-02-25 21:41:48 +00002848 DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002849 } else {
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002850 SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002851 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002852 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002853 NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002854 DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002855 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002856 Created->push_back(NPQ.getNode());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002857 NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002858 if (Created)
Gabor Greifba36cb52008-08-28 21:40:38 +00002859 Created->push_back(NPQ.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002860 return DAG.getNode(ISD::SRL, dl, VT, NPQ,
Owen Anderson95771af2011-02-25 21:41:48 +00002861 DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00002862 }
2863}