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/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Debug.h"
37 std::unique_ptr
<LLVMContext
> Context
;
38 std::unique_ptr
<Module
> M
;
41 CFGHolder(StringRef ModuleName
= "m", StringRef FunctionName
= "foo");
42 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
46 /// CFGBuilder builds IR with specific CFG, based on the supplied list of arcs.
47 /// It's able to apply the provided updates and automatically modify the IR.
49 /// Internally it makes every basic block end with either SwitchInst or with
50 /// UnreachableInst. When all arc to a BB are deleted, the BB remains in the
51 /// function and doesn't get deleted.
59 friend bool operator<(const Arc
&LHS
, const Arc
&RHS
) {
60 return std::tie(LHS
.From
, LHS
.To
) <
61 std::tie(RHS
.From
, RHS
.To
);
65 enum class ActionKind
{ Insert
, Delete
};
71 CFGBuilder(Function
*F
, const std::vector
<Arc
> &InitialArcs
,
72 std::vector
<Update
> Updates
);
74 BasicBlock
*getOrAddBlock(StringRef BlockName
);
75 Optional
<Update
> getNextUpdate() const;
76 Optional
<Update
> applyUpdate();
77 void dump(raw_ostream
&OS
= dbgs()) const;
80 void buildCFG(const std::vector
<Arc
> &Arcs
);
81 bool connect(const Arc
&A
);
82 bool disconnect(const Arc
&A
);
85 unsigned UpdateIdx
= 0;
86 StringMap
<BasicBlock
*> NameToBlock
;
88 std::vector
<Update
> Updates
;