annotate unittests/IR/CFGBuilder.h @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- CFGBuilder.h - CFG building and updating utility ----------*- C++ -*-==//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 /// \file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 /// CFGBuilders provides utilities fo building and updating CFG for testing
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 /// purposes.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #ifndef LLVM_UNITTESTS_CFG_BUILDER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #define LLVM_UNITTESTS_CFG_BUILDER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/ADT/DenseMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/ADT/Optional.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/ADT/StringMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/ADT/StringRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Support/Debug.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include <memory>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include <set>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include <tuple>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 class LLVMContext;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 class Module;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 class Function;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 class BasicBlock;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 class raw_ostream;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 struct CFGHolder {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 std::unique_ptr<LLVMContext> Context;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 std::unique_ptr<Module> M;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 Function *F;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 CFGHolder(StringRef ModuleName = "m", StringRef FunctionName = "foo");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 /// \brief
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 /// CFGBuilder builds IR with specific CFG, based on the supplied list of arcs.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 /// It's able to apply the provided updates and automatically modify the IR.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 /// Internally it makes every basic block end with either SwitchInst or with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 /// UnreachableInst. When all arc to a BB are deleted, the BB remains in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 /// function and doesn't get deleted.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 class CFGBuilder {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 struct Arc {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 StringRef From;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 StringRef To;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 friend bool operator<(const Arc &LHS, const Arc &RHS) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 return std::tie(LHS.From, LHS.To) <
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 std::tie(RHS.From, RHS.To);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 enum class ActionKind { Insert, Delete };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 struct Update {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 ActionKind Action;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 Arc Edge;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 CFGBuilder(Function *F, const std::vector<Arc> &InitialArcs,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 std::vector<Update> Updates);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 BasicBlock *getOrAddBlock(StringRef BlockName);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 Optional<Update> getNextUpdate() const;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 Optional<Update> applyUpdate();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 void dump(raw_ostream &OS = dbgs()) const;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 void buildCFG(const std::vector<Arc> &Arcs);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 bool connect(const Arc &A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 bool disconnect(const Arc &A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 Function *F;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 unsigned UpdateIdx = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 StringMap<BasicBlock *> NameToBlock;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 std::set<Arc> Arcs;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 std::vector<Update> Updates;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 } // namespace llvm
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 #endif