LLVM API Documentation

ValueTracking.cpp

Go to the documentation of this file.
00001 //===- ValueTracking.cpp - Walk computations to compute properties --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains routines that help analyze properties that chains of
00011 // computations have.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Analysis/ValueTracking.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/Instructions.h"
00018 #include "llvm/GlobalVariable.h"
00019 #include "llvm/IntrinsicInst.h"
00020 #include "llvm/Target/TargetData.h"
00021 #include "llvm/Support/GetElementPtrTypeIterator.h"
00022 #include "llvm/Support/MathExtras.h"
00023 #include <cstring>
00024 using namespace llvm;
00025 
00026 /// getOpcode - If this is an Instruction or a ConstantExpr, return the
00027 /// opcode value. Otherwise return UserOp1.
00028 static unsigned getOpcode(const Value *V) {
00029   if (const Instruction *I = dyn_cast<Instruction>(V))
00030     return I->getOpcode();
00031   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00032     return CE->getOpcode();
00033   // Use UserOp1 to mean there's no opcode.
00034   return Instruction::UserOp1;
00035 }
00036 
00037 
00038 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
00039 /// known to be either zero or one and return them in the KnownZero/KnownOne
00040 /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit
00041 /// processing.
00042 /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that
00043 /// we cannot optimize based on the assumption that it is zero without changing
00044 /// it to be an explicit zero.  If we don't change it to zero, other code could
00045 /// optimized based on the contradictory assumption that it is non-zero.
00046 /// Because instcombine aggressively folds operations with undef args anyway,
00047 /// this won't lose us code quality.
00048 void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
00049                              APInt &KnownZero, APInt &KnownOne,
00050                              TargetData *TD, unsigned Depth) {
00051   assert(V && "No Value?");
00052   assert(Depth <= 6 && "Limit Search Depth");
00053   uint32_t BitWidth = Mask.getBitWidth();
00054   assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) &&
00055          "Not integer or pointer type!");
00056   assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) &&
00057          (!isa<IntegerType>(V->getType()) ||
00058           V->getType()->getPrimitiveSizeInBits() == BitWidth) &&
00059          KnownZero.getBitWidth() == BitWidth && 
00060          KnownOne.getBitWidth() == BitWidth &&
00061          "V, Mask, KnownOne and KnownZero should have same BitWidth");
00062 
00063   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00064     // We know all of the bits for a constant!
00065     KnownOne = CI->getValue() & Mask;
00066     KnownZero = ~KnownOne & Mask;
00067     return;
00068   }
00069   // Null is all-zeros.
00070   if (isa<ConstantPointerNull>(V)) {
00071     KnownOne.clear();
00072     KnownZero = Mask;
00073     return;
00074   }
00075   // The address of an aligned GlobalValue has trailing zeros.
00076   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00077     unsigned Align = GV->getAlignment();
00078     if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) 
00079       Align = TD->getPrefTypeAlignment(GV->getType()->getElementType());
00080     if (Align > 0)
00081       KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
00082                                               CountTrailingZeros_32(Align));
00083     else
00084       KnownZero.clear();
00085     KnownOne.clear();
00086     return;
00087   }
00088 
00089   KnownZero.clear(); KnownOne.clear();   // Start out not knowing anything.
00090 
00091   if (Depth == 6 || Mask == 0)
00092     return;  // Limit search depth.
00093 
00094   User *I = dyn_cast<User>(V);
00095   if (!I) return;
00096 
00097   APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
00098   switch (getOpcode(I)) {
00099   default: break;
00100   case Instruction::And: {
00101     // If either the LHS or the RHS are Zero, the result is zero.
00102     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
00103     APInt Mask2(Mask & ~KnownZero);
00104     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
00105                       Depth+1);
00106     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00107     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00108     
00109     // Output known-1 bits are only known if set in both the LHS & RHS.
00110     KnownOne &= KnownOne2;
00111     // Output known-0 are known to be clear if zero in either the LHS | RHS.
00112     KnownZero |= KnownZero2;
00113     return;
00114   }
00115   case Instruction::Or: {
00116     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
00117     APInt Mask2(Mask & ~KnownOne);
00118     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
00119                       Depth+1);
00120     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00121     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00122     
00123     // Output known-0 bits are only known if clear in both the LHS & RHS.
00124     KnownZero &= KnownZero2;
00125     // Output known-1 are known to be set if set in either the LHS | RHS.
00126     KnownOne |= KnownOne2;
00127     return;
00128   }
00129   case Instruction::Xor: {
00130     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
00131     ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, TD,
00132                       Depth+1);
00133     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00134     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00135     
00136     // Output known-0 bits are known if clear or set in both the LHS & RHS.
00137     APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
00138     // Output known-1 are known to be set if set in only one of the LHS, RHS.
00139     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
00140     KnownZero = KnownZeroOut;
00141     return;
00142   }
00143   case Instruction::Mul: {
00144     APInt Mask2 = APInt::getAllOnesValue(BitWidth);
00145     ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1);
00146     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
00147                       Depth+1);
00148     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00149     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00150     
00151     // If low bits are zero in either operand, output low known-0 bits.
00152     // Also compute a conserative estimate for high known-0 bits.
00153     // More trickiness is possible, but this is sufficient for the
00154     // interesting case of alignment computation.
00155     KnownOne.clear();
00156     unsigned TrailZ = KnownZero.countTrailingOnes() +
00157                       KnownZero2.countTrailingOnes();
00158     unsigned LeadZ =  std::max(KnownZero.countLeadingOnes() +
00159                                KnownZero2.countLeadingOnes(),
00160                                BitWidth) - BitWidth;
00161 
00162     TrailZ = std::min(TrailZ, BitWidth);
00163     LeadZ = std::min(LeadZ, BitWidth);
00164     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
00165                 APInt::getHighBitsSet(BitWidth, LeadZ);
00166     KnownZero &= Mask;
00167     return;
00168   }
00169   case Instruction::UDiv: {
00170     // For the purposes of computing leading zeros we can conservatively
00171     // treat a udiv as a logical right shift by the power of 2 known to
00172     // be less than the denominator.
00173     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
00174     ComputeMaskedBits(I->getOperand(0),
00175                       AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
00176     unsigned LeadZ = KnownZero2.countLeadingOnes();
00177 
00178     KnownOne2.clear();
00179     KnownZero2.clear();
00180     ComputeMaskedBits(I->getOperand(1),
00181                       AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
00182     unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
00183     if (RHSUnknownLeadingOnes != BitWidth)
00184       LeadZ = std::min(BitWidth,
00185                        LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
00186 
00187     KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
00188     return;
00189   }
00190   case Instruction::Select:
00191     ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, TD, Depth+1);
00192     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, TD,
00193                       Depth+1);
00194     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00195     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00196 
00197     // Only known if known in both the LHS and RHS.
00198     KnownOne &= KnownOne2;
00199     KnownZero &= KnownZero2;
00200     return;
00201   case Instruction::FPTrunc:
00202   case Instruction::FPExt:
00203   case Instruction::FPToUI:
00204   case Instruction::FPToSI:
00205   case Instruction::SIToFP:
00206   case Instruction::UIToFP:
00207     return; // Can't work with floating point.
00208   case Instruction::PtrToInt:
00209   case Instruction::IntToPtr:
00210     // We can't handle these if we don't know the pointer size.
00211     if (!TD) return;
00212     // FALL THROUGH and handle them the same as zext/trunc.
00213   case Instruction::ZExt:
00214   case Instruction::Trunc: {
00215     // Note that we handle pointer operands here because of inttoptr/ptrtoint
00216     // which fall through here.
00217     const Type *SrcTy = I->getOperand(0)->getType();
00218     uint32_t SrcBitWidth = TD ?
00219       TD->getTypeSizeInBits(SrcTy) :
00220       SrcTy->getPrimitiveSizeInBits();
00221     APInt MaskIn(Mask);
00222     MaskIn.zextOrTrunc(SrcBitWidth);
00223     KnownZero.zextOrTrunc(SrcBitWidth);
00224     KnownOne.zextOrTrunc(SrcBitWidth);
00225     ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
00226                       Depth+1);
00227     KnownZero.zextOrTrunc(BitWidth);
00228     KnownOne.zextOrTrunc(BitWidth);
00229     // Any top bits are known to be zero.
00230     if (BitWidth > SrcBitWidth)
00231       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
00232     return;
00233   }
00234   case Instruction::BitCast: {
00235     const Type *SrcTy = I->getOperand(0)->getType();
00236     if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) {
00237       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, TD,
00238                         Depth+1);
00239       return;
00240     }
00241     break;
00242   }
00243   case Instruction::SExt: {
00244     // Compute the bits in the result that are not present in the input.
00245     const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
00246     uint32_t SrcBitWidth = SrcTy->getBitWidth();
00247       
00248     APInt MaskIn(Mask); 
00249     MaskIn.trunc(SrcBitWidth);
00250     KnownZero.trunc(SrcBitWidth);
00251     KnownOne.trunc(SrcBitWidth);
00252     ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
00253                       Depth+1);
00254     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00255     KnownZero.zext(BitWidth);
00256     KnownOne.zext(BitWidth);
00257 
00258     // If the sign bit of the input is known set or clear, then we know the
00259     // top bits of the result.
00260     if (KnownZero[SrcBitWidth-1])             // Input sign bit known zero
00261       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
00262     else if (KnownOne[SrcBitWidth-1])           // Input sign bit known set
00263       KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
00264     return;
00265   }
00266   case Instruction::Shl:
00267     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
00268     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
00269       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
00270       APInt Mask2(Mask.lshr(ShiftAmt));
00271       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
00272                         Depth+1);
00273       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00274       KnownZero <<= ShiftAmt;
00275       KnownOne  <<= ShiftAmt;
00276       KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
00277       return;
00278     }
00279     break;
00280   case Instruction::LShr:
00281     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
00282     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
00283       // Compute the new bits that are at the top now.
00284       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
00285       
00286       // Unsigned shift right.
00287       APInt Mask2(Mask.shl(ShiftAmt));
00288       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne, TD,
00289                         Depth+1);
00290       assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
00291       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
00292       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
00293       // high bits known zero.
00294       KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
00295       return;
00296     }
00297     break;
00298   case Instruction::AShr:
00299     // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
00300     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
00301       // Compute the new bits that are at the top now.
00302       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
00303       
00304       // Signed shift right.
00305       APInt Mask2(Mask.shl(ShiftAmt));
00306       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
00307                         Depth+1);
00308       assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
00309       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
00310       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
00311         
00312       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
00313       if (KnownZero[BitWidth-ShiftAmt-1])    // New bits are known zero.
00314         KnownZero |= HighBits;
00315       else if (KnownOne[BitWidth-ShiftAmt-1])  // New bits are known one.
00316         KnownOne |= HighBits;
00317       return;
00318     }
00319     break;
00320   case Instruction::Sub: {
00321     if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
00322       // We know that the top bits of C-X are clear if X contains less bits
00323       // than C (i.e. no wrap-around can happen).  For example, 20-X is
00324       // positive if we can prove that X is >= 0 and < 16.
00325       if (!CLHS->getValue().isNegative()) {
00326         unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
00327         // NLZ can't be BitWidth with no sign bit
00328         APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
00329         ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
00330                           TD, Depth+1);
00331     
00332         // If all of the MaskV bits are known to be zero, then we know the
00333         // output top bits are zero, because we now know that the output is
00334         // from [0-C].
00335         if ((KnownZero2 & MaskV) == MaskV) {
00336           unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
00337           // Top bits known zero.
00338           KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
00339         }
00340       }        
00341     }
00342   }
00343   // fall through
00344   case Instruction::Add: {
00345     // Output known-0 bits are known if clear or set in both the low clear bits
00346     // common to both LHS & RHS.  For example, 8+(X<<3) is known to have the
00347     // low 3 bits clear.
00348     APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
00349     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
00350                       Depth+1);
00351     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00352     unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
00353 
00354     ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, TD, 
00355                       Depth+1);
00356     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 
00357     KnownZeroOut = std::min(KnownZeroOut, 
00358                             KnownZero2.countTrailingOnes());
00359 
00360     KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
00361     return;
00362   }
00363   case Instruction::SRem:
00364     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
00365       APInt RA = Rem->getValue();
00366       if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
00367         APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
00368         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
00369         ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD, 
00370                           Depth+1);
00371 
00372         // If the sign bit of the first operand is zero, the sign bit of
00373         // the result is zero. If the first operand has no one bits below
00374         // the second operand's single 1 bit, its sign will be zero.
00375         if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
00376           KnownZero2 |= ~LowBits;
00377 
00378         KnownZero |= KnownZero2 & Mask;
00379 
00380         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); 
00381       }
00382     }
00383     break;
00384   case Instruction::URem: {
00385     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
00386       APInt RA = Rem->getValue();
00387       if (RA.isPowerOf2()) {
00388         APInt LowBits = (RA - 1);
00389         APInt Mask2 = LowBits & Mask;
00390         KnownZero |= ~LowBits & Mask;
00391         ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
00392                           Depth+1);
00393         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
00394         break;
00395       }
00396     }
00397 
00398     // Since the result is less than or equal to either operand, any leading
00399     // zero bits in either operand must also exist in the result.
00400     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
00401     ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
00402                       TD, Depth+1);
00403     ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
00404                       TD, Depth+1);
00405 
00406     uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
00407                                 KnownZero2.countLeadingOnes());
00408     KnownOne.clear();
00409     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
00410     break;
00411   }
00412 
00413   case Instruction::Alloca:
00414   case Instruction::Malloc: {
00415     AllocationInst *AI = cast<AllocationInst>(V);
00416     unsigned Align = AI->getAlignment();
00417     if (Align == 0 && TD) {
00418       if (isa<AllocaInst>(AI))
00419         Align = TD->getPrefTypeAlignment(AI->getType()->getElementType());
00420       else if (isa<MallocInst>(AI)) {
00421         // Malloc returns maximally aligned memory.
00422         Align = TD->getABITypeAlignment(AI->getType()->getElementType());
00423         Align =
00424           std::max(Align,
00425                    (unsigned)TD->getABITypeAlignment(Type::DoubleTy));
00426         Align =
00427           std::max(Align,
00428                    (unsigned)TD->getABITypeAlignment(Type::Int64Ty));
00429       }
00430     }
00431     
00432     if (Align > 0)
00433       KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
00434                                               CountTrailingZeros_32(Align));
00435     break;
00436   }
00437   case Instruction::GetElementPtr: {
00438     // Analyze all of the subscripts of this getelementptr instruction
00439     // to determine if we can prove known low zero bits.
00440     APInt LocalMask = APInt::getAllOnesValue(BitWidth);
00441     APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
00442     ComputeMaskedBits(I->getOperand(0), LocalMask,
00443                       LocalKnownZero, LocalKnownOne, TD, Depth+1);
00444     unsigned TrailZ = LocalKnownZero.countTrailingOnes();
00445 
00446     gep_type_iterator GTI = gep_type_begin(I);
00447     for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
00448       Value *Index = I->getOperand(i);
00449       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
00450         // Handle struct member offset arithmetic.
00451         if (!TD) return;
00452         const StructLayout *SL = TD->getStructLayout(STy);
00453         unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
00454         uint64_t Offset = SL->getElementOffset(Idx);
00455         TrailZ = std::min(TrailZ,
00456                           CountTrailingZeros_64(Offset));
00457       } else {
00458         // Handle array index arithmetic.
00459         const Type *IndexedTy = GTI.getIndexedType();
00460         if (!IndexedTy->isSized()) return;
00461         unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits();
00462         uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1;
00463         LocalMask = APInt::getAllOnesValue(GEPOpiBits);
00464         LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
00465         ComputeMaskedBits(Index, LocalMask,
00466                           LocalKnownZero, LocalKnownOne, TD, Depth+1);
00467         TrailZ = std::min(TrailZ,
00468                           CountTrailingZeros_64(TypeSize) +
00469                             LocalKnownZero.countTrailingOnes());
00470       }
00471     }
00472     
00473     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
00474     break;
00475   }
00476   case Instruction::PHI: {
00477     PHINode *P = cast<PHINode>(I);
00478     // Handle the case of a simple two-predecessor recurrence PHI.
00479     // There's a lot more that could theoretically be done here, but
00480     // this is sufficient to catch some interesting cases.
00481     if (P->getNumIncomingValues() == 2) {
00482       for (unsigned i = 0; i != 2; ++i) {
00483         Value *L = P->getIncomingValue(i);
00484         Value *R = P->getIncomingValue(!i);
00485         User *LU = dyn_cast<User>(L);
00486         if (!LU)
00487           continue;
00488         unsigned Opcode = getOpcode(LU);
00489         // Check for operations that have the property that if
00490         // both their operands have low zero bits, the result
00491         // will have low zero bits.
00492         if (Opcode == Instruction::Add ||
00493             Opcode == Instruction::Sub ||
00494             Opcode == Instruction::And ||
00495             Opcode == Instruction::Or ||
00496             Opcode == Instruction::Mul) {
00497           Value *LL = LU->getOperand(0);
00498           Value *LR = LU->getOperand(1);
00499           // Find a recurrence.
00500           if (LL == I)
00501             L = LR;
00502           else if (LR == I)
00503             L = LL;
00504           else
00505             break;
00506           // Ok, we have a PHI of the form L op= R. Check for low
00507           // zero bits.
00508           APInt Mask2 = APInt::getAllOnesValue(BitWidth);
00509           ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1);
00510           Mask2 = APInt::getLowBitsSet(BitWidth,
00511                                        KnownZero2.countTrailingOnes());
00512 
00513           // We need to take the minimum number of known bits
00514           APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
00515           ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1);
00516 
00517           KnownZero = Mask &
00518                       APInt::getLowBitsSet(BitWidth,
00519                                            std::min(KnownZero2.countTrailingOnes(),
00520                                                     KnownZero3.countTrailingOnes()));
00521           break;
00522         }
00523       }
00524     }
00525     break;
00526   }
00527   case Instruction::Call:
00528     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00529       switch (II->getIntrinsicID()) {
00530       default: break;
00531       case Intrinsic::ctpop:
00532       case Intrinsic::ctlz:
00533       case Intrinsic::cttz: {
00534         unsigned LowBits = Log2_32(BitWidth)+1;
00535         KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
00536         break;
00537       }
00538       }
00539     }
00540     break;
00541   }
00542 }
00543 
00544 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00545 /// this predicate to simplify operations downstream.  Mask is known to be zero
00546 /// for bits that V cannot have.
00547 bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask,
00548                              TargetData *TD, unsigned Depth) {
00549   APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
00550   ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
00551   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 
00552   return (KnownZero & Mask) == Mask;
00553 }
00554 
00555 
00556 
00557 /// ComputeNumSignBits - Return the number of times the sign bit of the
00558 /// register is replicated into the other bits.  We know that at least 1 bit
00559 /// is always equal to the sign bit (itself), but other cases can give us
00560 /// information.  For example, immediately after an "ashr X, 2", we know that
00561 /// the top 3 bits are all equal to each other, so we return 3.
00562 ///
00563 /// 'Op' must have a scalar integer type.
00564 ///
00565 unsigned llvm::ComputeNumSignBits(Value *V, TargetData *TD, unsigned Depth) {
00566   const IntegerType *Ty = cast<IntegerType>(V->getType());
00567   unsigned TyBits = Ty->getBitWidth();
00568   unsigned Tmp, Tmp2;
00569   unsigned FirstAnswer = 1;
00570 
00571   // Note that ConstantInt is handled by the general ComputeMaskedBits case
00572   // below.
00573 
00574   if (Depth == 6)
00575     return 1;  // Limit search depth.
00576   
00577   User *U = dyn_cast<User>(V);
00578   switch (getOpcode(V)) {
00579   default: break;
00580   case Instruction::SExt:
00581     Tmp = TyBits-cast<IntegerType>(U->getOperand(0)->getType())->getBitWidth();
00582     return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp;
00583     
00584   case Instruction::AShr:
00585     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
00586     // ashr X, C   -> adds C sign bits.
00587     if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
00588       Tmp += C->getZExtValue();
00589       if (Tmp > TyBits) Tmp = TyBits;
00590     }
00591     return Tmp;
00592   case Instruction::Shl:
00593     if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
00594       // shl destroys sign bits.
00595       Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
00596       if (C->getZExtValue() >= TyBits ||      // Bad shift.
00597           C->getZExtValue() >= Tmp) break;    // Shifted all sign bits out.
00598       return Tmp - C->getZExtValue();
00599     }
00600     break;
00601   case Instruction::And:
00602   case Instruction::Or:
00603   case Instruction::Xor:    // NOT is handled here.
00604     // Logical binary ops preserve the number of sign bits at the worst.
00605     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
00606     if (Tmp != 1) {
00607       Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
00608       FirstAnswer = std::min(Tmp, Tmp2);
00609       // We computed what we know about the sign bits as our first
00610       // answer. Now proceed to the generic code that uses
00611       // ComputeMaskedBits, and pick whichever answer is better.
00612     }
00613     break;
00614 
00615   case Instruction::Select:
00616     Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
00617     if (Tmp == 1) return 1;  // Early out.
00618     Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1);
00619     return std::min(Tmp, Tmp2);
00620     
00621   case Instruction::Add:
00622     // Add can have at most one carry bit.  Thus we know that the output
00623     // is, at worst, one more bit than the inputs.
00624     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
00625     if (Tmp == 1) return 1;  // Early out.
00626       
00627     // Special case decrementing a value (ADD X, -1):
00628     if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(0)))
00629       if (CRHS->isAllOnesValue()) {
00630         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
00631         APInt Mask = APInt::getAllOnesValue(TyBits);
00632         ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD,
00633                           Depth+1);
00634         
00635         // If the input is known to be 0 or 1, the output is 0/-1, which is all
00636         // sign bits set.
00637         if ((KnownZero | APInt(TyBits, 1)) == Mask)
00638           return TyBits;
00639         
00640         // If we are subtracting one from a positive number, there is no carry
00641         // out of the result.
00642         if (KnownZero.isNegative())
00643           return Tmp;
00644       }
00645       
00646     Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
00647     if (Tmp2 == 1) return 1;
00648       return std::min(Tmp, Tmp2)-1;
00649     break;
00650     
00651   case Instruction::Sub:
00652     Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
00653     if (Tmp2 == 1) return 1;
00654       
00655     // Handle NEG.
00656     if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0)))
00657       if (CLHS->isNullValue()) {
00658         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
00659         APInt Mask = APInt::getAllOnesValue(TyBits);
00660         ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne, 
00661                           TD, Depth+1);
00662         // If the input is known to be 0 or 1, the output is 0/-1, which is all
00663         // sign bits set.
00664         if ((KnownZero | APInt(TyBits, 1)) == Mask)
00665           return TyBits;
00666         
00667         // If the input is known to be positive (the sign bit is known clear),
00668         // the output of the NEG has the same number of sign bits as the input.
00669         if (KnownZero.isNegative())
00670           return Tmp2;
00671         
00672         // Otherwise, we treat this like a SUB.
00673       }
00674     
00675     // Sub can have at most one carry bit.  Thus we know that the output
00676     // is, at worst, one more bit than the inputs.
00677     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
00678     if (Tmp == 1) return 1;  // Early out.
00679       return std::min(Tmp, Tmp2)-1;
00680     break;
00681   case Instruction::Trunc:
00682     // FIXME: it's tricky to do anything useful for this, but it is an important
00683     // case for targets like X86.
00684     break;
00685   }
00686   
00687   // Finally, if we can prove that the top bits of the result are 0's or 1's,
00688   // use this information.
00689   APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
00690   APInt Mask = APInt::getAllOnesValue(TyBits);
00691   ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
00692   
00693   if (KnownZero.isNegative()) {        // sign bit is 0
00694     Mask = KnownZero;
00695   } else if (KnownOne.isNegative()) {  // sign bit is 1;
00696     Mask = KnownOne;
00697   } else {
00698     // Nothing known.
00699     return FirstAnswer;
00700   }
00701   
00702   // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine
00703   // the number of identical bits in the top of the input value.
00704   Mask = ~Mask;
00705   Mask <<= Mask.getBitWidth()-TyBits;
00706   // Return # leading zeros.  We use 'min' here in case Val was zero before
00707   // shifting.  We don't want to return '64' as for an i32 "0".
00708   return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
00709 }
00710 
00711 /// CannotBeNegativeZero - Return true if we can prove that the specified FP 
00712 /// value is never equal to -0.0.
00713 ///
00714 /// NOTE: this function will need to be revisited when we support non-default
00715 /// rounding modes!
00716 ///
00717 bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
00718   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
00719     return !CFP->getValueAPF().isNegZero();
00720   
00721   if (Depth == 6)
00722     return 1;  // Limit search depth.
00723 
00724   const Instruction *I = dyn_cast<Instruction>(V);
00725   if (I == 0) return false;
00726   
00727   // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
00728   if (I->getOpcode() == Instruction::Add &&
00729       isa<ConstantFP>(I->getOperand(1)) && 
00730       cast<ConstantFP>(I->getOperand(1))->isNullValue())
00731     return true;
00732     
00733   // sitofp and uitofp turn into +0.0 for zero.
00734   if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
00735     return true;
00736   
00737   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
00738     // sqrt(-0.0) = -0.0, no other negative results are possible.
00739     if (II->getIntrinsicID() == Intrinsic::sqrt)
00740       return CannotBeNegativeZero(II->getOperand(1), Depth+1);
00741   
00742   if (const CallInst *CI = dyn_cast<CallInst>(I))
00743     if (const Function *F = CI->getCalledFunction()) {
00744       if (F->isDeclaration()) {
00745         switch (F->getNameLen()) {
00746         case 3:  // abs(x) != -0.0
00747           if (!strcmp(F->getNameStart(), "abs")) return true;
00748           break;
00749         case 4:  // abs[lf](x) != -0.0
00750           if (!strcmp(F->getNameStart(), "absf")) return true;
00751           if (!strcmp(F->getNameStart(), "absl")) return true;
00752           break;
00753         }
00754       }
00755     }
00756   
00757   return false;
00758 }
00759 
00760 // This is the recursive version of BuildSubAggregate. It takes a few different
00761 // arguments. Idxs is the index within the nested struct From that we are
00762 // looking at now (which is of type IndexedType). IdxSkip is the number of
00763 // indices from Idxs that should be left out when inserting into the resulting
00764 // struct. To is the result struct built so far, new insertvalue instructions
00765 // build on that.
00766 Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType,
00767                                  SmallVector<unsigned, 10> &Idxs,
00768                                  unsigned IdxSkip,
00769                                  Instruction *InsertBefore) {
00770   const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType);
00771   if (STy) {
00772     // Save the original To argument so we can modify it
00773     Value *OrigTo = To;
00774     // General case, the type indexed by Idxs is a struct
00775     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
00776       // Process each struct element recursively
00777       Idxs.push_back(i);
00778       Value *PrevTo = To;
00779       To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
00780                              InsertBefore);
00781       Idxs.pop_back();
00782       if (!To) {
00783         // Couldn't find any inserted value for this index? Cleanup
00784         while (PrevTo != OrigTo) {
00785           InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
00786           PrevTo = Del->getAggregateOperand();
00787           Del->eraseFromParent();
00788         }
00789         // Stop processing elements
00790         break;
00791       }
00792     }
00793     // If we succesfully found a value for each of our subaggregates 
00794     if (To)
00795       return To;
00796   }
00797   // Base case, the type indexed by SourceIdxs is not a struct, or not all of
00798   // the struct's elements had a value that was inserted directly. In the latter
00799   // case, perhaps we can't determine each of the subelements individually, but
00800   // we might be able to find the complete struct somewhere.
00801   
00802   // Find the value that is at that particular spot
00803   Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end());
00804 
00805   if (!V)
00806     return NULL;
00807 
00808   // Insert the value in the new (sub) aggregrate
00809   return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip,
00810                                        Idxs.end(), "tmp", InsertBefore);
00811 }
00812 
00813 // This helper takes a nested struct and extracts a part of it (which is again a
00814 // struct) into a new value. For example, given the struct:
00815 // { a, { b, { c, d }, e } }
00816 // and the indices "1, 1" this returns
00817 // { c, d }.
00818 //
00819 // It does this by inserting an insertvalue for each element in the resulting
00820 // struct, as opposed to just inserting a single struct. This will only work if
00821 // each of the elements of the substruct are known (ie, inserted into From by an
00822 // insertvalue instruction somewhere).
00823 //
00824 // All inserted insertvalue instructions are inserted before InsertBefore
00825 Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
00826                          const unsigned *idx_end, Instruction *InsertBefore) {
00827   assert(InsertBefore && "Must have someplace to insert!");
00828   const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
00829                                                              idx_begin,
00830                                                              idx_end);
00831   Value *To = UndefValue::get(IndexedType);
00832   SmallVector<unsigned, 10> Idxs(idx_begin, idx_end);
00833   unsigned IdxSkip = Idxs.size();
00834 
00835   return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
00836 }
00837 
00838 /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
00839 /// the scalar value indexed is already around as a register, for example if it
00840 /// were inserted directly into the aggregrate.
00841 ///
00842 /// If InsertBefore is not null, this function will duplicate (modified)
00843 /// insertvalues when a part of a nested struct is extracted.
00844 Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
00845                          const unsigned *idx_end, Instruction *InsertBefore) {
00846   // Nothing to index? Just return V then (this is useful at the end of our
00847   // recursion)
00848   if (idx_begin == idx_end)
00849     return V;
00850   // We have indices, so V should have an indexable type
00851   assert((isa<StructType>(V->getType()) || isa<ArrayType>(V->getType()))
00852          && "Not looking at a struct or array?");
00853   assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end)
00854          && "Invalid indices for type?");
00855   const CompositeType *PTy = cast<CompositeType>(V->getType());
00856   
00857   if (isa<UndefValue>(V))
00858     return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
00859                                                               idx_begin,
00860                                                               idx_end));
00861   else if (isa<ConstantAggregateZero>(V))
00862     return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy, 
00863                                                                      idx_begin,
00864                                                                      idx_end));
00865   else if (Constant *C = dyn_cast<Constant>(V)) {
00866     if (isa<ConstantArray>(C) || isa<ConstantStruct>(C))
00867       // Recursively process this constant
00868       return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1, idx_end,
00869                                InsertBefore);
00870   } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
00871     // Loop the indices for the insertvalue instruction in parallel with the
00872     // requested indices
00873     const unsigned *req_idx = idx_begin;
00874     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
00875          i != e; ++i, ++req_idx) {
00876       if (req_idx == idx_end) {
00877         if (InsertBefore)
00878           // The requested index identifies a part of a nested aggregate. Handle
00879           // this specially. For example,
00880           // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
00881           // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
00882           // %C = extractvalue {i32, { i32, i32 } } %B, 1
00883           // This can be changed into
00884           // %A = insertvalue {i32, i32 } undef, i32 10, 0
00885           // %C = insertvalue {i32, i32 } %A, i32 11, 1
00886           // which allows the unused 0,0 element from the nested struct to be
00887           // removed.
00888           return BuildSubAggregate(V, idx_begin, req_idx, InsertBefore);
00889         else
00890           // We can't handle this without inserting insertvalues
00891           return 0;
00892       }
00893       
00894       // This insert value inserts something else than what we are looking for.
00895       // See if the (aggregrate) value inserted into has the value we are
00896       // looking for, then.
00897       if (*req_idx != *i)
00898         return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end,
00899                                  InsertBefore);
00900     }
00901     // If we end up here, the indices of the insertvalue match with those
00902     // requested (though possibly only partially). Now we recursively look at
00903     // the inserted value, passing any remaining indices.
00904     return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end,
00905                              InsertBefore);
00906   } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
00907     // If we're extracting a value from an aggregrate that was extracted from
00908     // something else, we can extract from that something else directly instead.
00909     // However, we will need to chain I's indices with the requested indices.
00910    
00911     // Calculate the number of indices required 
00912     unsigned size = I->getNumIndices() + (idx_end - idx_begin);
00913     // Allocate some space to put the new indices in
00914     SmallVector<unsigned, 5> Idxs;
00915     Idxs.reserve(size);
00916     // Add indices from the extract value instruction
00917     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
00918          i != e; ++i)
00919       Idxs.push_back(*i);
00920     
00921     // Add requested indices
00922     for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i)
00923       Idxs.push_back(*i);
00924 
00925     assert(Idxs.size() == size 
00926            && "Number of indices added not correct?");
00927     
00928     return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(),
00929                              InsertBefore);
00930   }
00931   // Otherwise, we don't know (such as, extracting from a function return value
00932   // or load instruction)
00933   return 0;
00934 }
00935 
00936 /// GetConstantStringInfo - This function computes the length of a
00937 /// null-terminated C string pointed to by V.  If successful, it returns true
00938 /// and returns the string in Str.  If unsuccessful, it returns false.
00939 bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset,
00940                                  bool StopAtNul) {
00941   // If V is NULL then return false;
00942   if (V == NULL) return false;
00943 
00944   /