1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This implements the TargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetLowering.h"
15 #include "llvm/Target/TargetData.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Target/MRegisterInfo.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/MathExtras.h"
24 TargetLowering::TargetLowering(TargetMachine &tm)
25 : TM(tm), TD(TM.getTargetData()) {
26 assert(ISD::BUILTIN_OP_END <= 156 &&
27 "Fixed size array in TargetLowering is not large enough!");
28 // All operations default to being supported.
29 memset(OpActions, 0, sizeof(OpActions));
30 memset(LoadXActions, 0, sizeof(LoadXActions));
31 memset(&StoreXActions, 0, sizeof(StoreXActions));
33 IsLittleEndian = TD->isLittleEndian();
34 UsesGlobalOffsetTable = false;
35 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
36 ShiftAmtHandling = Undefined;
37 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
38 memset(TargetDAGCombineArray, 0,
39 sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
40 maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
41 allowUnalignedMemoryAccesses = false;
42 UseUnderscoreSetJmpLongJmp = false;
43 IntDivIsCheap = false;
44 Pow2DivIsCheap = false;
45 StackPointerRegisterToSaveRestore = 0;
46 SchedPreferenceInfo = SchedulingForLatency;
51 TargetLowering::~TargetLowering() {}
53 /// setValueTypeAction - Set the action for a particular value type. This
54 /// assumes an action has not already been set for this value type.
55 static void SetValueTypeAction(MVT::ValueType VT,
56 TargetLowering::LegalizeAction Action,
58 MVT::ValueType *TransformToType,
59 TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
60 ValueTypeActions.setTypeAction(VT, Action);
61 if (Action == TargetLowering::Promote) {
62 MVT::ValueType PromoteTo;
66 unsigned LargerReg = VT+1;
67 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
69 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
70 "Nothing to promote to??");
72 PromoteTo = (MVT::ValueType)LargerReg;
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;
80 } else if (Action == TargetLowering::Expand) {
81 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
82 "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);
89 /// computeRegisterProperties - Once all of the register classes are added,
90 /// this allows us to compute derived properties we expose.
91 void TargetLowering::computeRegisterProperties() {
92 assert(MVT::LAST_VALUETYPE <= 32 &&
93 "Too many value types for ValueTypeActions to hold!");
95 // Everything defaults to one.
96 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
97 NumElementsForVT[i] = 1;
99 // 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!");
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];
110 // 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)
114 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
116 else if (!isTypeLegal((MVT::ValueType)IntReg))
117 // Otherwise, if we don't have native support, we must promote to a
119 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
120 TransformToType, ValueTypeActions);
122 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
124 // If the target does not have native support for F32, promote it to F64.
125 if (!isTypeLegal(MVT::f32))
126 SetValueTypeAction(MVT::f32, Promote, *this,
127 TransformToType, ValueTypeActions);
129 TransformToType[MVT::f32] = MVT::f32;
131 // Set MVT::Vector to always be Expanded
132 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
135 // Loop over all of the legal vector value types, specifying an identity type
137 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
138 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
139 if (isTypeLegal((MVT::ValueType)i))
140 TransformToType[i] = (MVT::ValueType)i;
143 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
144 TransformToType[MVT::f64] = MVT::f64;
147 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
151 /// getPackedTypeBreakdown - Packed types are broken down into some number of
152 /// legal first class types. For example, <8 x float> maps to 2 MVT::v4f32
153 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
155 /// This method returns the number and type of the resultant breakdown.
157 unsigned TargetLowering::getPackedTypeBreakdown(const PackedType *PTy,
158 MVT::ValueType &PTyElementVT,
159 MVT::ValueType &PTyLegalElementVT) const {
160 // Figure out the right, legal destination reg to copy into.
161 unsigned NumElts = PTy->getNumElements();
162 MVT::ValueType EltTy = getValueType(PTy->getElementType());
164 unsigned NumVectorRegs = 1;
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))) {
177 VT = getVectorType(EltTy, NumElts);
181 MVT::ValueType DestVT = getTypeToTransformTo(VT);
182 PTyLegalElementVT = DestVT;
184 // Value is expanded, e.g. i64 -> i16.
185 return NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
187 // Otherwise, promotion or legal types use the same number of registers as
188 // the vector decimated to the appropriate level.
189 return NumVectorRegs;
195 //===----------------------------------------------------------------------===//
196 // Optimization Methods
197 //===----------------------------------------------------------------------===//
199 /// 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.
203 bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op,
205 // FIXME: ISD::SELECT, ISD::SELECT_CC
206 switch(Op.getOpcode()) {
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(),
217 return CombineTo(Op, New);
224 /// 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.
231 bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
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()) {
240 // If not at the root, Just compute the KnownZero/KnownOne bits to
241 // simplify things downstream.
242 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
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()));
253 } else if (Depth == 6) { // Limit search depth.
257 uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
258 switch (Op.getOpcode()) {
260 // We know all of the bits for a constant!
261 KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
262 KnownZero = ~KnownOne & DemandedMask;
263 return false; // Don't fall through, will infinitely loop.
265 // 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
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
278 if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
282 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
283 KnownOne, TLO, Depth+1))
285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
286 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
287 KnownZero2, KnownOne2, TLO, Depth+1))
289 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
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))
304 // 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;
310 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
311 KnownOne, TLO, Depth+1))
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))
317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
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'.
321 if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
322 return TLO.CombineTo(Op, Op.getOperand(0));
323 if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
324 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))
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;
343 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
344 KnownOne, TLO, Depth+1))
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))
350 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
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));
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);
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(),
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),
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))
390 KnownZero = KnownZeroOut;
391 KnownOne = KnownOneOut;
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);
399 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero,
400 KnownOne, TLO, Depth+1))
402 if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
403 KnownOne2, TLO, Depth+1))
405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
406 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
408 // If the operands are constants, see if we can simplify them.
409 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
412 // Only known if known in both the LHS and RHS.
413 KnownOne &= KnownOne2;
414 KnownZero &= KnownZero2;
417 if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero,
418 KnownOne, TLO, Depth+1))
420 if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
421 KnownOne2, TLO, Depth+1))
423 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
424 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
426 // If the operands are constants, see if we can simplify them.
427 if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
430 // Only known if known in both the LHS and RHS.
431 KnownOne &= KnownOne2;
432 KnownZero &= KnownZero2;
435 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
436 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
437 KnownZero, KnownOne, TLO, Depth+1))
439 KnownZero <<= SA->getValue();
440 KnownOne <<= SA->getValue();
441 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
445 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
446 MVT::ValueType VT = Op.getValueType();
447 unsigned ShAmt = SA->getValue();
449 // Compute the new bits that are at the top now.
450 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
451 if (SimplifyDemandedBits(Op.getOperand(0),
452 (DemandedMask << ShAmt) & TypeMask,
453 KnownZero, KnownOne, TLO, Depth+1))
455 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
456 KnownZero &= TypeMask;
457 KnownOne &= TypeMask;
461 uint64_t HighBits = (1ULL << ShAmt)-1;
462 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
463 KnownZero |= HighBits; // High bits known zero.
467 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
468 MVT::ValueType VT = Op.getValueType();
469 unsigned ShAmt = SA->getValue();
471 // Compute the new bits that are at the top now.
472 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
474 uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask;
476 // If any of the demanded bits are produced by the sign extension, we also
477 // demand the input sign bit.
478 uint64_t HighBits = (1ULL << ShAmt)-1;
479 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
480 if (HighBits & DemandedMask)
481 InDemandedMask |= MVT::getIntVTSignBit(VT);
483 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
484 KnownZero, KnownOne, TLO, Depth+1))
486 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
487 KnownZero &= TypeMask;
488 KnownOne &= TypeMask;
492 // Handle the sign bits.
493 uint64_t SignBit = MVT::getIntVTSignBit(VT);
494 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
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),
501 } else if (KnownOne & SignBit) { // New bits are known one.
502 KnownOne |= HighBits;
506 case ISD::SIGN_EXTEND_INREG: {
507 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
509 // Sign extension. Compute the demanded bits in the result that are not
510 // present in the input.
511 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
513 // If none of the extended bits are demanded, eliminate the sextinreg.
515 return TLO.CombineTo(Op, Op.getOperand(0));
517 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
518 int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
520 // Since the sign extended bits are demanded, we know that the sign
522 InputDemandedBits |= InSignBit;
524 if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
525 KnownZero, KnownOne, TLO, Depth+1))
527 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
529 // If the sign bit of the input is known set or clear, then we know the
530 // top bits of the result.
532 // If the input sign bit is known zero, convert this into a zero extension.
533 if (KnownZero & InSignBit)
534 return TLO.CombineTo(Op,
535 TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
537 if (KnownOne & InSignBit) { // Input sign bit known set
539 KnownZero &= ~NewBits;
540 } else { // Input sign bit unknown
541 KnownZero &= ~NewBits;
542 KnownOne &= ~NewBits;
549 MVT::ValueType VT = Op.getValueType();
550 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
551 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
556 if (ISD::isZEXTLoad(Op.Val)) {
557 LoadSDNode *LD = cast<LoadSDNode>(Op);
558 MVT::ValueType VT = LD->getLoadedVT();
559 KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
563 case ISD::ZERO_EXTEND: {
564 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
566 // If none of the top bits are demanded, convert this into an any_extend.
567 uint64_t NewBits = (~InMask) & DemandedMask;
569 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,
573 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
574 KnownZero, KnownOne, TLO, Depth+1))
576 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
577 KnownZero |= NewBits;
580 case ISD::SIGN_EXTEND: {
581 MVT::ValueType InVT = Op.getOperand(0).getValueType();
582 uint64_t InMask = MVT::getIntVTBitMask(InVT);
583 uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
584 uint64_t NewBits = (~InMask) & DemandedMask;
586 // If none of the top bits are demanded, convert this into an any_extend.
588 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
591 // Since some of the sign extended bits are demanded, we know that the sign
593 uint64_t InDemandedBits = DemandedMask & InMask;
594 InDemandedBits |= InSignBit;
596 if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
597 KnownOne, TLO, Depth+1))
600 // If the sign bit is known zero, convert this to a zero extend.
601 if (KnownZero & InSignBit)
602 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND,
606 // If the sign bit is known one, the top bits match.
607 if (KnownOne & InSignBit) {
609 KnownZero &= ~NewBits;
610 } else { // Otherwise, top bits aren't known.
611 KnownOne &= ~NewBits;
612 KnownZero &= ~NewBits;
616 case ISD::ANY_EXTEND: {
617 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
618 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
619 KnownZero, KnownOne, TLO, Depth+1))
621 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
624 case ISD::TRUNCATE: {
625 // Simplify the input, using demanded bit information, and compute the known
626 // zero/one bits live out.
627 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask,
628 KnownZero, KnownOne, TLO, Depth+1))
631 // If the input is only used by this truncate, see if we can shrink it based
632 // on the known demanded bits.
633 if (Op.getOperand(0).Val->hasOneUse()) {
634 SDOperand In = Op.getOperand(0);
635 switch (In.getOpcode()) {
638 // Shrink SRL by a constant if none of the high bits shifted in are
640 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
641 uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType());
642 HighBits &= ~MVT::getIntVTBitMask(Op.getValueType());
643 HighBits >>= ShAmt->getValue();
645 if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) &&
646 (DemandedMask & HighBits) == 0) {
647 // None of the shifted in bits are needed. Add a truncate of the
648 // shift input, then shift it.
649 SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE,
652 return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
653 NewTrunc, In.getOperand(1)));
660 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
661 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
662 KnownZero &= OutMask;
666 case ISD::AssertZext: {
667 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
668 uint64_t InMask = MVT::getIntVTBitMask(VT);
669 if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
670 KnownZero, KnownOne, TLO, Depth+1))
672 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
673 KnownZero |= ~InMask & DemandedMask;
678 case ISD::INTRINSIC_WO_CHAIN:
679 case ISD::INTRINSIC_W_CHAIN:
680 case ISD::INTRINSIC_VOID:
681 // Just use ComputeMaskedBits to compute output bits.
682 ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
686 // If we know the value of all of the demanded bits, return this as a
688 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
689 return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
694 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
695 /// this predicate to simplify operations downstream. Mask is known to be zero
696 /// for bits that V cannot have.
697 bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
698 unsigned Depth) const {
699 uint64_t KnownZero, KnownOne;
700 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
701 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
702 return (KnownZero & Mask) == Mask;
705 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
706 /// known to be either zero or one and return them in the KnownZero/KnownOne
707 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
709 void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
710 uint64_t &KnownZero, uint64_t &KnownOne,
711 unsigned Depth) const {
712 KnownZero = KnownOne = 0; // Don't know anything.
713 if (Depth == 6 || Mask == 0)
714 return; // Limit search depth.
716 uint64_t KnownZero2, KnownOne2;
718 switch (Op.getOpcode()) {
720 // We know all of the bits for a constant!
721 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
722 KnownZero = ~KnownOne & Mask;
725 // If either the LHS or the RHS are Zero, the result is zero.
726 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
728 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
729 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
730 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
732 // Output known-1 bits are only known if set in both the LHS & RHS.
733 KnownOne &= KnownOne2;
734 // Output known-0 are known to be clear if zero in either the LHS | RHS.
735 KnownZero |= KnownZero2;
738 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
740 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
741 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
742 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
744 // Output known-0 bits are only known if clear in both the LHS & RHS.
745 KnownZero &= KnownZero2;
746 // Output known-1 are known to be set if set in either the LHS | RHS.
747 KnownOne |= KnownOne2;
750 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
751 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
752 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
753 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
755 // Output known-0 bits are known if clear or set in both the LHS & RHS.
756 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
757 // Output known-1 are known to be set if set in only one of the LHS, RHS.
758 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
759 KnownZero = KnownZeroOut;
763 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
764 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
765 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
766 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
768 // Only known if known in both the LHS and RHS.
769 KnownOne &= KnownOne2;
770 KnownZero &= KnownZero2;
773 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
774 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
775 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
776 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
778 // Only known if known in both the LHS and RHS.
779 KnownOne &= KnownOne2;
780 KnownZero &= KnownZero2;
783 // If we know the result of a setcc has the top bits zero, use this info.
784 if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
785 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
788 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
789 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
790 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
791 KnownZero, KnownOne, Depth+1);
792 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
793 KnownZero <<= SA->getValue();
794 KnownOne <<= SA->getValue();
795 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
799 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
800 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
801 MVT::ValueType VT = Op.getValueType();
802 unsigned ShAmt = SA->getValue();
804 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
805 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
806 KnownZero, KnownOne, Depth+1);
807 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
808 KnownZero &= TypeMask;
809 KnownOne &= TypeMask;
813 uint64_t HighBits = (1ULL << ShAmt)-1;
814 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
815 KnownZero |= HighBits; // High bits known zero.
819 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
820 MVT::ValueType VT = Op.getValueType();
821 unsigned ShAmt = SA->getValue();
823 // Compute the new bits that are at the top now.
824 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
826 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
827 // If any of the demanded bits are produced by the sign extension, we also
828 // demand the input sign bit.
829 uint64_t HighBits = (1ULL << ShAmt)-1;
830 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
832 InDemandedMask |= MVT::getIntVTSignBit(VT);
834 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
837 KnownZero &= TypeMask;
838 KnownOne &= TypeMask;
842 // Handle the sign bits.
843 uint64_t SignBit = MVT::getIntVTSignBit(VT);
844 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
846 if (KnownZero & SignBit) {
847 KnownZero |= HighBits; // New bits are known zero.
848 } else if (KnownOne & SignBit) {
849 KnownOne |= HighBits; // New bits are known one.
853 case ISD::SIGN_EXTEND_INREG: {
854 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
856 // Sign extension. Compute the demanded bits in the result that are not
857 // present in the input.
858 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
860 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
861 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
863 // If the sign extended bits are demanded, we know that the sign
866 InputDemandedBits |= InSignBit;
868 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
869 KnownZero, KnownOne, Depth+1);
870 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
872 // If the sign bit of the input is known set or clear, then we know the
873 // top bits of the result.
874 if (KnownZero & InSignBit) { // Input sign bit known clear
875 KnownZero |= NewBits;
876 KnownOne &= ~NewBits;
877 } else if (KnownOne & InSignBit) { // Input sign bit known set
879 KnownZero &= ~NewBits;
880 } else { // Input sign bit unknown
881 KnownZero &= ~NewBits;
882 KnownOne &= ~NewBits;
889 MVT::ValueType VT = Op.getValueType();
890 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
891 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
896 if (ISD::isZEXTLoad(Op.Val)) {
897 LoadSDNode *LD = cast<LoadSDNode>(Op);
898 MVT::ValueType VT = LD->getLoadedVT();
899 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
903 case ISD::ZERO_EXTEND: {
904 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
905 uint64_t NewBits = (~InMask) & Mask;
906 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
908 KnownZero |= NewBits & Mask;
909 KnownOne &= ~NewBits;
912 case ISD::SIGN_EXTEND: {
913 MVT::ValueType InVT = Op.getOperand(0).getValueType();
914 unsigned InBits = MVT::getSizeInBits(InVT);
915 uint64_t InMask = MVT::getIntVTBitMask(InVT);
916 uint64_t InSignBit = 1ULL << (InBits-1);
917 uint64_t NewBits = (~InMask) & Mask;
918 uint64_t InDemandedBits = Mask & InMask;
920 // If any of the sign extended bits are demanded, we know that the sign
923 InDemandedBits |= InSignBit;
925 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
927 // If the sign bit is known zero or one, the top bits match.
928 if (KnownZero & InSignBit) {
929 KnownZero |= NewBits;
930 KnownOne &= ~NewBits;
931 } else if (KnownOne & InSignBit) {
933 KnownZero &= ~NewBits;
934 } else { // Otherwise, top bits aren't known.
935 KnownOne &= ~NewBits;
936 KnownZero &= ~NewBits;
940 case ISD::ANY_EXTEND: {
941 MVT::ValueType VT = Op.getOperand(0).getValueType();
942 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
943 KnownZero, KnownOne, Depth+1);
946 case ISD::TRUNCATE: {
947 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
948 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
949 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
950 KnownZero &= OutMask;
954 case ISD::AssertZext: {
955 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
956 uint64_t InMask = MVT::getIntVTBitMask(VT);
957 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
959 KnownZero |= (~InMask) & Mask;
963 // If either the LHS or the RHS are Zero, the result is zero.
964 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
965 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
966 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
967 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
969 // Output known-0 bits are known if clear or set in both the low clear bits
970 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
972 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
973 CountTrailingZeros_64(~KnownZero2));
975 KnownZero = (1ULL << KnownZeroOut) - 1;
980 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
983 // We know that the top bits of C-X are clear if X contains less bits
984 // than C (i.e. no wrap-around can happen). For example, 20-X is
985 // positive if we can prove that X is >= 0 and < 16.
986 MVT::ValueType VT = CLHS->getValueType(0);
987 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
988 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
989 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
990 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
991 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
993 // If all of the MaskV bits are known to be zero, then we know the output
994 // top bits are zero, because we now know that the output is from [0-C].
995 if ((KnownZero & MaskV) == MaskV) {
996 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
997 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
998 KnownOne = 0; // No one bits known.
1000 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1006 // Allow the target to implement this method for its nodes.
1007 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1008 case ISD::INTRINSIC_WO_CHAIN:
1009 case ISD::INTRINSIC_W_CHAIN:
1010 case ISD::INTRINSIC_VOID:
1011 computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
1017 /// computeMaskedBitsForTargetNode - Determine which of the bits specified
1018 /// in Mask are known to be either zero or one and return them in the
1019 /// KnownZero/KnownOne bitsets.
1020 void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1022 uint64_t &KnownZero,
1024 unsigned Depth) const {
1025 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1026 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1027 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1028 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1029 "Should use MaskedValueIsZero if you don't know whether Op"
1030 " is a target node!");
1035 /// ComputeNumSignBits - Return the number of times the sign bit of the
1036 /// register is replicated into the other bits. We know that at least 1 bit
1037 /// is always equal to the sign bit (itself), but other cases can give us
1038 /// information. For example, immediately after an "SRA X, 2", we know that
1039 /// the top 3 bits are all equal to each other, so we return 3.
1040 unsigned TargetLowering::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1041 MVT::ValueType VT = Op.getValueType();
1042 assert(MVT::isInteger(VT) && "Invalid VT!");
1043 unsigned VTBits = MVT::getSizeInBits(VT);
1047 return 1; // Limit search depth.
1049 switch (Op.getOpcode()) {
1051 case ISD::AssertSext:
1052 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1053 return VTBits-Tmp+1;
1054 case ISD::AssertZext:
1055 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1058 case ISD::Constant: {
1059 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1060 // If negative, invert the bits, then look at it.
1061 if (Val & MVT::getIntVTSignBit(VT))
1064 // Shift the bits so they are the leading bits in the int64_t.
1067 // Return # leading zeros. We use 'min' here in case Val was zero before
1068 // shifting. We don't want to return '64' as for an i32 "0".
1069 return std::min(VTBits, CountLeadingZeros_64(Val));
1072 case ISD::SIGN_EXTEND:
1073 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1074 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1076 case ISD::SIGN_EXTEND_INREG:
1077 // Max of the input and what this extends.
1078 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1081 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1082 return std::max(Tmp, Tmp2);
1085 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1086 // SRA X, C -> adds C sign bits.
1087 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1088 Tmp += C->getValue();
1089 if (Tmp > VTBits) Tmp = VTBits;
1093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1094 // shl destroys sign bits.
1095 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1096 if (C->getValue() >= VTBits || // Bad shift.
1097 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1098 return Tmp - C->getValue();
1103 case ISD::XOR: // NOT is handled here.
1104 // Logical binary ops preserve the number of sign bits.
1105 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1106 if (Tmp == 1) return 1; // Early out.
1107 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1108 return std::min(Tmp, Tmp2);
1111 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1112 if (Tmp == 1) return 1; // Early out.
1113 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1114 return std::min(Tmp, Tmp2);
1117 // If setcc returns 0/-1, all bits are sign bits.
1118 if (getSetCCResultContents() == ZeroOrNegativeOneSetCCResult)
1123 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1124 unsigned RotAmt = C->getValue() & (VTBits-1);
1126 // Handle rotate right by N like a rotate left by 32-N.
1127 if (Op.getOpcode() == ISD::ROTR)
1128 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1130 // If we aren't rotating out all of the known-in sign bits, return the
1131 // number that are left. This handles rotl(sext(x), 1) for example.
1132 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1133 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1137 // Add can have at most one carry bit. Thus we know that the output
1138 // is, at worst, one more bit than the inputs.
1139 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1140 if (Tmp == 1) return 1; // Early out.
1142 // Special case decrementing a value (ADD X, -1):
1143 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1144 if (CRHS->isAllOnesValue()) {
1145 uint64_t KnownZero, KnownOne;
1146 uint64_t Mask = MVT::getIntVTBitMask(VT);
1147 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1149 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1151 if ((KnownZero|1) == Mask)
1154 // If we are subtracting one from a positive number, there is no carry
1155 // out of the result.
1156 if (KnownZero & MVT::getIntVTSignBit(VT))
1160 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1161 if (Tmp2 == 1) return 1;
1162 return std::min(Tmp, Tmp2)-1;
1166 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1167 if (Tmp2 == 1) return 1;
1170 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1171 if (CLHS->getValue() == 0) {
1172 uint64_t KnownZero, KnownOne;
1173 uint64_t Mask = MVT::getIntVTBitMask(VT);
1174 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1175 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1177 if ((KnownZero|1) == Mask)
1180 // If the input is known to be positive (the sign bit is known clear),
1181 // the output of the NEG has the same number of sign bits as the input.
1182 if (KnownZero & MVT::getIntVTSignBit(VT))
1185 // Otherwise, we treat this like a SUB.
1188 // Sub can have at most one carry bit. Thus we know that the output
1189 // is, at worst, one more bit than the inputs.
1190 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1191 if (Tmp == 1) return 1; // Early out.
1192 return std::min(Tmp, Tmp2)-1;
1195 // FIXME: it's tricky to do anything useful for this, but it is an important
1196 // case for targets like X86.
1200 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1201 if (Op.getOpcode() == ISD::LOAD) {
1202 LoadSDNode *LD = cast<LoadSDNode>(Op);
1203 unsigned ExtType = LD->getExtensionType();
1206 case ISD::SEXTLOAD: // '17' bits known
1207 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1208 return VTBits-Tmp+1;
1209 case ISD::ZEXTLOAD: // '16' bits known
1210 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1215 // Allow the target to implement this method for its nodes.
1216 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1217 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1218 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1219 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1220 unsigned NumBits = ComputeNumSignBitsForTargetNode(Op, Depth);
1221 if (NumBits > 1) return NumBits;
1224 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1225 // use this information.
1226 uint64_t KnownZero, KnownOne;
1227 uint64_t Mask = MVT::getIntVTBitMask(VT);
1228 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1230 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1231 if (KnownZero & SignBit) { // SignBit is 0
1233 } else if (KnownOne & SignBit) { // SignBit is 1;
1240 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1241 // the number of identical bits in the top of the input value.
1244 // Return # leading zeros. We use 'min' here in case Val was zero before
1245 // shifting. We don't want to return '64' as for an i32 "0".
1246 return std::min(VTBits, CountLeadingZeros_64(Mask));
1251 /// ComputeNumSignBitsForTargetNode - This method can be implemented by
1252 /// targets that want to expose additional information about sign bits to the
1254 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1255 unsigned Depth) const {
1256 assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1257 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1258 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1259 Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1260 "Should use ComputeNumSignBits if you don't know whether Op"
1261 " is a target node!");
1266 SDOperand TargetLowering::
1267 PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1268 // Default implementation: no optimization.
1272 //===----------------------------------------------------------------------===//
1273 // Inline Assembler Implementation Methods
1274 //===----------------------------------------------------------------------===//
1276 TargetLowering::ConstraintType
1277 TargetLowering::getConstraintType(char ConstraintLetter) const {
1278 // FIXME: lots more standard ones to handle.
1279 switch (ConstraintLetter) {
1280 default: return C_Unknown;
1281 case 'r': return C_RegisterClass;
1283 case 'o': // offsetable
1284 case 'V': // not offsetable
1286 case 'i': // Simple Integer or Relocatable Constant
1287 case 'n': // Simple Integer
1288 case 's': // Relocatable Constant
1289 case 'I': // Target registers.
1301 /// isOperandValidForConstraint - Return the specified operand (possibly
1302 /// modified) if the specified SDOperand is valid for the specified target
1303 /// constraint letter, otherwise return null.
1304 SDOperand TargetLowering::isOperandValidForConstraint(SDOperand Op,
1305 char ConstraintLetter,
1306 SelectionDAG &DAG) {
1307 switch (ConstraintLetter) {
1308 default: return SDOperand(0,0);
1309 case 'i': // Simple Integer or Relocatable Constant
1310 case 'n': // Simple Integer
1311 case 's': // Relocatable Constant
1312 return Op; // FIXME: not right.
1316 std::vector<unsigned> TargetLowering::
1317 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1318 MVT::ValueType VT) const {
1319 return std::vector<unsigned>();
1323 std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
1324 getRegForInlineAsmConstraint(const std::string &Constraint,
1325 MVT::ValueType VT) const {
1326 if (Constraint[0] != '{')
1327 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
1328 assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1330 // Remove the braces from around the name.
1331 std::string RegName(Constraint.begin()+1, Constraint.end()-1);
1333 // Figure out which register class contains this reg.
1334 const MRegisterInfo *RI = TM.getRegisterInfo();
1335 for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1336 E = RI->regclass_end(); RCI != E; ++RCI) {
1337 const TargetRegisterClass *RC = *RCI;
1339 // If none of the the value types for this register class are valid, we
1340 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1341 bool isLegal = false;
1342 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1344 if (isTypeLegal(*I)) {
1350 if (!isLegal) continue;
1352 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1354 if (StringsEqualNoCase(RegName, RI->get(*I).Name))
1355 return std::make_pair(*I, RC);
1359 return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
1362 //===----------------------------------------------------------------------===//
1363 // Loop Strength Reduction hooks
1364 //===----------------------------------------------------------------------===//
1366 /// isLegalAddressImmediate - Return true if the integer value or
1367 /// GlobalValue can be used as the offset of the target addressing mode.
1368 bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
1371 bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1376 // Magic for divide replacement
1379 int64_t m; // magic number
1380 int64_t s; // shift amount
1384 uint64_t m; // magic number
1385 int64_t a; // add indicator
1386 int64_t s; // shift amount
1389 /// magic - calculate the magic numbers required to codegen an integer sdiv as
1390 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1392 static ms magic32(int32_t d) {
1394 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
1395 const uint32_t two31 = 0x80000000U;
1399 t = two31 + ((uint32_t)d >> 31);
1400 anc = t - 1 - t%ad; // absolute value of nc
1401 p = 31; // initialize p
1402 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
1403 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1404 q2 = two31/ad; // initialize q2 = 2p/abs(d)
1405 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
1408 q1 = 2*q1; // update q1 = 2p/abs(nc)
1409 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1410 if (r1 >= anc) { // must be unsigned comparison
1414 q2 = 2*q2; // update q2 = 2p/abs(d)
1415 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1416 if (r2 >= ad) { // must be unsigned comparison
1421 } while (q1 < delta || (q1 == delta && r1 == 0));
1423 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
1424 if (d < 0) mag.m = -mag.m; // resulting magic number
1425 mag.s = p - 32; // resulting shift
1429 /// magicu - calculate the magic numbers required to codegen an integer udiv as
1430 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1431 static mu magicu32(uint32_t d) {
1433 uint32_t nc, delta, q1, r1, q2, r2;
1435 magu.a = 0; // initialize "add" indicator
1437 p = 31; // initialize p
1438 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
1439 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
1440 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
1441 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
1444 if (r1 >= nc - r1 ) {
1445 q1 = 2*q1 + 1; // update q1
1446 r1 = 2*r1 - nc; // update r1
1449 q1 = 2*q1; // update q1
1450 r1 = 2*r1; // update r1
1452 if (r2 + 1 >= d - r2) {
1453 if (q2 >= 0x7FFFFFFF) magu.a = 1;
1454 q2 = 2*q2 + 1; // update q2
1455 r2 = 2*r2 + 1 - d; // update r2
1458 if (q2 >= 0x80000000) magu.a = 1;
1459 q2 = 2*q2; // update q2
1460 r2 = 2*r2 + 1; // update r2
1463 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
1464 magu.m = q2 + 1; // resulting magic number
1465 magu.s = p - 32; // resulting shift
1469 /// magic - calculate the magic numbers required to codegen an integer sdiv as
1470 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
1472 static ms magic64(int64_t d) {
1474 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
1475 const uint64_t two63 = 9223372036854775808ULL; // 2^63
1478 ad = d >= 0 ? d : -d;
1479 t = two63 + ((uint64_t)d >> 63);
1480 anc = t - 1 - t%ad; // absolute value of nc
1481 p = 63; // initialize p
1482 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
1483 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
1484 q2 = two63/ad; // initialize q2 = 2p/abs(d)
1485 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
1488 q1 = 2*q1; // update q1 = 2p/abs(nc)
1489 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
1490 if (r1 >= anc) { // must be unsigned comparison
1494 q2 = 2*q2; // update q2 = 2p/abs(d)
1495 r2 = 2*r2; // update r2 = rem(2p/abs(d))
1496 if (r2 >= ad) { // must be unsigned comparison
1501 } while (q1 < delta || (q1 == delta && r1 == 0));
1504 if (d < 0) mag.m = -mag.m; // resulting magic number
1505 mag.s = p - 64; // resulting shift
1509 /// magicu - calculate the magic numbers required to codegen an integer udiv as
1510 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
1511 static mu magicu64(uint64_t d)
1514 uint64_t nc, delta, q1, r1, q2, r2;
1516 magu.a = 0; // initialize "add" indicator
1518 p = 63; // initialize p
1519 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
1520 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
1521 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
1522 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
1525 if (r1 >= nc - r1 ) {
1526 q1 = 2*q1 + 1; // update q1
1527 r1 = 2*r1 - nc; // update r1
1530 q1 = 2*q1; // update q1
1531 r1 = 2*r1; // update r1
1533 if (r2 + 1 >= d - r2) {
1534 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
1535 q2 = 2*q2 + 1; // update q2
1536 r2 = 2*r2 + 1 - d; // update r2
1539 if (q2 >= 0x8000000000000000ull) magu.a = 1;
1540 q2 = 2*q2; // update q2
1541 r2 = 2*r2 + 1; // update r2
1544 } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0)));
1545 magu.m = q2 + 1; // resulting magic number
1546 magu.s = p - 64; // resulting shift
1550 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
1551 /// return a DAG expression to select that will generate the same value by
1552 /// multiplying by a magic number. See:
1553 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1554 SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
1555 std::vector<SDNode*>* Created) const {
1556 MVT::ValueType VT = N->getValueType(0);
1558 // Check to see if we can do this.
1559 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1560 return SDOperand(); // BuildSDIV only operates on i32 or i64
1561 if (!isOperationLegal(ISD::MULHS, VT))
1562 return SDOperand(); // Make sure the target supports MULHS.
1564 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
1565 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
1567 // Multiply the numerator (operand 0) by the magic value
1568 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
1569 DAG.getConstant(magics.m, VT));
1570 // If d > 0 and m < 0, add the numerator
1571 if (d > 0 && magics.m < 0) {
1572 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
1574 Created->push_back(Q.Val);
1576 // If d < 0 and m > 0, subtract the numerator.
1577 if (d < 0 && magics.m > 0) {
1578 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
1580 Created->push_back(Q.Val);
1582 // Shift right algebraic if shift value is nonzero
1584 Q = DAG.getNode(ISD::SRA, VT, Q,
1585 DAG.getConstant(magics.s, getShiftAmountTy()));
1587 Created->push_back(Q.Val);
1589 // Extract the sign bit and add it to the quotient
1591 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
1592 getShiftAmountTy()));
1594 Created->push_back(T.Val);
1595 return DAG.getNode(ISD::ADD, VT, Q, T);
1598 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
1599 /// return a DAG expression to select that will generate the same value by
1600 /// multiplying by a magic number. See:
1601 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
1602 SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
1603 std::vector<SDNode*>* Created) const {
1604 MVT::ValueType VT = N->getValueType(0);
1606 // Check to see if we can do this.
1607 if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
1608 return SDOperand(); // BuildUDIV only operates on i32 or i64
1609 if (!isOperationLegal(ISD::MULHU, VT))
1610 return SDOperand(); // Make sure the target supports MULHU.
1612 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1613 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
1615 // Multiply the numerator (operand 0) by the magic value
1616 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
1617 DAG.getConstant(magics.m, VT));
1619 Created->push_back(Q.Val);
1621 if (magics.a == 0) {
1622 return DAG.getNode(ISD::SRL, VT, Q,
1623 DAG.getConstant(magics.s, getShiftAmountTy()));
1625 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
1627 Created->push_back(NPQ.Val);
1628 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
1629 DAG.getConstant(1, getShiftAmountTy()));
1631 Created->push_back(NPQ.Val);
1632 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
1634 Created->push_back(NPQ.Val);
1635 return DAG.getNode(ISD::SRL, VT, NPQ,
1636 DAG.getConstant(magics.s-1, getShiftAmountTy()));