blob: 3ee811a68302f49a0b5a4b245acc383e89c9fbfc [file] [log] [blame]
Florian Hahn7e64c1e2018-07-11 11:54:30 +00001//===- llvm/unittest/Transforms/Vectorize/VPlanTestBase.h -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Florian Hahn7e64c1e2018-07-11 11:54:30 +00006//
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/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"
22
23namespace llvm {
24
25/// Helper class to create a module from an assembly string and VPlans for a
26/// given loop entry block.
27class VPlanTestBase : public testing::Test {
28protected:
29 std::unique_ptr<LLVMContext> Ctx;
30 std::unique_ptr<Module> M;
31 std::unique_ptr<LoopInfo> LI;
32 std::unique_ptr<DominatorTree> DT;
33
34 VPlanTestBase() : Ctx(new LLVMContext) {}
35
36 Module &parseModule(const char *ModuleString) {
37 SMDiagnostic Err;
38 M = parseAssemblyString(ModuleString, Err, *Ctx);
39 EXPECT_TRUE(M);
40 return *M;
41 }
42
43 void doAnalysis(Function &F) {
44 DT.reset(new DominatorTree(F));
45 LI.reset(new LoopInfo(*DT));
46 }
47
48 VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
49 doAnalysis(*LoopHeader->getParent());
50
51 auto Plan = llvm::make_unique<VPlan>();
Diego Caballero2a34ac82018-07-30 21:33:31 +000052 VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
53 HCFGBuilder.buildHierarchicalCFG();
54 return Plan;
55 }
56
57 /// Build the VPlan plain CFG for the loop starting from \p LoopHeader.
58 VPlanPtr buildPlainCFG(BasicBlock *LoopHeader) {
59 doAnalysis(*LoopHeader->getParent());
60
61 auto Plan = llvm::make_unique<VPlan>();
62 VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get(), *Plan);
63 VPRegionBlock *TopRegion = HCFGBuilder.buildPlainCFG();
64 Plan->setEntry(TopRegion);
Florian Hahn7e64c1e2018-07-11 11:54:30 +000065 return Plan;
66 }
67};
68
69} // namespace llvm
70
71#endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H