CFGBuilder.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===- CFGBuilder.h - CFG building and updating utility ----------*- C++ -*-==//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// CFGBuilders provides utilities fo building and updating CFG for testing
  10. /// purposes.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_UNITTESTS_CFG_BUILDER_H
  14. #define LLVM_UNITTESTS_CFG_BUILDER_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/Support/Debug.h"
  20. #include <memory>
  21. #include <set>
  22. #include <tuple>
  23. #include <vector>
  24. namespace llvm {
  25. class LLVMContext;
  26. class Module;
  27. class Function;
  28. class BasicBlock;
  29. class raw_ostream;
  30. struct CFGHolder {
  31. std::unique_ptr<LLVMContext> Context;
  32. std::unique_ptr<Module> M;
  33. Function *F;
  34. CFGHolder(StringRef ModuleName = "m", StringRef FunctionName = "foo");
  35. ~CFGHolder(); // Defined in the .cpp file so we can use forward declarations.
  36. };
  37. /// \brief
  38. /// CFGBuilder builds IR with specific CFG, based on the supplied list of arcs.
  39. /// It's able to apply the provided updates and automatically modify the IR.
  40. ///
  41. /// Internally it makes every basic block end with either SwitchInst or with
  42. /// UnreachableInst. When all arc to a BB are deleted, the BB remains in the
  43. /// function and doesn't get deleted.
  44. ///
  45. class CFGBuilder {
  46. public:
  47. struct Arc {
  48. StringRef From;
  49. StringRef To;
  50. friend bool operator<(const Arc &LHS, const Arc &RHS) {
  51. return std::tie(LHS.From, LHS.To) <
  52. std::tie(RHS.From, RHS.To);
  53. }
  54. };
  55. enum class ActionKind { Insert, Delete };
  56. struct Update {
  57. ActionKind Action;
  58. Arc Edge;
  59. };
  60. CFGBuilder(Function *F, const std::vector<Arc> &InitialArcs,
  61. std::vector<Update> Updates);
  62. BasicBlock *getOrAddBlock(StringRef BlockName);
  63. Optional<Update> getNextUpdate() const;
  64. Optional<Update> applyUpdate();
  65. void dump(raw_ostream &OS = dbgs()) const;
  66. private:
  67. void buildCFG(const std::vector<Arc> &Arcs);
  68. bool connect(const Arc &A);
  69. bool disconnect(const Arc &A);
  70. Function *F;
  71. unsigned UpdateIdx = 0;
  72. StringMap<BasicBlock *> NameToBlock;
  73. std::set<Arc> Arcs;
  74. std::vector<Update> Updates;
  75. };
  76. } // namespace llvm
  77. #endif