1 //===- CFGBuilder.h - CFG building and updating utility ----------*- C++ -*-==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// CFGBuilders provides utilities fo building and updating CFG for testing
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_UNITTESTS_CFG_BUILDER_H
15 #define LLVM_UNITTESTS_CFG_BUILDER_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Debug.h"
36 std::unique_ptr
<LLVMContext
> Context
;
37 std::unique_ptr
<Module
> M
;
40 CFGHolder(StringRef ModuleName
= "m", StringRef FunctionName
= "foo");
41 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
45 /// CFGBuilder builds IR with specific CFG, based on the supplied list of arcs.
46 /// It's able to apply the provided updates and automatically modify the IR.
48 /// Internally it makes every basic block end with either SwitchInst or with
49 /// UnreachableInst. When all arc to a BB are deleted, the BB remains in the
50 /// function and doesn't get deleted.
58 friend bool operator<(const Arc
&LHS
, const Arc
&RHS
) {
59 return std::tie(LHS
.From
, LHS
.To
) <
60 std::tie(RHS
.From
, RHS
.To
);
64 enum class ActionKind
{ Insert
, Delete
};
70 CFGBuilder(Function
*F
, const std::vector
<Arc
> &InitialArcs
,
71 std::vector
<Update
> Updates
);
73 BasicBlock
*getOrAddBlock(StringRef BlockName
);
74 std::optional
<Update
> getNextUpdate() const;
75 std::optional
<Update
> applyUpdate();
76 void dump(raw_ostream
&OS
= dbgs()) const;
79 void buildCFG(const std::vector
<Arc
> &Arcs
);
80 bool connect(const Arc
&A
);
81 bool disconnect(const Arc
&A
);
84 unsigned UpdateIdx
= 0;
85 StringMap
<BasicBlock
*> NameToBlock
;
87 std::vector
<Update
> Updates
;