16
16
#include " VPlan.h"
17
17
#include " VPlanAnalysis.h"
18
18
#include " VPlanCFG.h"
19
- #include " VPlanConstantFolder.h"
20
19
#include " VPlanDominatorTree.h"
21
20
#include " VPlanHelpers.h"
22
21
#include " VPlanPatternMatch.h"
29
28
#include " llvm/ADT/TypeSwitch.h"
30
29
#include " llvm/Analysis/IVDescriptors.h"
31
30
#include " llvm/Analysis/LoopInfo.h"
31
+ #include " llvm/Analysis/TargetFolder.h"
32
32
#include " llvm/Analysis/VectorUtils.h"
33
33
#include " llvm/IR/Intrinsics.h"
34
34
#include " llvm/IR/PatternMatch.h"
@@ -938,6 +938,64 @@ 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 ],
979
+ 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 ;
996
+ }
997
+ };
998
+
941
999
// / Try to simplify recipe \p R.
942
1000
static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
943
1001
const DataLayout &DL) {
@@ -949,8 +1007,8 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
949
1007
.Case <VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
950
1008
VPReplicateRecipe>([&](auto *I) {
951
1009
VPlan *Plan = R.getParent ()->getPlan ();
952
- ArrayRef<VPValue *> Ops (I-> op_begin (), I-> op_end ());
953
- Value *V = Folder.tryToConstantFold (R, I->getOpcode (), Ops );
1010
+ Value *V =
1011
+ Folder.tryToConstantFold (R, I->getOpcode (), I-> operands () );
954
1012
if (V)
955
1013
R.getVPSingleValue ()->replaceAllUsesWith (Plan->getOrAddLiveIn (V));
956
1014
return V;
0 commit comments