@@ -938,79 +938,71 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
938
938
}
939
939
}
940
940
941
- class VPConstantFolder {
942
- TargetFolder Folder;
943
- VPTypeAnalysis TypeInfo;
944
-
945
- public:
946
- VPConstantFolder (const DataLayout &DL, const VPTypeAnalysis &TypeInfo)
947
- : Folder(DL), TypeInfo(TypeInfo) {}
948
-
949
- Value *tryToConstantFold (VPRecipeBase &R, unsigned Opcode,
950
- ArrayRef<VPValue *> Operands) {
951
- SmallVector<Value *, 4 > Ops;
952
- for (VPValue *Op : Operands) {
953
- if (!Op->isLiveIn () || !Op->getLiveInIRValue ())
954
- return nullptr ;
955
- Ops.emplace_back (Op->getLiveInIRValue ());
956
- }
957
- switch (Opcode) {
958
- case Instruction::BinaryOps::Add:
959
- case Instruction::BinaryOps::Sub:
960
- case Instruction::BinaryOps::Mul:
961
- case Instruction::BinaryOps::AShr:
962
- case Instruction::BinaryOps::LShr:
963
- case Instruction::BinaryOps::And:
964
- case Instruction::BinaryOps::Or:
965
- case Instruction::BinaryOps::Xor:
966
- return Folder.FoldBinOp (static_cast <Instruction::BinaryOps>(Opcode),
967
- Ops[0 ], Ops[1 ]);
968
- case VPInstruction::LogicalAnd:
969
- return Folder.FoldSelect (Ops[0 ], Ops[1 ],
970
- ConstantInt::getNullValue (Ops[1 ]->getType ()));
971
- case VPInstruction::Not:
972
- return Folder.FoldBinOp (Instruction::BinaryOps::Xor, Ops[0 ],
973
- Constant::getAllOnesValue (Ops[0 ]->getType ()));
974
- case Instruction::Select:
975
- return Folder.FoldSelect (Ops[0 ], Ops[1 ], Ops[2 ]);
976
- case Instruction::ICmp:
977
- case Instruction::FCmp:
978
- return Folder.FoldCmp (cast<VPRecipeWithIRFlags>(R).getPredicate (), Ops[0 ],
941
+ // / Try to fold \p R using TargetFolder to a constant. Will succeed for a
942
+ // / handled \p Opcode if all \p Operands are constant.
943
+ static Value *tryToConstantFold (const VPRecipeBase &R, unsigned Opcode,
944
+ ArrayRef<VPValue *> Operands,
945
+ const DataLayout &DL,
946
+ VPTypeAnalysis &TypeInfo) {
947
+ SmallVector<Value *, 4 > Ops;
948
+ for (VPValue *Op : Operands) {
949
+ if (!Op->isLiveIn () || !Op->getLiveInIRValue ())
950
+ return nullptr ;
951
+ Ops.push_back (Op->getLiveInIRValue ());
952
+ }
953
+
954
+ TargetFolder Folder (DL);
955
+ if (Instruction::isBinaryOp (Opcode))
956
+ return Folder.FoldBinOp (static_cast <Instruction::BinaryOps>(Opcode), Ops[0 ],
979
957
Ops[1 ]);
980
- case Instruction::GetElementPtr:
981
- case VPInstruction::PtrAdd:
982
- return Folder.FoldGEP (TypeInfo.inferScalarType (R.getVPSingleValue ()),
983
- Ops[0 ], drop_begin (Ops),
984
- cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags ());
985
- case Instruction::InsertElement:
986
- return Folder.FoldInsertElement (Ops[0 ], Ops[1 ], Ops[2 ]);
987
- case Instruction::ExtractElement:
988
- return Folder.FoldExtractElement (Ops[0 ], Ops[1 ]);
989
- case Instruction::CastOps::SExt:
990
- case Instruction::CastOps::ZExt:
991
- case Instruction::CastOps::Trunc:
992
- return Folder.FoldCast (static_cast <Instruction::CastOps>(Opcode), Ops[0 ],
993
- TypeInfo.inferScalarType (R.getVPSingleValue ()));
994
- }
995
- return nullptr ;
958
+ if (Instruction::isCast (Opcode))
959
+ return Folder.FoldCast (static_cast <Instruction::CastOps>(Opcode), Ops[0 ],
960
+ TypeInfo.inferScalarType (R.getVPSingleValue ()));
961
+ switch (Opcode) {
962
+ case VPInstruction::LogicalAnd:
963
+ return Folder.FoldSelect (Ops[0 ], Ops[1 ],
964
+ ConstantInt::getNullValue (Ops[1 ]->getType ()));
965
+ case VPInstruction::Not:
966
+ return Folder.FoldBinOp (Instruction::BinaryOps::Xor, Ops[0 ],
967
+ Constant::getAllOnesValue (Ops[0 ]->getType ()));
968
+ case Instruction::Select:
969
+ return Folder.FoldSelect (Ops[0 ], Ops[1 ], Ops[2 ]);
970
+ case Instruction::ICmp:
971
+ case Instruction::FCmp:
972
+ return Folder.FoldCmp (cast<VPRecipeWithIRFlags>(R).getPredicate (), Ops[0 ],
973
+ Ops[1 ]);
974
+ case Instruction::GetElementPtr: {
975
+ auto &RFlags = cast<VPRecipeWithIRFlags>(R);
976
+ auto *GEP = cast<GetElementPtrInst>(RFlags.getUnderlyingInstr ());
977
+ return Folder.FoldGEP (GEP->getSourceElementType (), Ops[0 ], drop_begin (Ops),
978
+ RFlags.getGEPNoWrapFlags ());
979
+ }
980
+ case VPInstruction::PtrAdd:
981
+ return Folder.FoldGEP (IntegerType::getInt8Ty (TypeInfo.getContext ()), Ops[0 ],
982
+ Ops[1 ],
983
+ cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags ());
984
+ case Instruction::InsertElement:
985
+ return Folder.FoldInsertElement (Ops[0 ], Ops[1 ], Ops[2 ]);
986
+ case Instruction::ExtractElement:
987
+ return Folder.FoldExtractElement (Ops[0 ], Ops[1 ]);
996
988
}
997
- };
989
+ return nullptr ;
990
+ }
998
991
999
992
// / Try to simplify recipe \p R.
1000
993
static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
1001
994
const DataLayout &DL) {
1002
995
using namespace llvm ::VPlanPatternMatch;
1003
996
1004
997
// Constant folding.
1005
- VPConstantFolder Folder (DL, TypeInfo);
1006
998
if (TypeSwitch<VPRecipeBase *, bool >(&R)
1007
999
.Case <VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
1008
1000
VPReplicateRecipe>([&](auto *I) {
1009
1001
VPlan *Plan = R.getParent ()->getPlan ();
1010
- Value *V =
1011
- Folder. tryToConstantFold (R, I-> getOpcode (), I-> operands () );
1002
+ Value *V = tryToConstantFold (*I, I-> getOpcode (), I-> operands (), DL,
1003
+ TypeInfo );
1012
1004
if (V)
1013
- R. getVPSingleValue () ->replaceAllUsesWith (Plan->getOrAddLiveIn (V));
1005
+ I ->replaceAllUsesWith (Plan->getOrAddLiveIn (V));
1014
1006
return V;
1015
1007
})
1016
1008
.Default ([](auto *) { return false ; }))
0 commit comments