<commit_before>//===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the following classes:
//  1. DominatorTree: Represent dominators as an explicit tree structure.
//  2. ETForest: Efficient data structure for dominance comparisons and 
//     nearest-common-ancestor queries.
//  3. DominanceFrontier: Calculate and hold the dominance frontier for a
//     function.
//
//  These data structures are listed in increasing order of complexity.  It
//  takes longer to calculate the dominator frontier, for example, than the
//  DominatorTree mapping.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_DOMINATORS_H
#define LLVM_ANALYSIS_DOMINATORS_H

#include "llvm/Analysis/ET-Forest.h"
#include "llvm/Pass.h"
#include <set>

namespace llvm {

class Instruction;

template <typename GraphType> struct GraphTraits;

//===----------------------------------------------------------------------===//
/// DominatorBase - Base class that other, more interesting dominator analyses
/// inherit from.
///
class DominatorBase : public FunctionPass {
protected:
  std::vector<BasicBlock*> Roots;
  const bool IsPostDominators;
  inline DominatorBase(intptr_t ID, bool isPostDom) : 
    FunctionPass(ID), Roots(), IsPostDominators(isPostDom) {}
public:

  /// getRoots -  Return the root blocks of the current CFG.  This may include
  /// multiple blocks if we are computing post dominators.  For forward
  /// dominators, this will always be a single block (the entry node).
  ///
  inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }

  /// isPostDominator - Returns true if analysis based of postdoms
  ///
  bool isPostDominator() const { return IsPostDominators; }
};


//===----------------------------------------------------------------------===//
// DomTreeNode - Dominator Tree Node

class DomTreeNode {
  BasicBlock *TheBB;
  DomTreeNode *IDom;
  ETNode *ETN;
  std::vector<DomTreeNode*> Children;
public:
  typedef std::vector<DomTreeNode*>::iterator iterator;
  typedef std::vector<DomTreeNode*>::const_iterator const_iterator;
  
  iterator begin()             { return Children.begin(); }
  iterator end()               { return Children.end(); }
  const_iterator begin() const { return Children.begin(); }
  const_iterator end()   const { return Children.end(); }
  
  inline BasicBlock *getBlock() const { return TheBB; }
  inline DomTreeNode *getIDom() const { return IDom; }
  inline ETNode *getETNode() const { return ETN; }
  inline const std::vector<DomTreeNode*> &getChildren() const { return Children; }
  
  inline DomTreeNode(BasicBlock *BB, DomTreeNode *iDom, ETNode *E) 
    : TheBB(BB), IDom(iDom), ETN(E) {
    if (IDom)
      ETN->setFather(IDom->getETNode());
  }
  inline DomTreeNode *addChild(DomTreeNode *C) { Children.push_back(C); return C; }
  void setIDom(DomTreeNode *NewIDom);
};

//===----------------------------------------------------------------------===//
/// DominatorTree - Calculate the immediate dominator tree for a function.
///
class DominatorTreeBase : public DominatorBase {

protected:
  void reset();
  typedef std::map<BasicBlock*, DomTreeNode*> DomTreeNodeMapType;
  DomTreeNodeMapType DomTreeNodes;
  DomTreeNode *RootNode;

  typedef std::map<BasicBlock*, ETNode*> ETMapType;
  ETMapType ETNodes;

  bool DFSInfoValid;
  unsigned int SlowQueries;
  // Information record used during immediate dominators computation.
  struct InfoRec {
    unsigned Semi;
    unsigned Size;
    BasicBlock *Label, *Parent, *Child, *Ancestor;

    std::vector<BasicBlock*> Bucket;

    InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){}
  };

  std::map<BasicBlock*, BasicBlock*> IDoms;

  // Vertex - Map the DFS number to the BasicBlock*
  std::vector<BasicBlock*> Vertex;

  // Info - Collection of information used during the computation of idoms.
  std::map<BasicBlock*, InfoRec> Info;

  public:
  DominatorTreeBase(intptr_t ID, bool isPostDom) 
    : DominatorBase(ID, isPostDom), DFSInfoValid(false), SlowQueries(0) {}
  ~DominatorTreeBase() { reset(); }

  virtual void releaseMemory() { reset(); }

  /// getNode - return the (Post)DominatorTree node for the specified basic
  /// block.  This is the same as using operator[] on this class.
  ///
  inline DomTreeNode *getNode(BasicBlock *BB) const {
    DomTreeNodeMapType::const_iterator i = DomTreeNodes.find(BB);
    return (i != DomTreeNodes.end()) ? i->second : 0;
  }

  inline DomTreeNode *operator[](BasicBlock *BB) const {
    return getNode(BB);
  }

  /// getIDomBlock - return basic block BB's immediate domiantor basic block.
  ///
  BasicBlock *getIDomBlock(BasicBlock *BB) {
    DomTreeNode *N = getNode(BB);
    assert (N && "Missing dominator tree node");
    DomTreeNode *I = N->getIDom();
    assert (N && "Missing immediate dominator");
    return I->getBlock();
  }

  /// getRootNode - This returns the entry node for the CFG of the function.  If
  /// this tree represents the post-dominance relations for a function, however,
  /// this root may be a node with the block == NULL.  This is the case when
  /// there are multiple exit nodes from a particular function.  Consumers of
  /// post-dominance information must be capable of dealing with this
  /// possibility.
  ///
  DomTreeNode *getRootNode() { return RootNode; }
  const DomTreeNode *getRootNode() const { return RootNode; }

  /// properlyDominates - Returns true iff this dominates N and this != N.
  /// Note that this is not a constant time operation!
  ///
  bool properlyDominates(const DomTreeNode *A, DomTreeNode *B) const {
    if (A == 0 || B == 0) return false;
    return dominatedBySlowTreeWalk(A, B);
  }

  inline bool properlyDominates(BasicBlock *A, BasicBlock *B) {
    return properlyDominates(getNode(A), getNode(B));
  }

  bool dominatedBySlowTreeWalk(const DomTreeNode *A, 
                               const DomTreeNode *B) const {
    const DomTreeNode *IDom;
    if (A == 0 || B == 0) return false;
    while ((IDom = B->getIDom()) != 0 && IDom != A)
      B = IDom;   // Walk up the tree
    return IDom != 0;
  }

  void updateDFSNumbers();  

  /// dominates - Returns true iff this dominates N.  Note that this is not a
  /// constant time operation!
  ///
  inline bool dominates(const DomTreeNode *A, DomTreeNode *B) {
    if (B == A) 
      return true;  // A node trivially dominates itself.

    if (A == 0 || B == 0)
      return false;

    ETNode *NodeA = A->getETNode();
    ETNode *NodeB = B->getETNode();
    
    if (DFSInfoValid)
      return NodeB->DominatedBy(NodeA);

    // If we end up with too many slow queries, just update the
    // DFS numbers on the theory that we are going to keep querying.
    SlowQueries++;
    if (SlowQueries > 32) {
      updateDFSNumbers();
      return NodeB->DominatedBy(NodeA);
    }
    //return NodeB->DominatedBySlow(NodeA);
    return dominatedBySlowTreeWalk(A, B);<commit_msg>Add new method - nearestCommonDominator().<commit_after>//===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the following classes:
//  1. DominatorTree: Represent dominators as an explicit tree structure.
//  2. ETForest: Efficient data structure for dominance comparisons and 
//     nearest-common-ancestor queries.
//  3. DominanceFrontier: Calculate and hold the dominance frontier for a
//     function.
//
//  These data structures are listed in increasing order of complexity.  It
//  takes longer to calculate the dominator frontier, for example, than the
//  DominatorTree mapping.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_DOMINATORS_H
#define LLVM_ANALYSIS_DOMINATORS_H

#include "llvm/Analysis/ET-Forest.h"
#include "llvm/Pass.h"
#include <set>

namespace llvm {

class Instruction;

template <typename GraphType> struct GraphTraits;

//===----------------------------------------------------------------------===//
/// DominatorBase - Base class that other, more interesting dominator analyses
/// inherit from.
///
class DominatorBase : public FunctionPass {
protected:
  std::vector<BasicBlock*> Roots;
  const bool IsPostDominators;
  inline DominatorBase(intptr_t ID, bool isPostDom) : 
    FunctionPass(ID), Roots(), IsPostDominators(isPostDom) {}
public:

  /// getRoots -  Return the root blocks of the current CFG.  This may include
  /// multiple blocks if we are computing post dominators.  For forward
  /// dominators, this will always be a single block (the entry node).
  ///
  inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }

  /// isPostDominator - Returns true if analysis based of postdoms
  ///
  bool isPostDominator() const { return IsPostDominators; }
};


//===----------------------------------------------------------------------===//
// DomTreeNode - Dominator Tree Node

class DomTreeNode {
  BasicBlock *TheBB;
  DomTreeNode *IDom;
  ETNode *ETN;
  std::vector<DomTreeNode*> Children;
public:
  typedef std::vector<DomTreeNode*>::iterator iterator;
  typedef std::vector<DomTreeNode*>::const_iterator const_iterator;
  
  iterator begin()             { return Children.begin(); }
  iterator end()               { return Children.end(); }
  const_iterator begin() const { return Children.begin(); }
  const_iterator end()   const { return Children.end(); }
  
  inline BasicBlock *getBlock() const { return TheBB; }
  inline DomTreeNode *getIDom() const { return IDom; }
  inline ETNode *getETNode() const { return ETN; }
  inline const std::vector<DomTreeNode*> &getChildren() const { return Children; }
  
  inline DomTreeNode(BasicBlock *BB, DomTreeNode *iDom, ETNode *E) 
    : TheBB(BB), IDom(iDom), ETN(E) {
    if (IDom)
      ETN->setFather(IDom->getETNode());
  }
  inline DomTreeNode *addChild(DomTreeNode *C) { Children.push_back(C); return C; }
  void setIDom(DomTreeNode *NewIDom);
};

//===----------------------------------------------------------------------===//
/// DominatorTree - Calculate the immediate dominator tree for a function.
///
class DominatorTreeBase : public DominatorBase {

protected:
  void reset();
  typedef std::map<BasicBlock*, DomTreeNode*> DomTreeNodeMapType;
  DomTreeNodeMapType DomTreeNodes;
  DomTreeNode *RootNode;

  typedef std::map<BasicBlock*, ETNode*> ETMapType;
  ETMapType ETNodes;

  bool DFSInfoValid;
  unsigned int SlowQueries;
  // Information record used during immediate dominators computation.
  struct InfoRec {
    unsigned Semi;
    unsigned Size;
    BasicBlock *Label, *Parent, *Child, *Ancestor;

    std::vector<BasicBlock*> Bucket;

    InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){}
  };

  std::map<BasicBlock*, BasicBlock*> IDoms;

  // Vertex - Map the DFS number to the BasicBlock*
  std::vector<BasicBlock*> Vertex;

  // Info - Collection of information used during the computation of idoms.
  std::map<BasicBlock*, InfoRec> Info;

  public:
  DominatorTreeBase(intptr_t ID, bool isPostDom) 
    : DominatorBase(ID, isPostDom), DFSInfoValid(false), SlowQueries(0) {}
  ~DominatorTreeBase() { reset(); }

  virtual void releaseMemory() { reset(); }

  /// getNode - return the (Post)DominatorTree node for the specified basic
  /// block.  This is the same as using operator[] on this class.
  ///
  inline DomTreeNode *getNode(BasicBlock *BB) const {
    DomTreeNodeMapType::const_iterator i = DomTreeNodes.find(BB);
    return (i != DomTreeNodes.end()) ? i->second : 0;
  }

  inline DomTreeNode *operator[](BasicBlock *BB) const {
    return getNode(BB);
  }

  /// getIDomBlock - return basic block BB's immediate domiantor basic block.
  ///
  BasicBlock *getIDomBlock(BasicBlock *BB) {
    DomTreeNode *N = getNode(BB);
    assert (N && "Missing dominator tree node");
    DomTreeNode *I = N->getIDom();
    assert (N && "Missing immediate dominator");
    return I->getBlock();
  }

  /// getRootNode - This returns the entry node for the CFG of the function.  If
  /// this tree represents the post-dominance relations for a function, however,
  /// this root may be a node with the block == NULL.  This is the case when
  /// there are multiple exit nodes from a particular function.  Consumers of
  /// post-dominance information must be capable of dealing with this
  /// possibility.
  ///
  DomTreeNode *getRootNode() { return RootNode; }
  const DomTreeNode *getRootNode() const { return RootNode; }

  /// properlyDominates - Returns true iff this dominates N and this != N.
  /// Note that this is not a constant time operation!
  ///
  bool properlyDominates(const DomTreeNode *A, DomTreeNode *B) const {
    if (A == 0 || B == 0) return false;
    return dominatedBySlowTreeWalk(A, B);
  }

  inline bool properlyDominates(BasicBlock *A, BasicBlock *B) {
    return properlyDominates(getNode(A), getNode(B));
  }

  bool dominatedBySlowTreeWalk(const DomTreeNode *A, 
                               const DomTreeNode *B) const {
    const DomTreeNode *IDom;
    if (A == 0 || B == 0) return false;
    while ((IDom = B->getIDom()) != 0 && IDom != A)
      B = IDom;   // Walk up the tree
    return IDom != 0;
  }

  void updateDFSNumbers();  

  /// Return the nearest common dominator of A and B.
  BasicBlock *nearestCommonDominator(BasicBlock *A, BasicBlock *B) const  {
    ETNode *NodeA = getNode(A)->getETNode();
    ETNode *NodeB = getNode(B)->getETNode();
    
    ETNode *Common = NodeA->NCA(NodeB);
    if (!Common)
      return NULL;
    return Common->getData<BasicBlock>();
  }
  
  /// dominates - Returns true iff this dominates N.  Note that this is not a
  /// constant time operation!
  ///
  inline bool dominates(const DomTreeNode *A, DomTreeNode *B) {
    if (B == A) 
      return true;  // A node trivially dominates itself.

    if (A == 0 || B == 0)
      return false;

    ETNode *NodeA = A->getETNode();
    ETNode *NodeB = B->getETNode();
    
    if (DFSInfoValid)
      return NodeB->DominatedBy(NodeA);

    // If we end up with too many slow queries, just update the
    // DFS numbers on the theory that we are going to keep querying.
    SlowQueries++;
    if (SlowQueries > 32) {
      updateDFSNumbers();
      return NodeB->DominatedBy(NodeA);
    }
    //return NodeB->DominatedBySlow(NodeA);
    return dominatedBySlowTreeWalk(A, B);
{'fraction_non_alphanumeric': 0.16196384497365085, 'fraction_numerical': 0.002801681008605163, 'mean_word_length': 2.725646123260437, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3291572', 'n_tokens_mistral': 4463, 'n_tokens_neox': 4347, 'n_words': 1418}