Skip to content

Commit d3a5271

Browse files
committed
[VPlan] Simplify and inline VPlanConstantFolder
1 parent ac75244 commit d3a5271

File tree

2 files changed

+61
-160
lines changed

2 files changed

+61
-160
lines changed

llvm/lib/Transforms/Vectorize/VPlanConstantFolder.h

Lines changed: 0 additions & 157 deletions
This file was deleted.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "VPlan.h"
1717
#include "VPlanAnalysis.h"
1818
#include "VPlanCFG.h"
19-
#include "VPlanConstantFolder.h"
2019
#include "VPlanDominatorTree.h"
2120
#include "VPlanHelpers.h"
2221
#include "VPlanPatternMatch.h"
@@ -29,6 +28,7 @@
2928
#include "llvm/ADT/TypeSwitch.h"
3029
#include "llvm/Analysis/IVDescriptors.h"
3130
#include "llvm/Analysis/LoopInfo.h"
31+
#include "llvm/Analysis/TargetFolder.h"
3232
#include "llvm/Analysis/VectorUtils.h"
3333
#include "llvm/IR/Intrinsics.h"
3434
#include "llvm/IR/PatternMatch.h"
@@ -938,6 +938,64 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
938938
}
939939
}
940940

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+
941999
/// Try to simplify recipe \p R.
9421000
static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
9431001
const DataLayout &DL) {
@@ -949,8 +1007,8 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo,
9491007
.Case<VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
9501008
VPReplicateRecipe>([&](auto *I) {
9511009
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());
9541012
if (V)
9551013
R.getVPSingleValue()->replaceAllUsesWith(Plan->getOrAddLiveIn(V));
9561014
return V;

0 commit comments

Comments
 (0)