LCOV - code coverage report
Current view: top level - lib/Support - KnownBits.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 21 21 100.0 %
Date: 2017-09-14 15:23:50 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file contains a class for representing known zeros and ones used by
      11             : // computeKnownBits.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/Support/KnownBits.h"
      16             : 
      17             : using namespace llvm;
      18             : 
      19     2344717 : KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
      20             :                                       const KnownBits &LHS, KnownBits RHS) {
      21             :   // Carry in a 1 for a subtract, rather than 0.
      22     2344717 :   bool CarryIn = false;
      23     2344717 :   if (!Add) {
      24             :     // Sum = LHS + ~RHS + 1
      25      386856 :     std::swap(RHS.Zero, RHS.One);
      26      193428 :     CarryIn = true;
      27             :   }
      28             : 
      29    30481321 :   APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + CarryIn;
      30    16413019 :   APInt PossibleSumOne = LHS.One + RHS.One + CarryIn;
      31             : 
      32             :   // Compute known bits of the carry.
      33    21102453 :   APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
      34    16413019 :   APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
      35             : 
      36             :   // Compute set of known bits (where all three relevant bits are known).
      37    11723585 :   APInt LHSKnownUnion = LHS.Zero | LHS.One;
      38    11723585 :   APInt RHSKnownUnion = RHS.Zero | RHS.One;
      39    11723585 :   APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
      40    16413019 :   APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
      41             : 
      42             :   assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
      43             :          "known bits of sum differ");
      44             : 
      45             :   // Compute known bits of the result.
      46     2344717 :   KnownBits KnownOut;
      47    18757736 :   KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
      48    14068302 :   KnownOut.One = std::move(PossibleSumOne) & Known;
      49             : 
      50             :   // Are we still trying to solve for the sign bit?
      51     4689434 :   if (!Known.isSignBitSet()) {
      52     2321172 :     if (NSW) {
      53             :       // Adding two non-negative numbers, or subtracting a negative number from
      54             :       // a non-negative one, can't wrap into negative.
      55      271851 :       if (LHS.isNonNegative() && RHS.isNonNegative())
      56             :         KnownOut.makeNonNegative();
      57             :       // Adding two negative numbers, or subtracting a non-negative number from
      58             :       // a negative one, can't wrap into non-negative.
      59      247289 :       else if (LHS.isNegative() && RHS.isNegative())
      60             :         KnownOut.makeNegative();
      61             :     }
      62             :   }
      63             : 
      64     2344717 :   return KnownOut;
      65             : }

Generated by: LCOV version 1.13