Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/Analysis/DomConditionCache.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DomConditionCache.cpp ----------------------------------------------===//
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
#include "llvm/Analysis/DomConditionCache.h"
10
#include "llvm/IR/PatternMatch.h"
11
12
using namespace llvm;
13
using namespace llvm::PatternMatch;
14
15
// TODO: This code is very similar to findAffectedValues() in
16
// AssumptionCache, but currently specialized to just the patterns that
17
// computeKnownBits() supports, and without the notion of result elem indices
18
// that are AC specific. Deduplicate this code once we have a clearer picture
19
// of how much they can be shared.
20
static void findAffectedValues(Value *Cond,
21
5.51k
                               SmallVectorImpl<Value *> &Affected) {
22
5.51k
  auto AddAffected = [&Affected](Value *V) {
23
2.39k
    if (isa<Argument>(V) || isa<GlobalValue>(V)) {
24
686
      Affected.push_back(V);
25
1.70k
    } else if (auto *I = dyn_cast<Instruction>(V)) {
26
1.70k
      Affected.push_back(I);
27
28
      // Peek through unary operators to find the source of the condition.
29
1.70k
      Value *Op;
30
1.70k
      if (match(I, m_PtrToInt(m_Value(Op)))) {
31
0
        if (isa<Instruction>(Op) || isa<Argument>(Op))
32
0
          Affected.push_back(Op);
33
0
      }
34
1.70k
    }
35
2.39k
  };
36
37
5.51k
  ICmpInst::Predicate Pred;
38
5.51k
  Value *A;
39
5.51k
  if (match(Cond, m_ICmp(Pred, m_Value(A), m_Constant()))) {
40
1.89k
    AddAffected(A);
41
42
1.89k
    if (ICmpInst::isEquality(Pred)) {
43
1.14k
      Value *X;
44
      // (X & C) or (X | C) or (X ^ C).
45
      // (X << C) or (X >>_s C) or (X >>_u C).
46
1.14k
      if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
47
1.14k
          match(A, m_Shift(m_Value(X), m_ConstantInt())))
48
440
        AddAffected(X);
49
1.14k
    } else {
50
752
      Value *X;
51
      // Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4.
52
752
      if (match(A, m_Add(m_Value(X), m_ConstantInt())))
53
55
        AddAffected(X);
54
752
    }
55
1.89k
  }
56
5.51k
}
57
58
5.51k
void DomConditionCache::registerBranch(BranchInst *BI) {
59
5.51k
  assert(BI->isConditional() && "Must be conditional branch");
60
0
  SmallVector<Value *, 16> Affected;
61
5.51k
  findAffectedValues(BI->getCondition(), Affected);
62
5.51k
  for (Value *V : Affected) {
63
2.39k
    auto &AV = AffectedValues[V];
64
2.39k
    if (!is_contained(AV, BI))
65
2.30k
      AV.push_back(BI);
66
2.39k
  }
67
5.51k
}