@@ -2790,34 +2790,32 @@ VPExpressionRecipe::VPExpressionRecipe(
27902790 R->removeFromParent ();
27912791 }
27922792
2793- // Keep track of how many instances of each recipe occur in the recipe list
2794- SmallMapVector<VPSingleDefRecipe *, unsigned , 4 > ExpressionRecipeCounts;
2795- for (auto *R : ExpressionRecipes) {
2796- auto *F = ExpressionRecipeCounts.find (R);
2797- if (F == ExpressionRecipeCounts.end ())
2798- ExpressionRecipeCounts.insert (std::make_pair (R, 1 ));
2799- else
2800- F->second ++;
2801- }
2802-
28032793 // Internalize all external operands to the expression recipes. To do so,
28042794 // create new temporary VPValues for all operands defined by a recipe outside
28052795 // the expression. The original operands are added as operands of the
28062796 // VPExpressionRecipe itself.
2797+
2798+ // This map caches the temporary placeholders so duplicates aren't created in
2799+ // the case that recipes share operands.
2800+ SmallMapVector<VPValue *, VPValue *, 4 > OperandPlaceholders;
2801+
28072802 for (auto *R : ExpressionRecipes) {
2808- auto *F = ExpressionRecipeCounts.find (R);
2809- F->second --;
28102803 for (const auto &[Idx, Op] : enumerate(R->operands ())) {
28112804 auto *Def = Op->getDefiningRecipe ();
28122805 if (Def && ExpressionRecipesAsSetOfUsers.contains (Def))
28132806 continue ;
28142807 addOperand (Op);
2815- auto *Tmp = new VPValue ();
2816- LiveInPlaceholders.push_back (Tmp);
2817- // Only modify this recipe's operands if it's the last time it occurs in
2818- // the recipe list
2819- if (F->second == 0 )
2820- R->setOperand (Idx, Tmp);
2808+ if (OperandPlaceholders.find (Op) == OperandPlaceholders.end ())
2809+ OperandPlaceholders[Op] = new VPValue ();
2810+ LiveInPlaceholders.push_back (OperandPlaceholders[Op]);
2811+ }
2812+ }
2813+
2814+ for (auto *R : ExpressionRecipes) {
2815+ for (const auto &[Idx, Op] : enumerate(R->operands ())) {
2816+ auto *Entry = OperandPlaceholders.find (Op);
2817+ if (Entry != OperandPlaceholders.end ())
2818+ R->setOperand (Idx, Entry->second );
28212819 }
28222820 }
28232821}
0 commit comments