|
| 1 | +//===- llvm/unittests/Transforms/Vectorize/VPlanUncountableExitTest.cpp ---===// |
| 2 | +// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "../lib/Transforms/Vectorize/VPlan.h" |
| 11 | +#include "../lib/Transforms/Vectorize/VPlanUtils.h" |
| 12 | +#include "VPlanTestBase.h" |
| 13 | +#include "llvm/ADT/SmallVector.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +namespace llvm { |
| 17 | + |
| 18 | +namespace { |
| 19 | +class VPUncountableExitTest : public VPlanTestIRBase {}; |
| 20 | + |
| 21 | +TEST_F(VPUncountableExitTest, FindUncountableExitRecipes) { |
| 22 | + const char *ModuleString = |
| 23 | + "define void @f(ptr %array, ptr %pred) {\n" |
| 24 | + "entry:\n" |
| 25 | + " br label %for.body\n" |
| 26 | + "for.body:\n" |
| 27 | + " %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.inc ]\n" |
| 28 | + " %st.addr = getelementptr inbounds i16, ptr %array, i64 %iv\n" |
| 29 | + " %data = load i16, ptr %st.addr, align 2\n" |
| 30 | + " %inc = add nsw i16 %data, 1\n" |
| 31 | + " store i16 %inc, ptr %st.addr, align 2\n" |
| 32 | + " %uncountable.addr = getelementptr inbounds nuw i16, ptr %pred, i64 " |
| 33 | + "%iv\n" |
| 34 | + " %uncountable.val = load i16, ptr %uncountable.addr, align 2\n" |
| 35 | + " %uncountable.cond = icmp sgt i16 %uncountable.val, 500\n" |
| 36 | + " br i1 %uncountable.cond, label %exit, label %for.inc\n" |
| 37 | + "for.inc:\n" |
| 38 | + " %iv.next = add nuw nsw i64 %iv, 1\n" |
| 39 | + " %countable.cond = icmp eq i64 %iv.next, 20\n" |
| 40 | + " br i1 %countable.cond, label %exit, label %for.body\n" |
| 41 | + "exit:\n" |
| 42 | + " ret void\n" |
| 43 | + "}\n"; |
| 44 | + |
| 45 | + Module &M = parseModule(ModuleString); |
| 46 | + |
| 47 | + Function *F = M.getFunction("f"); |
| 48 | + BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor(); |
| 49 | + auto Plan = buildVPlan(LoopHeader, /*HasUncountableExit=*/true); |
| 50 | + VPlanTransforms::tryToConvertVPInstructionsToVPRecipes( |
| 51 | + Plan, [](PHINode *P) { return nullptr; }, *TLI); |
| 52 | + VPlanTransforms::runPass(VPlanTransforms::optimize, *Plan); |
| 53 | + |
| 54 | + SmallVector<VPRecipeBase *> Recipes; |
| 55 | + SmallVector<VPRecipeBase *> GEPs; |
| 56 | + |
| 57 | + std::optional<VPValue *> UncountableCondition = |
| 58 | + vputils::getRecipesForUncountableExit(*Plan, Recipes, GEPs); |
| 59 | + ASSERT_TRUE(UncountableCondition.has_value()); |
| 60 | + ASSERT_EQ(GEPs.size(), 1ull); |
| 61 | + ASSERT_EQ(Recipes.size(), 3ull); |
| 62 | +} |
| 63 | + |
| 64 | +TEST_F(VPUncountableExitTest, NoUncountableExit) { |
| 65 | + const char *ModuleString = |
| 66 | + "define void @f(ptr %array, ptr %pred) {\n" |
| 67 | + "entry:\n" |
| 68 | + " br label %for.body\n" |
| 69 | + "for.body:\n" |
| 70 | + " %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]\n" |
| 71 | + " %st.addr = getelementptr inbounds i16, ptr %array, i64 %iv\n" |
| 72 | + " %data = load i16, ptr %st.addr, align 2\n" |
| 73 | + " %inc = add nsw i16 %data, 1\n" |
| 74 | + " store i16 %inc, ptr %st.addr, align 2\n" |
| 75 | + " %iv.next = add nuw nsw i64 %iv, 1\n" |
| 76 | + " %countable.cond = icmp eq i64 %iv.next, 20\n" |
| 77 | + " br i1 %countable.cond, label %exit, label %for.body\n" |
| 78 | + "exit:\n" |
| 79 | + " ret void\n" |
| 80 | + "}\n"; |
| 81 | + |
| 82 | + Module &M = parseModule(ModuleString); |
| 83 | + |
| 84 | + Function *F = M.getFunction("f"); |
| 85 | + BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor(); |
| 86 | + auto Plan = buildVPlan(LoopHeader); |
| 87 | + VPlanTransforms::tryToConvertVPInstructionsToVPRecipes( |
| 88 | + Plan, [](PHINode *P) { return nullptr; }, *TLI); |
| 89 | + VPlanTransforms::runPass(VPlanTransforms::optimize, *Plan); |
| 90 | + |
| 91 | + SmallVector<VPRecipeBase *> Recipes; |
| 92 | + SmallVector<VPRecipeBase *> GEPs; |
| 93 | + |
| 94 | + std::optional<VPValue *> UncountableCondition = |
| 95 | + vputils::getRecipesForUncountableExit(*Plan, Recipes, GEPs); |
| 96 | + ASSERT_FALSE(UncountableCondition.has_value()); |
| 97 | + ASSERT_EQ(GEPs.size(), 0ull); |
| 98 | + ASSERT_EQ(Recipes.size(), 0ull); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | +} // namespace llvm |
0 commit comments