2014-03-04 12:45:46 +01:00
|
|
|
//===- CFG.h - Process LLVM structures as graphs ----------------*- C++ -*-===//
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 22:02:53 +01:00
|
|
|
//
|
2002-04-07 22:49:59 +02:00
|
|
|
// This file defines specializations of GraphTraits that allow Function and
|
2002-02-12 22:02:53 +01:00
|
|
|
// BasicBlock graphs to be treated as proper graphs for generic algorithms.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:45:46 +01:00
|
|
|
#ifndef LLVM_IR_CFG_H
|
|
|
|
#define LLVM_IR_CFG_H
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2016-11-23 23:25:16 +01:00
|
|
|
#include "llvm/ADT/iterator.h"
|
2015-02-04 20:14:57 +01:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2016-11-23 23:25:16 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
2016-11-23 23:25:16 +01:00
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/type_traits.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
2002-02-12 23:35:32 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2009-08-28 01:44:33 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 23:35:32 +01:00
|
|
|
// BasicBlock pred_iterator definition
|
2009-08-28 01:44:33 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 23:35:32 +01:00
|
|
|
|
2010-04-14 17:59:02 +02:00
|
|
|
template <class Ptr, class USE_iterator> // Predecessor Iterator
|
2009-08-28 01:44:33 +02:00
|
|
|
class PredIterator : public std::iterator<std::forward_iterator_tag,
|
Seciton 24.2.2 of the C++ standard, [iterator.iterators], Table 106
requires that the return type of *r for all iterators r be reference,
where reference is defined in [iterator.requirements.general]/p11 as
iterator_traits<X>::reference, and X is the type of r.
But in CFG.h, the dereference operator of PredIterator and SuccIterator
return pointer, not reference.
Furthermore the nested type reference is value_type&, which is not the
type returned from operator*().
This patch simply makes the iterator::reference type value_type*, which
is what the operator*() returns, and then re-lables the return type as
reference.
From a functionality point of view, the only difference is that the
nested reference type is now value_type* instead of value_type&.
llvm-svn: 178240
2013-03-28 16:47:50 +01:00
|
|
|
Ptr, ptrdiff_t, Ptr*, Ptr*> {
|
2017-05-06 00:30:37 +02:00
|
|
|
using super =
|
|
|
|
std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t, Ptr*, Ptr*>;
|
|
|
|
using Self = PredIterator<Ptr, USE_iterator>;
|
2010-04-14 17:41:50 +02:00
|
|
|
USE_iterator It;
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2008-04-25 18:53:59 +02:00
|
|
|
inline void advancePastNonTerminators() {
|
2013-12-05 06:44:44 +01:00
|
|
|
// Loop to ignore non-terminator uses (for example BlockAddresses).
|
2008-04-25 18:53:59 +02:00
|
|
|
while (!It.atEnd() && !isa<TerminatorInst>(*It))
|
2003-04-26 19:38:26 +02:00
|
|
|
++It;
|
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2010-04-14 17:33:04 +02:00
|
|
|
public:
|
2017-05-06 00:30:37 +02:00
|
|
|
using pointer = typename super::pointer;
|
|
|
|
using reference = typename super::reference;
|
2010-04-14 17:33:04 +02:00
|
|
|
|
2016-11-23 23:25:16 +01:00
|
|
|
PredIterator() = default;
|
2014-03-09 04:16:01 +01:00
|
|
|
explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
|
2008-04-25 18:53:59 +02:00
|
|
|
advancePastNonTerminators();
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
2014-03-09 04:16:01 +01:00
|
|
|
inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2010-04-14 17:41:50 +02:00
|
|
|
inline bool operator==(const Self& x) const { return It == x.It; }
|
|
|
|
inline bool operator!=(const Self& x) const { return !operator==(x); }
|
2005-04-21 22:48:15 +02:00
|
|
|
|
Seciton 24.2.2 of the C++ standard, [iterator.iterators], Table 106
requires that the return type of *r for all iterators r be reference,
where reference is defined in [iterator.requirements.general]/p11 as
iterator_traits<X>::reference, and X is the type of r.
But in CFG.h, the dereference operator of PredIterator and SuccIterator
return pointer, not reference.
Furthermore the nested type reference is value_type&, which is not the
type returned from operator*().
This patch simply makes the iterator::reference type value_type*, which
is what the operator*() returns, and then re-lables the return type as
reference.
From a functionality point of view, the only difference is that the
nested reference type is now value_type* instead of value_type&.
llvm-svn: 178240
2013-03-28 16:47:50 +01:00
|
|
|
inline reference operator*() const {
|
2007-10-11 06:18:11 +02:00
|
|
|
assert(!It.atEnd() && "pred_iterator out of range!");
|
2008-04-25 18:53:59 +02:00
|
|
|
return cast<TerminatorInst>(*It)->getParent();
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
2010-07-08 18:52:57 +02:00
|
|
|
inline pointer *operator->() const { return &operator*(); }
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2010-04-14 17:41:50 +02:00
|
|
|
inline Self& operator++() { // Preincrement
|
2007-10-11 06:18:11 +02:00
|
|
|
assert(!It.atEnd() && "pred_iterator out of range!");
|
2008-04-25 18:53:59 +02:00
|
|
|
++It; advancePastNonTerminators();
|
2005-04-21 22:48:15 +02:00
|
|
|
return *this;
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2010-04-14 17:41:50 +02:00
|
|
|
inline Self operator++(int) { // Postincrement
|
|
|
|
Self tmp = *this; ++*this; return tmp;
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
2011-03-02 01:11:28 +01:00
|
|
|
|
|
|
|
/// getOperandNo - Return the operand number in the predecessor's
|
|
|
|
/// terminator of the successor.
|
|
|
|
unsigned getOperandNo() const {
|
|
|
|
return It.getOperandNo();
|
|
|
|
}
|
2012-02-17 19:59:53 +01:00
|
|
|
|
|
|
|
/// getUse - Return the operand Use in the predecessor's terminator
|
|
|
|
/// of the successor.
|
|
|
|
Use &getUse() const {
|
|
|
|
return It.getUse();
|
|
|
|
}
|
2002-02-12 23:35:32 +01:00
|
|
|
};
|
|
|
|
|
2017-05-06 00:30:37 +02:00
|
|
|
using pred_iterator = PredIterator<BasicBlock, Value::user_iterator>;
|
|
|
|
using const_pred_iterator =
|
|
|
|
PredIterator<const BasicBlock, Value::const_user_iterator>;
|
|
|
|
using pred_range = iterator_range<pred_iterator>;
|
|
|
|
using pred_const_range = iterator_range<const_pred_iterator>;
|
2002-02-12 23:35:32 +01:00
|
|
|
|
|
|
|
inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
|
2010-03-26 00:25:28 +01:00
|
|
|
inline const_pred_iterator pred_begin(const BasicBlock *BB) {
|
|
|
|
return const_pred_iterator(BB);
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
|
|
|
inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
|
2010-03-26 00:25:28 +01:00
|
|
|
inline const_pred_iterator pred_end(const BasicBlock *BB) {
|
|
|
|
return const_pred_iterator(BB, true);
|
2002-02-12 23:35:32 +01:00
|
|
|
}
|
2015-01-13 04:46:47 +01:00
|
|
|
inline bool pred_empty(const BasicBlock *BB) {
|
|
|
|
return pred_begin(BB) == pred_end(BB);
|
|
|
|
}
|
2015-02-04 20:14:57 +01:00
|
|
|
inline pred_range predecessors(BasicBlock *BB) {
|
|
|
|
return pred_range(pred_begin(BB), pred_end(BB));
|
|
|
|
}
|
|
|
|
inline pred_const_range predecessors(const BasicBlock *BB) {
|
|
|
|
return pred_const_range(pred_begin(BB), pred_end(BB));
|
|
|
|
}
|
2002-02-12 23:35:32 +01:00
|
|
|
|
2009-08-28 01:44:33 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-08-05 19:43:01 +02:00
|
|
|
// BasicBlock succ_iterator helpers
|
2009-08-28 01:44:33 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 23:35:32 +01:00
|
|
|
|
2017-05-06 00:30:37 +02:00
|
|
|
using succ_iterator =
|
|
|
|
TerminatorInst::SuccIterator<TerminatorInst *, BasicBlock>;
|
|
|
|
using succ_const_iterator =
|
|
|
|
TerminatorInst::SuccIterator<const TerminatorInst *, const BasicBlock>;
|
|
|
|
using succ_range = iterator_range<succ_iterator>;
|
|
|
|
using succ_const_range = iterator_range<succ_const_iterator>;
|
2002-02-12 23:35:32 +01:00
|
|
|
|
|
|
|
inline succ_iterator succ_begin(BasicBlock *BB) {
|
|
|
|
return succ_iterator(BB->getTerminator());
|
|
|
|
}
|
|
|
|
inline succ_const_iterator succ_begin(const BasicBlock *BB) {
|
|
|
|
return succ_const_iterator(BB->getTerminator());
|
|
|
|
}
|
|
|
|
inline succ_iterator succ_end(BasicBlock *BB) {
|
|
|
|
return succ_iterator(BB->getTerminator(), true);
|
|
|
|
}
|
|
|
|
inline succ_const_iterator succ_end(const BasicBlock *BB) {
|
|
|
|
return succ_const_iterator(BB->getTerminator(), true);
|
|
|
|
}
|
2015-01-13 04:46:47 +01:00
|
|
|
inline bool succ_empty(const BasicBlock *BB) {
|
|
|
|
return succ_begin(BB) == succ_end(BB);
|
|
|
|
}
|
2015-02-04 20:14:57 +01:00
|
|
|
inline succ_range successors(BasicBlock *BB) {
|
|
|
|
return succ_range(succ_begin(BB), succ_end(BB));
|
|
|
|
}
|
|
|
|
inline succ_const_range successors(const BasicBlock *BB) {
|
|
|
|
return succ_const_range(succ_begin(BB), succ_end(BB));
|
|
|
|
}
|
|
|
|
|
2015-08-05 19:43:01 +02:00
|
|
|
template <typename T, typename U>
|
|
|
|
struct isPodLike<TerminatorInst::SuccIterator<T, U>> {
|
2013-05-20 15:12:58 +02:00
|
|
|
static const bool value = isPodLike<T>::value;
|
|
|
|
};
|
|
|
|
|
2002-02-12 22:02:53 +01:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// GraphTraits specializations for basic block graphs (CFGs)
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
// Provide specializations of GraphTraits to be able to treat a function as a
|
2002-02-12 22:02:53 +01:00
|
|
|
// graph of basic blocks...
|
|
|
|
|
|
|
|
template <> struct GraphTraits<BasicBlock*> {
|
2017-05-06 00:30:37 +02:00
|
|
|
using NodeRef = BasicBlock *;
|
|
|
|
using ChildIteratorType = succ_iterator;
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<const BasicBlock*> {
|
2017-05-06 00:30:37 +02:00
|
|
|
using NodeRef = const BasicBlock *;
|
|
|
|
using ChildIteratorType = succ_const_iterator;
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(const BasicBlock *BB) { return BB; }
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
// Provide specializations of GraphTraits to be able to treat a function as a
|
2002-02-12 22:02:53 +01:00
|
|
|
// graph of basic blocks... and to walk it in inverse order. Inverse order for
|
2002-04-07 22:49:59 +02:00
|
|
|
// a function is considered to be when traversing the predecessor edges of a BB
|
2002-02-12 22:02:53 +01:00
|
|
|
// instead of the successor edges.
|
|
|
|
//
|
2016-11-23 23:25:16 +01:00
|
|
|
template <> struct GraphTraits<Inverse<BasicBlock*>> {
|
2017-05-06 00:30:37 +02:00
|
|
|
using NodeRef = BasicBlock *;
|
|
|
|
using ChildIteratorType = pred_iterator;
|
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
|
|
|
|
2016-11-23 23:25:16 +01:00
|
|
|
template <> struct GraphTraits<Inverse<const BasicBlock*>> {
|
2017-05-06 00:30:37 +02:00
|
|
|
using NodeRef = const BasicBlock *;
|
|
|
|
using ChildIteratorType = const_pred_iterator;
|
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(Inverse<const BasicBlock *> G) { return G.Graph; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
2002-04-07 22:49:59 +02:00
|
|
|
// GraphTraits specializations for function basic block graphs (CFGs)
|
2002-02-12 22:02:53 +01:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
// Provide specializations of GraphTraits to be able to treat a function as a
|
2002-02-12 22:02:53 +01:00
|
|
|
// graph of basic blocks... these are the same as the basic block iterators,
|
2002-04-07 22:49:59 +02:00
|
|
|
// except that the root node is implicitly the first node of the function.
|
2002-02-12 22:02:53 +01:00
|
|
|
//
|
2002-04-07 22:49:59 +02:00
|
|
|
template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(Function *F) { return &F->getEntryBlock(); }
|
2002-10-11 00:31:31 +02:00
|
|
|
|
|
|
|
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
|
2017-05-06 00:30:37 +02:00
|
|
|
using nodes_iterator = pointer_iterator<Function::iterator>;
|
2016-11-23 23:25:16 +01:00
|
|
|
|
2016-08-19 23:20:13 +02:00
|
|
|
static nodes_iterator nodes_begin(Function *F) {
|
|
|
|
return nodes_iterator(F->begin());
|
|
|
|
}
|
2016-11-23 23:25:16 +01:00
|
|
|
|
2016-08-19 23:20:13 +02:00
|
|
|
static nodes_iterator nodes_end(Function *F) {
|
|
|
|
return nodes_iterator(F->end());
|
|
|
|
}
|
2016-11-23 23:25:16 +01:00
|
|
|
|
|
|
|
static size_t size(Function *F) { return F->size(); }
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
2002-04-07 22:49:59 +02:00
|
|
|
template <> struct GraphTraits<const Function*> :
|
2002-02-12 22:02:53 +01:00
|
|
|
public GraphTraits<const BasicBlock*> {
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(const Function *F) { return &F->getEntryBlock(); }
|
2002-10-11 00:31:31 +02:00
|
|
|
|
|
|
|
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
|
2017-05-06 00:30:37 +02:00
|
|
|
using nodes_iterator = pointer_iterator<Function::const_iterator>;
|
2016-11-23 23:25:16 +01:00
|
|
|
|
2016-08-19 23:20:13 +02:00
|
|
|
static nodes_iterator nodes_begin(const Function *F) {
|
|
|
|
return nodes_iterator(F->begin());
|
|
|
|
}
|
2016-11-23 23:25:16 +01:00
|
|
|
|
2016-08-19 23:20:13 +02:00
|
|
|
static nodes_iterator nodes_end(const Function *F) {
|
|
|
|
return nodes_iterator(F->end());
|
|
|
|
}
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2016-11-23 23:25:16 +01:00
|
|
|
static size_t size(const Function *F) { return F->size(); }
|
|
|
|
};
|
2002-02-12 22:02:53 +01:00
|
|
|
|
2005-04-21 22:48:15 +02:00
|
|
|
// Provide specializations of GraphTraits to be able to treat a function as a
|
2002-02-12 22:02:53 +01:00
|
|
|
// graph of basic blocks... and to walk it in inverse order. Inverse order for
|
2002-04-07 22:49:59 +02:00
|
|
|
// a function is considered to be when traversing the predecessor edges of a BB
|
2002-02-12 22:02:53 +01:00
|
|
|
// instead of the successor edges.
|
|
|
|
//
|
2016-11-23 23:25:16 +01:00
|
|
|
template <> struct GraphTraits<Inverse<Function*>> :
|
|
|
|
public GraphTraits<Inverse<BasicBlock*>> {
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(Inverse<Function *> G) {
|
2003-09-20 16:39:18 +02:00
|
|
|
return &G.Graph->getEntryBlock();
|
2002-04-07 22:49:59 +02:00
|
|
|
}
|
2002-02-12 22:02:53 +01:00
|
|
|
};
|
2016-11-23 23:25:16 +01:00
|
|
|
template <> struct GraphTraits<Inverse<const Function*>> :
|
|
|
|
public GraphTraits<Inverse<const BasicBlock*>> {
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(Inverse<const Function *> G) {
|
2003-09-20 16:39:18 +02:00
|
|
|
return &G.Graph->getEntryBlock();
|
2002-02-12 22:02:53 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 23:25:16 +01:00
|
|
|
} // end namespace llvm
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2016-11-23 23:25:16 +01:00
|
|
|
#endif // LLVM_IR_CFG_H
|