1 //===- llvm/unittest/Transforms/Vectorize/VPlanTestBase.h -----------------===//
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 /// 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
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"
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
{
29 std::unique_ptr
<LLVMContext
> Ctx
;
30 std::unique_ptr
<Module
> M
;
31 std::unique_ptr
<LoopInfo
> LI
;
32 std::unique_ptr
<DominatorTree
> DT
;
34 VPlanTestBase() : Ctx(new LLVMContext
) {}
36 Module
&parseModule(const char *ModuleString
) {
38 M
= parseAssemblyString(ModuleString
, Err
, *Ctx
);
43 void doAnalysis(Function
&F
) {
44 DT
.reset(new DominatorTree(F
));
45 LI
.reset(new LoopInfo(*DT
));
48 VPlanPtr
buildHCFG(BasicBlock
*LoopHeader
) {
49 doAnalysis(*LoopHeader
->getParent());
51 auto Plan
= std::make_unique
<VPlan
>();
52 VPlanHCFGBuilder
HCFGBuilder(LI
->getLoopFor(LoopHeader
), LI
.get(), *Plan
);
53 HCFGBuilder
.buildHierarchicalCFG();
57 /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
58 VPlanPtr
buildPlainCFG(BasicBlock
*LoopHeader
) {
59 doAnalysis(*LoopHeader
->getParent());
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
);
71 #endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H