reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
//===- PhiValues.cpp - Phi Value Analysis ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/PhiValues.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Instructions.h"

using namespace llvm;

void PhiValues::PhiValuesCallbackVH::deleted() {
  PV->invalidateValue(getValPtr());
}

void PhiValues::PhiValuesCallbackVH::allUsesReplacedWith(Value *) {
  // We could potentially update the cached values we have with the new value,
  // but it's simpler to just treat the old value as invalidated.
  PV->invalidateValue(getValPtr());
}

bool PhiValues::invalidate(Function &, const PreservedAnalyses &PA,
                           FunctionAnalysisManager::Invalidator &) {
  // PhiValues is invalidated if it isn't preserved.
  auto PAC = PA.getChecker<PhiValuesAnalysis>();
  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
}

// The goal here is to find all of the non-phi values reachable from this phi,
// and to do the same for all of the phis reachable from this phi, as doing so
// is necessary anyway in order to get the values for this phi. We do this using
// Tarjan's algorithm with Nuutila's improvements to find the strongly connected
// components of the phi graph rooted in this phi:
//  * All phis in a strongly connected component will have the same reachable
//    non-phi values. The SCC may not be the maximal subgraph for that set of
//    reachable values, but finding out that isn't really necessary (it would
//    only reduce the amount of memory needed to store the values).
//  * Tarjan's algorithm completes components in a bottom-up manner, i.e. it
//    never completes a component before the components reachable from it have
//    been completed. This means that when we complete a component we have
//    everything we need to collect the values reachable from that component.
//  * We collect both the non-phi values reachable from each SCC, as that's what
//    we're ultimately interested in, and all of the reachable values, i.e.
//    including phis, as that makes invalidateValue easier.
void PhiValues::processPhi(const PHINode *Phi,
                           SmallVector<const PHINode *, 8> &Stack) {
  // Initialize the phi with the next depth number.
  assert(DepthMap.lookup(Phi) == 0);
  assert(NextDepthNumber != UINT_MAX);
  unsigned int DepthNumber = ++NextDepthNumber;
  DepthMap[Phi] = DepthNumber;

  // Recursively process the incoming phis of this phi.
  TrackedValues.insert(PhiValuesCallbackVH(const_cast<PHINode *>(Phi), this));
  for (Value *PhiOp : Phi->incoming_values()) {
    if (PHINode *PhiPhiOp = dyn_cast<PHINode>(PhiOp)) {
      // Recurse if the phi has not yet been visited.
      if (DepthMap.lookup(PhiPhiOp) == 0)
        processPhi(PhiPhiOp, Stack);
      assert(DepthMap.lookup(PhiPhiOp) != 0);
      // If the phi did not become part of a component then this phi and that
      // phi are part of the same component, so adjust the depth number.
      if (!ReachableMap.count(DepthMap[PhiPhiOp]))
        DepthMap[Phi] = std::min(DepthMap[Phi], DepthMap[PhiPhiOp]);
    } else {
      TrackedValues.insert(PhiValuesCallbackVH(PhiOp, this));
    }
  }

  // Now that incoming phis have been handled, push this phi to the stack.
  Stack.push_back(Phi);

  // If the depth number has not changed then we've finished collecting the phis
  // of a strongly connected component.
  if (DepthMap[Phi] == DepthNumber) {
    // Collect the reachable values for this component. The phis of this
    // component will be those on top of the depth stach with the same or
    // greater depth number.
    ConstValueSet Reachable;
    while (!Stack.empty() && DepthMap[Stack.back()] >= DepthNumber) {
      const PHINode *ComponentPhi = Stack.pop_back_val();
      Reachable.insert(ComponentPhi);
      DepthMap[ComponentPhi] = DepthNumber;
      for (Value *Op : ComponentPhi->incoming_values()) {
        if (PHINode *PhiOp = dyn_cast<PHINode>(Op)) {
          // If this phi is not part of the same component then that component
          // is guaranteed to have been completed before this one. Therefore we
          // can just add its reachable values to the reachable values of this
          // component.
          auto It = ReachableMap.find(DepthMap[PhiOp]);
          if (It != ReachableMap.end())
            Reachable.insert(It->second.begin(), It->second.end());
        } else {
          Reachable.insert(Op);
        }
      }
    }
    ReachableMap.insert({DepthNumber,Reachable});

    // Filter out phis to get the non-phi reachable values.
    ValueSet NonPhi;
    for (const Value *V : Reachable)
      if (!isa<PHINode>(V))
        NonPhi.insert(const_cast<Value*>(V));
    NonPhiReachableMap.insert({DepthNumber,NonPhi});
  }
}

