108 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8/// \file9/// This file defines a VPlanTestBase class, which provides helpers to parse10/// a LLVM IR string and create VPlans given a loop entry block.11//===----------------------------------------------------------------------===//12#ifndef LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H13#define LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H14 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 28namespace llvm {29 30/// Helper class to create a module from an assembly string and VPlans for a31/// given loop entry block.32class VPlanTestIRBase : public testing::Test {33protected: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, bool HasUncountableExit = false) {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::buildVPlan0(L, *LI, IntegerType::get(*Ctx, 64),76 {}, PSE);77 78 VPlanTransforms::handleEarlyExits(*Plan, HasUncountableExit);79 VPlanTransforms::addMiddleCheck(*Plan, true, false);80 81 VPlanTransforms::createLoopRegions(*Plan);82 return Plan;83 }84};85 86class VPlanTestBase : public testing::Test {87protected:88 LLVMContext C;89 std::unique_ptr<BasicBlock> ScalarHeader;90 SmallVector<std::unique_ptr<VPlan>> Plans;91 92 VPlanTestBase() : ScalarHeader(BasicBlock::Create(C, "scalar.header")) {93 BranchInst::Create(&*ScalarHeader, &*ScalarHeader);94 }95 96 VPlan &getPlan() {97 Plans.push_back(std::make_unique<VPlan>(&*ScalarHeader));98 VPlan &Plan = *Plans.back();99 VPValue *DefaultTC = Plan.getConstantInt(32, 1024);100 Plan.setTripCount(DefaultTC);101 return Plan;102 }103};104 105} // namespace llvm106 107#endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H108