//===- CFG.h ----------------------------------------------------*- C++ -*-===// // // 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 // //===----------------------------------------------------------------------===// /// \file /// /// This file provides various utilities for inspecting and working with the /// control flow graph in LLVM IR. This includes generic facilities for /// iterating successors and predecessors of basic blocks, the successors of /// specific terminator instructions, etc. It also defines specializations of /// GraphTraits that allow Function and BasicBlock graphs to be treated as /// proper graphs for generic algorithms. /// //===----------------------------------------------------------------------===// #ifndef LLVM_IR_CFG_H #define LLVM_IR_CFG_H #include "llvm/ADT/GraphTraits.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Function.h" #include "llvm/IR/Value.h" #include <cassert> #include <cstddef> #include <iterator> namespace llvm { class Instruction; class Use; //===----------------------------------------------------------------------===// // BasicBlock pred_iterator definition //===----------------------------------------------------------------------===// template <class Ptr, class USE_iterator> // Predecessor Iterator class PredIterator { … }; pred_iterator; const_pred_iterator; pred_range; const_pred_range; inline pred_iterator pred_begin(BasicBlock *BB) { … } inline const_pred_iterator pred_begin(const BasicBlock *BB) { … } inline pred_iterator pred_end(BasicBlock *BB) { … } inline const_pred_iterator pred_end(const BasicBlock *BB) { … } inline bool pred_empty(const BasicBlock *BB) { … } /// Get the number of predecessors of \p BB. This is a linear time operation. /// Use \ref BasicBlock::hasNPredecessors() or hasNPredecessorsOrMore if able. inline unsigned pred_size(const BasicBlock *BB) { … } inline pred_range predecessors(BasicBlock *BB) { … } inline const_pred_range predecessors(const BasicBlock *BB) { … } //===----------------------------------------------------------------------===// // Instruction and BasicBlock succ_iterator helpers //===----------------------------------------------------------------------===// template <class InstructionT, class BlockT> class SuccIterator : public iterator_facade_base<SuccIterator<InstructionT, BlockT>, std::random_access_iterator_tag, BlockT, int, BlockT *, BlockT *> { … }; succ_iterator; const_succ_iterator; succ_range; const_succ_range; inline succ_iterator succ_begin(Instruction *I) { … } inline const_succ_iterator succ_begin(const Instruction *I) { … } inline succ_iterator succ_end(Instruction *I) { … } inline const_succ_iterator succ_end(const Instruction *I) { … } inline bool succ_empty(const Instruction *I) { … } inline unsigned succ_size(const Instruction *I) { … } inline succ_range successors(Instruction *I) { … } inline const_succ_range successors(const Instruction *I) { … } inline succ_iterator succ_begin(BasicBlock *BB) { … } inline const_succ_iterator succ_begin(const BasicBlock *BB) { … } inline succ_iterator succ_end(BasicBlock *BB) { … } inline const_succ_iterator succ_end(const BasicBlock *BB) { … } inline bool succ_empty(const BasicBlock *BB) { … } inline unsigned succ_size(const BasicBlock *BB) { … } inline succ_range successors(BasicBlock *BB) { … } inline const_succ_range successors(const BasicBlock *BB) { … } //===--------------------------------------------------------------------===// // GraphTraits specializations for basic block graphs (CFGs) //===--------------------------------------------------------------------===// // Provide specializations of GraphTraits to be able to treat a function as a // graph of basic blocks... template <> struct GraphTraits<BasicBlock*> { … }; static_assert …; template <> struct GraphTraits<const BasicBlock*> { … }; static_assert …; // Provide specializations of GraphTraits to be able to treat a function as a // graph of basic blocks... and to walk it in inverse order. Inverse order for // a function is considered to be when traversing the predecessor edges of a BB // instead of the successor edges. // template <> struct GraphTraits<Inverse<BasicBlock*>> { … }; static_assert …; template <> struct GraphTraits<Inverse<const BasicBlock*>> { … }; static_assert …; //===--------------------------------------------------------------------===// // GraphTraits specializations for function basic block graphs (CFGs) //===--------------------------------------------------------------------===// // Provide specializations of GraphTraits to be able to treat a function as a // graph of basic blocks... these are the same as the basic block iterators, // except that the root node is implicitly the first node of the function. // template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> { … }; template <> struct GraphTraits<const Function*> : public GraphTraits<const BasicBlock*> { … }; // Provide specializations of GraphTraits to be able to treat a function as a // graph of basic blocks... and to walk it in inverse order. Inverse order for // a function is considered to be when traversing the predecessor edges of a BB // instead of the successor edges. // template <> struct GraphTraits<Inverse<Function*>> : public GraphTraits<Inverse<BasicBlock*>> { … }; template <> struct GraphTraits<Inverse<const Function*>> : public GraphTraits<Inverse<const BasicBlock*>> { … }; } // end namespace llvm #endif // LLVM_IR_CFG_H