xref: /llvm-project/llvm/unittests/Transforms/Vectorize/VPlanTestBase.h (revision 8caeb2e0c2fb8a5f1689c11775b81ceee76de958)
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/IR/Verifier.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gtest/gtest.h"
26 
27 namespace llvm {
28 
29 /// Helper class to create a module from an assembly string and VPlans for a
30 /// given loop entry block.
31 class VPlanTestIRBase : public testing::Test {
32 protected:
33   TargetLibraryInfoImpl TLII;
34   TargetLibraryInfo TLI;
35   DataLayout DL;
36 
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;
43 
44   VPlanTestIRBase()
45       : TLII(), TLI(TLII),
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:"
48            "16:32:64-S128"),
49         Ctx(new LLVMContext) {}
50 
51   Module &parseModule(const char *ModuleString) {
52     SMDiagnostic Err;
53     M = parseAssemblyString(ModuleString, Err, *Ctx);
54     EXPECT_TRUE(M);
55     return *M;
56   }
57 
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));
63   }
64 
65   VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
66     Function &F = *LoopHeader->getParent();
67     assert(!verifyFunction(F) && "input function must be valid");
68     doAnalysis(F);
69 
70     Loop *L = LI->getLoopFor(LoopHeader);
71     PredicatedScalarEvolution PSE(*SE, *L);
72     auto Plan = VPlan::createInitialVPlan(IntegerType::get(*Ctx, 64), PSE, true,
73                                           false, L);
74     VPlanHCFGBuilder HCFGBuilder(L, LI.get(), *Plan);
75     HCFGBuilder.buildHierarchicalCFG();
76     return Plan;
77   }
78 
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");
83     doAnalysis(F);
84 
85     Loop *L = LI->getLoopFor(LoopHeader);
86     PredicatedScalarEvolution PSE(*SE, *L);
87     auto Plan = VPlan::createInitialVPlan(IntegerType::get(*Ctx, 64), PSE, true,
88                                           false, L);
89     VPlanHCFGBuilder HCFGBuilder(L, LI.get(), *Plan);
90     HCFGBuilder.buildPlainCFG();
91     return Plan;
92   }
93 };
94 
95 class VPlanTestBase : public testing::Test {
96 protected:
97   LLVMContext C;
98   std::unique_ptr<BasicBlock> ScalarHeader;
99   SmallVector<std::unique_ptr<VPlan>> Plans;
100 
101   VPlanTestBase() : ScalarHeader(BasicBlock::Create(C, "scalar.header")) {
102     BranchInst::Create(&*ScalarHeader, &*ScalarHeader);
103   }
104 
105   VPlan &getPlan(VPValue *TC = nullptr) {
106     Plans.push_back(std::make_unique<VPlan>(&*ScalarHeader, TC));
107     return *Plans.back();
108   }
109 };
110 
111 } // namespace llvm
112 
113 #endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
114