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/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/Support/SourceMgr.h"
24 #include "gtest/gtest.h"
25 
26 namespace llvm {
27 
28 /// Helper class to create a module from an assembly string and VPlans for a
29 /// given loop entry block.
30 class VPlanTestBase : public testing::Test {
31 protected:
32   TargetLibraryInfoImpl TLII;
33   TargetLibraryInfo TLI;
34   DataLayout DL;
35 
36   std::unique_ptr<LLVMContext> Ctx;
37   std::unique_ptr<Module> M;
38   std::unique_ptr<LoopInfo> LI;
39   std::unique_ptr<DominatorTree> DT;
40   std::unique_ptr<AssumptionCache> AC;
41   std::unique_ptr<ScalarEvolution> SE;
42 
VPlanTestBase()43   VPlanTestBase()
44       : TLII(), TLI(TLII),
45         DL("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-"
46            "f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:"
47            "16:32:64-S128"),
48         Ctx(new LLVMContext) {}
49 
parseModule(const char * ModuleString)50   Module &parseModule(const char *ModuleString) {
51     SMDiagnostic Err;
52     M = parseAssemblyString(ModuleString, Err, *Ctx);
53     EXPECT_TRUE(M);
54     return *M;
55   }
56 
doAnalysis(Function & F)57   void doAnalysis(Function &F) {
58     DT.reset(new DominatorTree(F));
59     LI.reset(new LoopInfo(*DT));
60     AC.reset(new AssumptionCache(F));
61     SE.reset(new ScalarEvolution(F, TLI, *AC, *DT, *LI));
62   }
63 
buildHCFG(BasicBlock * LoopHeader)64   VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
65     doAnalysis(*LoopHeader->getParent());
66 
67     auto Plan = std::make_unique<VPlan>();
68     VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
69     HCFGBuilder.buildHierarchicalCFG();
70     return Plan;
71   }
72 
73   /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
buildPlainCFG(BasicBlock * LoopHeader)74   VPlanPtr buildPlainCFG(BasicBlock *LoopHeader) {
75     doAnalysis(*LoopHeader->getParent());
76 
77     auto Plan = std::make_unique<VPlan>();
78     VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
79     VPRegionBlock *TopRegion = HCFGBuilder.buildPlainCFG();
80     Plan->setEntry(TopRegion);
81     return Plan;
82   }
83 };
84 
85 } // namespace llvm
86 
87 #endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
88