blob: d9d9c378e110324204c38b93ae09fc05fcd489f1 [file] [log] [blame]
Jakub Kuderski40d67722017-07-13 21:16:01 +00001//===- CFGBuilder.h - CFG building and updating utility ----------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// CFGBuilders provides utilities fo building and updating CFG for testing
11/// purposes.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_UNITTESTS_CFG_BUILDER_H
16#define LLVM_UNITTESTS_CFG_BUILDER_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Debug.h"
23
24#include <memory>
25#include <set>
Jakub Kuderski486906f2017-07-13 23:11:57 +000026#include <tuple>
Jakub Kuderski40d67722017-07-13 21:16:01 +000027#include <vector>
28
29namespace llvm {
30
31class LLVMContext;
32class Module;
33class Function;
34class BasicBlock;
35class raw_ostream;
36
37struct CFGHolder {
38 std::unique_ptr<LLVMContext> Context;
39 std::unique_ptr<Module> M;
40 Function *F;
41
42 CFGHolder(StringRef ModuleName = "m", StringRef FunctionName = "foo");
43 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
44};
45
46/// \brief
47/// CFGBuilder builds IR with specific CFG, based on the supplied list of arcs.
48/// It's able to apply the provided updates and automatically modify the IR.
49///
50/// Internally it makes every basic block end with either SwitchInst or with
51/// UnreachableInst. When all arc to a BB are deleted, the BB remains in the
52/// function and doesn't get deleted.
53///
54class CFGBuilder {
55public:
56 struct Arc {
57 StringRef From;
58 StringRef To;
59
Jakub Kuderski486906f2017-07-13 23:11:57 +000060 friend bool operator<(const Arc &LHS, const Arc &RHS) {
61 return std::tie(LHS.From, LHS.To) <
62 std::tie(RHS.From, RHS.To);
63 }
Jakub Kuderski40d67722017-07-13 21:16:01 +000064 };
65
66 enum class ActionKind { Insert, Delete };
67 struct Update {
68 ActionKind Action;
Jakub Kuderski5073cf02017-07-13 21:52:56 +000069 Arc Edge;
Jakub Kuderski40d67722017-07-13 21:16:01 +000070 };
71
72 CFGBuilder(Function *F, const std::vector<Arc> &InitialArcs,
73 std::vector<Update> Updates);
74
75 BasicBlock *getOrAddBlock(StringRef BlockName);
76 Optional<Update> getNextUpdate() const;
77 Optional<Update> applyUpdate();
78 void dump(raw_ostream &OS = dbgs()) const;
79
80private:
81 void buildCFG(const std::vector<Arc> &Arcs);
82 bool connect(const Arc &A);
83 bool disconnect(const Arc &A);
84
85 Function *F;
86 unsigned UpdateIdx = 0;
87 StringMap<BasicBlock *> NameToBlock;
88 std::set<Arc> Arcs;
89 std::vector<Update> Updates;
90};
91
92} // namespace llvm
93
94#endif