blob: 129bcd9525916be65c4b46851ceebc6fbf2e271d [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
24TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000025 : TM(tm), TD(TM.getTargetData()) {
Evan Cheng33143dc2006-03-03 06:58:59 +000026 assert(ISD::BUILTIN_OP_END <= 156 &&
Chris Lattner310968c2005-01-07 07:44:53 +000027 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000028 // All operations default to being supported.
29 memset(OpActions, 0, sizeof(OpActions));
Evan Chengc5484282006-10-04 00:56:09 +000030 memset(LoadXActions, 0, sizeof(LoadXActions));
Evan Cheng8b2794a2006-10-13 21:14:26 +000031 memset(&StoreXActions, 0, sizeof(StoreXActions));
Chris Lattner310968c2005-01-07 07:44:53 +000032
Owen Andersona69571c2006-05-03 01:29:57 +000033 IsLittleEndian = TD->isLittleEndian();
Chris Lattnercf9668f2006-10-06 22:52:08 +000034 UsesGlobalOffsetTable = false;
Owen Andersona69571c2006-05-03 01:29:57 +000035 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000036 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000037 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Chris Lattner00ffed02006-03-01 04:52:55 +000038 memset(TargetDAGCombineArray, 0,
39 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
Evan Chenga03a5dc2006-02-14 08:38:30 +000040 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
Reid Spencer0f9beca2005-08-27 19:09:02 +000041 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000042 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000043 IntDivIsCheap = false;
44 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000045 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000046 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner7acf5f32006-09-05 17:39:15 +000047 JumpBufSize = 0;
Duraid Madina0c9e0ff2006-09-04 07:44:11 +000048 JumpBufAlignment = 0;
Chris Lattner310968c2005-01-07 07:44:53 +000049}
50
Chris Lattnercba82f92005-01-16 07:28:11 +000051TargetLowering::~TargetLowering() {}
52
Chris Lattnerbb97d812005-01-16 01:10:58 +000053/// setValueTypeAction - Set the action for a particular value type. This
54/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000055static void SetValueTypeAction(MVT::ValueType VT,
56 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000057 TargetLowering &TLI,
58 MVT::ValueType *TransformToType,
Chris Lattner3e6e8cc2006-01-29 08:41:12 +000059 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
60 ValueTypeActions.setTypeAction(VT, Action);
Chris Lattnercba82f92005-01-16 07:28:11 +000061 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000062 MVT::ValueType PromoteTo;
63 if (VT == MVT::f32)
64 PromoteTo = MVT::f64;
65 else {
66 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000067 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000068 ++LargerReg;
69 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
70 "Nothing to promote to??");
71 }
72 PromoteTo = (MVT::ValueType)LargerReg;
73 }
74
75 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
76 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
77 "Can only promote from int->int or fp->fp!");
78 assert(VT < PromoteTo && "Must promote to a larger type!");
79 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000080 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000081 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000082 "Cannot expand this type: target must support SOME integer reg!");
83 // Expand to the next smaller integer type!
84 TransformToType[VT] = (MVT::ValueType)(VT-1);
85 }
86}
87
88
Chris Lattner310968c2005-01-07 07:44:53 +000089/// computeRegisterProperties - Once all of the register classes are added,
90/// this allows us to compute derived properties we expose.
91void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000092 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000093 "Too many value types for ValueTypeActions to hold!");
94
Chris Lattner310968c2005-01-07 07:44:53 +000095 // Everything defaults to one.
96 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
97 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000098
Chris Lattner310968c2005-01-07 07:44:53 +000099 // Find the largest integer register class.
100 unsigned LargestIntReg = MVT::i128;
101 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
102 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
103
104 // Every integer value type larger than this largest register takes twice as
105 // many registers to represent as the previous ValueType.
106 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
107 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
108 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +0000109
Chris Lattnerbb97d812005-01-16 01:10:58 +0000110 // Inspect all of the ValueType's possible, deciding how to process them.
111 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
112 // If we are expanding this type, expand it!
113 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000114 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000115 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000116 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000117 // Otherwise, if we don't have native support, we must promote to a
118 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000119 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
120 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000121 else
122 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000123
Chris Lattnerbb97d812005-01-16 01:10:58 +0000124 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000125 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000126 SetValueTypeAction(MVT::f32, Promote, *this,
127 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000128 else
129 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000130
131 // Set MVT::Vector to always be Expanded
132 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
133 ValueTypeActions);
Chris Lattner3a5935842006-03-16 19:50:01 +0000134
135 // Loop over all of the legal vector value types, specifying an identity type
136 // transformation.
137 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
Evan Cheng677274b2006-03-23 23:24:51 +0000138 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Chris Lattner3a5935842006-03-16 19:50:01 +0000139 if (isTypeLegal((MVT::ValueType)i))
140 TransformToType[i] = (MVT::ValueType)i;
141 }
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000142
Chris Lattner9ed62c12005-08-24 16:34:12 +0000143 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000144 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000145}
Chris Lattnercba82f92005-01-16 07:28:11 +0000146
Evan Cheng72261582005-12-20 06:22:03 +0000147const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
148 return NULL;
149}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000150
Chris Lattnerdc879292006-03-31 00:28:56 +0000151/// getPackedTypeBreakdown - Packed types are broken down into some number of
Evan Cheng7e399c12006-05-17 18:22:14 +0000152/// legal first class types. For example, <8 x float> maps to 2 MVT::v4f32
Chris Lattnerdc879292006-03-31 00:28:56 +0000153/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
154///
155/// This method returns the number and type of the resultant breakdown.
156///
Chris Lattner79227e22006-03-31 00:46:36 +0000157unsigned TargetLowering::getPackedTypeBreakdown(const PackedType *PTy,
158 MVT::ValueType &PTyElementVT,
159 MVT::ValueType &PTyLegalElementVT) const {
Chris Lattnerdc879292006-03-31 00:28:56 +0000160 // Figure out the right, legal destination reg to copy into.
161 unsigned NumElts = PTy->getNumElements();
162 MVT::ValueType EltTy = getValueType(PTy->getElementType());
163
164 unsigned NumVectorRegs = 1;
165
166 // Divide the input until we get to a supported size. This will always
167 // end with a scalar if the target doesn't support vectors.
168 while (NumElts > 1 && !isTypeLegal(getVectorType(EltTy, NumElts))) {
169 NumElts >>= 1;
170 NumVectorRegs <<= 1;
171 }
172
173 MVT::ValueType VT;
Chris Lattnera6c9de42006-03-31 01:50:09 +0000174 if (NumElts == 1) {
Chris Lattnerdc879292006-03-31 00:28:56 +0000175 VT = EltTy;
Chris Lattnera6c9de42006-03-31 01:50:09 +0000176 } else {
177 VT = getVectorType(EltTy, NumElts);
178 }
179 PTyElementVT = VT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000180
181 MVT::ValueType DestVT = getTypeToTransformTo(VT);
Chris Lattner79227e22006-03-31 00:46:36 +0000182 PTyLegalElementVT = DestVT;
Chris Lattnerdc879292006-03-31 00:28:56 +0000183 if (DestVT < VT) {
184 // Value is expanded, e.g. i64 -> i16.
Chris Lattner79227e22006-03-31 00:46:36 +0000185 return NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
Chris Lattnerdc879292006-03-31 00:28:56 +0000186 } else {
187 // Otherwise, promotion or legal types use the same number of registers as
188 // the vector decimated to the appropriate level.
Chris Lattner79227e22006-03-31 00:46:36 +0000189 return NumVectorRegs;
Chris Lattnerdc879292006-03-31 00:28:56 +0000190 }
191
Evan Chenge9b3da12006-05-17 18:10:06 +0000192 return 1;
Chris Lattnerdc879292006-03-31 00:28:56 +0000193}
194
Chris Lattnereb8146b2006-02-04 02:13:02 +0000195//===----------------------------------------------------------------------===//
196// Optimization Methods
197//===----------------------------------------------------------------------===//
198
Nate Begeman368e18d2006-02-16 21:11:51 +0000199/// ShrinkDemandedConstant - Check to see if the specified operand of the
200/// specified instruction is a constant integer. If so, check to see if there
201/// are any bits set in the constant that are not demanded. If so, shrink the
202/// constant and return true.
203bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
204 uint64_t Demanded) {
Chris Lattnerec665152006-02-26 23:36:02 +0000205 // FIXME: ISD::SELECT, ISD::SELECT_CC
Nate Begeman368e18d2006-02-16 21:11:51 +0000206 switch(Op.getOpcode()) {
207 default: break;
Nate Begemande996292006-02-03 22:24:05 +0000208 case ISD::AND:
Nate Begeman368e18d2006-02-16 21:11:51 +0000209 case ISD::OR:
210 case ISD::XOR:
211 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
212 if ((~Demanded & C->getValue()) != 0) {
213 MVT::ValueType VT = Op.getValueType();
214 SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
215 DAG.getConstant(Demanded & C->getValue(),
216 VT));
217 return CombineTo(Op, New);
Nate Begemande996292006-02-03 22:24:05 +0000218 }
Nate Begemande996292006-02-03 22:24:05 +0000219 break;
220 }
221 return false;
222}
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000223
Nate Begeman368e18d2006-02-16 21:11:51 +0000224/// SimplifyDemandedBits - Look at Op. At this point, we know that only the
225/// DemandedMask bits of the result of Op are ever used downstream. If we can
226/// use this information to simplify Op, create a new simplified DAG node and
227/// return true, returning the original and new nodes in Old and New. Otherwise,
228/// analyze the expression and return a mask of KnownOne and KnownZero bits for
229/// the expression (used to simplify the caller). The KnownZero/One bits may
230/// only be accurate for those bits in the DemandedMask.
231bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
232 uint64_t &KnownZero,
233 uint64_t &KnownOne,
234 TargetLoweringOpt &TLO,
235 unsigned Depth) const {
236 KnownZero = KnownOne = 0; // Don't know anything.
237 // Other users may use these bits.
238 if (!Op.Val->hasOneUse()) {
239 if (Depth != 0) {
240 // If not at the root, Just compute the KnownZero/KnownOne bits to
241 // simplify things downstream.
242 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
243 return false;
244 }
245 // If this is the root being simplified, allow it to have multiple uses,
246 // just set the DemandedMask to all bits.
247 DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
248 } else if (DemandedMask == 0) {
249 // Not demanding any bits from Op.
250 if (Op.getOpcode() != ISD::UNDEF)
251 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
252 return false;
253 } else if (Depth == 6) { // Limit search depth.
254 return false;
255 }
256
257 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000258 switch (Op.getOpcode()) {
259 case ISD::Constant:
Nate Begeman368e18d2006-02-16 21:11:51 +0000260 // We know all of the bits for a constant!
261 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
262 KnownZero = ~KnownOne & DemandedMask;
Chris Lattnerec665152006-02-26 23:36:02 +0000263 return false; // Don't fall through, will infinitely loop.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000264 case ISD::AND:
Chris Lattner81cd3552006-02-27 00:36:27 +0000265 // If the RHS is a constant, check to see if the LHS would be zero without
266 // using the bits from the RHS. Below, we use knowledge about the RHS to
267 // simplify the LHS, here we're using information from the LHS to simplify
268 // the RHS.
269 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
270 uint64_t LHSZero, LHSOne;
271 ComputeMaskedBits(Op.getOperand(0), DemandedMask,
272 LHSZero, LHSOne, Depth+1);
273 // If the LHS already has zeros where RHSC does, this and is dead.
274 if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
275 return TLO.CombineTo(Op, Op.getOperand(0));
276 // If any of the set bits in the RHS are known zero on the LHS, shrink
277 // the constant.
278 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
279 return true;
280 }
281
Nate Begeman368e18d2006-02-16 21:11:51 +0000282 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
283 KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000284 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Nate Begeman368e18d2006-02-16 21:11:51 +0000286 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
287 KnownZero2, KnownOne2, TLO, Depth+1))
288 return true;
289 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
290
291 // If all of the demanded bits are known one on one side, return the other.
292 // These bits cannot contribute to the result of the 'and'.
293 if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
294 return TLO.CombineTo(Op, Op.getOperand(0));
295 if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
296 return TLO.CombineTo(Op, Op.getOperand(1));
297 // If all of the demanded bits in the inputs are known zeros, return zero.
298 if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
299 return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
300 // If the RHS is a constant, see if we can simplify it.
301 if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
302 return true;
Chris Lattner5f0c6582006-02-27 00:22:28 +0000303
Nate Begeman368e18d2006-02-16 21:11:51 +0000304 // Output known-1 bits are only known if set in both the LHS & RHS.
305 KnownOne &= KnownOne2;
306 // Output known-0 are known to be clear if zero in either the LHS | RHS.
307 KnownZero |= KnownZero2;
308 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000309 case ISD::OR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000310 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
311 KnownOne, TLO, Depth+1))
312 return true;
313 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
314 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne,
315 KnownZero2, KnownOne2, TLO, Depth+1))
316 return true;
317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
318
319 // If all of the demanded bits are known zero on one side, return the other.
320 // These bits cannot contribute to the result of the 'or'.
Jeff Cohen5755b172006-02-17 02:12:18 +0000321 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
Nate Begeman368e18d2006-02-16 21:11:51 +0000322 return TLO.CombineTo(Op, Op.getOperand(0));
Jeff Cohen5755b172006-02-17 02:12:18 +0000323 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
Nate Begeman368e18d2006-02-16 21:11:51 +0000324 return TLO.CombineTo(Op, Op.getOperand(1));
325 // If all of the potentially set bits on one side are known to be set on
326 // the other side, just use the 'other' side.
327 if ((DemandedMask & (~KnownZero) & KnownOne2) ==
328 (DemandedMask & (~KnownZero)))
329 return TLO.CombineTo(Op, Op.getOperand(0));
330 if ((DemandedMask & (~KnownZero2) & KnownOne) ==
331 (DemandedMask & (~KnownZero2)))
332 return TLO.CombineTo(Op, Op.getOperand(1));
333 // If the RHS is a constant, see if we can simplify it.
334 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
335 return true;
336
337 // Output known-0 bits are only known if clear in both the LHS & RHS.
338 KnownZero &= KnownZero2;
339 // Output known-1 are known to be set if set in either the LHS | RHS.
340 KnownOne |= KnownOne2;
341 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000342 case ISD::XOR:
Nate Begeman368e18d2006-02-16 21:11:51 +0000343 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
344 KnownOne, TLO, Depth+1))
345 return true;
346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
347 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
348 KnownOne2, TLO, Depth+1))
349 return true;
350 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
351
352 // If all of the demanded bits are known zero on one side, return the other.
353 // These bits cannot contribute to the result of the 'xor'.
354 if ((DemandedMask & KnownZero) == DemandedMask)
355 return TLO.CombineTo(Op, Op.getOperand(0));
356 if ((DemandedMask & KnownZero2) == DemandedMask)
357 return TLO.CombineTo(Op, Op.getOperand(1));
358
359 // Output known-0 bits are known if clear or set in both the LHS & RHS.
360 KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
361 // Output known-1 are known to be set if set in only one of the LHS, RHS.
362 KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
363
364 // If all of the unknown bits are known to be zero on one side or the other
365 // (but not both) turn this into an *inclusive* or.
366 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
367 if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
368 if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
369 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
370 Op.getOperand(0),
371 Op.getOperand(1)));
372 // If all of the demanded bits on one side are known, and all of the set
373 // bits on that side are also known to be set on the other side, turn this
374 // into an AND, as we know the bits will be cleared.
375 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
376 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
377 if ((KnownOne & KnownOne2) == KnownOne) {
378 MVT::ValueType VT = Op.getValueType();
379 SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
380 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
381 ANDC));
382 }
383 }
384
385 // If the RHS is a constant, see if we can simplify it.
386 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
387 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
388 return true;
389
390 KnownZero = KnownZeroOut;
391 KnownOne = KnownOneOut;
392 break;
393 case ISD::SETCC:
394 // If we know the result of a setcc has the top bits zero, use this info.
395 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
396 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
397 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000398 case ISD::SELECT:
Nate Begeman368e18d2006-02-16 21:11:51 +0000399 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
400 KnownOne, TLO, Depth+1))
401 return true;
402 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
403 KnownOne2, TLO, Depth+1))
404 return true;
405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
406 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
407
408 // If the operands are constants, see if we can simplify them.
409 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
410 return true;
411
412 // Only known if known in both the LHS and RHS.
413 KnownOne &= KnownOne2;
414 KnownZero &= KnownZero2;
415 break;
Chris Lattnerec665152006-02-26 23:36:02 +0000416 case ISD::SELECT_CC:
417 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
418 KnownOne, TLO, Depth+1))
419 return true;
420 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
421 KnownOne2, TLO, Depth+1))
422 return true;
423 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
424 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
425
426 // If the operands are constants, see if we can simplify them.
427 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
428 return true;
429
430 // Only known if known in both the LHS and RHS.
431 KnownOne &= KnownOne2;
432 KnownZero &= KnownZero2;
433 break;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000434 case ISD::SHL:
Nate Begeman368e18d2006-02-16 21:11:51 +0000435 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
436 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
437 KnownZero, KnownOne, TLO, Depth+1))
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000438 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000439 KnownZero <<= SA->getValue();
440 KnownOne <<= SA->getValue();
441 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000442 }
443 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000444 case ISD::SRL:
445 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
446 MVT::ValueType VT = Op.getValueType();
447 unsigned ShAmt = SA->getValue();
448
449 // Compute the new bits that are at the top now.
Nate Begeman368e18d2006-02-16 21:11:51 +0000450 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
Nate Begeman368e18d2006-02-16 21:11:51 +0000451 if (SimplifyDemandedBits(Op.getOperand(0),
452 (DemandedMask << ShAmt) & TypeMask,
453 KnownZero, KnownOne, TLO, Depth+1))
454 return true;
455 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
456 KnownZero &= TypeMask;
457 KnownOne &= TypeMask;
458 KnownZero >>= ShAmt;
459 KnownOne >>= ShAmt;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000460
461 uint64_t HighBits = (1ULL << ShAmt)-1;
462 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
463 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000464 }
465 break;
466 case ISD::SRA:
467 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
468 MVT::ValueType VT = Op.getValueType();
469 unsigned ShAmt = SA->getValue();
470
471 // Compute the new bits that are at the top now.
Nate Begeman368e18d2006-02-16 21:11:51 +0000472 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
473
Chris Lattner1b737132006-05-08 17:22:53 +0000474 uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask;
475
476 // If any of the demanded bits are produced by the sign extension, we also
477 // demand the input sign bit.
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000478 uint64_t HighBits = (1ULL << ShAmt)-1;
479 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
Chris Lattner1b737132006-05-08 17:22:53 +0000480 if (HighBits & DemandedMask)
481 InDemandedMask |= MVT::getIntVTSignBit(VT);
482
483 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
Nate Begeman368e18d2006-02-16 21:11:51 +0000484 KnownZero, KnownOne, TLO, Depth+1))
485 return true;
486 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
487 KnownZero &= TypeMask;
488 KnownOne &= TypeMask;
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000489 KnownZero >>= ShAmt;
490 KnownOne >>= ShAmt;
Nate Begeman368e18d2006-02-16 21:11:51 +0000491
492 // Handle the sign bits.
493 uint64_t SignBit = MVT::getIntVTSignBit(VT);
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000494 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
Nate Begeman368e18d2006-02-16 21:11:51 +0000495
496 // If the input sign bit is known to be zero, or if none of the top bits
497 // are demanded, turn this into an unsigned shift right.
498 if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
499 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
500 Op.getOperand(1)));
501 } else if (KnownOne & SignBit) { // New bits are known one.
502 KnownOne |= HighBits;
503 }
504 }
505 break;
506 case ISD::SIGN_EXTEND_INREG: {
507 MVT::ValueType VT = Op.getValueType();
508 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
509
Chris Lattnerec665152006-02-26 23:36:02 +0000510 // Sign extension. Compute the demanded bits in the result that are not
Nate Begeman368e18d2006-02-16 21:11:51 +0000511 // present in the input.
Chris Lattnerec665152006-02-26 23:36:02 +0000512 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000513
Chris Lattnerec665152006-02-26 23:36:02 +0000514 // If none of the extended bits are demanded, eliminate the sextinreg.
515 if (NewBits == 0)
516 return TLO.CombineTo(Op, Op.getOperand(0));
517
Nate Begeman368e18d2006-02-16 21:11:51 +0000518 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
519 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
520
Chris Lattnerec665152006-02-26 23:36:02 +0000521 // Since the sign extended bits are demanded, we know that the sign
Nate Begeman368e18d2006-02-16 21:11:51 +0000522 // bit is demanded.
Chris Lattnerec665152006-02-26 23:36:02 +0000523 InputDemandedBits |= InSignBit;
Nate Begeman368e18d2006-02-16 21:11:51 +0000524
525 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
526 KnownZero, KnownOne, TLO, Depth+1))
527 return true;
528 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
529
530 // If the sign bit of the input is known set or clear, then we know the
531 // top bits of the result.
532
Chris Lattnerec665152006-02-26 23:36:02 +0000533 // If the input sign bit is known zero, convert this into a zero extension.
534 if (KnownZero & InSignBit)
535 return TLO.CombineTo(Op,
536 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
537
538 if (KnownOne & InSignBit) { // Input sign bit known set
Nate Begeman368e18d2006-02-16 21:11:51 +0000539 KnownOne |= NewBits;
540 KnownZero &= ~NewBits;
Chris Lattnerec665152006-02-26 23:36:02 +0000541 } else { // Input sign bit unknown
Nate Begeman368e18d2006-02-16 21:11:51 +0000542 KnownZero &= ~NewBits;
543 KnownOne &= ~NewBits;
544 }
545 break;
546 }
Chris Lattnerec665152006-02-26 23:36:02 +0000547 case ISD::CTTZ:
548 case ISD::CTLZ:
549 case ISD::CTPOP: {
550 MVT::ValueType VT = Op.getValueType();
551 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
552 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
553 KnownOne = 0;
554 break;
555 }
Evan Cheng466685d2006-10-09 20:57:25 +0000556 case ISD::LOAD: {
Evan Chengc5484282006-10-04 00:56:09 +0000557 if (ISD::isZEXTLoad(Op.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +0000558 LoadSDNode *LD = cast<LoadSDNode>(Op);
Evan Cheng2e49f092006-10-11 07:10:22 +0000559 MVT::ValueType VT = LD->getLoadedVT();
Evan Chengc5484282006-10-04 00:56:09 +0000560 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
561 }
Chris Lattnerec665152006-02-26 23:36:02 +0000562 break;
563 }
564 case ISD::ZERO_EXTEND: {
565 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
566
567 // If none of the top bits are demanded, convert this into an any_extend.
568 uint64_t NewBits = (~InMask) & DemandedMask;
569 if (NewBits == 0)
570 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
571 Op.getValueType(),
572 Op.getOperand(0)));
573
574 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
575 KnownZero, KnownOne, TLO, Depth+1))
576 return true;
577 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
578 KnownZero |= NewBits;
579 break;
580 }
581 case ISD::SIGN_EXTEND: {
582 MVT::ValueType InVT = Op.getOperand(0).getValueType();
583 uint64_t InMask = MVT::getIntVTBitMask(InVT);
584 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
585 uint64_t NewBits = (~InMask) & DemandedMask;
586
587 // If none of the top bits are demanded, convert this into an any_extend.
588 if (NewBits == 0)
589 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
590 Op.getOperand(0)));
591
592 // Since some of the sign extended bits are demanded, we know that the sign
593 // bit is demanded.
594 uint64_t InDemandedBits = DemandedMask & InMask;
595 InDemandedBits |= InSignBit;
596
597 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
598 KnownOne, TLO, Depth+1))
599 return true;
600
601 // If the sign bit is known zero, convert this to a zero extend.
602 if (KnownZero & InSignBit)
603 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
604 Op.getValueType(),
605 Op.getOperand(0)));
606
607 // If the sign bit is known one, the top bits match.
608 if (KnownOne & InSignBit) {
609 KnownOne |= NewBits;
610 KnownZero &= ~NewBits;
611 } else { // Otherwise, top bits aren't known.
612 KnownOne &= ~NewBits;
613 KnownZero &= ~NewBits;
614 }
615 break;
616 }
617 case ISD::ANY_EXTEND: {
618 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
619 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
620 KnownZero, KnownOne, TLO, Depth+1))
621 return true;
622 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
623 break;
624 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000625 case ISD::TRUNCATE: {
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000626 // Simplify the input, using demanded bit information, and compute the known
627 // zero/one bits live out.
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000628 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask,
629 KnownZero, KnownOne, TLO, Depth+1))
630 return true;
Chris Lattnerc93dfda2006-05-06 00:11:52 +0000631
632 // If the input is only used by this truncate, see if we can shrink it based
633 // on the known demanded bits.
634 if (Op.getOperand(0).Val->hasOneUse()) {
635 SDOperand In = Op.getOperand(0);
636 switch (In.getOpcode()) {
637 default: break;
638 case ISD::SRL:
639 // Shrink SRL by a constant if none of the high bits shifted in are
640 // demanded.
641 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
642 uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType());
643 HighBits &= ~MVT::getIntVTBitMask(Op.getValueType());
644 HighBits >>= ShAmt->getValue();
645
646 if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) &&
647 (DemandedMask & HighBits) == 0) {
648 // None of the shifted in bits are needed. Add a truncate of the
649 // shift input, then shift it.
650 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
651 Op.getValueType(),
652 In.getOperand(0));
653 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
654 NewTrunc, In.getOperand(1)));
655 }
656 }
657 break;
658 }
659 }
660
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000661 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
662 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
663 KnownZero &= OutMask;
664 KnownOne &= OutMask;
665 break;
666 }
Chris Lattnerec665152006-02-26 23:36:02 +0000667 case ISD::AssertZext: {
668 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
669 uint64_t InMask = MVT::getIntVTBitMask(VT);
670 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
671 KnownZero, KnownOne, TLO, Depth+1))
672 return true;
673 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
674 KnownZero |= ~InMask & DemandedMask;
675 break;
676 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000677 case ISD::ADD:
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000678 case ISD::SUB:
Chris Lattner1482b5f2006-04-02 06:15:09 +0000679 case ISD::INTRINSIC_WO_CHAIN:
680 case ISD::INTRINSIC_W_CHAIN:
681 case ISD::INTRINSIC_VOID:
682 // Just use ComputeMaskedBits to compute output bits.
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000683 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
684 break;
Nate Begeman368e18d2006-02-16 21:11:51 +0000685 }
Chris Lattnerec665152006-02-26 23:36:02 +0000686
687 // If we know the value of all of the demanded bits, return this as a
688 // constant.
689 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
690 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
691
Nate Begeman368e18d2006-02-16 21:11:51 +0000692 return false;
693}
694
695/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
696/// this predicate to simplify operations downstream. Mask is known to be zero
697/// for bits that V cannot have.
698bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
699 unsigned Depth) const {
700 uint64_t KnownZero, KnownOne;
701 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
702 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
703 return (KnownZero & Mask) == Mask;
704}
705
706/// ComputeMaskedBits - Determine which of the bits specified in Mask are
707/// known to be either zero or one and return them in the KnownZero/KnownOne
708/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
709/// processing.
710void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
711 uint64_t &KnownZero, uint64_t &KnownOne,
712 unsigned Depth) const {
713 KnownZero = KnownOne = 0; // Don't know anything.
714 if (Depth == 6 || Mask == 0)
715 return; // Limit search depth.
716
717 uint64_t KnownZero2, KnownOne2;
718
719 switch (Op.getOpcode()) {
720 case ISD::Constant:
721 // We know all of the bits for a constant!
722 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
723 KnownZero = ~KnownOne & Mask;
724 return;
725 case ISD::AND:
726 // If either the LHS or the RHS are Zero, the result is zero.
727 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
728 Mask &= ~KnownZero;
729 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
730 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
731 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
732
733 // Output known-1 bits are only known if set in both the LHS & RHS.
734 KnownOne &= KnownOne2;
735 // Output known-0 are known to be clear if zero in either the LHS | RHS.
736 KnownZero |= KnownZero2;
737 return;
738 case ISD::OR:
739 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
740 Mask &= ~KnownOne;
741 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
742 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
743 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
744
745 // Output known-0 bits are only known if clear in both the LHS & RHS.
746 KnownZero &= KnownZero2;
747 // Output known-1 are known to be set if set in either the LHS | RHS.
748 KnownOne |= KnownOne2;
749 return;
750 case ISD::XOR: {
751 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
752 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
753 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
754 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
755
756 // Output known-0 bits are known if clear or set in both the LHS & RHS.
757 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
758 // Output known-1 are known to be set if set in only one of the LHS, RHS.
759 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
760 KnownZero = KnownZeroOut;
761 return;
762 }
763 case ISD::SELECT:
764 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
765 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
766 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
767 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
768
769 // Only known if known in both the LHS and RHS.
770 KnownOne &= KnownOne2;
771 KnownZero &= KnownZero2;
772 return;
773 case ISD::SELECT_CC:
774 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
775 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
776 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
777 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
778
779 // Only known if known in both the LHS and RHS.
780 KnownOne &= KnownOne2;
781 KnownZero &= KnownZero2;
782 return;
783 case ISD::SETCC:
784 // If we know the result of a setcc has the top bits zero, use this info.
785 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
786 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
787 return;
788 case ISD::SHL:
789 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
790 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000791 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
792 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000793 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
794 KnownZero <<= SA->getValue();
795 KnownOne <<= SA->getValue();
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000796 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000797 }
Nate Begeman003a2722006-02-18 02:43:25 +0000798 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000799 case ISD::SRL:
800 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
801 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000802 MVT::ValueType VT = Op.getValueType();
803 unsigned ShAmt = SA->getValue();
804
805 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
806 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
807 KnownZero, KnownOne, Depth+1);
Nate Begeman003a2722006-02-18 02:43:25 +0000808 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000809 KnownZero &= TypeMask;
810 KnownOne &= TypeMask;
811 KnownZero >>= ShAmt;
812 KnownOne >>= ShAmt;
813
814 uint64_t HighBits = (1ULL << ShAmt)-1;
815 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
816 KnownZero |= HighBits; // High bits known zero.
Nate Begeman368e18d2006-02-16 21:11:51 +0000817 }
Nate Begeman003a2722006-02-18 02:43:25 +0000818 return;
Nate Begeman368e18d2006-02-16 21:11:51 +0000819 case ISD::SRA:
820 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000821 MVT::ValueType VT = Op.getValueType();
822 unsigned ShAmt = SA->getValue();
823
824 // Compute the new bits that are at the top now.
825 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
826
827 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
828 // If any of the demanded bits are produced by the sign extension, we also
829 // demand the input sign bit.
830 uint64_t HighBits = (1ULL << ShAmt)-1;
831 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
832 if (HighBits & Mask)
833 InDemandedMask |= MVT::getIntVTSignBit(VT);
834
835 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
836 Depth+1);
837 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
838 KnownZero &= TypeMask;
839 KnownOne &= TypeMask;
840 KnownZero >>= ShAmt;
841 KnownOne >>= ShAmt;
Nate Begeman368e18d2006-02-16 21:11:51 +0000842
843 // Handle the sign bits.
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000844 uint64_t SignBit = MVT::getIntVTSignBit(VT);
845 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
Nate Begeman368e18d2006-02-16 21:11:51 +0000846
Jim Laskey9bfa2dc2006-06-13 13:08:58 +0000847 if (KnownZero & SignBit) {
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000848 KnownZero |= HighBits; // New bits are known zero.
Jim Laskey9bfa2dc2006-06-13 13:08:58 +0000849 } else if (KnownOne & SignBit) {
Chris Lattnerc4fa6032006-06-13 16:52:37 +0000850 KnownOne |= HighBits; // New bits are known one.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000851 }
852 }
Nate Begeman003a2722006-02-18 02:43:25 +0000853 return;
Chris Lattnerec665152006-02-26 23:36:02 +0000854 case ISD::SIGN_EXTEND_INREG: {
855 MVT::ValueType VT = Op.getValueType();
856 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
857
858 // Sign extension. Compute the demanded bits in the result that are not
859 // present in the input.
860 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
861
862 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
863 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
864
865 // If the sign extended bits are demanded, we know that the sign
866 // bit is demanded.
867 if (NewBits)
868 InputDemandedBits |= InSignBit;
869
870 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
871 KnownZero, KnownOne, Depth+1);
872 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
873
874 // If the sign bit of the input is known set or clear, then we know the
875 // top bits of the result.
876 if (KnownZero & InSignBit) { // Input sign bit known clear
877 KnownZero |= NewBits;
878 KnownOne &= ~NewBits;
879 } else if (KnownOne & InSignBit) { // Input sign bit known set
880 KnownOne |= NewBits;
881 KnownZero &= ~NewBits;
882 } else { // Input sign bit unknown
883 KnownZero &= ~NewBits;
884 KnownOne &= ~NewBits;
885 }
886 return;
887 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000888 case ISD::CTTZ:
889 case ISD::CTLZ:
Nate Begeman368e18d2006-02-16 21:11:51 +0000890 case ISD::CTPOP: {
891 MVT::ValueType VT = Op.getValueType();
892 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
893 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
894 KnownOne = 0;
895 return;
896 }
Evan Cheng466685d2006-10-09 20:57:25 +0000897 case ISD::LOAD: {
Evan Chengc5484282006-10-04 00:56:09 +0000898 if (ISD::isZEXTLoad(Op.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +0000899 LoadSDNode *LD = cast<LoadSDNode>(Op);
Evan Cheng2e49f092006-10-11 07:10:22 +0000900 MVT::ValueType VT = LD->getLoadedVT();
Evan Chengc5484282006-10-04 00:56:09 +0000901 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
902 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000903 return;
904 }
905 case ISD::ZERO_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000906 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
907 uint64_t NewBits = (~InMask) & Mask;
908 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
909 KnownOne, Depth+1);
910 KnownZero |= NewBits & Mask;
911 KnownOne &= ~NewBits;
912 return;
913 }
914 case ISD::SIGN_EXTEND: {
915 MVT::ValueType InVT = Op.getOperand(0).getValueType();
916 unsigned InBits = MVT::getSizeInBits(InVT);
917 uint64_t InMask = MVT::getIntVTBitMask(InVT);
918 uint64_t InSignBit = 1ULL << (InBits-1);
919 uint64_t NewBits = (~InMask) & Mask;
920 uint64_t InDemandedBits = Mask & InMask;
921
922 // If any of the sign extended bits are demanded, we know that the sign
923 // bit is demanded.
924 if (NewBits & Mask)
925 InDemandedBits |= InSignBit;
926
927 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
928 KnownOne, Depth+1);
929 // If the sign bit is known zero or one, the top bits match.
930 if (KnownZero & InSignBit) {
931 KnownZero |= NewBits;
932 KnownOne &= ~NewBits;
933 } else if (KnownOne & InSignBit) {
934 KnownOne |= NewBits;
935 KnownZero &= ~NewBits;
936 } else { // Otherwise, top bits aren't known.
937 KnownOne &= ~NewBits;
938 KnownZero &= ~NewBits;
939 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000940 return;
941 }
942 case ISD::ANY_EXTEND: {
Chris Lattnerec665152006-02-26 23:36:02 +0000943 MVT::ValueType VT = Op.getOperand(0).getValueType();
944 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
945 KnownZero, KnownOne, Depth+1);
Nate Begeman368e18d2006-02-16 21:11:51 +0000946 return;
947 }
Chris Lattnerfe8babf2006-05-05 22:32:12 +0000948 case ISD::TRUNCATE: {
949 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
950 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
951 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
952 KnownZero &= OutMask;
953 KnownOne &= OutMask;
954 break;
955 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000956 case ISD::AssertZext: {
Chris Lattnerec665152006-02-26 23:36:02 +0000957 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
958 uint64_t InMask = MVT::getIntVTBitMask(VT);
959 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
960 KnownOne, Depth+1);
961 KnownZero |= (~InMask) & Mask;
Nate Begeman368e18d2006-02-16 21:11:51 +0000962 return;
963 }
964 case ISD::ADD: {
965 // If either the LHS or the RHS are Zero, the result is zero.
966 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
967 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
968 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
969 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
970
971 // Output known-0 bits are known if clear or set in both the low clear bits
Chris Lattnerb6b17ff2006-03-13 06:42:16 +0000972 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
973 // low 3 bits clear.
Nate Begeman368e18d2006-02-16 21:11:51 +0000974 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
975 CountTrailingZeros_64(~KnownZero2));
976
977 KnownZero = (1ULL << KnownZeroOut) - 1;
978 KnownOne = 0;
979 return;
980 }
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000981 case ISD::SUB: {
982 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
983 if (!CLHS) return;
984
Nate Begeman368e18d2006-02-16 21:11:51 +0000985 // We know that the top bits of C-X are clear if X contains less bits
986 // than C (i.e. no wrap-around can happen). For example, 20-X is
Chris Lattnera6bc5a42006-02-27 01:00:42 +0000987 // positive if we can prove that X is >= 0 and < 16.
988 MVT::ValueType VT = CLHS->getValueType(0);
989 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
990 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
991 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
992 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
993 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
994
995 // If all of the MaskV bits are known to be zero, then we know the output
996 // top bits are zero, because we now know that the output is from [0-C].
997 if ((KnownZero & MaskV) == MaskV) {
998 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
999 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1000 KnownOne = 0; // No one bits known.
1001 } else {
Evan Cheng42f75a92006-07-07 21:37:21 +00001002 KnownZero = KnownOne = 0; // Otherwise, nothing known.
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001003 }
1004 }
Nate Begeman003a2722006-02-18 02:43:25 +00001005 return;
Chris Lattnera6bc5a42006-02-27 01:00:42 +00001006 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001007 default:
1008 // Allow the target to implement this method for its nodes.
Chris Lattner1482b5f2006-04-02 06:15:09 +00001009 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1010 case ISD::INTRINSIC_WO_CHAIN:
1011 case ISD::INTRINSIC_W_CHAIN:
1012 case ISD::INTRINSIC_VOID:
Nate Begeman368e18d2006-02-16 21:11:51 +00001013 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
Chris Lattner1482b5f2006-04-02 06:15:09 +00001014 }
Nate Begeman003a2722006-02-18 02:43:25 +00001015 return;
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001016 }
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001017}
1018
Nate Begeman368e18d2006-02-16 21:11:51 +00001019/// computeMaskedBitsForTargetNode - Determine which of the bits specified
1020/// in Mask are known to be either zero or one and return them in the
1021/// KnownZero/KnownOne bitsets.
1022void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1023 uint64_t Mask,
1024 uint64_t &KnownZero,
1025 uint64_t &KnownOne,
1026 unsigned Depth) const {
Chris Lattner1b5232a2006-04-02 06:19:46 +00001027 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1028 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1029 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1030 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001031 "Should use MaskedValueIsZero if you don't know whether Op"
1032 " is a target node!");
Nate Begeman368e18d2006-02-16 21:11:51 +00001033 KnownZero = 0;
1034 KnownOne = 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001035}
Chris Lattner4ccb0702006-01-26 20:37:03 +00001036
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001037/// ComputeNumSignBits - Return the number of times the sign bit of the
1038/// register is replicated into the other bits. We know that at least 1 bit
1039/// is always equal to the sign bit (itself), but other cases can give us
1040/// information. For example, immediately after an "SRA X, 2", we know that
1041/// the top 3 bits are all equal to each other, so we return 3.
1042unsigned TargetLowering::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1043 MVT::ValueType VT = Op.getValueType();
1044 assert(MVT::isInteger(VT) && "Invalid VT!");
1045 unsigned VTBits = MVT::getSizeInBits(VT);
1046 unsigned Tmp, Tmp2;
1047
1048 if (Depth == 6)
1049 return 1; // Limit search depth.
1050
1051 switch (Op.getOpcode()) {
Chris Lattnerd6f7fe72006-05-06 22:39:59 +00001052 default: break;
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001053 case ISD::AssertSext:
1054 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1055 return VTBits-Tmp+1;
1056 case ISD::AssertZext:
1057 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1058 return VTBits-Tmp;
Chris Lattnerd6f7fe72006-05-06 22:39:59 +00001059
1060 case ISD::Constant: {
1061 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1062 // If negative, invert the bits, then look at it.
1063 if (Val & MVT::getIntVTSignBit(VT))
1064 Val = ~Val;
1065
1066 // Shift the bits so they are the leading bits in the int64_t.
1067 Val <<= 64-VTBits;
1068
1069 // Return # leading zeros. We use 'min' here in case Val was zero before
1070 // shifting. We don't want to return '64' as for an i32 "0".
1071 return std::min(VTBits, CountLeadingZeros_64(Val));
1072 }
1073
1074 case ISD::SIGN_EXTEND:
1075 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1076 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1077
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001078 case ISD::SIGN_EXTEND_INREG:
1079 // Max of the input and what this extends.
1080 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1081 Tmp = VTBits-Tmp+1;
1082
1083 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1084 return std::max(Tmp, Tmp2);
1085
1086 case ISD::SRA:
1087 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1088 // SRA X, C -> adds C sign bits.
1089 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1090 Tmp += C->getValue();
1091 if (Tmp > VTBits) Tmp = VTBits;
1092 }
1093 return Tmp;
Chris Lattnerd6f7fe72006-05-06 22:39:59 +00001094 case ISD::SHL:
1095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1096 // shl destroys sign bits.
1097 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1098 if (C->getValue() >= VTBits || // Bad shift.
1099 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1100 return Tmp - C->getValue();
1101 }
1102 break;
Chris Lattnerd6f7fe72006-05-06 22:39:59 +00001103 case ISD::AND:
1104 case ISD::OR:
1105 case ISD::XOR: // NOT is handled here.
1106 // Logical binary ops preserve the number of sign bits.
1107 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1108 if (Tmp == 1) return 1; // Early out.
1109 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1110 return std::min(Tmp, Tmp2);
1111
1112 case ISD::SELECT:
1113 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1114 if (Tmp == 1) return 1; // Early out.
1115 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1116 return std::min(Tmp, Tmp2);
1117
1118 case ISD::SETCC:
1119 // If setcc returns 0/-1, all bits are sign bits.
1120 if (getSetCCResultContents() == ZeroOrNegativeOneSetCCResult)
1121 return VTBits;
1122 break;
Chris Lattnere60351b2006-05-06 23:40:29 +00001123 case ISD::ROTL:
1124 case ISD::ROTR:
1125 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1126 unsigned RotAmt = C->getValue() & (VTBits-1);
1127
1128 // Handle rotate right by N like a rotate left by 32-N.
1129 if (Op.getOpcode() == ISD::ROTR)
1130 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1131
1132 // If we aren't rotating out all of the known-in sign bits, return the
1133 // number that are left. This handles rotl(sext(x), 1) for example.
1134 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1135 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1136 }
1137 break;
1138 case ISD::ADD:
1139 // Add can have at most one carry bit. Thus we know that the output
1140 // is, at worst, one more bit than the inputs.
1141 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1142 if (Tmp == 1) return 1; // Early out.
1143
1144 // Special case decrementing a value (ADD X, -1):
1145 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1146 if (CRHS->isAllOnesValue()) {
1147 uint64_t KnownZero, KnownOne;
1148 uint64_t Mask = MVT::getIntVTBitMask(VT);
1149 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1150
1151 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1152 // sign bits set.
1153 if ((KnownZero|1) == Mask)
1154 return VTBits;
1155
1156 // If we are subtracting one from a positive number, there is no carry
1157 // out of the result.
1158 if (KnownZero & MVT::getIntVTSignBit(VT))
1159 return Tmp;
1160 }
1161
1162 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1163 if (Tmp2 == 1) return 1;
1164 return std::min(Tmp, Tmp2)-1;
1165 break;
1166
1167 case ISD::SUB:
1168 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1169 if (Tmp2 == 1) return 1;
1170
1171 // Handle NEG.
1172 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1173 if (CLHS->getValue() == 0) {
1174 uint64_t KnownZero, KnownOne;
1175 uint64_t Mask = MVT::getIntVTBitMask(VT);
1176 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1177 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1178 // sign bits set.
1179 if ((KnownZero|1) == Mask)
1180 return VTBits;
1181
1182 // If the input is known to be positive (the sign bit is known clear),
1183 // the output of the NEG has the same number of sign bits as the input.
1184 if (KnownZero & MVT::getIntVTSignBit(VT))
1185 return Tmp2;
1186
1187 // Otherwise, we treat this like a SUB.
1188 }
1189
1190 // Sub can have at most one carry bit. Thus we know that the output
1191 // is, at worst, one more bit than the inputs.
1192 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1193 if (Tmp == 1) return 1; // Early out.
1194 return std::min(Tmp, Tmp2)-1;
1195 break;
1196 case ISD::TRUNCATE:
1197 // FIXME: it's tricky to do anything useful for this, but it is an important
1198 // case for targets like X86.
1199 break;
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001200 }
1201
Evan Chengc5484282006-10-04 00:56:09 +00001202 // Handle LOADX separately here. EXTLOAD case will fallthrough.
Evan Cheng466685d2006-10-09 20:57:25 +00001203 if (Op.getOpcode() == ISD::LOAD) {
1204 LoadSDNode *LD = cast<LoadSDNode>(Op);
1205 unsigned ExtType = LD->getExtensionType();
1206 switch (ExtType) {
Evan Chengc5484282006-10-04 00:56:09 +00001207 default: break;
1208 case ISD::SEXTLOAD: // '17' bits known
Evan Cheng2e49f092006-10-11 07:10:22 +00001209 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
Evan Chengc5484282006-10-04 00:56:09 +00001210 return VTBits-Tmp+1;
1211 case ISD::ZEXTLOAD: // '16' bits known
Evan Cheng2e49f092006-10-11 07:10:22 +00001212 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
Evan Chengc5484282006-10-04 00:56:09 +00001213 return VTBits-Tmp;
1214 }
1215 }
1216
Chris Lattnerd6f7fe72006-05-06 22:39:59 +00001217 // Allow the target to implement this method for its nodes.
1218 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1219 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1220 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1221 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1222 unsigned NumBits = ComputeNumSignBitsForTargetNode(Op, Depth);
1223 if (NumBits > 1) return NumBits;
1224 }
1225
Chris Lattner822db932006-05-06 23:48:13 +00001226 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1227 // use this information.
1228 uint64_t KnownZero, KnownOne;
1229 uint64_t Mask = MVT::getIntVTBitMask(VT);
1230 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1231
1232 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1233 if (KnownZero & SignBit) { // SignBit is 0
1234 Mask = KnownZero;
1235 } else if (KnownOne & SignBit) { // SignBit is 1;
1236 Mask = KnownOne;
1237 } else {
1238 // Nothing known.
1239 return 1;
1240 }
1241
1242 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1243 // the number of identical bits in the top of the input value.
1244 Mask ^= ~0ULL;
1245 Mask <<= 64-VTBits;
1246 // Return # leading zeros. We use 'min' here in case Val was zero before
1247 // shifting. We don't want to return '64' as for an i32 "0".
1248 return std::min(VTBits, CountLeadingZeros_64(Mask));
Chris Lattner5c3e21d2006-05-06 09:27:13 +00001249}
1250
1251
1252
1253/// ComputeNumSignBitsForTargetNode - This method can be implemented by
1254/// targets that want to expose additional information about sign bits to the
1255/// DAG Combiner.
1256unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1257 unsigned Depth) const {
1258 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1259 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1260 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1261 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1262 "Should use ComputeNumSignBits if you don't know whether Op"
1263 " is a target node!");
1264 return 1;
1265}
1266
1267
Chris Lattner00ffed02006-03-01 04:52:55 +00001268SDOperand TargetLowering::
1269PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1270 // Default implementation: no optimization.
1271 return SDOperand();
1272}
1273
Chris Lattnereb8146b2006-02-04 02:13:02 +00001274//===----------------------------------------------------------------------===//
1275// Inline Assembler Implementation Methods
1276//===----------------------------------------------------------------------===//
1277
1278TargetLowering::ConstraintType
1279TargetLowering::getConstraintType(char ConstraintLetter) const {
1280 // FIXME: lots more standard ones to handle.
1281 switch (ConstraintLetter) {
1282 default: return C_Unknown;
1283 case 'r': return C_RegisterClass;
Chris Lattner2b7401e2006-02-24 01:10:46 +00001284 case 'm': // memory
1285 case 'o': // offsetable
1286 case 'V': // not offsetable
1287 return C_Memory;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001288 case 'i': // Simple Integer or Relocatable Constant
1289 case 'n': // Simple Integer
1290 case 's': // Relocatable Constant
1291 case 'I': // Target registers.
1292 case 'J':
1293 case 'K':
1294 case 'L':
1295 case 'M':
1296 case 'N':
1297 case 'O':
Chris Lattner2b7401e2006-02-24 01:10:46 +00001298 case 'P':
1299 return C_Other;
Chris Lattnereb8146b2006-02-04 02:13:02 +00001300 }
1301}
1302
Chris Lattnerdba1aee2006-10-31 19:40:43 +00001303/// isOperandValidForConstraint - Return the specified operand (possibly
1304/// modified) if the specified SDOperand is valid for the specified target
1305/// constraint letter, otherwise return null.
1306SDOperand TargetLowering::isOperandValidForConstraint(SDOperand Op,
1307 char ConstraintLetter,
1308 SelectionDAG &DAG) {
Chris Lattnereb8146b2006-02-04 02:13:02 +00001309 switch (ConstraintLetter) {
Chris Lattnerdba1aee2006-10-31 19:40:43 +00001310 default: return SDOperand(0,0);
Chris Lattnereb8146b2006-02-04 02:13:02 +00001311 case 'i': // Simple Integer or Relocatable Constant
1312 case 'n': // Simple Integer
1313 case 's': // Relocatable Constant
Chris Lattnerdba1aee2006-10-31 19:40:43 +00001314 return Op; // FIXME: not right.
Chris Lattnereb8146b2006-02-04 02:13:02 +00001315 }
1316}
1317
Chris Lattner4ccb0702006-01-26 20:37:03 +00001318std::vector<unsigned> TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001319getRegClassForInlineAsmConstraint(const std::string &Constraint,
1320 MVT::ValueType VT) const {
1321 return std::vector<unsigned>();
1322}
1323
1324
1325std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
Chris Lattner4217ca8dc2006-02-21 23:11:00 +00001326getRegForInlineAsmConstraint(const std::string &Constraint,
1327 MVT::ValueType VT) const {
Chris Lattner1efa40f2006-02-22 00:56:39 +00001328 if (Constraint[0] != '{')
1329 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattnera55079a2006-02-01 01:29:47 +00001330 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1331
1332 // Remove the braces from around the name.
1333 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001334
1335 // Figure out which register class contains this reg.
Chris Lattner4ccb0702006-01-26 20:37:03 +00001336 const MRegisterInfo *RI = TM.getRegisterInfo();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001337 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1338 E = RI->regclass_end(); RCI != E; ++RCI) {
1339 const TargetRegisterClass *RC = *RCI;
Chris Lattnerb3befd42006-02-22 23:00:51 +00001340
1341 // If none of the the value types for this register class are valid, we
1342 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1343 bool isLegal = false;
1344 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1345 I != E; ++I) {
1346 if (isTypeLegal(*I)) {
1347 isLegal = true;
1348 break;
1349 }
1350 }
1351
1352 if (!isLegal) continue;
1353
Chris Lattner1efa40f2006-02-22 00:56:39 +00001354 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1355 I != E; ++I) {
Chris Lattnerb3befd42006-02-22 23:00:51 +00001356 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
Chris Lattner1efa40f2006-02-22 00:56:39 +00001357 return std::make_pair(*I, RC);
Chris Lattner1efa40f2006-02-22 00:56:39 +00001358 }
Chris Lattner4ccb0702006-01-26 20:37:03 +00001359 }
Chris Lattnera55079a2006-02-01 01:29:47 +00001360
Chris Lattner1efa40f2006-02-22 00:56:39 +00001361 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
Chris Lattner4ccb0702006-01-26 20:37:03 +00001362}
Evan Cheng30b37b52006-03-13 23:18:16 +00001363
1364//===----------------------------------------------------------------------===//
1365// Loop Strength Reduction hooks
1366//===----------------------------------------------------------------------===//
1367
1368/// isLegalAddressImmediate - Return true if the integer value or
1369/// GlobalValue can be used as the offset of the target addressing mode.
1370bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
1371 return false;
1372}
1373bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1374 return false;
1375}
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001376
1377
1378// Magic for divide replacement
1379
1380struct ms {
1381 int64_t m; // magic number
1382 int64_t s; // shift amount
1383};
1384
1385struct mu {
1386 uint64_t m; // magic number
1387 int64_t a; // add indicator
1388 int64_t s; // shift amount
1389};
1390
1391/// magic - calculate the magic numbers required to codegen an integer sdiv as
1392/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1393/// or -1.
1394static ms magic32(int32_t d) {
1395 int32_t p;
1396 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1397 const uint32_t two31 = 0x80000000U;
1398 struct ms mag;
1399
1400 ad = abs(d);
1401 t = two31 + ((uint32_t)d >> 31);
1402 anc = t - 1 - t%ad; // absolute value of nc
1403 p = 31; // initialize p
1404 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1405 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1406 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1407 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1408 do {
1409 p = p + 1;
1410 q1 = 2*q1; // update q1 = 2p/abs(nc)
1411 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1412 if (r1 >= anc) { // must be unsigned comparison
1413 q1 = q1 + 1;
1414 r1 = r1 - anc;
1415 }
1416 q2 = 2*q2; // update q2 = 2p/abs(d)
1417 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1418 if (r2 >= ad) { // must be unsigned comparison
1419 q2 = q2 + 1;
1420 r2 = r2 - ad;
1421 }
1422 delta = ad - r2;
1423 } while (q1 < delta || (q1 == delta && r1 == 0));
1424
1425 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1426 if (d < 0) mag.m = -mag.m; // resulting magic number
1427 mag.s = p - 32; // resulting shift
1428 return mag;
1429}
1430
1431/// magicu - calculate the magic numbers required to codegen an integer udiv as
1432/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1433static mu magicu32(uint32_t d) {
1434 int32_t p;
1435 uint32_t nc, delta, q1, r1, q2, r2;
1436 struct mu magu;
1437 magu.a = 0; // initialize "add" indicator
1438 nc = - 1 - (-d)%d;
1439 p = 31; // initialize p
1440 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1441 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1442 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1443 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1444 do {
1445 p = p + 1;
1446 if (r1 >= nc - r1 ) {
1447 q1 = 2*q1 + 1; // update q1
1448 r1 = 2*r1 - nc; // update r1
1449 }
1450 else {
1451 q1 = 2*q1; // update q1
1452 r1 = 2*r1; // update r1
1453 }
1454 if (r2 + 1 >= d - r2) {
1455 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1456 q2 = 2*q2 + 1; // update q2
1457 r2 = 2*r2 + 1 - d; // update r2
1458 }
1459 else {
1460 if (q2 >= 0x80000000) magu.a = 1;
1461 q2 = 2*q2; // update q2
1462 r2 = 2*r2 + 1; // update r2
1463 }
1464 delta = d - 1 - r2;
1465 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1466 magu.m = q2 + 1; // resulting magic number
1467 magu.s = p - 32; // resulting shift
1468 return magu;
1469}
1470
1471/// magic - calculate the magic numbers required to codegen an integer sdiv as
1472/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1473/// or -1.
1474static ms magic64(int64_t d) {
1475 int64_t p;
1476 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1477 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1478 struct ms mag;
1479
1480 ad = d >= 0 ? d : -d;
1481 t = two63 + ((uint64_t)d >> 63);
1482 anc = t - 1 - t%ad; // absolute value of nc
1483 p = 63; // initialize p
1484 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1485 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1486 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1487 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1488 do {
1489 p = p + 1;
1490 q1 = 2*q1; // update q1 = 2p/abs(nc)
1491 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1492 if (r1 >= anc) { // must be unsigned comparison
1493 q1 = q1 + 1;
1494 r1 = r1 - anc;
1495 }
1496 q2 = 2*q2; // update q2 = 2p/abs(d)
1497 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1498 if (r2 >= ad) { // must be unsigned comparison
1499 q2 = q2 + 1;
1500 r2 = r2 - ad;
1501 }
1502 delta = ad - r2;
1503 } while (q1 < delta || (q1 == delta && r1 == 0));
1504
1505 mag.m = q2 + 1;
1506 if (d < 0) mag.m = -mag.m; // resulting magic number
1507 mag.s = p - 64; // resulting shift
1508 return mag;
1509}
1510
1511/// magicu - calculate the magic numbers required to codegen an integer udiv as
1512/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1513static mu magicu64(uint64_t d)
1514{
1515 int64_t p;
1516 uint64_t nc, delta, q1, r1, q2, r2;
1517 struct mu magu;
1518 magu.a = 0; // initialize "add" indicator
1519 nc = - 1 - (-d)%d;
1520 p = 63; // initialize p
1521 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
1522 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
1523 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
1524 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
1525 do {
1526 p = p + 1;
1527 if (r1 >= nc - r1 ) {
1528 q1 = 2*q1 + 1; // update q1
1529 r1 = 2*r1 - nc; // update r1
1530 }
1531 else {
1532 q1 = 2*q1; // update q1
1533 r1 = 2*r1; // update r1
1534 }
1535 if (r2 + 1 >= d - r2) {
1536 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
1537 q2 = 2*q2 + 1; // update q2
1538 r2 = 2*r2 + 1 - d; // update r2
1539 }
1540 else {
1541 if (q2 >= 0x8000000000000000ull) magu.a = 1;
1542 q2 = 2*q2; // update q2
1543 r2 = 2*r2 + 1; // update r2
1544 }
1545 delta = d - 1 - r2;
Andrew Lenharth3e348492006-05-16 17:45:23 +00001546 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001547 magu.m = q2 + 1; // resulting magic number
1548 magu.s = p - 64; // resulting shift
1549 return magu;
1550}
1551
1552/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
1553/// return a DAG expression to select that will generate the same value by
1554/// multiplying by a magic number. See:
1555/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1556SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
Andrew Lenharth232c9102006-06-12 16:07:18 +00001557 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001558 MVT::ValueType VT = N->getValueType(0);
1559
1560 // Check to see if we can do this.
1561 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1562 return SDOperand(); // BuildSDIV only operates on i32 or i64
1563 if (!isOperationLegal(ISD::MULHS, VT))
1564 return SDOperand(); // Make sure the target supports MULHS.
1565
1566 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
1567 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
1568
1569 // Multiply the numerator (operand 0) by the magic value
1570 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
1571 DAG.getConstant(magics.m, VT));
1572 // If d > 0 and m < 0, add the numerator
1573 if (d > 0 && magics.m < 0) {
1574 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
1575 if (Created)
1576 Created->push_back(Q.Val);
1577 }
1578 // If d < 0 and m > 0, subtract the numerator.
1579 if (d < 0 && magics.m > 0) {
1580 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
1581 if (Created)
1582 Created->push_back(Q.Val);
1583 }
1584 // Shift right algebraic if shift value is nonzero
1585 if (magics.s > 0) {
1586 Q = DAG.getNode(ISD::SRA, VT, Q,
1587 DAG.getConstant(magics.s, getShiftAmountTy()));
1588 if (Created)
1589 Created->push_back(Q.Val);
1590 }
1591 // Extract the sign bit and add it to the quotient
1592 SDOperand T =
1593 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
1594 getShiftAmountTy()));
1595 if (Created)
1596 Created->push_back(T.Val);
1597 return DAG.getNode(ISD::ADD, VT, Q, T);
1598}
1599
1600/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
1601/// return a DAG expression to select that will generate the same value by
1602/// multiplying by a magic number. See:
1603/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1604SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Andrew Lenharth232c9102006-06-12 16:07:18 +00001605 std::vector<SDNode*>* Created) const {
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00001606 MVT::ValueType VT = N->getValueType(0);
1607
1608 // Check to see if we can do this.
1609 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1610 return SDOperand(); // BuildUDIV only operates on i32 or i64
1611 if (!isOperationLegal(ISD::MULHU, VT))
1612 return SDOperand(); // Make sure the target supports MULHU.
1613
1614 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1615 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
1616
1617 // Multiply the numerator (operand 0) by the magic value
1618 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
1619 DAG.getConstant(magics.m, VT));
1620 if (Created)
1621 Created->push_back(Q.Val);
1622
1623 if (magics.a == 0) {
1624 return DAG.getNode(ISD::SRL, VT, Q,
1625 DAG.getConstant(magics.s, getShiftAmountTy()));
1626 } else {
1627 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
1628 if (Created)
1629 Created->push_back(NPQ.Val);
1630 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
1631 DAG.getConstant(1, getShiftAmountTy()));
1632 if (Created)
1633 Created->push_back(NPQ.Val);
1634 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
1635 if (Created)
1636 Created->push_back(NPQ.Val);
1637 return DAG.getNode(ISD::SRL, VT, NPQ,
1638 DAG.getConstant(magics.s-1, getShiftAmountTy()));
1639 }
1640}