[llvm-exegesis] Fix missing std::move.
[llvm-complete.git] / unittests / IR / CFGBuilder.h
blobd9d9c378e110324204c38b93ae09fc05fcd489f1
1 //===- 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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UNITTESTS_CFG_BUILDER_H
16 #define LLVM_UNITTESTS_CFG_BUILDER_H
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"
24 #include <memory>
25 #include <set>
26 #include <tuple>
27 #include <vector>
29 namespace llvm {
31 class LLVMContext;
32 class Module;
33 class Function;
34 class BasicBlock;
35 class raw_ostream;
37 struct CFGHolder {
38 std::unique_ptr<LLVMContext> Context;
39 std::unique_ptr<Module> M;
40 Function *F;
42 CFGHolder(StringRef ModuleName = "m", StringRef FunctionName = "foo");
43 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
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 ///
54 class CFGBuilder {
55 public:
56 struct Arc {
57 StringRef From;
58 StringRef To;
60 friend bool operator<(const Arc &LHS, const Arc &RHS) {
61 return std::tie(LHS.From, LHS.To) <
62 std::tie(RHS.From, RHS.To);
66 enum class ActionKind { Insert, Delete };
67 struct Update {
68 ActionKind Action;
69 Arc Edge;
72 CFGBuilder(Function *F, const std::vector<Arc> &InitialArcs,
73 std::vector<Update> Updates);
75 BasicBlock *getOrAddBlock(StringRef BlockName);
76 Optional<Update> getNextUpdate() const;
77 Optional<Update> applyUpdate();
78 void dump(raw_ostream &OS = dbgs()) const;
80 private:
81 void buildCFG(const std::vector<Arc> &Arcs);
82 bool connect(const Arc &A);
83 bool disconnect(const Arc &A);
85 Function *F;
86 unsigned UpdateIdx = 0;
87 StringMap<BasicBlock *> NameToBlock;
88 std::set<Arc> Arcs;
89 std::vector<Update> Updates;
92 } // namespace llvm
94 #endif