Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/KnownBits.cpp
Line
Count
Source
1
//===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains a class for representing known zeros and ones used by
10
// computeKnownBits.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Support/KnownBits.h"
15
16
using namespace llvm;
17
18
static KnownBits computeForAddCarry(
19
    const KnownBits &LHS, const KnownBits &RHS,
20
71.8M
    bool CarryZero, bool CarryOne) {
21
71.8M
  assert(!(CarryZero && CarryOne) &&
22
71.8M
         "Carry can't be zero and one at the same time");
23
71.8M
24
71.8M
  APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;
25
71.8M
  APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;
26
71.8M
27
71.8M
  // Compute known bits of the carry.
28
71.8M
  APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
29
71.8M
  APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
30
71.8M
31
71.8M
  // Compute set of known bits (where all three relevant bits are known).
32
71.8M
  APInt LHSKnownUnion = LHS.Zero | LHS.One;
33
71.8M
  APInt RHSKnownUnion = RHS.Zero | RHS.One;
34
71.8M
  APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
35
71.8M
  APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
36
71.8M
37
71.8M
  assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
38
71.8M
         "known bits of sum differ");
39
71.8M
40
71.8M
  // Compute known bits of the result.
41
71.8M
  KnownBits KnownOut;
42
71.8M
  KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
43
71.8M
  KnownOut.One = std::move(PossibleSumOne) & Known;
44
71.8M
  return KnownOut;
45
71.8M
}
46
47
KnownBits KnownBits::computeForAddCarry(
48
6.65M
    const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
49
6.65M
  assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
50
6.65M
  return ::computeForAddCarry(
51
6.65M
      LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
52
6.65M
}
53
54
KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
55
65.1M
                                      const KnownBits &LHS, KnownBits RHS) {
56
65.1M
  KnownBits KnownOut;
57
65.1M
  if (Add) {
58
52.3M
    // Sum = LHS + RHS + 0
59
52.3M
    KnownOut = ::computeForAddCarry(
60
52.3M
        LHS, RHS, /*CarryZero*/true, /*CarryOne*/false);
61
52.3M
  } else {
62
12.7M
    // Sum = LHS + ~RHS + 1
63
12.7M
    std::swap(RHS.Zero, RHS.One);
64
12.7M
    KnownOut = ::computeForAddCarry(
65
12.7M
        LHS, RHS, /*CarryZero*/false, /*CarryOne*/true);
66
12.7M
  }
67
65.1M
68
65.1M
  // Are we still trying to solve for the sign bit?
69
65.1M
  if (!KnownOut.isNegative() && 
!KnownOut.isNonNegative()65.1M
) {
70
63.6M
    if (NSW) {
71
24.9M
      // Adding two non-negative numbers, or subtracting a negative number from
72
24.9M
      // a non-negative one, can't wrap into negative.
73
24.9M
      if (LHS.isNonNegative() && 
RHS.isNonNegative()8.43M
)
74
3.87M
        KnownOut.makeNonNegative();
75
21.0M
      // Adding two negative numbers, or subtracting a non-negative number from
76
21.0M
      // a negative one, can't wrap into non-negative.
77
21.0M
      else if (LHS.isNegative() && 
RHS.isNegative()34.3k
)
78
2.35k
        KnownOut.makeNegative();
79
24.9M
    }
80
63.6M
  }
81
65.1M
82
65.1M
  return KnownOut;
83
65.1M
}