Florian Hahn | e60b36c | 2019-12-07 08:52:36 +0000 | [diff] [blame] | 1 | //===- VPlanTransforms.h - Utility VPlan to VPlan transforms --------------===// |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file provides utility VPlan to VPlan transformations. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Florian Hahn | e60b36c | 2019-12-07 08:52:36 +0000 | [diff] [blame] | 13 | #ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANTRANSFORMS_H |
| 14 | #define LLVM_TRANSFORMS_VECTORIZE_VPLANTRANSFORMS_H |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 15 | |
| 16 | #include "VPlan.h" |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 17 | #include "VPlanVerifier.h" |
serge-sans-paille | 1b89c83 | 2022-03-21 21:52:21 +0100 | [diff] [blame] | 18 | #include "llvm/ADT/STLFunctionalExtras.h" |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | |
Florian Hahn | 978883d | 2021-12-10 09:55:09 +0000 | [diff] [blame] | 23 | class InductionDescriptor; |
Florian Hahn | 49999d4 | 2021-04-15 19:07:28 +0100 | [diff] [blame] | 24 | class Instruction; |
Florian Hahn | 978883d | 2021-12-10 09:55:09 +0000 | [diff] [blame] | 25 | class PHINode; |
Florian Hahn | 49999d4 | 2021-04-15 19:07:28 +0100 | [diff] [blame] | 26 | class ScalarEvolution; |
Florian Hahn | e1650c8 | 2022-12-23 12:51:20 +0000 | [diff] [blame] | 27 | class PredicatedScalarEvolution; |
Florian Hahn | fc444dd | 2022-09-01 13:14:40 +0100 | [diff] [blame] | 28 | class TargetLibraryInfo; |
Florian Hahn | c83fdc9 | 2023-02-08 15:49:29 +0000 | [diff] [blame] | 29 | class VPBuilder; |
Florian Hahn | 16910a2 | 2024-08-28 15:56:09 +0100 | [diff] [blame] | 30 | class VPRecipeBuilder; |
Kazu Hirata | f0621b3 | 2025-04-17 16:23:51 -0700 | [diff] [blame^] | 31 | struct VFRange; |
Florian Hahn | 49999d4 | 2021-04-15 19:07:28 +0100 | [diff] [blame] | 32 | |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 33 | extern cl::opt<bool> VerifyEachVPlan; |
| 34 | |
Florian Hahn | ae1e353 | 2020-04-12 10:51:17 +0100 | [diff] [blame] | 35 | struct VPlanTransforms { |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 36 | /// Helper to run a VPlan transform \p Transform on \p VPlan, forwarding extra |
| 37 | /// arguments to the transform. Returns the boolean returned by the transform. |
| 38 | template <typename... ArgsTy> |
| 39 | static bool runPass(bool (*Transform)(VPlan &, ArgsTy...), VPlan &Plan, |
| 40 | typename std::remove_reference<ArgsTy>::type &...Args) { |
| 41 | bool Res = Transform(Plan, Args...); |
| 42 | if (VerifyEachVPlan) |
| 43 | verifyVPlanIsValid(Plan); |
| 44 | return Res; |
| 45 | } |
| 46 | /// Helper to run a VPlan transform \p Transform on \p VPlan, forwarding extra |
| 47 | /// arguments to the transform. |
| 48 | template <typename... ArgsTy> |
| 49 | static void runPass(void (*Fn)(VPlan &, ArgsTy...), VPlan &Plan, |
| 50 | typename std::remove_reference<ArgsTy>::type &...Args) { |
| 51 | Fn(Plan, Args...); |
| 52 | if (VerifyEachVPlan) |
| 53 | verifyVPlanIsValid(Plan); |
| 54 | } |
| 55 | |
Florian Hahn | bc03d6c | 2025-04-16 13:30:45 +0200 | [diff] [blame] | 56 | /// Replace loops in \p Plan's flat CFG with VPRegionBlocks, turing \p Plan's |
| 57 | /// flat CFG into a hierarchical CFG. It also creates a VPValue expression for |
| 58 | /// the original trip count. It will also introduce a dedicated VPBasicBlock |
| 59 | /// for the vector pre-header as well a VPBasicBlock as exit block of the |
| 60 | /// region (middle.block). If a check is needed to guard executing the scalar |
Florian Hahn | fd26708 | 2025-03-09 15:05:35 +0000 | [diff] [blame] | 61 | /// epilogue loop, it will be added to the middle block, together with |
| 62 | /// VPBasicBlocks for the scalar preheader and exit blocks. \p InductionTy is |
| 63 | /// the type of the canonical induction and used for related values, like the |
| 64 | /// trip count expression. |
Florian Hahn | bc03d6c | 2025-04-16 13:30:45 +0200 | [diff] [blame] | 65 | static void createLoopRegions(VPlan &Plan, Type *InductionTy, |
| 66 | PredicatedScalarEvolution &PSE, |
| 67 | bool RequiresScalarEpilogueCheck, |
| 68 | bool TailFolded, Loop *TheLoop); |
Florian Hahn | fd26708 | 2025-03-09 15:05:35 +0000 | [diff] [blame] | 69 | |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 70 | /// Replaces the VPInstructions in \p Plan with corresponding |
Florian Hahn | 2e13ec5 | 2025-03-19 21:35:15 +0000 | [diff] [blame] | 71 | /// widen recipes. Returns false if any VPInstructions could not be converted |
| 72 | /// to a wide recipe if needed. |
| 73 | static bool tryToConvertVPInstructionsToVPRecipes( |
| 74 | VPlanPtr &Plan, |
| 75 | function_ref<const InductionDescriptor *(PHINode *)> |
| 76 | GetIntOrFpInductionDescriptor, |
| 77 | ScalarEvolution &SE, const TargetLibraryInfo &TLI); |
Florian Hahn | 65d3dd7 | 2021-05-24 14:14:08 +0100 | [diff] [blame] | 78 | |
Florian Hahn | 2dfb1c6 | 2024-10-23 13:12:03 -0700 | [diff] [blame] | 79 | /// Try to have all users of fixed-order recurrences appear after the recipe |
| 80 | /// defining their previous value, by either sinking users or hoisting recipes |
| 81 | /// defining their previous value (and its operands). Then introduce |
| 82 | /// FirstOrderRecurrenceSplice VPInstructions to combine the value from the |
| 83 | /// recurrence phis and previous values. |
Florian Hahn | a6d6730 | 2023-08-04 13:21:19 +0100 | [diff] [blame] | 84 | /// \returns true if all users of fixed-order recurrences could be re-arranged |
| 85 | /// as needed or false if it is not possible. In the latter case, \p Plan is |
| 86 | /// not valid. |
| 87 | static bool adjustFixedOrderRecurrences(VPlan &Plan, VPBuilder &Builder); |
| 88 | |
| 89 | /// Clear NSW/NUW flags from reduction instructions if necessary. |
| 90 | static void clearReductionWrapFlags(VPlan &Plan); |
| 91 | |
Florian Hahn | 8ec4067 | 2024-09-21 19:47:37 +0100 | [diff] [blame] | 92 | /// Explicitly unroll \p Plan by \p UF. |
| 93 | static void unrollByUF(VPlan &Plan, unsigned UF, LLVMContext &Ctx); |
| 94 | |
Florian Hahn | a6d6730 | 2023-08-04 13:21:19 +0100 | [diff] [blame] | 95 | /// Optimize \p Plan based on \p BestVF and \p BestUF. This may restrict the |
| 96 | /// resulting plan to \p BestVF and \p BestUF. |
| 97 | static void optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF, |
| 98 | unsigned BestUF, |
| 99 | PredicatedScalarEvolution &PSE); |
| 100 | |
| 101 | /// Apply VPlan-to-VPlan optimizations to \p Plan, including induction recipe |
| 102 | /// optimizations, dead recipe removal, replicate region optimizations and |
| 103 | /// block merging. |
David Sherwood | f3029b3 | 2024-09-13 12:09:00 +0100 | [diff] [blame] | 104 | static void optimize(VPlan &Plan); |
Florian Hahn | a6d6730 | 2023-08-04 13:21:19 +0100 | [diff] [blame] | 105 | |
Florian Hahn | 79272ec | 2023-03-08 20:11:27 +0100 | [diff] [blame] | 106 | /// Wrap predicated VPReplicateRecipes with a mask operand in an if-then |
Florian Hahn | eca14a8 | 2023-03-16 17:06:44 +0000 | [diff] [blame] | 107 | /// region block and remove the mask operand. Optimize the created regions by |
| 108 | /// iteratively sinking scalar operands into the region, followed by merging |
| 109 | /// regions until no improvements are remaining. |
Florian Hahn | 371bb2c | 2023-03-18 20:30:48 +0000 | [diff] [blame] | 110 | static void createAndOptimizeReplicateRegions(VPlan &Plan); |
Florian Hahn | 505ad03 | 2021-12-10 13:56:43 +0000 | [diff] [blame] | 111 | |
Florian Hahn | 97687b7 | 2023-09-25 13:34:44 +0100 | [diff] [blame] | 112 | /// Replace (ICMP_ULE, wide canonical IV, backedge-taken-count) checks with an |
| 113 | /// (active-lane-mask recipe, wide canonical IV, trip-count). If \p |
| 114 | /// UseActiveLaneMaskForControlFlow is true, introduce an |
| 115 | /// VPActiveLaneMaskPHIRecipe. If \p DataAndControlFlowWithoutRuntimeCheck is |
| 116 | /// true, no minimum-iteration runtime check will be created (during skeleton |
| 117 | /// creation) and instead it is handled using active-lane-mask. \p |
| 118 | /// DataAndControlFlowWithoutRuntimeCheck implies \p |
| 119 | /// UseActiveLaneMaskForControlFlow. |
| 120 | static void addActiveLaneMask(VPlan &Plan, |
| 121 | bool UseActiveLaneMaskForControlFlow, |
| 122 | bool DataAndControlFlowWithoutRuntimeCheck); |
| 123 | |
Florian Hahn | 70535f5 | 2023-12-02 16:12:38 +0000 | [diff] [blame] | 124 | /// Insert truncates and extends for any truncated recipe. Redundant casts |
| 125 | /// will be folded later. |
| 126 | static void |
| 127 | truncateToMinimalBitwidths(VPlan &Plan, |
David Sherwood | f3029b3 | 2024-09-13 12:09:00 +0100 | [diff] [blame] | 128 | const MapVector<Instruction *, uint64_t> &MinBWs); |
Florian Hahn | 70535f5 | 2023-12-02 16:12:38 +0000 | [diff] [blame] | 129 | |
Florian Hahn | debca7e | 2024-02-14 12:28:58 +0000 | [diff] [blame] | 130 | /// Drop poison flags from recipes that may generate a poison value that is |
| 131 | /// used after vectorization, even when their operands are not poison. Those |
| 132 | /// recipes meet the following conditions: |
| 133 | /// * Contribute to the address computation of a recipe generating a widen |
| 134 | /// memory load/store (VPWidenMemoryInstructionRecipe or |
| 135 | /// VPInterleaveRecipe). |
| 136 | /// * Such a widen memory load/store has at least one underlying Instruction |
| 137 | /// that is in a basic block that needs predication and after vectorization |
| 138 | /// the generated instruction won't be predicated. |
| 139 | /// Uses \p BlockNeedsPredication to check if a block needs predicating. |
| 140 | /// TODO: Replace BlockNeedsPredication callback with retrieving info from |
| 141 | /// VPlan directly. |
| 142 | static void dropPoisonGeneratingRecipes( |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 143 | VPlan &Plan, |
| 144 | const std::function<bool(BasicBlock *)> &BlockNeedsPredication); |
Alexey Bataev | 413a66f | 2024-04-04 18:30:17 -0400 | [diff] [blame] | 145 | |
| 146 | /// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and |
| 147 | /// replaces all uses except the canonical IV increment of |
| 148 | /// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe. |
| 149 | /// VPCanonicalIVPHIRecipe is only used to control the loop after |
| 150 | /// this transformation. |
Shih-Po Hung | 0338c55 | 2024-05-25 08:22:49 +0800 | [diff] [blame] | 151 | /// \returns true if the transformation succeeds, or false if it doesn't. |
Alexey Bataev | f148d57 | 2024-10-18 15:51:49 -0400 | [diff] [blame] | 152 | static bool |
| 153 | tryAddExplicitVectorLength(VPlan &Plan, |
| 154 | const std::optional<unsigned> &MaxEVLSafeElements); |
Florian Hahn | 16910a2 | 2024-08-28 15:56:09 +0100 | [diff] [blame] | 155 | |
| 156 | // For each Interleave Group in \p InterleaveGroups replace the Recipes |
| 157 | // widening its memory instructions with a single VPInterleaveRecipe at its |
| 158 | // insertion point. |
| 159 | static void createInterleaveGroups( |
Florian Hahn | 7f74651 | 2024-10-06 22:53:13 +0100 | [diff] [blame] | 160 | VPlan &Plan, |
| 161 | const SmallPtrSetImpl<const InterleaveGroup<Instruction> *> |
| 162 | &InterleaveGroups, |
Florian Hahn | 2b55ef1 | 2025-01-29 10:50:01 +0000 | [diff] [blame] | 163 | VPRecipeBuilder &RecipeBuilder, const bool &ScalarEpilogueAllowed); |
Florian Hahn | 53266f7 | 2024-09-22 21:50:14 +0100 | [diff] [blame] | 164 | |
| 165 | /// Remove dead recipes from \p Plan. |
| 166 | static void removeDeadRecipes(VPlan &Plan); |
Florian Hahn | a7fda0e | 2024-12-03 14:53:51 +0000 | [diff] [blame] | 167 | |
Florian Hahn | 5fae408 | 2024-12-11 21:11:05 +0000 | [diff] [blame] | 168 | /// Update \p Plan to account for the uncountable early exit block in \p |
| 169 | /// UncountableExitingBlock by |
| 170 | /// * updating the condition exiting the vector loop to include the early |
| 171 | /// exit conditions |
| 172 | /// * splitting the original middle block to branch to the early exit block |
| 173 | /// if taken. |
David Sherwood | 3bc2dad | 2025-01-30 10:37:00 +0000 | [diff] [blame] | 174 | static void handleUncountableEarlyExit(VPlan &Plan, ScalarEvolution &SE, |
Florian Hahn | 5fae408 | 2024-12-11 21:11:05 +0000 | [diff] [blame] | 175 | Loop *OrigLoop, |
| 176 | BasicBlock *UncountableExitingBlock, |
Elvis Wang | 69ade7c | 2025-04-18 06:51:36 +0800 | [diff] [blame] | 177 | VPRecipeBuilder &RecipeBuilder, |
| 178 | VFRange &Range); |
Florian Hahn | 5fae408 | 2024-12-11 21:11:05 +0000 | [diff] [blame] | 179 | |
Florian Hahn | 54b33eb | 2025-04-14 22:20:44 +0100 | [diff] [blame] | 180 | /// Lower abstract recipes to concrete ones, that can be codegen'd. Use \p |
| 181 | /// CanonicalIVTy as type for all un-typed live-ins in VPTypeAnalysis. |
| 182 | static void convertToConcreteRecipes(VPlan &Plan, Type &CanonicalIVTy); |
Florian Hahn | 2c87133 | 2025-01-19 19:32:02 +0000 | [diff] [blame] | 183 | |
Florian Hahn | 6ff8a06 | 2025-02-08 13:33:46 +0000 | [diff] [blame] | 184 | /// Perform instcombine-like simplifications on recipes in \p Plan. Use \p |
| 185 | /// CanonicalIVTy as type for all un-typed live-ins in VPTypeAnalysis. |
| 186 | static void simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy); |
| 187 | |
Florian Hahn | 2c87133 | 2025-01-19 19:32:02 +0000 | [diff] [blame] | 188 | /// If there's a single exit block, optimize its phi recipes that use exiting |
| 189 | /// IV values by feeding them precomputed end values instead, possibly taken |
| 190 | /// one step backwards. |
| 191 | static void |
| 192 | optimizeInductionExitUsers(VPlan &Plan, |
| 193 | DenseMap<VPValue *, VPValue *> &EndValues); |
Florian Hahn | 4277c21 | 2025-02-26 13:57:51 +0000 | [diff] [blame] | 194 | |
Florian Hahn | 62994c3 | 2025-03-12 22:03:18 +0000 | [diff] [blame] | 195 | /// Add explicit broadcasts for live-ins and VPValues defined in \p Plan's entry block if they are used as vectors. |
| 196 | static void materializeBroadcasts(VPlan &Plan); |
Florian Hahn | c73ad7b | 2025-03-20 19:41:36 +0000 | [diff] [blame] | 197 | |
| 198 | /// Try to convert a plan with interleave groups with VF elements to a plan |
| 199 | /// with the interleave groups replaced by wide loads and stores processing VF |
| 200 | /// elements, if all transformed interleave groups access the full vector |
| 201 | /// width (checked via \o VectorRegWidth). This effectively is a very simple |
| 202 | /// form of loop-aware SLP, where we use interleave groups to identify |
| 203 | /// candidates. |
| 204 | static void narrowInterleaveGroups(VPlan &Plan, ElementCount VF, |
| 205 | unsigned VectorRegWidth); |
Florian Hahn | 3385caa | 2018-06-18 18:28:49 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | } // namespace llvm |
| 209 | |
Florian Hahn | e60b36c | 2019-12-07 08:52:36 +0000 | [diff] [blame] | 210 | #endif // LLVM_TRANSFORMS_VECTORIZE_VPLANTRANSFORMS_H |