blob: 0fc4a9a21c8809a6c597118c24ddb6ae94afd2de [file] [log] [blame]
Florian Hahn7e64c1e2018-07-11 11:54:30 +00001//===- llvm/unittest/Transforms/Vectorize/VPlanTestBase.h -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file defines a VPlanTestBase class, which provides helpers to parse
11/// a LLVM IR string and create VPlans given a loop entry block.
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
14#define LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
15
16#include "../lib/Transforms/Vectorize/VPlan.h"
17#include "../lib/Transforms/Vectorize/VPlanHCFGBuilder.h"
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/AsmParser/Parser.h"
20#include "llvm/IR/Dominators.h"
21#include "llvm/Support/SourceMgr.h"
22#include "gtest/gtest.h"
23
24namespace llvm {
25
26/// Helper class to create a module from an assembly string and VPlans for a
27/// given loop entry block.
28class VPlanTestBase : public testing::Test {
29protected:
30 std::unique_ptr<LLVMContext> Ctx;
31 std::unique_ptr<Module> M;
32 std::unique_ptr<LoopInfo> LI;
33 std::unique_ptr<DominatorTree> DT;
34
35 VPlanTestBase() : Ctx(new LLVMContext) {}
36
37 Module &parseModule(const char *ModuleString) {
38 SMDiagnostic Err;
39 M = parseAssemblyString(ModuleString, Err, *Ctx);
40 EXPECT_TRUE(M);
41 return *M;
42 }
43
44 void doAnalysis(Function &F) {
45 DT.reset(new DominatorTree(F));
46 LI.reset(new LoopInfo(*DT));
47 }
48
49 VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
50 doAnalysis(*LoopHeader->getParent());
51
52 auto Plan = llvm::make_unique<VPlan>();
Diego Caballero2a34ac82018-07-30 21:33:31 +000053 VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
54 HCFGBuilder.buildHierarchicalCFG();
55 return Plan;
56 }
57
58 /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
59 VPlanPtr buildPlainCFG(BasicBlock *LoopHeader) {
60 doAnalysis(*LoopHeader->getParent());
61
62 auto Plan = llvm::make_unique<VPlan>();
63 VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
64 VPRegionBlock *TopRegion = HCFGBuilder.buildPlainCFG();
65 Plan->setEntry(TopRegion);
Florian Hahn7e64c1e2018-07-11 11:54:30 +000066 return Plan;
67 }
68};
69
70} // namespace llvm
71
72#endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H