const PhiValues::ValueSet &PhiValues::getValuesForPhi(const PHINode *PN) {
  if (DepthMap.count(PN) == 0) {
    SmallVector<const PHINode *, 8> Stack;
    processPhi(PN, Stack);
    assert(Stack.empty());
  }
  assert(DepthMap.lookup(PN) != 0);
  return NonPhiReachableMap[DepthMap[PN]];
}

void PhiValues::invalidateValue(const Value *V) {
  // Components that can reach V are invalid.
  SmallVector<unsigned int, 8> InvalidComponents;
  for (auto &Pair : ReachableMap)
    if (Pair.second.count(V))
      InvalidComponents.push_back(Pair.first);

  for (unsigned int N : InvalidComponents) {
    for (const Value *V : ReachableMap[N])
      if (const PHINode *PN = dyn_cast<PHINode>(V))
        DepthMap.erase(PN);
    NonPhiReachableMap.erase(N);
    ReachableMap.erase(N);
  }
  // This value is no longer tracked
  auto It = TrackedValues.find_as(V);
  if (It != TrackedValues.end())
    TrackedValues.erase(It);
}

void PhiValues::releaseMemory() {
  DepthMap.clear();
  NonPhiReachableMap.clear();
  ReachableMap.clear();
}

void PhiValues::print(raw_ostream &OS) const {
  // Iterate through the phi nodes of the function rather than iterating through
  // DepthMap in order to get predictable ordering.
  for (const BasicBlock &BB : F) {
    for (const PHINode &PN : BB.phis()) {
      OS << "PHI ";
      PN.printAsOperand(OS, false);
      OS << " has values:\n";
      unsigned int N = DepthMap.lookup(&PN);
      auto It = NonPhiReachableMap.find(N);
      if (It == NonPhiReachableMap.end())
        OS << "  UNKNOWN\n";
      else if (It->second.empty())
        OS << "  NONE\n";
      else
        for (Value *V : It->second)
          // Printing of an instruction prints two spaces at the start, so
          // handle instructions and everything else slightly differently in
          // order to get consistent indenting.
          if (Instruction *I = dyn_cast<Instruction>(V))
            OS << *I << "\n";
          else
            OS << "  " << *V << "\n";
    }
  }
}

AnalysisKey PhiValuesAnalysis::Key;
PhiValues PhiValuesAnalysis::run(Function &F, FunctionAnalysisManager &) {
  return PhiValues(F);
}

PreservedAnalyses PhiValuesPrinterPass::run(Function &F,
                                            FunctionAnalysisManager &AM) {
  OS << "PHI Values for function: " << F.getName() << "\n";
  PhiValues &PI = AM.getResult<PhiValuesAnalysis>(F);
  for (const BasicBlock &BB : F)
    for (const PHINode &PN : BB.phis())
      PI.getValuesForPhi(&PN);
  PI.print(OS);
  return PreservedAnalyses::all();
}

PhiValuesWrapperPass::PhiValuesWrapperPass() : FunctionPass(ID) {
  initializePhiValuesWrapperPassPass(*PassRegistry::getPassRegistry());
}

bool PhiValuesWrapperPass::runOnFunction(Function &F) {
  Result.reset(new PhiValues(F));
  return false;
}

void PhiValuesWrapperPass::releaseMemory() {
  Result->releaseMemory();
}

void PhiValuesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  AU.setPreservesAll();
}

char PhiValuesWrapperPass::ID = 0;

INITIALIZE_PASS(PhiValuesWrapperPass, "phi-values", "Phi Values Analysis", false,
                true)