Skip to content

[IR] Add block number traits to CFG #102758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions llvm/include/llvm/IR/CFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ template <> struct GraphTraits<BasicBlock*> {
static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }

static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
};

static_assert(GraphHasNodeNumbers<BasicBlock *>,
"GraphTraits getNumber() not detected");

template <> struct GraphTraits<const BasicBlock*> {
using NodeRef = const BasicBlock *;
using ChildIteratorType = const_succ_iterator;
Expand All @@ -314,8 +319,13 @@ template <> struct GraphTraits<const BasicBlock*> {

static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }

static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
};

static_assert(GraphHasNodeNumbers<const BasicBlock *>,
"GraphTraits getNumber() not detected");

// 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
Expand All @@ -328,17 +338,27 @@ template <> struct GraphTraits<Inverse<BasicBlock*>> {
static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }

static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
};

static_assert(GraphHasNodeNumbers<Inverse<BasicBlock *>>,
"GraphTraits getNumber() not detected");

template <> struct GraphTraits<Inverse<const BasicBlock*>> {
using NodeRef = const BasicBlock *;
using ChildIteratorType = const_pred_iterator;

static NodeRef getEntryNode(Inverse<const BasicBlock *> G) { return G.Graph; }
static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }

static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
};

static_assert(GraphHasNodeNumbers<Inverse<const BasicBlock *>>,
"GraphTraits getNumber() not detected");

//===--------------------------------------------------------------------===//
// GraphTraits specializations for function basic block graphs (CFGs)
//===--------------------------------------------------------------------===//
Expand All @@ -362,6 +382,13 @@ template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
}

static size_t size(Function *F) { return F->size(); }

static unsigned getMaxNumber(const Function *F) {
return F->getMaxBlockNumber();
}
static unsigned getNumberEpoch(const Function *F) {
return F->getBlockNumberEpoch();
}
};
template <> struct GraphTraits<const Function*> :
public GraphTraits<const BasicBlock*> {
Expand All @@ -379,6 +406,13 @@ template <> struct GraphTraits<const Function*> :
}

static size_t size(const Function *F) { return F->size(); }

static unsigned getMaxNumber(const Function *F) {
return F->getMaxBlockNumber();
}
static unsigned getNumberEpoch(const Function *F) {
return F->getBlockNumberEpoch();
}
};

// Provide specializations of GraphTraits to be able to treat a function as a
Expand All @@ -391,12 +425,26 @@ template <> struct GraphTraits<Inverse<Function*>> :
static NodeRef getEntryNode(Inverse<Function *> G) {
return &G.Graph->getEntryBlock();
}

static unsigned getMaxNumber(const Function *F) {
return F->getMaxBlockNumber();
}
static unsigned getNumberEpoch(const Function *F) {
return F->getBlockNumberEpoch();
}
};
template <> struct GraphTraits<Inverse<const Function*>> :
public GraphTraits<Inverse<const BasicBlock*>> {
static NodeRef getEntryNode(Inverse<const Function *> G) {
return &G.Graph->getEntryBlock();
}

static unsigned getMaxNumber(const Function *F) {
return F->getMaxBlockNumber();
}
static unsigned getNumberEpoch(const Function *F) {
return F->getBlockNumberEpoch();
}
};

} // end namespace llvm
Expand Down
Loading