blob: b2c90166392559307f7a1374e7779b188ad391df [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Owen Anderson07000c62006-05-12 06:33:49 +000015#include "llvm/Target/TargetData.h"
Chris Lattner310968c2005-01-07 07:44:53 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000017#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerdc879292006-03-31 00:28:56 +000018#include "llvm/DerivedTypes.h"
Chris Lattner310968c2005-01-07 07:44:53 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000021#include "llvm/Support/MathExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000022using namespace llvm;
23
Evan Cheng56966222007-01-12 02:11:51 +000024/// InitLibcallNames - Set default libcall names.
25///
Evan Cheng79cca502007-01-12 22:51:10 +000026static void InitLibcallNames(const char **Names) {
Evan Cheng56966222007-01-12 02:11:51 +000027 Names[RTLIB::SHL_I32] = "__ashlsi3";
28 Names[RTLIB::SHL_I64] = "__ashldi3";
29 Names[RTLIB::SRL_I32] = "__lshrsi3";
30 Names[RTLIB::SRL_I64] = "__lshrdi3";
31 Names[RTLIB::SRA_I32] = "__ashrsi3";
32 Names[RTLIB::SRA_I64] = "__ashrdi3";
33 Names[RTLIB::MUL_I32] = "__mulsi3";
34 Names[RTLIB::MUL_I64] = "__muldi3";
35 Names[RTLIB::SDIV_I32] = "__divsi3";
36 Names[RTLIB::SDIV_I64] = "__divdi3";
37 Names[RTLIB::UDIV_I32] = "__udivsi3";
38 Names[RTLIB::UDIV_I64] = "__udivdi3";
39 Names[RTLIB::SREM_I32] = "__modsi3";
40 Names[RTLIB::SREM_I64] = "__moddi3";
41 Names[RTLIB::UREM_I32] = "__umodsi3";
42 Names[RTLIB::UREM_I64] = "__umoddi3";
43 Names[RTLIB::NEG_I32] = "__negsi2";
44 Names[RTLIB::NEG_I64] = "__negdi2";
45 Names[RTLIB::ADD_F32] = "__addsf3";
46 Names[RTLIB::ADD_F64] = "__adddf3";
47 Names[RTLIB::SUB_F32] = "__subsf3";
48 Names[RTLIB::SUB_F64] = "__subdf3";
49 Names[RTLIB::MUL_F32] = "__mulsf3";
50 Names[RTLIB::MUL_F64] = "__muldf3";
51 Names[RTLIB::DIV_F32] = "__divsf3";
52 Names[RTLIB::DIV_F64] = "__divdf3";
53 Names[RTLIB::REM_F32] = "fmodf";
54 Names[RTLIB::REM_F64] = "fmod";
55 Names[RTLIB::NEG_F32] = "__negsf2";
56 Names[RTLIB::NEG_F64] = "__negdf2";
57 Names[RTLIB::POWI_F32] = "__powisf2";
58 Names[RTLIB::POWI_F64] = "__powidf2";
59 Names[RTLIB::SQRT_F32] = "sqrtf";
60 Names[RTLIB::SQRT_F64] = "sqrt";
61 Names[RTLIB::SIN_F32] = "sinf";
62 Names[RTLIB::SIN_F64] = "sin";
63 Names[RTLIB::COS_F32] = "cosf";
64 Names[RTLIB::COS_F64] = "cos";
65 Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
66 Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
67 Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
68 Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
69 Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
70 Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
71 Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
72 Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
73 Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
74 Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
75 Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
76 Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
77 Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
78 Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
79 Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
80 Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
81 Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
82 Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
83 Names[RTLIB::OEQ_F32] = "__eqsf2";
84 Names[RTLIB::OEQ_F64] = "__eqdf2";
85 Names[RTLIB::UNE_F32] = "__nesf2";
86 Names[RTLIB::UNE_F64] = "__nedf2";
87 Names[RTLIB::OGE_F32] = "__gesf2";
88 Names[RTLIB::OGE_F64] = "__gedf2";
89 Names[RTLIB::OLT_F32] = "__ltsf2";
90 Names[RTLIB::OLT_F64] = "__ltdf2";
91 Names[RTLIB::OLE_F32] = "__lesf2";
92 Names[RTLIB::OLE_F64] = "__ledf2";
93 Names[RTLIB::OGT_F32] = "__gtsf2";
94 Names[RTLIB::OGT_F64] = "__gtdf2";
95 Names[RTLIB::UO_F32] = "__unordsf2";
96 Names[RTLIB::UO_F64] = "__unorddf2";
Evan Chengd385fd62007-01-31 09:29:11 +000097 Names[RTLIB::O_F32] = "__unordsf2";
98 Names[RTLIB::O_F64] = "__unorddf2";
99}
100
101/// InitCmpLibcallCCs - Set default comparison libcall CC.
102///
103static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
104 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
105 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
106 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
107 CCs[RTLIB::UNE_F32] = ISD::SETNE;
108 CCs[RTLIB::UNE_F64] = ISD::SETNE;
109 CCs[RTLIB::OGE_F32] = ISD::SETGE;
110 CCs[RTLIB::OGE_F64] = ISD::SETGE;
111 CCs[RTLIB::OLT_F32] = ISD::SETLT;
112 CCs[RTLIB::OLT_F64] = ISD::SETLT;
113 CCs[RTLIB::OLE_F32] = ISD::SETLE;
114 CCs[RTLIB::OLE_F64] = ISD::SETLE;
115 CCs[RTLIB::OGT_F32] = ISD::SETGT;
116 CCs[RTLIB::OGT_F64] = ISD::SETGT;
117 CCs[RTLIB::UO_F32] = ISD::SETNE;
118 CCs[RTLIB::UO_F64] = ISD::SETNE;
119 CCs[RTLIB::O_F32] = ISD::SETEQ;
120 CCs[RTLIB::O_F64] = ISD::SETEQ;
Evan Cheng56966222007-01-12 02:11:51 +0000121}
122
Chris Lattner310968c2005-01-07 07:44:53 +0000123TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000124 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +0000125 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +0000126 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +0000127 // All operations default to being supported.
128 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +0000129 memset(LoadXActions, 0, sizeof(LoadXActions));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000130 memset(&StoreXActions, 0, sizeof(StoreXActions));
Evan Cheng5ff839f2006-11-09 18:56:43 +0000131 // Initialize all indexed load / store to expand.
132 for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
133 for (unsigned IM = (unsigned)ISD::PRE_INC;
134 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
135 setIndexedLoadAction(IM, (MVT::ValueType)VT, Expand);
136 setIndexedStoreAction(IM, (MVT::ValueType)VT, Expand);
137 }
138 }
Chris Lattner310968c2005-01-07 07:44:53 +0000139
Owen Andersona69571c2006-05-03 01:29:57 +0000140 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +0000141 UsesGlobalOffsetTable = false;
Owen Andersona69571c2006-05-03 01:29:57 +0000142 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +0000143 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +0000144 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Chris Lattner00ffed02006-03-01 04:52:55 +0000145 memset(TargetDAGCombineArray, 0,
146 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
Evan Chenga03a5dc2006-02-14 08:38:30 +0000147 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +0000148 allowUnalignedMemoryAccesses = false;
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000149 UseUnderscoreSetJmp = false;
150 UseUnderscoreLongJmp = false;
Chris Lattner66180392007-02-25 01:28:05 +0000151 SelectIsExpensive = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +0000152 IntDivIsCheap = false;
153 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +0000154 StackPointerRegisterToSaveRestore = 0;
Jim Laskey9bb3c932007-02-22 18:04:49 +0000155 ExceptionPointerRegister = 0;
156 ExceptionSelectorRegister = 0;
Evan Cheng0577a222006-01-25 18:52:42 +0000157 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +0000158 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +0000159 JumpBufAlignment = 0;
Evan Chengd60483e2007-05-16 23:45:53 +0000160 IfCvtBlockSizeLimit = 2;
Evan Cheng56966222007-01-12 02:11:51 +0000161
162 InitLibcallNames(LibcallRoutineNames);
Evan Chengd385fd62007-01-31 09:29:11 +0000163 InitCmpLibcallCCs(CmpLibcallCCs);
Chris Lattner310968c2005-01-07 07:44:53 +0000164}
165
Chris Lattnercba82f92005-01-16 07:28:11 +0000166TargetLowering::~TargetLowering() {}
167
Chris Lattnerbb97d812005-01-16 01:10:58 +0000168/// setValueTypeAction - Set the action for a particular value type. This
169/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000170static void SetValueTypeAction(MVT::ValueType VT,
171 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000172 TargetLowering &TLI,
173 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +0000174 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
175 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +0000176 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +0000177 MVT::ValueType PromoteTo;
178 if (VT == MVT::f32)
179 PromoteTo = MVT::f64;
180 else {
181 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +0000182 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +0000183 ++LargerReg;
184 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
185 "Nothing to promote to??");
186 }
187 PromoteTo = (MVT::ValueType)LargerReg;
188 }
189
190 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
191 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
192 "Can only promote from int->int or fp->fp!");
193 assert(VT < PromoteTo && "Must promote to a larger type!");
194 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +0000195 } else if (Action == TargetLowering::Expand) {
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000196 // f32 and f64 is each expanded to corresponding integer type of same size.
197 if (VT == MVT::f32)
198 TransformToType[VT] = MVT::i32;
199 else if (VT == MVT::f64)
200 TransformToType[VT] = MVT::i64;
201 else {
202 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
203 "Cannot expand this type: target must support SOME integer reg!");
204 // Expand to the next smaller integer type!
205 TransformToType[VT] = (MVT::ValueType)(VT-1);
206 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000207 }
208}
209
210
Chris Lattner310968c2005-01-07 07:44:53 +0000211/// computeRegisterProperties - Once all of the register classes are added,
212/// this allows us to compute derived properties we expose.
213void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +0000214 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +0000215 "Too many value types for ValueTypeActions to hold!");
216
Chris Lattner310968c2005-01-07 07:44:53 +0000217 // Everything defaults to one.
218 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
Dan Gohmanb9f10192007-06-21 14:42:22 +0000219 NumRegistersForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +0000220
Chris Lattner310968c2005-01-07 07:44:53 +0000221 // Find the largest integer register class.
222 unsigned LargestIntReg = MVT::i128;
223 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
224 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
225
226 // Every integer value type larger than this largest register takes twice as
227 // many registers to represent as the previous ValueType.
228 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
229 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
Dan Gohmanb9f10192007-06-21 14:42:22 +0000230 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000231
Chris Lattnerbb97d812005-01-16 01:10:58 +0000232 // Inspect all of the ValueType's possible, deciding how to process them.
233 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
234 // If we are expanding this type, expand it!
Dan Gohmanb9f10192007-06-21 14:42:22 +0000235 if (getNumRegisters((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000236 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000237 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000238 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000239 // Otherwise, if we don't have native support, we must promote to a
240 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000241 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
242 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000243 else
244 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000245
Dan Gohman2d74a312007-06-21 14:48:26 +0000246 // If the target does not have native f64 support, expand it to i64. We will
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000247 // be generating soft float library calls. If the target does not have native
Dan Gohman2d74a312007-06-21 14:48:26 +0000248 // support for f32, promote it to f64 if it is legal. Otherwise, expand it to
249 // i32.
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000250 if (isTypeLegal(MVT::f64))
251 TransformToType[MVT::f64] = MVT::f64;
252 else {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000253 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000254 SetValueTypeAction(MVT::f64, Expand, *this, TransformToType,
255 ValueTypeActions);
256 }
257 if (isTypeLegal(MVT::f32))
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000258 TransformToType[MVT::f32] = MVT::f32;
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000259 else if (isTypeLegal(MVT::f64))
260 SetValueTypeAction(MVT::f32, Promote, *this, TransformToType,
261 ValueTypeActions);
262 else {
Dan Gohmanb9f10192007-06-21 14:42:22 +0000263 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
Evan Cheng1a8f1fe2006-12-09 02:42:38 +0000264 SetValueTypeAction(MVT::f32, Expand, *this, TransformToType,
265 ValueTypeActions);
266 }
Nate Begeman4ef3b812005-11-22 01:29:36 +0000267
268 // Set MVT::Vector to always be Expanded
269 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
270 ValueTypeActions);
Chris Lattner3a5935842006-03-16 19:50:01 +0000271
272 // Loop over all of the legal vector value types, specifying an identity type
273 // transformation.
274 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
Evan Cheng677274b2006-03-23 23:24:51 +0000275 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Chris Lattner3a5935842006-03-16 19:50:01 +0000276 if (isTypeLegal((MVT::ValueType)i))
277 TransformToType[i] = (MVT::ValueType)i;
278 }
Chris Lattnerbb97d812005-01-16 01:10:58 +0000279}
Chris Lattnercba82f92005-01-16 07:28:11 +0000280
Evan Cheng72261582005-12-20 06:22:03 +0000281const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
282 return NULL;
283}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000284
Reid Spencer9d6565a2007-02-15 02:26:10 +0000285/// getVectorTypeBreakdown - Packed types are broken down into some number of
Evan Cheng7e399c12006-05-17 18:22:14 +0000286/// legal first class types. For example, <8 x float> maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000287/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
288///
289/// This method returns the number and type of the resultant breakdown.
290///
Reid Spencer9d6565a2007-02-15 02:26:10 +0000291unsigned TargetLowering::getVectorTypeBreakdown(const VectorType *PTy,
Chris Lattner79227e22006-03-31 00:46:36 +0000292 MVT::ValueType &PTyElementVT,
293 MVT::ValueType &PTyLegalElementVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000294 // Figure out the right, legal destination reg to copy into.
295 unsigned NumElts = PTy->getNumElements();
296 MVT::ValueType EltTy = getValueType(PTy->getElementType());
297
298 unsigned NumVectorRegs = 1;
299
300 // Divide the input until we get to a supported size. This will always
301 // end with a scalar if the target doesn't support vectors.
Dan Gohmanb55757e2007-05-18 17:52:13 +0000302 while (NumElts > 1 && !isTypeLegal(MVT::getVectorType(EltTy, NumElts))) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000303 NumElts >>= 1;
304 NumVectorRegs <<= 1;
305 }
306
Dan Gohmanb55757e2007-05-18 17:52:13 +0000307 MVT::ValueType VT = MVT::getVectorType(EltTy, NumElts);
Chris Lattnerc2941772007-04-12 04:44:28 +0000308 if (!isTypeLegal(VT))
Chris Lattnerdc879292006-03-31 00:28:56 +0000309 VT = EltTy;
Chris Lattnera6c9de42006-03-31 01:50:09 +0000310 PTyElementVT = VT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000311
312 MVT::ValueType DestVT = getTypeToTransformTo(VT);
Chris Lattner79227e22006-03-31 00:46:36 +0000313 PTyLegalElementVT = DestVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000314 if (DestVT < VT) {
315 // Value is expanded, e.g. i64 -> i16.
Chris Lattner79227e22006-03-31 00:46:36 +0000316 return NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
Chris Lattnerdc879292006-03-31 00:28:56 +0000317 } else {
318 // Otherwise, promotion or legal types use the same number of registers as
319 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000320 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000321 }
322
Evan Chenge9b3da12006-05-17 18:10:06 +0000323 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000324}
325
Chris Lattnereb8146b2006-02-04 02:13:02 +0000326//===----------------------------------------------------------------------===//
327// Optimization Methods
328//===----------------------------------------------------------------------===//
329
Nate Begeman368e18d2006-02-16 21:11:51 +0000330/// ShrinkDemandedConstant - Check to see if the specified operand of the
331/// specified instruction is a constant integer. If so, check to see if there
332/// are any bits set in the constant that are not demanded. If so, shrink the
333/// constant and return true.
334bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
335 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000336 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000337 switch(Op.getOpcode()) {
338 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000339 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000340 case ISD::OR:
341 case ISD::XOR:
342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
343 if ((~Demanded & C->getValue()) != 0) {
344 MVT::ValueType VT = Op.getValueType();
345 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
346 DAG.getConstant(Demanded & C->getValue(),
347 VT));
348 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000349 }
Nate Begemande996292006-02-03 22:24:05 +0000350 break;
351 }
352 return false;
353}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000354
Nate Begeman368e18d2006-02-16 21:11:51 +0000355/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
356/// DemandedMask bits of the result of Op are ever used downstream. If we can
357/// use this information to simplify Op, create a new simplified DAG node and
358/// return true, returning the original and new nodes in Old and New. Otherwise,
359/// analyze the expression and return a mask of KnownOne and KnownZero bits for
360/// the expression (used to simplify the caller). The KnownZero/One bits may
361/// only be accurate for those bits in the DemandedMask.
362bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
363 uint64_t &KnownZero,
364 uint64_t &KnownOne,
365 TargetLoweringOpt &TLO,
366 unsigned Depth) const {
367 KnownZero = KnownOne = 0; // Don't know anything.
Chris Lattner3fc5b012007-05-17 18:19:23 +0000368
369 // The masks are not wide enough to represent this type! Should use APInt.
370 if (Op.getValueType() == MVT::i128)
371 return false;
372
Nate Begeman368e18d2006-02-16 21:11:51 +0000373 // Other users may use these bits.
374 if (!Op.Val->hasOneUse()) {
375 if (Depth != 0) {
376 // If not at the root, Just compute the KnownZero/KnownOne bits to
377 // simplify things downstream.
Dan Gohmanea859be2007-06-22 14:59:07 +0000378 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Nate Begeman368e18d2006-02-16 21:11:51 +0000379 return false;
380 }
381 // If this is the root being simplified, allow it to have multiple uses,
382 // just set the DemandedMask to all bits.
383 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
384 } else if (DemandedMask == 0) {
385 // Not demanding any bits from Op.
386 if (Op.getOpcode() != ISD::UNDEF)
387 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
388 return false;
389 } else if (Depth == 6) { // Limit search depth.
390 return false;
391 }
392
393 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000394 switch (Op.getOpcode()) {
395 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000396 // We know all of the bits for a constant!
397 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
398 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000399 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000400 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000401 // If the RHS is a constant, check to see if the LHS would be zero without
402 // using the bits from the RHS. Below, we use knowledge about the RHS to
403 // simplify the LHS, here we're using information from the LHS to simplify
404 // the RHS.
405 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
406 uint64_t LHSZero, LHSOne;
Dan Gohmanea859be2007-06-22 14:59:07 +0000407 TLO.DAG.ComputeMaskedBits(Op.getOperand(0), DemandedMask,
408 LHSZero, LHSOne, Depth+1);
Chris Lattner81cd3552006-02-27 00:36:27 +0000409 // If the LHS already has zeros where RHSC does, this and is dead.
410 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
411 return TLO.CombineTo(Op, Op.getOperand(0));
412 // If any of the set bits in the RHS are known zero on the LHS, shrink
413 // the constant.
414 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
415 return true;
416 }
417
Nate Begeman368e18d2006-02-16 21:11:51 +0000418 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
419 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000420 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000421 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000422 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
423 KnownZero2, KnownOne2, TLO, Depth+1))
424 return true;
425 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
426
427 // If all of the demanded bits are known one on one side, return the other.
428 // These bits cannot contribute to the result of the 'and'.
429 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
430 return TLO.CombineTo(Op, Op.getOperand(0));
431 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
432 return TLO.CombineTo(Op, Op.getOperand(1));
433 // If all of the demanded bits in the inputs are known zeros, return zero.
434 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
435 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
436 // If the RHS is a constant, see if we can simplify it.
437 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
438 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000439
Nate Begeman368e18d2006-02-16 21:11:51 +0000440 // Output known-1 bits are only known if set in both the LHS & RHS.
441 KnownOne &= KnownOne2;
442 // Output known-0 are known to be clear if zero in either the LHS | RHS.
443 KnownZero |= KnownZero2;
444 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000445 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000446 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
447 KnownOne, TLO, Depth+1))
448 return true;
449 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
450 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
451 KnownZero2, KnownOne2, TLO, Depth+1))
452 return true;
453 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
454
455 // If all of the demanded bits are known zero on one side, return the other.
456 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000457 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000458 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000459 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000460 return TLO.CombineTo(Op, Op.getOperand(1));
461 // If all of the potentially set bits on one side are known to be set on
462 // the other side, just use the 'other' side.
463 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
464 (DemandedMask & (~KnownZero)))
465 return TLO.CombineTo(Op, Op.getOperand(0));
466 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
467 (DemandedMask & (~KnownZero2)))
468 return TLO.CombineTo(Op, Op.getOperand(1));
469 // If the RHS is a constant, see if we can simplify it.
470 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
471 return true;
472
473 // Output known-0 bits are only known if clear in both the LHS & RHS.
474 KnownZero &= KnownZero2;
475 // Output known-1 are known to be set if set in either the LHS | RHS.
476 KnownOne |= KnownOne2;
477 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000478 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000479 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
480 KnownOne, TLO, Depth+1))
481 return true;
482 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
483 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
484 KnownOne2, TLO, Depth+1))
485 return true;
486 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
487
488 // If all of the demanded bits are known zero on one side, return the other.
489 // These bits cannot contribute to the result of the 'xor'.
490 if ((DemandedMask & KnownZero) == DemandedMask)
491 return TLO.CombineTo(Op, Op.getOperand(0));
492 if ((DemandedMask & KnownZero2) == DemandedMask)
493 return TLO.CombineTo(Op, Op.getOperand(1));
Chris Lattner3687c1a2006-11-27 21:50:02 +0000494
495 // If all of the unknown bits are known to be zero on one side or the other
496 // (but not both) turn this into an *inclusive* or.
497 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
498 if ((DemandedMask & ~KnownZero & ~KnownZero2) == 0)
499 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
500 Op.getOperand(0),
501 Op.getOperand(1)));
Nate Begeman368e18d2006-02-16 21:11:51 +0000502
503 // Output known-0 bits are known if clear or set in both the LHS & RHS.
504 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
505 // Output known-1 are known to be set if set in only one of the LHS, RHS.
506 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
507
Nate Begeman368e18d2006-02-16 21:11:51 +0000508 // If all of the demanded bits on one side are known, and all of the set
509 // bits on that side are also known to be set on the other side, turn this
510 // into an AND, as we know the bits will be cleared.
511 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
512 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
513 if ((KnownOne & KnownOne2) == KnownOne) {
514 MVT::ValueType VT = Op.getValueType();
515 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
516 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
517 ANDC));
518 }
519 }
520
521 // If the RHS is a constant, see if we can simplify it.
522 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
523 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
524 return true;
525
526 KnownZero = KnownZeroOut;
527 KnownOne = KnownOneOut;
528 break;
529 case ISD::SETCC:
530 // If we know the result of a setcc has the top bits zero, use this info.
531 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
532 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
533 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000534 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000535 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
536 KnownOne, TLO, Depth+1))
537 return true;
538 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
539 KnownOne2, TLO, Depth+1))
540 return true;
541 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
542 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
543
544 // If the operands are constants, see if we can simplify them.
545 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
546 return true;
547
548 // Only known if known in both the LHS and RHS.
549 KnownOne &= KnownOne2;
550 KnownZero &= KnownZero2;
551 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000552 case ISD::SELECT_CC:
553 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
554 KnownOne, TLO, Depth+1))
555 return true;
556 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
557 KnownOne2, TLO, Depth+1))
558 return true;
559 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
560 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
561
562 // If the operands are constants, see if we can simplify them.
563 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
564 return true;
565
566 // Only known if known in both the LHS and RHS.
567 KnownOne &= KnownOne2;
568 KnownZero &= KnownZero2;
569 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000570 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000571 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner895c4ab2007-04-17 21:14:16 +0000572 unsigned ShAmt = SA->getValue();
573 SDOperand InOp = Op.getOperand(0);
574
575 // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
576 // single shift. We can do this if the bottom bits (which are shifted
577 // out) are never demanded.
578 if (InOp.getOpcode() == ISD::SRL &&
579 isa<ConstantSDNode>(InOp.getOperand(1))) {
580 if (ShAmt && (DemandedMask & ((1ULL << ShAmt)-1)) == 0) {
581 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
582 unsigned Opc = ISD::SHL;
583 int Diff = ShAmt-C1;
584 if (Diff < 0) {
585 Diff = -Diff;
586 Opc = ISD::SRL;
587 }
588
589 SDOperand NewSA =
Chris Lattner4e7e6cd2007-05-30 16:30:06 +0000590 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000591 MVT::ValueType VT = Op.getValueType();
Chris Lattner0a16a1f2007-04-18 03:01:40 +0000592 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
Chris Lattner895c4ab2007-04-17 21:14:16 +0000593 InOp.getOperand(0), NewSA));
594 }
595 }
596
597 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> ShAmt,
Nate Begeman368e18d2006-02-16 21:11:51 +0000598 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000599 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000600 KnownZero <<= SA->getValue();
601 KnownOne <<= SA->getValue();
602 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000603 }
604 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000605 case ISD::SRL:
606 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
607 MVT::ValueType VT = Op.getValueType();
608 unsigned ShAmt = SA->getValue();
Chris Lattner895c4ab2007-04-17 21:14:16 +0000609 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
610 unsigned VTSize = MVT::getSizeInBits(VT);
611 SDOperand InOp = Op.getOperand(0);
612
613 // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
614 // single shift. We can do this if the top bits (which are shifted out)
615 // are never demanded.
616 if (InOp.getOpcode() == ISD::SHL &&
617 isa<ConstantSDNode>(InOp.getOperand(1))) {
618 if (ShAmt && (DemandedMask & (~0ULL << (VTSize-ShAmt))) == 0) {
619 unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue();
620 unsigned Opc = ISD::SRL;
621 int Diff = ShAmt-C1;
622 if (Diff < 0) {
623 Diff = -Diff;
624 Opc = ISD::SHL;
625 }
626
627 SDOperand NewSA =
Chris Lattner8c7d2d52007-04-17 22:53:02 +0000628 TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
Chris Lattner895c4ab2007-04-17 21:14:16 +0000629 return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT,
630 InOp.getOperand(0), NewSA));
631 }
632 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000633
634 // Compute the new bits that are at the top now.
Chris Lattner895c4ab2007-04-17 21:14:16 +0000635 if (SimplifyDemandedBits(InOp, (DemandedMask << ShAmt) & TypeMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000636 KnownZero, KnownOne, TLO, Depth+1))
637 return true;
638 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
639 KnownZero &= TypeMask;
640 KnownOne &= TypeMask;
641 KnownZero >>= ShAmt;
642 KnownOne >>= ShAmt;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000643
644 uint64_t HighBits = (1ULL << ShAmt)-1;
Chris Lattner895c4ab2007-04-17 21:14:16 +0000645 HighBits <<= VTSize - ShAmt;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000646 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000647 }
648 break;
649 case ISD::SRA:
650 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
651 MVT::ValueType VT = Op.getValueType();
652 unsigned ShAmt = SA->getValue();
653
654 // Compute the new bits that are at the top now.
Nate Begeman368e18d2006-02-16 21:11:51 +0000655 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
656
Chris Lattner1b737132006-05-08 17:22:53 +0000657 uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask;
658
659 // If any of the demanded bits are produced by the sign extension, we also
660 // demand the input sign bit.
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000661 uint64_t HighBits = (1ULL << ShAmt)-1;
662 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
Chris Lattner1b737132006-05-08 17:22:53 +0000663 if (HighBits & DemandedMask)
664 InDemandedMask |= MVT::getIntVTSignBit(VT);
665
666 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000667 KnownZero, KnownOne, TLO, Depth+1))
668 return true;
669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
670 KnownZero &= TypeMask;
671 KnownOne &= TypeMask;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000672 KnownZero >>= ShAmt;
673 KnownOne >>= ShAmt;
Nate Begeman368e18d2006-02-16 21:11:51 +0000674
675 // Handle the sign bits.
676 uint64_t SignBit = MVT::getIntVTSignBit(VT);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000677 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
Nate Begeman368e18d2006-02-16 21:11:51 +0000678
679 // If the input sign bit is known to be zero, or if none of the top bits
680 // are demanded, turn this into an unsigned shift right.
681 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
682 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
683 Op.getOperand(1)));
684 } else if (KnownOne & SignBit) { // New bits are known one.
685 KnownOne |= HighBits;
686 }
687 }
688 break;
689 case ISD::SIGN_EXTEND_INREG: {
Nate Begeman368e18d2006-02-16 21:11:51 +0000690 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
691
Chris Lattnerec665152006-02-26 23:36:02 +0000692 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000693 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000694 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000695
Chris Lattnerec665152006-02-26 23:36:02 +0000696 // If none of the extended bits are demanded, eliminate the sextinreg.
697 if (NewBits == 0)
698 return TLO.CombineTo(Op, Op.getOperand(0));
699
Nate Begeman368e18d2006-02-16 21:11:51 +0000700 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
701 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
702
Chris Lattnerec665152006-02-26 23:36:02 +0000703 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000704 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000705 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000706
707 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
708 KnownZero, KnownOne, TLO, Depth+1))
709 return true;
710 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
711
712 // If the sign bit of the input is known set or clear, then we know the
713 // top bits of the result.
714
Chris Lattnerec665152006-02-26 23:36:02 +0000715 // If the input sign bit is known zero, convert this into a zero extension.
716 if (KnownZero & InSignBit)
717 return TLO.CombineTo(Op,
718 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
719
720 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000721 KnownOne |= NewBits;
722 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000723 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000724 KnownZero &= ~NewBits;
725 KnownOne &= ~NewBits;
726 }
727 break;
728 }
Chris Lattnerec665152006-02-26 23:36:02 +0000729 case ISD::CTTZ:
730 case ISD::CTLZ:
731 case ISD::CTPOP: {
732 MVT::ValueType VT = Op.getValueType();
733 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
734 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
735 KnownOne = 0;
736 break;
737 }
Evan Cheng466685d2006-10-09 20:57:25 +0000738 case ISD::LOAD: {
Evan Chengc5484282006-10-04 00:56:09 +0000739 if (ISD::isZEXTLoad(Op.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +0000740 LoadSDNode *LD = cast<LoadSDNode>(Op);
Evan Cheng2e49f092006-10-11 07:10:22 +0000741 MVT::ValueType VT = LD->getLoadedVT();
Evan Chengc5484282006-10-04 00:56:09 +0000742 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
743 }
Chris Lattnerec665152006-02-26 23:36:02 +0000744 break;
745 }
746 case ISD::ZERO_EXTEND: {
747 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
748
749 // If none of the top bits are demanded, convert this into an any_extend.
750 uint64_t NewBits = (~InMask) & DemandedMask;
751 if (NewBits == 0)
752 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
753 Op.getValueType(),
754 Op.getOperand(0)));
755
756 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
757 KnownZero, KnownOne, TLO, Depth+1))
758 return true;
759 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
760 KnownZero |= NewBits;
761 break;
762 }
763 case ISD::SIGN_EXTEND: {
764 MVT::ValueType InVT = Op.getOperand(0).getValueType();
765 uint64_t InMask = MVT::getIntVTBitMask(InVT);
766 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
767 uint64_t NewBits = (~InMask) & DemandedMask;
768
769 // If none of the top bits are demanded, convert this into an any_extend.
770 if (NewBits == 0)
Chris Lattnerfea997a2007-02-01 04:55:59 +0000771 return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
Chris Lattnerec665152006-02-26 23:36:02 +0000772 Op.getOperand(0)));
773
774 // Since some of the sign extended bits are demanded, we know that the sign
775 // bit is demanded.
776 uint64_t InDemandedBits = DemandedMask & InMask;
777 InDemandedBits |= InSignBit;
778
779 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
780 KnownOne, TLO, Depth+1))
781 return true;
782
783 // If the sign bit is known zero, convert this to a zero extend.
784 if (KnownZero & InSignBit)
785 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
786 Op.getValueType(),
787 Op.getOperand(0)));
788
789 // If the sign bit is known one, the top bits match.
790 if (KnownOne & InSignBit) {
791 KnownOne |= NewBits;
792 KnownZero &= ~NewBits;
793 } else { // Otherwise, top bits aren't known.
794 KnownOne &= ~NewBits;
795 KnownZero &= ~NewBits;
796 }
797 break;
798 }
799 case ISD::ANY_EXTEND: {
800 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
801 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
802 KnownZero, KnownOne, TLO, Depth+1))
803 return true;
804 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
805 break;
806 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000807 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000808 // Simplify the input, using demanded bit information, and compute the known
809 // zero/one bits live out.
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000810 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask,
811 KnownZero, KnownOne, TLO, Depth+1))
812 return true;
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000813
814 // If the input is only used by this truncate, see if we can shrink it based
815 // on the known demanded bits.
816 if (Op.getOperand(0).Val->hasOneUse()) {
817 SDOperand In = Op.getOperand(0);
818 switch (In.getOpcode()) {
819 default: break;
820 case ISD::SRL:
821 // Shrink SRL by a constant if none of the high bits shifted in are
822 // demanded.
823 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
824 uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType());
825 HighBits &= ~MVT::getIntVTBitMask(Op.getValueType());
826 HighBits >>= ShAmt->getValue();
827
828 if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) &&
829 (DemandedMask & HighBits) == 0) {
830 // None of the shifted in bits are needed. Add a truncate of the
831 // shift input, then shift it.
832 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
833 Op.getValueType(),
834 In.getOperand(0));
835 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
836 NewTrunc, In.getOperand(1)));
837 }
838 }
839 break;
840 }
841 }
842
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000843 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
844 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
845 KnownZero &= OutMask;
846 KnownOne &= OutMask;
847 break;
848 }
Chris Lattnerec665152006-02-26 23:36:02 +0000849 case ISD::AssertZext: {
850 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
851 uint64_t InMask = MVT::getIntVTBitMask(VT);
852 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
853 KnownZero, KnownOne, TLO, Depth+1))
854 return true;
855 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
856 KnownZero |= ~InMask & DemandedMask;
857 break;
858 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000859 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000860 case ISD::SUB:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000861 case ISD::INTRINSIC_WO_CHAIN:
862 case ISD::INTRINSIC_W_CHAIN:
863 case ISD::INTRINSIC_VOID:
864 // Just use ComputeMaskedBits to compute output bits.
Dan Gohmanea859be2007-06-22 14:59:07 +0000865 TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000866 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000867 }
Chris Lattnerec665152006-02-26 23:36:02 +0000868
869 // If we know the value of all of the demanded bits, return this as a
870 // constant.
871 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
872 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
873
Nate Begeman368e18d2006-02-16 21:11:51 +0000874 return false;
875}
876
Nate Begeman368e18d2006-02-16 21:11:51 +0000877/// computeMaskedBitsForTargetNode - Determine which of the bits specified
878/// in Mask are known to be either zero or one and return them in the
879/// KnownZero/KnownOne bitsets.
880void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
881 uint64_t Mask,
882 uint64_t &KnownZero,
883 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +0000884 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +0000885 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +0000886 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
887 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
888 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
889 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000890 "Should use MaskedValueIsZero if you don't know whether Op"
891 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +0000892 KnownZero = 0;
893 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000894}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000895
Chris Lattner5c3e21d2006-05-06 09:27:13 +0000896/// ComputeNumSignBitsForTargetNode - This method can be implemented by
897/// targets that want to expose additional information about sign bits to the
898/// DAG Combiner.
899unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
900 unsigned Depth) const {
901 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
902 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
903 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
904 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
905 "Should use ComputeNumSignBits if you don't know whether Op"
906 " is a target node!");
907 return 1;
908}
909
910
Evan Chengfa1eb272007-02-08 22:13:59 +0000911/// SimplifySetCC - Try to simplify a setcc built with the specified operands
912/// and cc. If it is unable to simplify it, return a null SDOperand.
913SDOperand
914TargetLowering::SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
915 ISD::CondCode Cond, bool foldBooleans,
916 DAGCombinerInfo &DCI) const {
917 SelectionDAG &DAG = DCI.DAG;
918
919 // These setcc operations always fold.
920 switch (Cond) {
921 default: break;
922 case ISD::SETFALSE:
923 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
924 case ISD::SETTRUE:
925 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
926 }
927
928 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
929 uint64_t C1 = N1C->getValue();
930 if (isa<ConstantSDNode>(N0.Val)) {
931 return DAG.FoldSetCC(VT, N0, N1, Cond);
932 } else {
933 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
934 // equality comparison, then we're just comparing whether X itself is
935 // zero.
936 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
937 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
938 N0.getOperand(1).getOpcode() == ISD::Constant) {
939 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
940 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
941 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
942 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
943 // (srl (ctlz x), 5) == 0 -> X != 0
944 // (srl (ctlz x), 5) != 1 -> X != 0
945 Cond = ISD::SETNE;
946 } else {
947 // (srl (ctlz x), 5) != 0 -> X == 0
948 // (srl (ctlz x), 5) == 1 -> X == 0
949 Cond = ISD::SETEQ;
950 }
951 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
952 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
953 Zero, Cond);
954 }
955 }
956
957 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
958 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
959 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
960
961 // If the comparison constant has bits in the upper part, the
962 // zero-extended value could never match.
963 if (C1 & (~0ULL << InSize)) {
964 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
965 switch (Cond) {
966 case ISD::SETUGT:
967 case ISD::SETUGE:
968 case ISD::SETEQ: return DAG.getConstant(0, VT);
969 case ISD::SETULT:
970 case ISD::SETULE:
971 case ISD::SETNE: return DAG.getConstant(1, VT);
972 case ISD::SETGT:
973 case ISD::SETGE:
974 // True if the sign bit of C1 is set.
Chris Lattner01ca65b2007-02-24 02:09:29 +0000975 return DAG.getConstant((C1 & (1ULL << (VSize-1))) != 0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +0000976 case ISD::SETLT:
977 case ISD::SETLE:
978 // True if the sign bit of C1 isn't set.
Chris Lattner01ca65b2007-02-24 02:09:29 +0000979 return DAG.getConstant((C1 & (1ULL << (VSize-1))) == 0, VT);
Evan Chengfa1eb272007-02-08 22:13:59 +0000980 default:
981 break;
982 }
983 }
984
985 // Otherwise, we can perform the comparison with the low bits.
986 switch (Cond) {
987 case ISD::SETEQ:
988 case ISD::SETNE:
989 case ISD::SETUGT:
990 case ISD::SETUGE:
991 case ISD::SETULT:
992 case ISD::SETULE:
993 return DAG.getSetCC(VT, N0.getOperand(0),
994 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
995 Cond);
996 default:
997 break; // todo, be more careful with signed comparisons
998 }
999 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1000 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1001 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1002 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1003 MVT::ValueType ExtDstTy = N0.getValueType();
1004 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1005
1006 // If the extended part has any inconsistent bits, it cannot ever
1007 // compare equal. In other words, they have to be all ones or all
1008 // zeros.
1009 uint64_t ExtBits =
1010 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1011 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1012 return DAG.getConstant(Cond == ISD::SETNE, VT);
1013
1014 SDOperand ZextOp;
1015 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1016 if (Op0Ty == ExtSrcTy) {
1017 ZextOp = N0.getOperand(0);
1018 } else {
1019 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1020 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1021 DAG.getConstant(Imm, Op0Ty));
1022 }
1023 if (!DCI.isCalledByLegalizer())
1024 DCI.AddToWorklist(ZextOp.Val);
1025 // Otherwise, make this a use of a zext.
1026 return DAG.getSetCC(VT, ZextOp,
1027 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1028 ExtDstTy),
1029 Cond);
1030 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
1031 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1032
1033 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
1034 if (N0.getOpcode() == ISD::SETCC) {
1035 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
1036 if (TrueWhenTrue)
1037 return N0;
1038
1039 // Invert the condition.
1040 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
1041 CC = ISD::getSetCCInverse(CC,
1042 MVT::isInteger(N0.getOperand(0).getValueType()));
1043 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
1044 }
1045
1046 if ((N0.getOpcode() == ISD::XOR ||
1047 (N0.getOpcode() == ISD::AND &&
1048 N0.getOperand(0).getOpcode() == ISD::XOR &&
1049 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
1050 isa<ConstantSDNode>(N0.getOperand(1)) &&
1051 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
1052 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
1053 // can only do this if the top bits are known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00001054 if (DAG.MaskedValueIsZero(N0,
1055 MVT::getIntVTBitMask(N0.getValueType())-1)){
Evan Chengfa1eb272007-02-08 22:13:59 +00001056 // Okay, get the un-inverted input value.
1057 SDOperand Val;
1058 if (N0.getOpcode() == ISD::XOR)
1059 Val = N0.getOperand(0);
1060 else {
1061 assert(N0.getOpcode() == ISD::AND &&
1062 N0.getOperand(0).getOpcode() == ISD::XOR);
1063 // ((X^1)&1)^1 -> X & 1
1064 Val = DAG.getNode(ISD::AND, N0.getValueType(),
1065 N0.getOperand(0).getOperand(0),
1066 N0.getOperand(1));
1067 }
1068 return DAG.getSetCC(VT, Val, N1,
1069 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
1070 }
1071 }
1072 }
1073
1074 uint64_t MinVal, MaxVal;
1075 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1076 if (ISD::isSignedIntSetCC(Cond)) {
1077 MinVal = 1ULL << (OperandBitSize-1);
1078 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1079 MaxVal = ~0ULL >> (65-OperandBitSize);
1080 else
1081 MaxVal = 0;
1082 } else {
1083 MinVal = 0;
1084 MaxVal = ~0ULL >> (64-OperandBitSize);
1085 }
1086
1087 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1088 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1089 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1090 --C1; // X >= C0 --> X > (C0-1)
1091 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1092 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1093 }
1094
1095 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1096 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1097 ++C1; // X <= C0 --> X < (C0+1)
1098 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1099 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1100 }
1101
1102 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1103 return DAG.getConstant(0, VT); // X < MIN --> false
1104 if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
1105 return DAG.getConstant(1, VT); // X >= MIN --> true
1106 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
1107 return DAG.getConstant(0, VT); // X > MAX --> false
1108 if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
1109 return DAG.getConstant(1, VT); // X <= MAX --> true
1110
1111 // Canonicalize setgt X, Min --> setne X, Min
1112 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1113 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1114 // Canonicalize setlt X, Max --> setne X, Max
1115 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
1116 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1117
1118 // If we have setult X, 1, turn it into seteq X, 0
1119 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1120 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1121 ISD::SETEQ);
1122 // If we have setugt X, Max-1, turn it into seteq X, Max
1123 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1124 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1125 ISD::SETEQ);
1126
1127 // If we have "setcc X, C0", check to see if we can shrink the immediate
1128 // by changing cc.
1129
1130 // SETUGT X, SINTMAX -> SETLT X, 0
1131 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1132 C1 == (~0ULL >> (65-OperandBitSize)))
1133 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1134 ISD::SETLT);
1135
1136 // FIXME: Implement the rest of these.
1137
1138 // Fold bit comparisons when we can.
1139 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1140 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1141 if (ConstantSDNode *AndRHS =
1142 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1143 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1144 // Perform the xform if the AND RHS is a single bit.
1145 if (isPowerOf2_64(AndRHS->getValue())) {
1146 return DAG.getNode(ISD::SRL, VT, N0,
1147 DAG.getConstant(Log2_64(AndRHS->getValue()),
1148 getShiftAmountTy()));
1149 }
1150 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1151 // (X & 8) == 8 --> (X & 8) >> 3
1152 // Perform the xform if C1 is a single bit.
1153 if (isPowerOf2_64(C1)) {
1154 return DAG.getNode(ISD::SRL, VT, N0,
1155 DAG.getConstant(Log2_64(C1), getShiftAmountTy()));
1156 }
1157 }
1158 }
1159 }
1160 } else if (isa<ConstantSDNode>(N0.Val)) {
1161 // Ensure that the constant occurs on the RHS.
1162 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1163 }
1164
1165 if (isa<ConstantFPSDNode>(N0.Val)) {
1166 // Constant fold or commute setcc.
1167 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
1168 if (O.Val) return O;
1169 }
1170
1171 if (N0 == N1) {
1172 // We can always fold X == X for integer setcc's.
1173 if (MVT::isInteger(N0.getValueType()))
1174 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1175 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1176 if (UOF == 2) // FP operators that are undefined on NaNs.
1177 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1178 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1179 return DAG.getConstant(UOF, VT);
1180 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1181 // if it is not already.
1182 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
1183 if (NewCond != Cond)
1184 return DAG.getSetCC(VT, N0, N1, NewCond);
1185 }
1186
1187 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1188 MVT::isInteger(N0.getValueType())) {
1189 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1190 N0.getOpcode() == ISD::XOR) {
1191 // Simplify (X+Y) == (X+Z) --> Y == Z
1192 if (N0.getOpcode() == N1.getOpcode()) {
1193 if (N0.getOperand(0) == N1.getOperand(0))
1194 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1195 if (N0.getOperand(1) == N1.getOperand(1))
1196 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1197 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
1198 // If X op Y == Y op X, try other combinations.
1199 if (N0.getOperand(0) == N1.getOperand(1))
1200 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1201 if (N0.getOperand(1) == N1.getOperand(0))
1202 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
1203 }
1204 }
1205
1206 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1207 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1208 // Turn (X+C1) == C2 --> X == C2-C1
1209 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
1210 return DAG.getSetCC(VT, N0.getOperand(0),
1211 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
1212 N0.getValueType()), Cond);
1213 }
1214
1215 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
1216 if (N0.getOpcode() == ISD::XOR)
1217 // If we know that all of the inverted bits are zero, don't bother
1218 // performing the inversion.
Dan Gohmanea859be2007-06-22 14:59:07 +00001219 if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Evan Chengfa1eb272007-02-08 22:13:59 +00001220 return DAG.getSetCC(VT, N0.getOperand(0),
1221 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
1222 N0.getValueType()), Cond);
1223 }
1224
1225 // Turn (C1-X) == C2 --> X == C1-C2
1226 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
1227 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
1228 return DAG.getSetCC(VT, N0.getOperand(1),
1229 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
1230 N0.getValueType()), Cond);
1231 }
1232 }
1233 }
1234
1235 // Simplify (X+Z) == X --> Z == 0
1236 if (N0.getOperand(0) == N1)
1237 return DAG.getSetCC(VT, N0.getOperand(1),
1238 DAG.getConstant(0, N0.getValueType()), Cond);
1239 if (N0.getOperand(1) == N1) {
1240 if (DAG.isCommutativeBinOp(N0.getOpcode()))
1241 return DAG.getSetCC(VT, N0.getOperand(0),
1242 DAG.getConstant(0, N0.getValueType()), Cond);
Chris Lattner2ad913b2007-05-19 00:43:44 +00001243 else if (N0.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001244 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1245 // (Z-X) == X --> Z == X<<1
1246 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1247 N1,
1248 DAG.getConstant(1, getShiftAmountTy()));
1249 if (!DCI.isCalledByLegalizer())
1250 DCI.AddToWorklist(SH.Val);
1251 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1252 }
1253 }
1254 }
1255
1256 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1257 N1.getOpcode() == ISD::XOR) {
1258 // Simplify X == (X+Z) --> Z == 0
1259 if (N1.getOperand(0) == N0) {
1260 return DAG.getSetCC(VT, N1.getOperand(1),
1261 DAG.getConstant(0, N1.getValueType()), Cond);
1262 } else if (N1.getOperand(1) == N0) {
1263 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
1264 return DAG.getSetCC(VT, N1.getOperand(0),
1265 DAG.getConstant(0, N1.getValueType()), Cond);
Chris Lattner7667c0b2007-05-19 00:46:51 +00001266 } else if (N1.Val->hasOneUse()) {
Evan Chengfa1eb272007-02-08 22:13:59 +00001267 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1268 // X == (Z-X) --> X<<1 == Z
1269 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1270 DAG.getConstant(1, getShiftAmountTy()));
1271 if (!DCI.isCalledByLegalizer())
1272 DCI.AddToWorklist(SH.Val);
1273 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1274 }
1275 }
1276 }
1277 }
1278
1279 // Fold away ALL boolean setcc's.
1280 SDOperand Temp;
1281 if (N0.getValueType() == MVT::i1 && foldBooleans) {
1282 switch (Cond) {
1283 default: assert(0 && "Unknown integer setcc!");
1284 case ISD::SETEQ: // X == Y -> (X^Y)^1
1285 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1286 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1287 if (!DCI.isCalledByLegalizer())
1288 DCI.AddToWorklist(Temp.Val);
1289 break;
1290 case ISD::SETNE: // X != Y --> (X^Y)
1291 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1292 break;
1293 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1294 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1295 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1296 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1297 if (!DCI.isCalledByLegalizer())
1298 DCI.AddToWorklist(Temp.Val);
1299 break;
1300 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1301 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1302 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1303 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1304 if (!DCI.isCalledByLegalizer())
1305 DCI.AddToWorklist(Temp.Val);
1306 break;
1307 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1308 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1309 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1310 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1311 if (!DCI.isCalledByLegalizer())
1312 DCI.AddToWorklist(Temp.Val);
1313 break;
1314 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1315 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1316 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1317 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1318 break;
1319 }
1320 if (VT != MVT::i1) {
1321 if (!DCI.isCalledByLegalizer())
1322 DCI.AddToWorklist(N0.Val);
1323 // FIXME: If running after legalize, we probably can't do this.
1324 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1325 }
1326 return N0;
1327 }
1328
1329 // Could not fold it.
1330 return SDOperand();
1331}
1332
Chris Lattner00ffed02006-03-01 04:52:55 +00001333SDOperand TargetLowering::
1334PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1335 // Default implementation: no optimization.
1336 return SDOperand();
1337}
1338
Chris Lattnereb8146b2006-02-04 02:13:02 +00001339//===----------------------------------------------------------------------===//
1340// Inline Assembler Implementation Methods
1341//===----------------------------------------------------------------------===//
1342
1343TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001344TargetLowering::getConstraintType(const std::string &Constraint) const {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001345 // FIXME: lots more standard ones to handle.
Chris Lattner4234f572007-03-25 02:14:49 +00001346 if (Constraint.size() == 1) {
1347 switch (Constraint[0]) {
1348 default: break;
1349 case 'r': return C_RegisterClass;
1350 case 'm': // memory
1351 case 'o': // offsetable
1352 case 'V': // not offsetable
1353 return C_Memory;
1354 case 'i': // Simple Integer or Relocatable Constant
1355 case 'n': // Simple Integer
1356 case 's': // Relocatable Constant
Chris Lattnerc13dd1c2007-03-25 04:35:41 +00001357 case 'X': // Allow ANY value.
Chris Lattner4234f572007-03-25 02:14:49 +00001358 case 'I': // Target registers.
1359 case 'J':
1360 case 'K':
1361 case 'L':
1362 case 'M':
1363 case 'N':
1364 case 'O':
1365 case 'P':
1366 return C_Other;
1367 }
Chris Lattnereb8146b2006-02-04 02:13:02 +00001368 }
Chris Lattner065421f2007-03-25 02:18:14 +00001369
1370 if (Constraint.size() > 1 && Constraint[0] == '{' &&
1371 Constraint[Constraint.size()-1] == '}')
1372 return C_Register;
Chris Lattner4234f572007-03-25 02:14:49 +00001373 return C_Unknown;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001374}
1375
Chris Lattnerdba1aee2006-10-31 19:40:43 +00001376/// isOperandValidForConstraint - Return the specified operand (possibly
1377/// modified) if the specified SDOperand is valid for the specified target
1378/// constraint letter, otherwise return null.
1379SDOperand TargetLowering::isOperandValidForConstraint(SDOperand Op,
1380 char ConstraintLetter,
1381 SelectionDAG &DAG) {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001382 switch (ConstraintLetter) {
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001383 default: break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001384 case 'i': // Simple Integer or Relocatable Constant
1385 case 'n': // Simple Integer
1386 case 's': // Relocatable Constant
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001387 case 'X': { // Allows any operand.
1388 // These operands are interested in values of the form (GV+C), where C may
1389 // be folded in as an offset of GV, or it may be explicitly added. Also, it
1390 // is possible and fine if either GV or C are missing.
1391 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1392 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1393
1394 // If we have "(add GV, C)", pull out GV/C
1395 if (Op.getOpcode() == ISD::ADD) {
1396 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1397 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
1398 if (C == 0 || GA == 0) {
1399 C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1400 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
1401 }
1402 if (C == 0 || GA == 0)
1403 C = 0, GA = 0;
1404 }
1405
1406 // If we find a valid operand, map to the TargetXXX version so that the
1407 // value itself doesn't get selected.
1408 if (GA) { // Either &GV or &GV+C
1409 if (ConstraintLetter != 'n') {
1410 int64_t Offs = GA->getOffset();
1411 if (C) Offs += C->getValue();
1412 return DAG.getTargetGlobalAddress(GA->getGlobal(), Op.getValueType(),
1413 Offs);
1414 }
1415 }
1416 if (C) { // just C, no GV.
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001417 // Simple constants are not allowed for 's'.
1418 if (ConstraintLetter != 's')
1419 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
1420 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001421 break;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001422 }
Chris Lattner75c7d2b2007-05-03 16:54:34 +00001423 }
Chris Lattner9ff6ee82007-02-17 06:00:35 +00001424 return SDOperand(0,0);
Chris Lattnereb8146b2006-02-04 02:13:02 +00001425}
1426
Chris Lattner4ccb0702006-01-26 20:37:03 +00001427std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001428getRegClassForInlineAsmConstraint(const std::string &Constraint,
1429 MVT::ValueType VT) const {
1430 return std::vector<unsigned>();
1431}
1432
1433
1434std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001435getRegForInlineAsmConstraint(const std::string &Constraint,
1436 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001437 if (Constraint[0] != '{')
1438 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001439 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1440
1441 // Remove the braces from around the name.
1442 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001443
1444 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +00001445 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001446 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1447 E = RI->regclass_end(); RCI != E; ++RCI) {
1448 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001449
1450 // If none of the the value types for this register class are valid, we
1451 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1452 bool isLegal = false;
1453 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1454 I != E; ++I) {
1455 if (isTypeLegal(*I)) {
1456 isLegal = true;
1457 break;
1458 }
1459 }
1460
1461 if (!isLegal) continue;
1462
Chris Lattner1efa40f2006-02-22 00:56:39 +00001463 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1464 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +00001465 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001466 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001467 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001468 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001469
Chris Lattner1efa40f2006-02-22 00:56:39 +00001470 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001471}
Evan Cheng30b37b52006-03-13 23:18:16 +00001472
1473//===----------------------------------------------------------------------===//
1474// Loop Strength Reduction hooks
1475//===----------------------------------------------------------------------===//
1476
Chris Lattner1436bb62007-03-30 23:14:50 +00001477/// isLegalAddressingMode - Return true if the addressing mode represented
1478/// by AM is legal for this target, for a load/store of the specified type.
1479bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1480 const Type *Ty) const {
1481 // The default implementation of this implements a conservative RISCy, r+r and
1482 // r+i addr mode.
1483
1484 // Allows a sign-extended 16-bit immediate field.
1485 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1486 return false;
1487
1488 // No global is ever allowed as a base.
1489 if (AM.BaseGV)
1490 return false;
1491
1492 // Only support r+r,
1493 switch (AM.Scale) {
1494 case 0: // "r+i" or just "i", depending on HasBaseReg.
1495 break;
1496 case 1:
1497 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1498 return false;
1499 // Otherwise we have r+r or r+i.
1500 break;
1501 case 2:
1502 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1503 return false;
1504 // Allow 2*r as r+r.
1505 break;
1506 }
1507
1508 return true;
1509}
1510
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001511// Magic for divide replacement
1512
1513struct ms {
1514 int64_t m; // magic number
1515 int64_t s; // shift amount
1516};
1517
1518struct mu {
1519 uint64_t m; // magic number
1520 int64_t a; // add indicator
1521 int64_t s; // shift amount
1522};
1523
1524/// magic - calculate the magic numbers required to codegen an integer sdiv as
1525/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1526/// or -1.
1527static ms magic32(int32_t d) {
1528 int32_t p;
1529 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1530 const uint32_t two31 = 0x80000000U;
1531 struct ms mag;
1532
1533 ad = abs(d);
1534 t = two31 + ((uint32_t)d >> 31);
1535 anc = t - 1 - t%ad; // absolute value of nc
1536 p = 31; // initialize p
1537 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1538 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1539 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1540 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1541 do {
1542 p = p + 1;
1543 q1 = 2*q1; // update q1 = 2p/abs(nc)
1544 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1545 if (r1 >= anc) { // must be unsigned comparison
1546 q1 = q1 + 1;
1547 r1 = r1 - anc;
1548 }
1549 q2 = 2*q2; // update q2 = 2p/abs(d)
1550 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1551 if (r2 >= ad) { // must be unsigned comparison
1552 q2 = q2 + 1;
1553 r2 = r2 - ad;
1554 }
1555 delta = ad - r2;
1556 } while (q1 < delta || (q1 == delta && r1 == 0));
1557
1558 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1559 if (d < 0) mag.m = -mag.m; // resulting magic number
1560 mag.s = p - 32; // resulting shift
1561 return mag;
1562}
1563
1564/// magicu - calculate the magic numbers required to codegen an integer udiv as
1565/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1566static mu magicu32(uint32_t d) {
1567 int32_t p;
1568 uint32_t nc, delta, q1, r1, q2, r2;
1569 struct mu magu;
1570 magu.a = 0; // initialize "add" indicator
1571 nc = - 1 - (-d)%d;
1572 p = 31; // initialize p
1573 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1574 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1575 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1576 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1577 do {
1578 p = p + 1;
1579 if (r1 >= nc - r1 ) {
1580 q1 = 2*q1 + 1; // update q1
1581 r1 = 2*r1 - nc; // update r1
1582 }
1583 else {
1584 q1 = 2*q1; // update q1
1585 r1 = 2*r1; // update r1
1586 }
1587 if (r2 + 1 >= d - r2) {
1588 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1589 q2 = 2*q2 + 1; // update q2
1590 r2 = 2*r2 + 1 - d; // update r2
1591 }
1592 else {
1593 if (q2 >= 0x80000000) magu.a = 1;
1594 q2 = 2*q2; // update q2
1595 r2 = 2*r2 + 1; // update r2
1596 }
1597 delta = d - 1 - r2;
1598 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1599 magu.m = q2 + 1; // resulting magic number
1600 magu.s = p - 32; // resulting shift
1601 return magu;
1602}
1603
1604/// magic - calculate the magic numbers required to codegen an integer sdiv as
1605/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1606/// or -1.
1607static ms magic64(int64_t d) {
1608 int64_t p;
1609 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1610 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1611 struct ms mag;
1612
1613 ad = d >= 0 ? d : -d;
1614 t = two63 + ((uint64_t)d >> 63);
1615 anc = t - 1 - t%ad; // absolute value of nc
1616 p = 63; // initialize p
1617 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1618 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1619 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1620 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1621 do {
1622 p = p + 1;
1623 q1 = 2*q1; // update q1 = 2p/abs(nc)
1624 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1625 if (r1 >= anc) { // must be unsigned comparison
1626 q1 = q1 + 1;
1627 r1 = r1 - anc;
1628 }
1629 q2 = 2*q2; // update q2 = 2p/abs(d)
1630 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1631 if (r2 >= ad) { // must be unsigned comparison
1632 q2 = q2 + 1;
1633 r2 = r2 - ad;
1634 }
1635 delta = ad - r2;
1636 } while (q1 < delta || (q1 == delta && r1 == 0));
1637
1638 mag.m = q2 + 1;
1639 if (d < 0) mag.m = -mag.m; // resulting magic number
1640 mag.s = p - 64; // resulting shift
1641 return mag;
1642}
1643
1644/// magicu - calculate the magic numbers required to codegen an integer udiv as
1645/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1646static mu magicu64(uint64_t d)
1647{
1648 int64_t p;
1649 uint64_t nc, delta, q1, r1, q2, r2;
1650 struct mu magu;
1651 magu.a = 0; // initialize "add" indicator
1652 nc = - 1 - (-d)%d;
1653 p = 63; // initialize p
1654 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
1655 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
1656 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
1657 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
1658 do {
1659 p = p + 1;
1660 if (r1 >= nc - r1 ) {
1661 q1 = 2*q1 + 1; // update q1
1662 r1 = 2*r1 - nc; // update r1
1663 }
1664 else {
1665 q1 = 2*q1; // update q1
1666 r1 = 2*r1; // update r1
1667 }
1668 if (r2 + 1 >= d - r2) {
1669 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
1670 q2 = 2*q2 + 1; // update q2
1671 r2 = 2*r2 + 1 - d; // update r2
1672 }
1673 else {
1674 if (q2 >= 0x8000000000000000ull) magu.a = 1;
1675 q2 = 2*q2; // update q2
1676 r2 = 2*r2 + 1; // update r2
1677 }
1678 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00001679 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001680 magu.m = q2 + 1; // resulting magic number
1681 magu.s = p - 64; // resulting shift
1682 return magu;
1683}
1684
1685/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
1686/// return a DAG expression to select that will generate the same value by
1687/// multiplying by a magic number. See:
1688/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1689SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001690 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001691 MVT::ValueType VT = N->getValueType(0);
1692
1693 // Check to see if we can do this.
1694 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1695 return SDOperand(); // BuildSDIV only operates on i32 or i64
1696 if (!isOperationLegal(ISD::MULHS, VT))
1697 return SDOperand(); // Make sure the target supports MULHS.
1698
1699 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
1700 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
1701
1702 // Multiply the numerator (operand 0) by the magic value
1703 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
1704 DAG.getConstant(magics.m, VT));
1705 // If d > 0 and m < 0, add the numerator
1706 if (d > 0 && magics.m < 0) {
1707 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
1708 if (Created)
1709 Created->push_back(Q.Val);
1710 }
1711 // If d < 0 and m > 0, subtract the numerator.
1712 if (d < 0 && magics.m > 0) {
1713 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
1714 if (Created)
1715 Created->push_back(Q.Val);
1716 }
1717 // Shift right algebraic if shift value is nonzero
1718 if (magics.s > 0) {
1719 Q = DAG.getNode(ISD::SRA, VT, Q,
1720 DAG.getConstant(magics.s, getShiftAmountTy()));
1721 if (Created)
1722 Created->push_back(Q.Val);
1723 }
1724 // Extract the sign bit and add it to the quotient
1725 SDOperand T =
1726 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
1727 getShiftAmountTy()));
1728 if (Created)
1729 Created->push_back(T.Val);
1730 return DAG.getNode(ISD::ADD, VT, Q, T);
1731}
1732
1733/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
1734/// return a DAG expression to select that will generate the same value by
1735/// multiplying by a magic number. See:
1736/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1737SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001738 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001739 MVT::ValueType VT = N->getValueType(0);
1740
1741 // Check to see if we can do this.
1742 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1743 return SDOperand(); // BuildUDIV only operates on i32 or i64
1744 if (!isOperationLegal(ISD::MULHU, VT))
1745 return SDOperand(); // Make sure the target supports MULHU.
1746
1747 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1748 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
1749
1750 // Multiply the numerator (operand 0) by the magic value
1751 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
1752 DAG.getConstant(magics.m, VT));
1753 if (Created)
1754 Created->push_back(Q.Val);
1755
1756 if (magics.a == 0) {
1757 return DAG.getNode(ISD::SRL, VT, Q,
1758 DAG.getConstant(magics.s, getShiftAmountTy()));
1759 } else {
1760 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
1761 if (Created)
1762 Created->push_back(NPQ.Val);
1763 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
1764 DAG.getConstant(1, getShiftAmountTy()));
1765 if (Created)
1766 Created->push_back(NPQ.Val);
1767 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
1768 if (Created)
1769 Created->push_back(NPQ.Val);
1770 return DAG.getNode(ISD::SRL, VT, NPQ,
1771 DAG.getConstant(magics.s-1, getShiftAmountTy()));
1772 }
1773}