@@ -940,79 +940,72 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
940940 }
941941}
942942
943- class VPConstantFolder {
944- TargetFolder Folder;
945- VPTypeAnalysis TypeInfo;
946-
947- public:
948- VPConstantFolder (const DataLayout &DL, const VPTypeAnalysis &TypeInfo)
949- : Folder(DL), TypeInfo(TypeInfo) {}
950-
951- Value *tryToConstantFold (VPRecipeBase &R, unsigned Opcode,
952- ArrayRef<VPValue *> Operands) {
953- SmallVector<Value *, 4 > Ops;
954- for (VPValue *Op : Operands) {
955- if (!Op->isLiveIn () || !Op->getLiveInIRValue ())
956- return nullptr ;
957- Ops.emplace_back (Op->getLiveInIRValue ());
958- }
959- switch (Opcode) {
960- case Instruction::BinaryOps::Add:
961- case Instruction::BinaryOps::Sub:
962- case Instruction::BinaryOps::Mul:
963- case Instruction::BinaryOps::AShr:
964- case Instruction::BinaryOps::LShr:
965- case Instruction::BinaryOps::And:
966- case Instruction::BinaryOps::Or:
967- case Instruction::BinaryOps::Xor:
968- return Folder.FoldBinOp (static_cast <Instruction::BinaryOps>(Opcode),
969- Ops[0 ], Ops[1 ]);
970- case VPInstruction::LogicalAnd:
971- return Folder.FoldSelect (Ops[0 ], Ops[1 ],
972- ConstantInt::getNullValue (Ops[1 ]->getType ()));
973- case VPInstruction::Not:
974- return Folder.FoldBinOp (Instruction::BinaryOps::Xor, Ops[0 ],
975- Constant::getAllOnesValue (Ops[0 ]->getType ()));
976- case Instruction::Select:
977- return Folder.FoldSelect (Ops[0 ], Ops[1 ], Ops[2 ]);
978- case Instruction::ICmp:
979- case Instruction::FCmp:
980- return Folder.FoldCmp (cast<VPRecipeWithIRFlags>(R).getPredicate (), Ops[0 ],
943+ // / Try to fold \p R using TargetFolder to a constant. Will succeed for a
944+ // / handled \p Opcode if all \p Operands are constant.
945+ static Value *tryToConstantFold (const VPRecipeBase &R, unsigned Opcode,
946+ ArrayRef<VPValue *> Operands,
947+ const DataLayout &DL,
948+ VPTypeAnalysis &TypeInfo) {
949+ SmallVector<Value *, 4 > Ops;
950+ for (VPValue *Op : Operands) {
951+ if (!Op->isLiveIn () || !Op->getLiveInIRValue ())
952+ return nullptr ;
953+ Ops.push_back (Op->getLiveInIRValue ());
954+ }
955+
956+ TargetFolder Folder (DL);
957+ if (Instruction::isBinaryOp (Opcode))
958+ return Folder.FoldBinOp (static_cast <Instruction::BinaryOps>(Opcode), Ops[0 ],
981959 Ops[1 ]);
982- case Instruction::GetElementPtr:
983- case VPInstruction::PtrAdd:
984- return Folder.FoldGEP (TypeInfo.inferScalarType (R.getVPSingleValue ()),
985- Ops[0 ], drop_begin (Ops),
986- cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags ());
987- case Instruction::InsertElement:
988- return Folder.FoldInsertElement (Ops[0 ], Ops[1 ], Ops[2 ]);
989- case Instruction::ExtractElement:
990- return Folder.FoldExtractElement (Ops[0 ], Ops[1 ]);
991- case Instruction::CastOps::SExt:
992- case Instruction::CastOps::ZExt:
993- case Instruction::CastOps::Trunc:
994- return Folder.FoldCast (static_cast <Instruction::CastOps>(Opcode), Ops[0 ],
995- TypeInfo.inferScalarType (R.getVPSingleValue ()));
996- }
997- return nullptr ;
960+ if (Instruction::isCast (Opcode))
961+ return Folder.FoldCast (static_cast <Instruction::CastOps>(Opcode), Ops[0 ],
962+ TypeInfo.inferScalarType (R.getVPSingleValue ()));
963+ switch (Opcode) {
964+ case VPInstruction::LogicalAnd:
965+ return Folder.FoldSelect (Ops[0 ], Ops[1 ],
966+ ConstantInt::getNullValue (Ops[1 ]->getType ()));
967+ case VPInstruction::Not:
968+ return Folder.FoldBinOp (Instruction::BinaryOps::Xor, Ops[0 ],
969+ Constant::getAllOnesValue (Ops[0 ]->getType ()));
970+ case Instruction::Select:
971+ return Folder.FoldSelect (Ops[0 ], Ops[1 ], Ops[2 ]);
972+ case Instruction::ICmp:
973+ case Instruction::FCmp:
974+ return Folder.FoldCmp (cast<VPRecipeWithIRFlags>(R).getPredicate (), Ops[0 ],
975+ Ops[1 ]);
976+ case Instruction::GetElementPtr: {
977+ auto &RFlags = cast<VPRecipeWithIRFlags>(R);
978+ auto *GEP = cast<GetElementPtrInst>(RFlags.getUnderlyingInstr ());
979+ return Folder.FoldGEP (GEP->getSourceElementType (), Ops[0 ], drop_begin (Ops),
980+ RFlags.getGEPNoWrapFlags ());
981+ }
982+ case VPInstruction::PtrAdd:
983+ return Folder.FoldGEP (IntegerType::getInt8Ty (TypeInfo.getContext ()), Ops[0 ],
984+ Ops[1 ],
985+ cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags ());
986+ case Instruction::InsertElement:
987+ return Folder.FoldInsertElement (Ops[0 ], Ops[1 ], Ops[2 ]);
988+ case Instruction::ExtractElement:
989+ return Folder.FoldExtractElement (Ops[0 ], Ops[1 ]);
998990 }
999- };
991+ return nullptr ;
992+ }
1000993
1001994// / Try to simplify recipe \p R.
1002- static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
1003- const DataLayout &DL) {
995+ static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
1004996 using namespace llvm ::VPlanPatternMatch;
1005997
1006998 // Constant folding.
1007- VPConstantFolder Folder (DL, TypeInfo);
1008999 if (TypeSwitch<VPRecipeBase *, bool >(&R)
10091000 .Case <VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
10101001 VPReplicateRecipe>([&](auto *I) {
10111002 VPlan *Plan = R.getParent ()->getPlan ();
1012- Value *V =
1013- Folder.tryToConstantFold (R, I->getOpcode (), I->operands ());
1003+ const DataLayout &DL =
1004+ Plan->getScalarHeader ()->getIRBasicBlock ()->getDataLayout ();
1005+ Value *V = tryToConstantFold (*I, I->getOpcode (), I->operands (), DL,
1006+ TypeInfo);
10141007 if (V)
1015- R. getVPSingleValue () ->replaceAllUsesWith (Plan->getOrAddLiveIn (V));
1008+ I ->replaceAllUsesWith (Plan->getOrAddLiveIn (V));
10161009 return V;
10171010 })
10181011 .Default ([](auto *) { return false ; }))
@@ -1150,14 +1143,13 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
11501143 }
11511144}
11521145
1153- void VPlanTransforms::simplifyRecipes (VPlan &Plan, Type &CanonicalIVTy,
1154- const DataLayout &DL) {
1146+ void VPlanTransforms::simplifyRecipes (VPlan &Plan, Type &CanonicalIVTy) {
11551147 ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT (
11561148 Plan.getEntry ());
11571149 VPTypeAnalysis TypeInfo (&CanonicalIVTy);
11581150 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
11591151 for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
1160- simplifyRecipe (R, TypeInfo, DL );
1152+ simplifyRecipe (R, TypeInfo);
11611153 }
11621154 }
11631155}
@@ -1418,8 +1410,7 @@ static bool simplifyBranchConditionForVFAndUF(VPlan &Plan, ElementCount BestVF,
14181410
14191411 VPBlockUtils::connectBlocks (Preheader, Header);
14201412 VPBlockUtils::connectBlocks (ExitingVPBB, Exit);
1421- VPlanTransforms::simplifyRecipes (Plan, *CanIVTy,
1422- PSE.getSE ()->getDataLayout ());
1413+ VPlanTransforms::simplifyRecipes (Plan, *CanIVTy);
14231414 } else {
14241415 // The vector region contains header phis for which we cannot remove the
14251416 // loop region yet.
@@ -1849,16 +1840,17 @@ static void removeBranchOnCondTrue(VPlan &Plan) {
18491840 VPBB->back ().eraseFromParent ();
18501841 }
18511842}
1852- void VPlanTransforms::optimize (VPlan &Plan, const DataLayout &DL) {
1843+
1844+ void VPlanTransforms::optimize (VPlan &Plan) {
18531845 runPass (removeRedundantCanonicalIVs, Plan);
18541846 runPass (removeRedundantInductionCasts, Plan);
18551847
1856- runPass (simplifyRecipes, Plan, *Plan.getCanonicalIV ()->getScalarType (), DL );
1848+ runPass (simplifyRecipes, Plan, *Plan.getCanonicalIV ()->getScalarType ());
18571849 runPass (simplifyBlends, Plan);
18581850 runPass (removeDeadRecipes, Plan);
18591851 runPass (legalizeAndOptimizeInductions, Plan);
18601852 runPass (removeRedundantExpandSCEVRecipes, Plan);
1861- runPass (simplifyRecipes, Plan, *Plan.getCanonicalIV ()->getScalarType (), DL );
1853+ runPass (simplifyRecipes, Plan, *Plan.getCanonicalIV ()->getScalarType ());
18621854 runPass (removeBranchOnCondTrue, Plan);
18631855 runPass (removeDeadRecipes, Plan);
18641856
0 commit comments