1 //===- CFGBuilder.h - CFG building and updating utility ----------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// CFGBuilders provides utilities fo building and updating CFG for testing
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"
38 std::unique_ptr
<LLVMContext
> Context
;
39 std::unique_ptr
<Module
> M
;
42 CFGHolder(StringRef ModuleName
= "m", StringRef FunctionName
= "foo");
43 ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
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.
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.
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
};
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;
81 void buildCFG(const std::vector
<Arc
> &Arcs
);
82 bool connect(const Arc
&A
);
83 bool disconnect(const Arc
&A
);
86 unsigned UpdateIdx
= 0;
87 StringMap
<BasicBlock
*> NameToBlock
;
89 std::vector
<Update
> Updates
;