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