Skip to content

Commit 1261041

Browse files
committed
Refactoring
1 parent 0909165 commit 1261041

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,16 +2495,19 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
24952495
}
24962496

24972497
/// Check if instruction depends on ZExt and this ZExt can be moved after the
2498-
/// instruction. Move ZExt if it is profitable
2498+
/// instruction. Move ZExt if it is profitable. For example:
2499+
/// logic(zext(x),y) -> zext(logic(x,trunc(y)))
2500+
/// lshr((zext(x),y) -> zext(lshr(x,trunc(y)))
2501+
/// Cost model calculations takes into account if zext(x) has other users and
2502+
/// whether it can be propagated through them too.
24992503
bool VectorCombine::shrinkType(llvm::Instruction &I) {
25002504
Value *ZExted, *OtherOperand;
25012505
if (!match(&I, m_c_BitwiseLogic(m_ZExt(m_Value(ZExted)),
25022506
m_Value(OtherOperand))) &&
25032507
!match(&I, m_LShr(m_ZExt(m_Value(ZExted)), m_Value(OtherOperand))))
25042508
return false;
25052509

2506-
Instruction *ZExtOperand =
2507-
cast<Instruction>(I.getOperand(I.getOperand(0) == OtherOperand ? 1 : 0));
2510+
Value *ZExtOperand = I.getOperand(I.getOperand(0) == OtherOperand ? 1 : 0);
25082511

25092512
auto *BigTy = cast<FixedVectorType>(I.getType());
25102513
auto *SmallTy = cast<FixedVectorType>(ZExted->getType());
@@ -2519,18 +2522,21 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
25192522

25202523
// Calculate costs of leaving current IR as it is and moving ZExt operation
25212524
// later, along with adding truncates if needed
2525+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
25222526
InstructionCost ZExtCost = TTI.getCastInstrCost(
25232527
Instruction::ZExt, BigTy, SmallTy,
2524-
TargetTransformInfo::CastContextHint::None, TTI::TCK_RecipThroughput);
2528+
TargetTransformInfo::CastContextHint::None, CostKind);
25252529
InstructionCost CurrentCost = ZExtCost;
25262530
InstructionCost ShrinkCost = 0;
25272531

25282532
// Calculate total cost and check that we can propagate through all ZExt users
25292533
for (User *U : ZExtOperand->users()) {
25302534
auto *UI = cast<Instruction>(U);
25312535
if (UI == &I) {
2532-
CurrentCost += TTI.getArithmeticInstrCost(UI->getOpcode(), BigTy);
2533-
ShrinkCost += TTI.getArithmeticInstrCost(UI->getOpcode(), SmallTy);
2536+
CurrentCost +=
2537+
TTI.getArithmeticInstrCost(UI->getOpcode(), BigTy, CostKind);
2538+
ShrinkCost +=
2539+
TTI.getArithmeticInstrCost(UI->getOpcode(), SmallTy, CostKind);
25342540
ShrinkCost += ZExtCost;
25352541
continue;
25362542
}
@@ -2540,12 +2546,13 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
25402546

25412547
// Check if we can propagate ZExt through its other users
25422548
KB = computeKnownBits(UI, *DL);
2543-
unsigned UBW = KB.getBitWidth() - KB.Zero.countLeadingOnes();
2549+
unsigned UBW = KB.getBitWidth() - KB.countMinLeadingZeros();
25442550
if (UBW > BW)
25452551
return false;
25462552

2547-
CurrentCost += TTI.getArithmeticInstrCost(UI->getOpcode(), BigTy);
2548-
ShrinkCost += TTI.getArithmeticInstrCost(UI->getOpcode(), SmallTy);
2553+
CurrentCost += TTI.getArithmeticInstrCost(UI->getOpcode(), BigTy, CostKind);
2554+
ShrinkCost +=
2555+
TTI.getArithmeticInstrCost(UI->getOpcode(), SmallTy, CostKind);
25492556
ShrinkCost += ZExtCost;
25502557
}
25512558

@@ -2554,7 +2561,7 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
25542561
if (!isa<Constant>(OtherOperand))
25552562
ShrinkCost += TTI.getCastInstrCost(
25562563
Instruction::Trunc, SmallTy, BigTy,
2557-
TargetTransformInfo::CastContextHint::None, TTI::TCK_RecipThroughput);
2564+
TargetTransformInfo::CastContextHint::None, CostKind);
25582565

25592566
// If the cost of shrinking types and leaving the IR is the same, we'll lean
25602567
// towards modifying the IR because shrinking opens opportunities for other

0 commit comments

Comments
 (0)