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/AssumptionCache.h"
18 #include "llvm/Analysis/BasicAliasAnalysis.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/AsmParser/Parser.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gtest/gtest.h"
29 /// Helper class to create a module from an assembly string and VPlans for a
30 /// given loop entry block.
31 class VPlanTestBase
: public testing::Test
{
33 TargetLibraryInfoImpl TLII
;
34 TargetLibraryInfo TLI
;
37 std::unique_ptr
<LLVMContext
> Ctx
;
38 std::unique_ptr
<Module
> M
;
39 std::unique_ptr
<LoopInfo
> LI
;
40 std::unique_ptr
<DominatorTree
> DT
;
41 std::unique_ptr
<AssumptionCache
> AC
;
42 std::unique_ptr
<ScalarEvolution
> SE
;
46 DL("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-"
47 "f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:"
49 Ctx(new LLVMContext
) {}
51 Module
&parseModule(const char *ModuleString
) {
53 M
= parseAssemblyString(ModuleString
, Err
, *Ctx
);
58 void doAnalysis(Function
&F
) {
59 DT
.reset(new DominatorTree(F
));
60 LI
.reset(new LoopInfo(*DT
));
61 AC
.reset(new AssumptionCache(F
));
62 SE
.reset(new ScalarEvolution(F
, TLI
, *AC
, *DT
, *LI
));
65 VPlanPtr
buildHCFG(BasicBlock
*LoopHeader
) {
66 Function
&F
= *LoopHeader
->getParent();
67 assert(!verifyFunction(F
) && "input function must be valid");
70 Loop
*L
= LI
->getLoopFor(LoopHeader
);
71 PredicatedScalarEvolution
PSE(*SE
, *L
);
72 auto Plan
= VPlan::createInitialVPlan(IntegerType::get(*Ctx
, 64), PSE
, true,
74 VPlanHCFGBuilder
HCFGBuilder(L
, LI
.get(), *Plan
);
75 HCFGBuilder
.buildHierarchicalCFG();
79 /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
80 VPlanPtr
buildPlainCFG(BasicBlock
*LoopHeader
) {
81 Function
&F
= *LoopHeader
->getParent();
82 assert(!verifyFunction(F
) && "input function must be valid");
85 Loop
*L
= LI
->getLoopFor(LoopHeader
);
86 PredicatedScalarEvolution
PSE(*SE
, *L
);
87 auto Plan
= VPlan::createInitialVPlan(IntegerType::get(*Ctx
, 64), PSE
, true,
89 VPlanHCFGBuilder
HCFGBuilder(L
, LI
.get(), *Plan
);
90 HCFGBuilder
.buildPlainCFG();
97 #endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H