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