When tracking demanded bits, if any bits from the sext of an SRA are demanded,
[oota-llvm.git] / lib / CodeGen / SelectionDAG / TargetLowering.cpp
1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the TargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetLowering.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/Target/MRegisterInfo.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/MathExtras.h"
21 using namespace llvm;
22
23 TargetLowering::TargetLowering(TargetMachine &tm)
24   : TM(tm), TD(TM.getTargetData()) {
25   assert(ISD::BUILTIN_OP_END <= 156 &&
26          "Fixed size array in TargetLowering is not large enough!");
27   // All operations default to being supported.
28   memset(OpActions, 0, sizeof(OpActions));
29
30   IsLittleEndian = TD->isLittleEndian();
31   ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType());
32   ShiftAmtHandling = Undefined;
33   memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
34   memset(TargetDAGCombineArray, 0, 
35          sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0]));
36   maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
37   allowUnalignedMemoryAccesses = false;
38   UseUnderscoreSetJmpLongJmp = false;
39   IntDivIsCheap = false;
40   Pow2DivIsCheap = false;
41   StackPointerRegisterToSaveRestore = 0;
42   SchedPreferenceInfo = SchedulingForLatency;
43 }
44
45 TargetLowering::~TargetLowering() {}
46
47 /// setValueTypeAction - Set the action for a particular value type.  This
48 /// assumes an action has not already been set for this value type.
49 static void SetValueTypeAction(MVT::ValueType VT,
50                                TargetLowering::LegalizeAction Action,
51                                TargetLowering &TLI,
52                                MVT::ValueType *TransformToType,
53                         TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
54   ValueTypeActions.setTypeAction(VT, Action);
55   if (Action == TargetLowering::Promote) {
56     MVT::ValueType PromoteTo;
57     if (VT == MVT::f32)
58       PromoteTo = MVT::f64;
59     else {
60       unsigned LargerReg = VT+1;
61       while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
62         ++LargerReg;
63         assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
64                "Nothing to promote to??");
65       }
66       PromoteTo = (MVT::ValueType)LargerReg;
67     }
68
69     assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
70            MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
71            "Can only promote from int->int or fp->fp!");
72     assert(VT < PromoteTo && "Must promote to a larger type!");
73     TransformToType[VT] = PromoteTo;
74   } else if (Action == TargetLowering::Expand) {
75     assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
76            "Cannot expand this type: target must support SOME integer reg!");
77     // Expand to the next smaller integer type!
78     TransformToType[VT] = (MVT::ValueType)(VT-1);
79   }
80 }
81
82
83 /// computeRegisterProperties - Once all of the register classes are added,
84 /// this allows us to compute derived properties we expose.
85 void TargetLowering::computeRegisterProperties() {
86   assert(MVT::LAST_VALUETYPE <= 32 &&
87          "Too many value types for ValueTypeActions to hold!");
88
89   // Everything defaults to one.
90   for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
91     NumElementsForVT[i] = 1;
92
93   // Find the largest integer register class.
94   unsigned LargestIntReg = MVT::i128;
95   for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
96     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
97
98   // Every integer value type larger than this largest register takes twice as
99   // many registers to represent as the previous ValueType.
100   unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
101   for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
102     NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
103
104   // Inspect all of the ValueType's possible, deciding how to process them.
105   for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
106     // If we are expanding this type, expand it!
107     if (getNumElements((MVT::ValueType)IntReg) != 1)
108       SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
109                          ValueTypeActions);
110     else if (!isTypeLegal((MVT::ValueType)IntReg))
111       // Otherwise, if we don't have native support, we must promote to a
112       // larger type.
113       SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
114                          TransformToType, ValueTypeActions);
115     else
116       TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
117
118   // If the target does not have native support for F32, promote it to F64.
119   if (!isTypeLegal(MVT::f32))
120     SetValueTypeAction(MVT::f32, Promote, *this,
121                        TransformToType, ValueTypeActions);
122   else
123     TransformToType[MVT::f32] = MVT::f32;
124   
125   // Set MVT::Vector to always be Expanded
126   SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType, 
127                      ValueTypeActions);
128   
129   // Loop over all of the legal vector value types, specifying an identity type
130   // transformation.
131   for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
132        i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
133     if (isTypeLegal((MVT::ValueType)i))
134       TransformToType[i] = (MVT::ValueType)i;
135   }
136
137   assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
138   TransformToType[MVT::f64] = MVT::f64;
139 }
140
141 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
142   return NULL;
143 }
144
145 /// getPackedTypeBreakdown - Packed types are broken down into some number of
146 /// legal scalar types.  For example, <8 x float> maps to 2 MVT::v2f32 values
147 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
148 ///
149 /// This method returns the number and type of the resultant breakdown.
150 ///
151 unsigned TargetLowering::getPackedTypeBreakdown(const PackedType *PTy, 
152                                                 MVT::ValueType &PTyElementVT,
153                                       MVT::ValueType &PTyLegalElementVT) const {
154   // Figure out the right, legal destination reg to copy into.
155   unsigned NumElts = PTy->getNumElements();
156   MVT::ValueType EltTy = getValueType(PTy->getElementType());
157   
158   unsigned NumVectorRegs = 1;
159   
160   // Divide the input until we get to a supported size.  This will always
161   // end with a scalar if the target doesn't support vectors.
162   while (NumElts > 1 && !isTypeLegal(getVectorType(EltTy, NumElts))) {
163     NumElts >>= 1;
164     NumVectorRegs <<= 1;
165   }
166   
167   MVT::ValueType VT;
168   if (NumElts == 1) {
169     VT = EltTy;
170   } else {
171     VT = getVectorType(EltTy, NumElts); 
172   }
173   PTyElementVT = VT;
174
175   MVT::ValueType DestVT = getTypeToTransformTo(VT);
176   PTyLegalElementVT = DestVT;
177   if (DestVT < VT) {
178     // Value is expanded, e.g. i64 -> i16.
179     return NumVectorRegs*(MVT::getSizeInBits(VT)/MVT::getSizeInBits(DestVT));
180   } else {
181     // Otherwise, promotion or legal types use the same number of registers as
182     // the vector decimated to the appropriate level.
183     return NumVectorRegs;
184   }
185   
186   return DestVT;
187 }
188
189 //===----------------------------------------------------------------------===//
190 //  Optimization Methods
191 //===----------------------------------------------------------------------===//
192
193 /// ShrinkDemandedConstant - Check to see if the specified operand of the 
194 /// specified instruction is a constant integer.  If so, check to see if there
195 /// are any bits set in the constant that are not demanded.  If so, shrink the
196 /// constant and return true.
197 bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op, 
198                                                             uint64_t Demanded) {
199   // FIXME: ISD::SELECT, ISD::SELECT_CC
200   switch(Op.getOpcode()) {
201   default: break;
202   case ISD::AND:
203   case ISD::OR:
204   case ISD::XOR:
205     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
206       if ((~Demanded & C->getValue()) != 0) {
207         MVT::ValueType VT = Op.getValueType();
208         SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0),
209                                     DAG.getConstant(Demanded & C->getValue(), 
210                                                     VT));
211         return CombineTo(Op, New);
212       }
213     break;
214   }
215   return false;
216 }
217
218 /// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
219 /// DemandedMask bits of the result of Op are ever used downstream.  If we can
220 /// use this information to simplify Op, create a new simplified DAG node and
221 /// return true, returning the original and new nodes in Old and New. Otherwise,
222 /// analyze the expression and return a mask of KnownOne and KnownZero bits for
223 /// the expression (used to simplify the caller).  The KnownZero/One bits may
224 /// only be accurate for those bits in the DemandedMask.
225 bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask, 
226                                           uint64_t &KnownZero,
227                                           uint64_t &KnownOne,
228                                           TargetLoweringOpt &TLO,
229                                           unsigned Depth) const {
230   KnownZero = KnownOne = 0;   // Don't know anything.
231   // Other users may use these bits.
232   if (!Op.Val->hasOneUse()) { 
233     if (Depth != 0) {
234       // If not at the root, Just compute the KnownZero/KnownOne bits to 
235       // simplify things downstream.
236       ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
237       return false;
238     }
239     // If this is the root being simplified, allow it to have multiple uses,
240     // just set the DemandedMask to all bits.
241     DemandedMask = MVT::getIntVTBitMask(Op.getValueType());
242   } else if (DemandedMask == 0) {   
243     // Not demanding any bits from Op.
244     if (Op.getOpcode() != ISD::UNDEF)
245       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType()));
246     return false;
247   } else if (Depth == 6) {        // Limit search depth.
248     return false;
249   }
250
251   uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
252   switch (Op.getOpcode()) {
253   case ISD::Constant:
254     // We know all of the bits for a constant!
255     KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask;
256     KnownZero = ~KnownOne & DemandedMask;
257     return false;   // Don't fall through, will infinitely loop.
258   case ISD::AND:
259     // If the RHS is a constant, check to see if the LHS would be zero without
260     // using the bits from the RHS.  Below, we use knowledge about the RHS to
261     // simplify the LHS, here we're using information from the LHS to simplify
262     // the RHS.
263     if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
264       uint64_t LHSZero, LHSOne;
265       ComputeMaskedBits(Op.getOperand(0), DemandedMask,
266                         LHSZero, LHSOne, Depth+1);
267       // If the LHS already has zeros where RHSC does, this and is dead.
268       if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask))
269         return TLO.CombineTo(Op, Op.getOperand(0));
270       // If any of the set bits in the RHS are known zero on the LHS, shrink
271       // the constant.
272       if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask))
273         return true;
274     }
275     
276     if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero,
277                              KnownOne, TLO, Depth+1))
278       return true;
279     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
280     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero,
281                              KnownZero2, KnownOne2, TLO, Depth+1))
282       return true;
283     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
284       
285     // If all of the demanded bits are known one on one side, return the other.
286     // These bits cannot contribute to the result of the 'and'.
287     if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2))
288       return TLO.CombineTo(Op, Op.getOperand(0));
289     if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero))
290       return TLO.CombineTo(Op, Op.getOperand(1));
291     // If all of the demanded bits in the inputs are known zeros, return zero.
292     if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask)
293       return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
294     // If the RHS is a constant, see if we can simplify it.
295     if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2))
296       return true;
297       
298     // Output known-1 bits are only known if set in both the LHS & RHS.
299     KnownOne &= KnownOne2;
300     // Output known-0 are known to be clear if zero in either the LHS | RHS.
301     KnownZero |= KnownZero2;
302     break;
303   case ISD::OR:
304     if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero, 
305                              KnownOne, TLO, Depth+1))
306       return true;
307     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
308     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne, 
309                              KnownZero2, KnownOne2, TLO, Depth+1))
310       return true;
311     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
312     
313     // If all of the demanded bits are known zero on one side, return the other.
314     // These bits cannot contribute to the result of the 'or'.
315     if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2))
316       return TLO.CombineTo(Op, Op.getOperand(0));
317     if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne))
318       return TLO.CombineTo(Op, Op.getOperand(1));
319     // If all of the potentially set bits on one side are known to be set on
320     // the other side, just use the 'other' side.
321     if ((DemandedMask & (~KnownZero) & KnownOne2) == 
322         (DemandedMask & (~KnownZero)))
323       return TLO.CombineTo(Op, Op.getOperand(0));
324     if ((DemandedMask & (~KnownZero2) & KnownOne) == 
325         (DemandedMask & (~KnownZero2)))
326       return TLO.CombineTo(Op, Op.getOperand(1));
327     // If the RHS is a constant, see if we can simplify it.
328     if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
329       return true;
330           
331     // Output known-0 bits are only known if clear in both the LHS & RHS.
332     KnownZero &= KnownZero2;
333     // Output known-1 are known to be set if set in either the LHS | RHS.
334     KnownOne |= KnownOne2;
335     break;
336   case ISD::XOR:
337     if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero, 
338                              KnownOne, TLO, Depth+1))
339       return true;
340     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
341     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2,
342                              KnownOne2, TLO, Depth+1))
343       return true;
344     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
345     
346     // If all of the demanded bits are known zero on one side, return the other.
347     // These bits cannot contribute to the result of the 'xor'.
348     if ((DemandedMask & KnownZero) == DemandedMask)
349       return TLO.CombineTo(Op, Op.getOperand(0));
350     if ((DemandedMask & KnownZero2) == DemandedMask)
351       return TLO.CombineTo(Op, Op.getOperand(1));
352     
353     // Output known-0 bits are known if clear or set in both the LHS & RHS.
354     KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
355     // Output known-1 are known to be set if set in only one of the LHS, RHS.
356     KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
357     
358     // If all of the unknown bits are known to be zero on one side or the other
359     // (but not both) turn this into an *inclusive* or.
360     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
361     if (uint64_t UnknownBits = DemandedMask & ~(KnownZeroOut|KnownOneOut))
362       if ((UnknownBits & (KnownZero|KnownZero2)) == UnknownBits)
363         return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(),
364                                                  Op.getOperand(0),
365                                                  Op.getOperand(1)));
366     // If all of the demanded bits on one side are known, and all of the set
367     // bits on that side are also known to be set on the other side, turn this
368     // into an AND, as we know the bits will be cleared.
369     //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
370     if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known
371       if ((KnownOne & KnownOne2) == KnownOne) {
372         MVT::ValueType VT = Op.getValueType();
373         SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT);
374         return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0),
375                                                  ANDC));
376       }
377     }
378     
379     // If the RHS is a constant, see if we can simplify it.
380     // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
381     if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
382       return true;
383     
384     KnownZero = KnownZeroOut;
385     KnownOne  = KnownOneOut;
386     break;
387   case ISD::SETCC:
388     // If we know the result of a setcc has the top bits zero, use this info.
389     if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
390       KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
391     break;
392   case ISD::SELECT:
393     if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero, 
394                              KnownOne, TLO, Depth+1))
395       return true;
396     if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2,
397                              KnownOne2, TLO, Depth+1))
398       return true;
399     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
400     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
401     
402     // If the operands are constants, see if we can simplify them.
403     if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
404       return true;
405     
406     // Only known if known in both the LHS and RHS.
407     KnownOne &= KnownOne2;
408     KnownZero &= KnownZero2;
409     break;
410   case ISD::SELECT_CC:
411     if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero, 
412                              KnownOne, TLO, Depth+1))
413       return true;
414     if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2,
415                              KnownOne2, TLO, Depth+1))
416       return true;
417     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
418     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
419     
420     // If the operands are constants, see if we can simplify them.
421     if (TLO.ShrinkDemandedConstant(Op, DemandedMask))
422       return true;
423       
424     // Only known if known in both the LHS and RHS.
425     KnownOne &= KnownOne2;
426     KnownZero &= KnownZero2;
427     break;
428   case ISD::SHL:
429     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
430       if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> SA->getValue(),
431                                KnownZero, KnownOne, TLO, Depth+1))
432         return true;
433       KnownZero <<= SA->getValue();
434       KnownOne  <<= SA->getValue();
435       KnownZero |= (1ULL << SA->getValue())-1;  // low bits known zero.
436     }
437     break;
438   case ISD::SRL:
439     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
440       MVT::ValueType VT = Op.getValueType();
441       unsigned ShAmt = SA->getValue();
442       
443       // Compute the new bits that are at the top now.
444       uint64_t HighBits = (1ULL << ShAmt)-1;
445       HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
446       uint64_t TypeMask = MVT::getIntVTBitMask(VT);
447       
448       if (SimplifyDemandedBits(Op.getOperand(0), 
449                                (DemandedMask << ShAmt) & TypeMask,
450                                KnownZero, KnownOne, TLO, Depth+1))
451         return true;
452       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
453       KnownZero &= TypeMask;
454       KnownOne  &= TypeMask;
455       KnownZero >>= ShAmt;
456       KnownOne  >>= ShAmt;
457       KnownZero |= HighBits;  // high bits known zero.
458     }
459     break;
460   case ISD::SRA:
461     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
462       MVT::ValueType VT = Op.getValueType();
463       unsigned ShAmt = SA->getValue();
464       
465       // Compute the new bits that are at the top now.
466       uint64_t HighBits = (1ULL << ShAmt)-1;
467       HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
468       uint64_t TypeMask = MVT::getIntVTBitMask(VT);
469       
470       uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask;
471
472       // If any of the demanded bits are produced by the sign extension, we also
473       // demand the input sign bit.
474       if (HighBits & DemandedMask)
475         InDemandedMask |= MVT::getIntVTSignBit(VT);
476       
477       if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
478                                KnownZero, KnownOne, TLO, Depth+1))
479         return true;
480       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
481       KnownZero &= TypeMask;
482       KnownOne  &= TypeMask;
483       KnownZero >>= SA->getValue();
484       KnownOne  >>= SA->getValue();
485       
486       // Handle the sign bits.
487       uint64_t SignBit = MVT::getIntVTSignBit(VT);
488       SignBit >>= SA->getValue();  // Adjust to where it is now in the mask.
489       
490       // If the input sign bit is known to be zero, or if none of the top bits
491       // are demanded, turn this into an unsigned shift right.
492       if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
493         return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0),
494                                                  Op.getOperand(1)));
495       } else if (KnownOne & SignBit) { // New bits are known one.
496         KnownOne |= HighBits;
497       }
498     }
499     break;
500   case ISD::SIGN_EXTEND_INREG: {
501     MVT::ValueType  VT = Op.getValueType();
502     MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
503
504     // Sign extension.  Compute the demanded bits in the result that are not 
505     // present in the input.
506     uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask;
507     
508     // If none of the extended bits are demanded, eliminate the sextinreg.
509     if (NewBits == 0)
510       return TLO.CombineTo(Op, Op.getOperand(0));
511
512     uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
513     int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT);
514     
515     // Since the sign extended bits are demanded, we know that the sign
516     // bit is demanded.
517     InputDemandedBits |= InSignBit;
518
519     if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
520                              KnownZero, KnownOne, TLO, Depth+1))
521       return true;
522     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
523
524     // If the sign bit of the input is known set or clear, then we know the
525     // top bits of the result.
526     
527     // If the input sign bit is known zero, convert this into a zero extension.
528     if (KnownZero & InSignBit)
529       return TLO.CombineTo(Op, 
530                            TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT));
531     
532     if (KnownOne & InSignBit) {    // Input sign bit known set
533       KnownOne |= NewBits;
534       KnownZero &= ~NewBits;
535     } else {                       // Input sign bit unknown
536       KnownZero &= ~NewBits;
537       KnownOne &= ~NewBits;
538     }
539     break;
540   }
541   case ISD::CTTZ:
542   case ISD::CTLZ:
543   case ISD::CTPOP: {
544     MVT::ValueType VT = Op.getValueType();
545     unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
546     KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
547     KnownOne  = 0;
548     break;
549   }
550   case ISD::ZEXTLOAD: {
551     MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
552     KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask;
553     break;
554   }
555   case ISD::ZERO_EXTEND: {
556     uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
557     
558     // If none of the top bits are demanded, convert this into an any_extend.
559     uint64_t NewBits = (~InMask) & DemandedMask;
560     if (NewBits == 0)
561       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, 
562                                                Op.getValueType(), 
563                                                Op.getOperand(0)));
564     
565     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
566                              KnownZero, KnownOne, TLO, Depth+1))
567       return true;
568     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
569     KnownZero |= NewBits;
570     break;
571   }
572   case ISD::SIGN_EXTEND: {
573     MVT::ValueType InVT = Op.getOperand(0).getValueType();
574     uint64_t InMask    = MVT::getIntVTBitMask(InVT);
575     uint64_t InSignBit = MVT::getIntVTSignBit(InVT);
576     uint64_t NewBits   = (~InMask) & DemandedMask;
577     
578     // If none of the top bits are demanded, convert this into an any_extend.
579     if (NewBits == 0)
580       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(),
581                                            Op.getOperand(0)));
582     
583     // Since some of the sign extended bits are demanded, we know that the sign
584     // bit is demanded.
585     uint64_t InDemandedBits = DemandedMask & InMask;
586     InDemandedBits |= InSignBit;
587     
588     if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero, 
589                              KnownOne, TLO, Depth+1))
590       return true;
591     
592     // If the sign bit is known zero, convert this to a zero extend.
593     if (KnownZero & InSignBit)
594       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, 
595                                                Op.getValueType(), 
596                                                Op.getOperand(0)));
597     
598     // If the sign bit is known one, the top bits match.
599     if (KnownOne & InSignBit) {
600       KnownOne  |= NewBits;
601       KnownZero &= ~NewBits;
602     } else {   // Otherwise, top bits aren't known.
603       KnownOne  &= ~NewBits;
604       KnownZero &= ~NewBits;
605     }
606     break;
607   }
608   case ISD::ANY_EXTEND: {
609     uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
610     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
611                              KnownZero, KnownOne, TLO, Depth+1))
612       return true;
613     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
614     break;
615   }
616   case ISD::TRUNCATE: {
617     // Simplify the input, using demanded bit information, and compute the known
618     // zero/one bits live out.
619     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask,
620                              KnownZero, KnownOne, TLO, Depth+1))
621       return true;
622     
623     // If the input is only used by this truncate, see if we can shrink it based
624     // on the known demanded bits.
625     if (Op.getOperand(0).Val->hasOneUse()) {
626       SDOperand In = Op.getOperand(0);
627       switch (In.getOpcode()) {
628       default: break;
629       case ISD::SRL:
630         // Shrink SRL by a constant if none of the high bits shifted in are
631         // demanded.
632         if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){
633           uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType());
634           HighBits &= ~MVT::getIntVTBitMask(Op.getValueType());
635           HighBits >>= ShAmt->getValue();
636           
637           if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) &&
638               (DemandedMask & HighBits) == 0) {
639             // None of the shifted in bits are needed.  Add a truncate of the
640             // shift input, then shift it.
641             SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, 
642                                                  Op.getValueType(), 
643                                                  In.getOperand(0));
644             return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(),
645                                                    NewTrunc, In.getOperand(1)));
646           }
647         }
648         break;
649       }
650     }
651     
652     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
653     uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
654     KnownZero &= OutMask;
655     KnownOne &= OutMask;
656     break;
657   }
658   case ISD::AssertZext: {
659     MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
660     uint64_t InMask = MVT::getIntVTBitMask(VT);
661     if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask,
662                              KnownZero, KnownOne, TLO, Depth+1))
663       return true;
664     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
665     KnownZero |= ~InMask & DemandedMask;
666     break;
667   }
668   case ISD::ADD:
669   case ISD::SUB:
670   case ISD::INTRINSIC_WO_CHAIN:
671   case ISD::INTRINSIC_W_CHAIN:
672   case ISD::INTRINSIC_VOID:
673     // Just use ComputeMaskedBits to compute output bits.
674     ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth);
675     break;
676   }
677   
678   // If we know the value of all of the demanded bits, return this as a
679   // constant.
680   if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
681     return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
682   
683   return false;
684 }
685
686 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
687 /// this predicate to simplify operations downstream.  Mask is known to be zero
688 /// for bits that V cannot have.
689 bool TargetLowering::MaskedValueIsZero(SDOperand Op, uint64_t Mask, 
690                                        unsigned Depth) const {
691   uint64_t KnownZero, KnownOne;
692   ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
693   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
694   return (KnownZero & Mask) == Mask;
695 }
696
697 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
698 /// known to be either zero or one and return them in the KnownZero/KnownOne
699 /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
700 /// processing.
701 void TargetLowering::ComputeMaskedBits(SDOperand Op, uint64_t Mask, 
702                                        uint64_t &KnownZero, uint64_t &KnownOne,
703                                        unsigned Depth) const {
704   KnownZero = KnownOne = 0;   // Don't know anything.
705   if (Depth == 6 || Mask == 0)
706     return;  // Limit search depth.
707   
708   uint64_t KnownZero2, KnownOne2;
709
710   switch (Op.getOpcode()) {
711   case ISD::Constant:
712     // We know all of the bits for a constant!
713     KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
714     KnownZero = ~KnownOne & Mask;
715     return;
716   case ISD::AND:
717     // If either the LHS or the RHS are Zero, the result is zero.
718     ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
719     Mask &= ~KnownZero;
720     ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
721     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
722     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
723
724     // Output known-1 bits are only known if set in both the LHS & RHS.
725     KnownOne &= KnownOne2;
726     // Output known-0 are known to be clear if zero in either the LHS | RHS.
727     KnownZero |= KnownZero2;
728     return;
729   case ISD::OR:
730     ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
731     Mask &= ~KnownOne;
732     ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
733     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
734     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
735     
736     // Output known-0 bits are only known if clear in both the LHS & RHS.
737     KnownZero &= KnownZero2;
738     // Output known-1 are known to be set if set in either the LHS | RHS.
739     KnownOne |= KnownOne2;
740     return;
741   case ISD::XOR: {
742     ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
743     ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
744     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
745     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
746     
747     // Output known-0 bits are known if clear or set in both the LHS & RHS.
748     uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
749     // Output known-1 are known to be set if set in only one of the LHS, RHS.
750     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
751     KnownZero = KnownZeroOut;
752     return;
753   }
754   case ISD::SELECT:
755     ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
756     ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
757     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
758     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
759     
760     // Only known if known in both the LHS and RHS.
761     KnownOne &= KnownOne2;
762     KnownZero &= KnownZero2;
763     return;
764   case ISD::SELECT_CC:
765     ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
766     ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
767     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
768     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
769     
770     // Only known if known in both the LHS and RHS.
771     KnownOne &= KnownOne2;
772     KnownZero &= KnownZero2;
773     return;
774   case ISD::SETCC:
775     // If we know the result of a setcc has the top bits zero, use this info.
776     if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
777       KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
778     return;
779   case ISD::SHL:
780     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
781     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
782       Mask >>= SA->getValue();
783       ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
784       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
785       KnownZero <<= SA->getValue();
786       KnownOne  <<= SA->getValue();
787       KnownZero |= (1ULL << SA->getValue())-1;  // low bits known zero.
788     }
789     return;
790   case ISD::SRL:
791     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
792     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
793       uint64_t HighBits = (1ULL << SA->getValue())-1;
794       HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
795       Mask <<= SA->getValue();
796       ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
797       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
798       KnownZero >>= SA->getValue();
799       KnownOne  >>= SA->getValue();
800       KnownZero |= HighBits;  // high bits known zero.
801     }
802     return;
803   case ISD::SRA:
804     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
805       uint64_t HighBits = (1ULL << SA->getValue())-1;
806       HighBits <<= MVT::getSizeInBits(Op.getValueType())-SA->getValue();
807       Mask <<= SA->getValue();
808       ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
809       assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
810       KnownZero >>= SA->getValue();
811       KnownOne  >>= SA->getValue();
812       
813       // Handle the sign bits.
814       uint64_t SignBit = 1ULL << (MVT::getSizeInBits(Op.getValueType())-1);
815       SignBit >>= SA->getValue();  // Adjust to where it is now in the mask.
816       
817       if (KnownZero & SignBit) {       // New bits are known zero.
818         KnownZero |= HighBits;
819       } else if (KnownOne & SignBit) { // New bits are known one.
820         KnownOne |= HighBits;
821       }
822     }
823     return;
824   case ISD::SIGN_EXTEND_INREG: {
825     MVT::ValueType  VT = Op.getValueType();
826     MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
827     
828     // Sign extension.  Compute the demanded bits in the result that are not 
829     // present in the input.
830     uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
831
832     uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
833     int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
834     
835     // If the sign extended bits are demanded, we know that the sign
836     // bit is demanded.
837     if (NewBits)
838       InputDemandedBits |= InSignBit;
839     
840     ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
841                       KnownZero, KnownOne, Depth+1);
842     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
843     
844     // If the sign bit of the input is known set or clear, then we know the
845     // top bits of the result.
846     if (KnownZero & InSignBit) {          // Input sign bit known clear
847       KnownZero |= NewBits;
848       KnownOne  &= ~NewBits;
849     } else if (KnownOne & InSignBit) {    // Input sign bit known set
850       KnownOne  |= NewBits;
851       KnownZero &= ~NewBits;
852     } else {                              // Input sign bit unknown
853       KnownZero &= ~NewBits;
854       KnownOne  &= ~NewBits;
855     }
856     return;
857   }
858   case ISD::CTTZ:
859   case ISD::CTLZ:
860   case ISD::CTPOP: {
861     MVT::ValueType VT = Op.getValueType();
862     unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
863     KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
864     KnownOne  = 0;
865     return;
866   }
867   case ISD::ZEXTLOAD: {
868     MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(3))->getVT();
869     KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
870     return;
871   }
872   case ISD::ZERO_EXTEND: {
873     uint64_t InMask  = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
874     uint64_t NewBits = (~InMask) & Mask;
875     ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 
876                       KnownOne, Depth+1);
877     KnownZero |= NewBits & Mask;
878     KnownOne  &= ~NewBits;
879     return;
880   }
881   case ISD::SIGN_EXTEND: {
882     MVT::ValueType InVT = Op.getOperand(0).getValueType();
883     unsigned InBits    = MVT::getSizeInBits(InVT);
884     uint64_t InMask    = MVT::getIntVTBitMask(InVT);
885     uint64_t InSignBit = 1ULL << (InBits-1);
886     uint64_t NewBits   = (~InMask) & Mask;
887     uint64_t InDemandedBits = Mask & InMask;
888
889     // If any of the sign extended bits are demanded, we know that the sign
890     // bit is demanded.
891     if (NewBits & Mask)
892       InDemandedBits |= InSignBit;
893     
894     ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero, 
895                       KnownOne, Depth+1);
896     // If the sign bit is known zero or one, the  top bits match.
897     if (KnownZero & InSignBit) {
898       KnownZero |= NewBits;
899       KnownOne  &= ~NewBits;
900     } else if (KnownOne & InSignBit) {
901       KnownOne  |= NewBits;
902       KnownZero &= ~NewBits;
903     } else {   // Otherwise, top bits aren't known.
904       KnownOne  &= ~NewBits;
905       KnownZero &= ~NewBits;
906     }
907     return;
908   }
909   case ISD::ANY_EXTEND: {
910     MVT::ValueType VT = Op.getOperand(0).getValueType();
911     ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
912                       KnownZero, KnownOne, Depth+1);
913     return;
914   }
915   case ISD::TRUNCATE: {
916     ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
917     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
918     uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
919     KnownZero &= OutMask;
920     KnownOne &= OutMask;
921     break;
922   }
923   case ISD::AssertZext: {
924     MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
925     uint64_t InMask = MVT::getIntVTBitMask(VT);
926     ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero, 
927                       KnownOne, Depth+1);
928     KnownZero |= (~InMask) & Mask;
929     return;
930   }
931   case ISD::ADD: {
932     // If either the LHS or the RHS are Zero, the result is zero.
933     ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
934     ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
935     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
936     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
937     
938     // Output known-0 bits are known if clear or set in both the low clear bits
939     // common to both LHS & RHS.  For example, 8+(X<<3) is known to have the
940     // low 3 bits clear.
941     uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero), 
942                                      CountTrailingZeros_64(~KnownZero2));
943     
944     KnownZero = (1ULL << KnownZeroOut) - 1;
945     KnownOne = 0;
946     return;
947   }
948   case ISD::SUB: {
949     ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
950     if (!CLHS) return;
951
952     // We know that the top bits of C-X are clear if X contains less bits
953     // than C (i.e. no wrap-around can happen).  For example, 20-X is
954     // positive if we can prove that X is >= 0 and < 16.
955     MVT::ValueType VT = CLHS->getValueType(0);
956     if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) {  // sign bit clear
957       unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
958       uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
959       MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
960       ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
961
962       // If all of the MaskV bits are known to be zero, then we know the output
963       // top bits are zero, because we now know that the output is from [0-C].
964       if ((KnownZero & MaskV) == MaskV) {
965         unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
966         KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask;  // Top bits known zero.
967         KnownOne = 0;   // No one bits known.
968       } else {
969         KnownOne = KnownOne = 0;  // Otherwise, nothing known.
970       }
971     }
972     return;
973   }
974   default:
975     // Allow the target to implement this method for its nodes.
976     if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
977   case ISD::INTRINSIC_WO_CHAIN:
978   case ISD::INTRINSIC_W_CHAIN:
979   case ISD::INTRINSIC_VOID:
980       computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne);
981     }
982     return;
983   }
984 }
985
986 /// computeMaskedBitsForTargetNode - Determine which of the bits specified 
987 /// in Mask are known to be either zero or one and return them in the 
988 /// KnownZero/KnownOne bitsets.
989 void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, 
990                                                     uint64_t Mask,
991                                                     uint64_t &KnownZero, 
992                                                     uint64_t &KnownOne,
993                                                     unsigned Depth) const {
994   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
995           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
996           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
997           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
998          "Should use MaskedValueIsZero if you don't know whether Op"
999          " is a target node!");
1000   KnownZero = 0;
1001   KnownOne = 0;
1002 }
1003
1004 /// ComputeNumSignBits - Return the number of times the sign bit of the
1005 /// register is replicated into the other bits.  We know that at least 1 bit
1006 /// is always equal to the sign bit (itself), but other cases can give us
1007 /// information.  For example, immediately after an "SRA X, 2", we know that
1008 /// the top 3 bits are all equal to each other, so we return 3.
1009 unsigned TargetLowering::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1010   MVT::ValueType VT = Op.getValueType();
1011   assert(MVT::isInteger(VT) && "Invalid VT!");
1012   unsigned VTBits = MVT::getSizeInBits(VT);
1013   unsigned Tmp, Tmp2;
1014   
1015   if (Depth == 6)
1016     return 1;  // Limit search depth.
1017
1018   switch (Op.getOpcode()) {
1019   default: break;
1020   case ISD::AssertSext:
1021     Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1022     return VTBits-Tmp+1;
1023   case ISD::AssertZext:
1024     Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1025     return VTBits-Tmp;
1026
1027   case ISD::SEXTLOAD:    // '17' bits known
1028     Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
1029     return VTBits-Tmp+1;
1030   case ISD::ZEXTLOAD:    // '16' bits known
1031     Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
1032     return VTBits-Tmp;
1033     
1034   case ISD::Constant: {
1035     uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1036     // If negative, invert the bits, then look at it.
1037     if (Val & MVT::getIntVTSignBit(VT))
1038       Val = ~Val;
1039     
1040     // Shift the bits so they are the leading bits in the int64_t.
1041     Val <<= 64-VTBits;
1042     
1043     // Return # leading zeros.  We use 'min' here in case Val was zero before
1044     // shifting.  We don't want to return '64' as for an i32 "0".
1045     return std::min(VTBits, CountLeadingZeros_64(Val));
1046   }
1047     
1048   case ISD::SIGN_EXTEND:
1049     Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1050     return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1051     
1052   case ISD::SIGN_EXTEND_INREG:
1053     // Max of the input and what this extends.
1054     Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1055     Tmp = VTBits-Tmp+1;
1056     
1057     Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1058     return std::max(Tmp, Tmp2);
1059
1060   case ISD::SRA:
1061     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1062     // SRA X, C   -> adds C sign bits.
1063     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1064       Tmp += C->getValue();
1065       if (Tmp > VTBits) Tmp = VTBits;
1066     }
1067     return Tmp;
1068   case ISD::SHL:
1069     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1070       // shl destroys sign bits.
1071       Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1072       if (C->getValue() >= VTBits ||      // Bad shift.
1073           C->getValue() >= Tmp) break;    // Shifted all sign bits out.
1074       return Tmp - C->getValue();
1075     }
1076     break;
1077   case ISD::AND:
1078   case ISD::OR:
1079   case ISD::XOR:    // NOT is handled here.
1080     // Logical binary ops preserve the number of sign bits.
1081     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1082     if (Tmp == 1) return 1;  // Early out.
1083     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1084     return std::min(Tmp, Tmp2);
1085
1086   case ISD::SELECT:
1087     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1088     if (Tmp == 1) return 1;  // Early out.
1089     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1090     return std::min(Tmp, Tmp2);
1091     
1092   case ISD::SETCC:
1093     // If setcc returns 0/-1, all bits are sign bits.
1094     if (getSetCCResultContents() == ZeroOrNegativeOneSetCCResult)
1095       return VTBits;
1096     break;
1097   case ISD::ROTL:
1098   case ISD::ROTR:
1099     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1100       unsigned RotAmt = C->getValue() & (VTBits-1);
1101       
1102       // Handle rotate right by N like a rotate left by 32-N.
1103       if (Op.getOpcode() == ISD::ROTR)
1104         RotAmt = (VTBits-RotAmt) & (VTBits-1);
1105
1106       // If we aren't rotating out all of the known-in sign bits, return the
1107       // number that are left.  This handles rotl(sext(x), 1) for example.
1108       Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1109       if (Tmp > RotAmt+1) return Tmp-RotAmt;
1110     }
1111     break;
1112   case ISD::ADD:
1113     // Add can have at most one carry bit.  Thus we know that the output
1114     // is, at worst, one more bit than the inputs.
1115     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1116     if (Tmp == 1) return 1;  // Early out.
1117       
1118     // Special case decrementing a value (ADD X, -1):
1119     if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1120       if (CRHS->isAllOnesValue()) {
1121         uint64_t KnownZero, KnownOne;
1122         uint64_t Mask = MVT::getIntVTBitMask(VT);
1123         ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1124         
1125         // If the input is known to be 0 or 1, the output is 0/-1, which is all
1126         // sign bits set.
1127         if ((KnownZero|1) == Mask)
1128           return VTBits;
1129         
1130         // If we are subtracting one from a positive number, there is no carry
1131         // out of the result.
1132         if (KnownZero & MVT::getIntVTSignBit(VT))
1133           return Tmp;
1134       }
1135       
1136     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1137     if (Tmp2 == 1) return 1;
1138       return std::min(Tmp, Tmp2)-1;
1139     break;
1140     
1141   case ISD::SUB:
1142     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1143     if (Tmp2 == 1) return 1;
1144       
1145     // Handle NEG.
1146     if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1147       if (CLHS->getValue() == 0) {
1148         uint64_t KnownZero, KnownOne;
1149         uint64_t Mask = MVT::getIntVTBitMask(VT);
1150         ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
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 the input is known to be positive (the sign bit is known clear),
1157         // the output of the NEG has the same number of sign bits as the input.
1158         if (KnownZero & MVT::getIntVTSignBit(VT))
1159           return Tmp2;
1160         
1161         // Otherwise, we treat this like a SUB.
1162       }
1163     
1164     // Sub can have at most one carry bit.  Thus we know that the output
1165     // is, at worst, one more bit than the inputs.
1166     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1167     if (Tmp == 1) return 1;  // Early out.
1168       return std::min(Tmp, Tmp2)-1;
1169     break;
1170   case ISD::TRUNCATE:
1171     // FIXME: it's tricky to do anything useful for this, but it is an important
1172     // case for targets like X86.
1173     break;
1174   }
1175   
1176   // Allow the target to implement this method for its nodes.
1177   if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1178       Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || 
1179       Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1180       Op.getOpcode() == ISD::INTRINSIC_VOID) {
1181     unsigned NumBits = ComputeNumSignBitsForTargetNode(Op, Depth);
1182     if (NumBits > 1) return NumBits;
1183   }
1184   
1185   // Finally, if we can prove that the top bits of the result are 0's or 1's,
1186   // use this information.
1187   uint64_t KnownZero, KnownOne;
1188   uint64_t Mask = MVT::getIntVTBitMask(VT);
1189   ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1190   
1191   uint64_t SignBit = MVT::getIntVTSignBit(VT);
1192   if (KnownZero & SignBit) {        // SignBit is 0
1193     Mask = KnownZero;
1194   } else if (KnownOne & SignBit) {  // SignBit is 1;
1195     Mask = KnownOne;
1196   } else {
1197     // Nothing known.
1198     return 1;
1199   }
1200   
1201   // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine
1202   // the number of identical bits in the top of the input value.
1203   Mask ^= ~0ULL;
1204   Mask <<= 64-VTBits;
1205   // Return # leading zeros.  We use 'min' here in case Val was zero before
1206   // shifting.  We don't want to return '64' as for an i32 "0".
1207   return std::min(VTBits, CountLeadingZeros_64(Mask));
1208 }
1209
1210
1211
1212 /// ComputeNumSignBitsForTargetNode - This method can be implemented by
1213 /// targets that want to expose additional information about sign bits to the
1214 /// DAG Combiner.
1215 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op,
1216                                                          unsigned Depth) const {
1217   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1218           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1219           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1220           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
1221          "Should use ComputeNumSignBits if you don't know whether Op"
1222          " is a target node!");
1223   return 1;
1224 }
1225
1226
1227 SDOperand TargetLowering::
1228 PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1229   // Default implementation: no optimization.
1230   return SDOperand();
1231 }
1232
1233 //===----------------------------------------------------------------------===//
1234 //  Inline Assembler Implementation Methods
1235 //===----------------------------------------------------------------------===//
1236
1237 TargetLowering::ConstraintType
1238 TargetLowering::getConstraintType(char ConstraintLetter) const {
1239   // FIXME: lots more standard ones to handle.
1240   switch (ConstraintLetter) {
1241   default: return C_Unknown;
1242   case 'r': return C_RegisterClass;
1243   case 'm':    // memory
1244   case 'o':    // offsetable
1245   case 'V':    // not offsetable
1246     return C_Memory;
1247   case 'i':    // Simple Integer or Relocatable Constant
1248   case 'n':    // Simple Integer
1249   case 's':    // Relocatable Constant
1250   case 'I':    // Target registers.
1251   case 'J':
1252   case 'K':
1253   case 'L':
1254   case 'M':
1255   case 'N':
1256   case 'O':
1257   case 'P':
1258     return C_Other;
1259   }
1260 }
1261
1262 bool TargetLowering::isOperandValidForConstraint(SDOperand Op, 
1263                                                  char ConstraintLetter) {
1264   switch (ConstraintLetter) {
1265   default: return false;
1266   case 'i':    // Simple Integer or Relocatable Constant
1267   case 'n':    // Simple Integer
1268   case 's':    // Relocatable Constant
1269     return true;   // FIXME: not right.
1270   }
1271 }
1272
1273
1274 std::vector<unsigned> TargetLowering::
1275 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1276                                   MVT::ValueType VT) const {
1277   return std::vector<unsigned>();
1278 }
1279
1280
1281 std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
1282 getRegForInlineAsmConstraint(const std::string &Constraint,
1283                              MVT::ValueType VT) const {
1284   if (Constraint[0] != '{')
1285     return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
1286   assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
1287
1288   // Remove the braces from around the name.
1289   std::string RegName(Constraint.begin()+1, Constraint.end()-1);
1290
1291   // Figure out which register class contains this reg.
1292   const MRegisterInfo *RI = TM.getRegisterInfo();
1293   for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
1294        E = RI->regclass_end(); RCI != E; ++RCI) {
1295     const TargetRegisterClass *RC = *RCI;
1296     
1297     // If none of the the value types for this register class are valid, we 
1298     // can't use it.  For example, 64-bit reg classes on 32-bit targets.
1299     bool isLegal = false;
1300     for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1301          I != E; ++I) {
1302       if (isTypeLegal(*I)) {
1303         isLegal = true;
1304         break;
1305       }
1306     }
1307     
1308     if (!isLegal) continue;
1309     
1310     for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); 
1311          I != E; ++I) {
1312       if (StringsEqualNoCase(RegName, RI->get(*I).Name))
1313         return std::make_pair(*I, RC);
1314     }
1315   }
1316   
1317   return std::pair<unsigned, const TargetRegisterClass*>(0, 0);
1318 }
1319
1320 //===----------------------------------------------------------------------===//
1321 //  Loop Strength Reduction hooks
1322 //===----------------------------------------------------------------------===//
1323
1324 /// isLegalAddressImmediate - Return true if the integer value or
1325 /// GlobalValue can be used as the offset of the target addressing mode.
1326 bool TargetLowering::isLegalAddressImmediate(int64_t V) const {
1327   return false;
1328 }
1329 bool TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1330   return false;
1331 }