| 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/VPlanHelpers.h" |
| 17 |
#include "../lib/Transforms/Vectorize/VPlanTransforms.h" |
| 18 |
#include "llvm/Analysis/AssumptionCache.h" |
| 19 |
#include "llvm/Analysis/BasicAliasAnalysis.h" |
| 20 |
#include "llvm/Analysis/LoopInfo.h" |
| 21 |
#include "llvm/Analysis/TargetLibraryInfo.h" |
| 22 |
#include "llvm/AsmParser/Parser.h" |
| 23 |
#include "llvm/IR/Dominators.h" |
| 24 |
#include "llvm/IR/Verifier.h" |
| 25 |
#include "llvm/Support/SourceMgr.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
namespace llvm { |
| 29 |
|
| 30 |
/// Helper class to create a module from an assembly string and VPlans for a |
| 31 |
/// given loop entry block. |
| 32 |
class VPlanTestIRBase : public testing::Test { |
| 33 |
protected: |
| 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 |
std::unique_ptr<TargetLibraryInfoImpl> TLII; |
| 43 |
std::unique_ptr<TargetLibraryInfo> TLI; |
| 44 |
|
| 45 |
VPlanTestIRBase() |
| 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 |
TLII = std::make_unique<TargetLibraryInfoImpl>(M->getTargetTriple()); |
| 56 |
TLI = std::make_unique<TargetLibraryInfo>(*TLII); |
| 57 |
return *M; |
| 58 |
} |
| 59 |
|
| 60 |
void doAnalysis(Function &F) { |
| 61 |
DT.reset(new DominatorTree(F)); |
| 62 |
LI.reset(new LoopInfo(*DT)); |
| 63 |
AC.reset(new AssumptionCache(F)); |
| 64 |
SE.reset(new ScalarEvolution(F, *TLI, *AC, *DT, *LI)); |
| 65 |
} |
| 66 |
|
| 67 |
/// Build the VPlan for the loop starting from \p LoopHeader. |
| 68 |
VPlanPtr buildVPlan(BasicBlock *LoopHeader) { |
| 69 |
Function &F = *LoopHeader->getParent(); |
| 70 |
assert(!verifyFunction(F) && "input function must be valid"); |
| 71 |
doAnalysis(F); |
| 72 |
|
| 73 |
Loop *L = LI->getLoopFor(LoopHeader); |
| 74 |
PredicatedScalarEvolution PSE(*SE, *L); |
| 75 |
auto Plan = VPlanTransforms::buildPlainCFG(L, *LI); |
| 76 |
VFRange R(ElementCount::getFixed(1), ElementCount::getFixed(2)); |
| 77 |
VPlanTransforms::prepareForVectorization(*Plan, IntegerType::get(*Ctx, 64), |
| 78 |
PSE, true, false, L, {}, false, R); |
| 79 |
VPlanTransforms::createLoopRegions(*Plan); |
| 80 |
return Plan; |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
class VPlanTestBase : public testing::Test { |
| 85 |
protected: |
| 86 |
LLVMContext C; |
| 87 |
std::unique_ptr<BasicBlock> ScalarHeader; |
| 88 |
SmallVector<std::unique_ptr<VPlan>> Plans; |
| 89 |
|
| 90 |
VPlanTestBase() : ScalarHeader(BasicBlock::Create(C, "scalar.header")) { |
| 91 |
BranchInst::Create(&*ScalarHeader, &*ScalarHeader); |
| 92 |
} |
| 93 |
|
| 94 |
VPlan &getPlan(VPValue *TC = nullptr) { |
| 95 |
Plans.push_back(std::make_unique<VPlan>(&*ScalarHeader, TC)); |
| 96 |
return *Plans.back(); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
} // namespace llvm |
| 101 |
|
| 102 |
#endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H |