VPlanTestBase.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===- llvm/unittest/Transforms/Vectorize/VPlanTestBase.h -----------------===//
  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. /// This file defines a VPlanTestBase class, which provides helpers to parse
  10. /// a LLVM IR string and create VPlans given a loop entry block.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
  13. #define LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
  14. #include "../lib/Transforms/Vectorize/VPlan.h"
  15. #include "../lib/Transforms/Vectorize/VPlanHCFGBuilder.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/AsmParser/Parser.h"
  18. #include "llvm/IR/Dominators.h"
  19. #include "llvm/Support/SourceMgr.h"
  20. #include "gtest/gtest.h"
  21. namespace llvm {
  22. /// Helper class to create a module from an assembly string and VPlans for a
  23. /// given loop entry block.
  24. class VPlanTestBase : public testing::Test {
  25. protected:
  26. std::unique_ptr<LLVMContext> Ctx;
  27. std::unique_ptr<Module> M;
  28. std::unique_ptr<LoopInfo> LI;
  29. std::unique_ptr<DominatorTree> DT;
  30. VPlanTestBase() : Ctx(new LLVMContext) {}
  31. Module &parseModule(const char *ModuleString) {
  32. SMDiagnostic Err;
  33. M = parseAssemblyString(ModuleString, Err, *Ctx);
  34. EXPECT_TRUE(M);
  35. return *M;
  36. }
  37. void doAnalysis(Function &F) {
  38. DT.reset(new DominatorTree(F));
  39. LI.reset(new LoopInfo(*DT));
  40. }
  41. VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
  42. doAnalysis(*LoopHeader->getParent());
  43. auto Plan = std::make_unique<VPlan>();
  44. VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
  45. HCFGBuilder.buildHierarchicalCFG();
  46. return Plan;
  47. }
  48. /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
  49. VPlanPtr buildPlainCFG(BasicBlock *LoopHeader) {
  50. doAnalysis(*LoopHeader->getParent());
  51. auto Plan = std::make_unique<VPlan>();
  52. VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
  53. VPRegionBlock *TopRegion = HCFGBuilder.buildPlainCFG();
  54. Plan->setEntry(TopRegion);
  55. return Plan;
  56. }
  57. };
  58. } // namespace llvm
  59. #endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H