Skip to content

Commit d46f279

Browse files
committed
[LV] Move isMaskRequired to LVLegality
1 parent d8bc3a7 commit d46f279

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,57 @@ bool LoopVectorizationLegality::blockNeedsPredication(BasicBlock *BB) const {
14081408
return LoopAccessInfo::blockNeedsPredication(BB, TheLoop, DT);
14091409
}
14101410

1411+
bool LoopVectorizationLegality::isMaskRequired(Instruction *I,
1412+
bool FoldTailByMasking) const {
1413+
if (isSafeToSpeculativelyExecute(I, TheLoop->getLatchCmpInst()) ||
1414+
(isa<LoadInst, StoreInst, CallInst>(I) && !MaskedOp.contains(I)) ||
1415+
isa<BranchInst, SwitchInst, PHINode, AllocaInst>(I))
1416+
return false;
1417+
1418+
// If the instruction was executed conditionally in the original scalar loop,
1419+
// predication is needed with a mask whose lanes are all possibly inactive.
1420+
if (blockNeedsPredication(I->getParent()))
1421+
return true;
1422+
1423+
// If we're not folding tail by masking, bail out now.
1424+
if (!FoldTailByMasking)
1425+
return false;
1426+
1427+
// All that remain are instructions with side-effects originally executed in
1428+
// the loop unconditionally, but now execute under a tail-fold mask (only)
1429+
// having at least one active lane (the first). If the side-effects of the
1430+
// instruction are invariant, executing it w/o (the tail-folding) mask is safe
1431+
// - it will cause the same side-effects as when masked.
1432+
switch (I->getOpcode()) {
1433+
default:
1434+
llvm_unreachable(
1435+
"instruction should have been considered by earlier checks");
1436+
case Instruction::Call:
1437+
// Side-effects of a Call are assumed to be non-invariant, needing a
1438+
// (fold-tail) mask.
1439+
assert(MaskedOp.contains(I) &&
1440+
"should have returned earlier for calls not needing a mask");
1441+
return true;
1442+
case Instruction::Load:
1443+
// If the address is loop invariant no predication is needed.
1444+
return !isInvariant(getLoadStorePointerOperand(I));
1445+
case Instruction::Store: {
1446+
// For stores, we need to prove both speculation safety (which follows from
1447+
// the same argument as loads), but also must prove the value being stored
1448+
// is correct. The easiest form of the later is to require that all values
1449+
// stored are the same.
1450+
return !(isInvariant(getLoadStorePointerOperand(I)) &&
1451+
TheLoop->isLoopInvariant(cast<StoreInst>(I)->getValueOperand()));
1452+
}
1453+
case Instruction::UDiv:
1454+
case Instruction::SDiv:
1455+
case Instruction::SRem:
1456+
case Instruction::URem:
1457+
// If the divisor is loop-invariant no predication is needed.
1458+
return !TheLoop->isLoopInvariant(I->getOperand(1));
1459+
}
1460+
}
1461+
14111462
bool LoopVectorizationLegality::blockCanBePredicated(
14121463
BasicBlock *BB, SmallPtrSetImpl<Value *> &SafePtrs,
14131464
SmallPtrSetImpl<const Instruction *> &MaskedOp) const {

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,57 +3052,6 @@ bool LoopVectorizationCostModel::isScalarWithPredication(
30523052
}
30533053
}
30543054

3055-
bool LoopVectorizationLegality::isMaskRequired(Instruction *I,
3056-
bool FoldTailByMasking) const {
3057-
if (isSafeToSpeculativelyExecute(I, TheLoop->getLatchCmpInst()) ||
3058-
(isa<LoadInst, StoreInst, CallInst>(I) && !MaskedOp.contains(I)) ||
3059-
isa<BranchInst, SwitchInst, PHINode, AllocaInst>(I))
3060-
return false;
3061-
3062-
// If the instruction was executed conditionally in the original scalar loop,
3063-
// predication is needed with a mask whose lanes are all possibly inactive.
3064-
if (blockNeedsPredication(I->getParent()))
3065-
return true;
3066-
3067-
// If we're not folding tail by masking, bail out now.
3068-
if (!FoldTailByMasking)
3069-
return false;
3070-
3071-
// All that remain are instructions with side-effects originally executed in
3072-
// the loop unconditionally, but now execute under a tail-fold mask (only)
3073-
// having at least one active lane (the first). If the side-effects of the
3074-
// instruction are invariant, executing it w/o (the tail-folding) mask is safe
3075-
// - it will cause the same side-effects as when masked.
3076-
switch (I->getOpcode()) {
3077-
default:
3078-
llvm_unreachable(
3079-
"instruction should have been considered by earlier checks");
3080-
case Instruction::Call:
3081-
// Side-effects of a Call are assumed to be non-invariant, needing a
3082-
// (fold-tail) mask.
3083-
assert(MaskedOp.contains(I) &&
3084-
"should have returned earlier for calls not needing a mask");
3085-
return true;
3086-
case Instruction::Load:
3087-
// If the address is loop invariant no predication is needed.
3088-
return !isInvariant(getLoadStorePointerOperand(I));
3089-
case Instruction::Store: {
3090-
// For stores, we need to prove both speculation safety (which follows from
3091-
// the same argument as loads), but also must prove the value being stored
3092-
// is correct. The easiest form of the later is to require that all values
3093-
// stored are the same.
3094-
return !(isInvariant(getLoadStorePointerOperand(I)) &&
3095-
TheLoop->isLoopInvariant(cast<StoreInst>(I)->getValueOperand()));
3096-
}
3097-
case Instruction::UDiv:
3098-
case Instruction::SDiv:
3099-
case Instruction::SRem:
3100-
case Instruction::URem:
3101-
// If the divisor is loop-invariant no predication is needed.
3102-
return !TheLoop->isLoopInvariant(I->getOperand(1));
3103-
}
3104-
}
3105-
31063055
std::pair<InstructionCost, InstructionCost>
31073056
LoopVectorizationCostModel::getDivRemSpeculationCost(Instruction *I,
31083057
ElementCount VF) const {

0 commit comments

Comments
 (0)