Skip to content

Commit 8061acf

Browse files
committed
[LV] Move isMaskRequired to LVLegality
1 parent a3f75cf commit 8061acf

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,59 @@ bool LoopVectorizationLegality::blockNeedsPredication(BasicBlock *BB) const {
14061406
return LoopAccessInfo::blockNeedsPredication(BB, TheLoop, DT);
14071407
}
14081408

1409+
bool LoopVectorizationLegality::isMaskRequired(Instruction *I,
1410+
bool FoldTailByMasking) const {
1411+
// TODO: We can use the loop-preheader as context point here and get
1412+
// context sensitive reasoning for isSafeToSpeculativelyExecute.
1413+
if (isSafeToSpeculativelyExecute(I) ||
1414+
(isa<LoadInst, StoreInst, CallInst>(I) && !isMasked(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 the tail by masking, predication is unnecessary.
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(isMasked(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 !isInvariant(I->getOperand(1));
1459+
}
1460+
}
1461+
14091462
bool LoopVectorizationLegality::blockCanBePredicated(
14101463
BasicBlock *BB, SmallPtrSetImpl<Value *> &SafePtrs,
14111464
SmallPtrSetImpl<const Instruction *> &MaskedOp) const {

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,59 +2975,6 @@ bool LoopVectorizationCostModel::isScalarWithPredication(
29752975
}
29762976
}
29772977

2978-
bool LoopVectorizationLegality::isMaskRequired(Instruction *I,
2979-
bool FoldTailByMasking) const {
2980-
// TODO: We can use the loop-preheader as context point here and get
2981-
// context sensitive reasoning for isSafeToSpeculativelyExecute.
2982-
if (isSafeToSpeculativelyExecute(I) ||
2983-
(isa<LoadInst, StoreInst, CallInst>(I) && !isMasked(I)) ||
2984-
isa<BranchInst, SwitchInst, PHINode, AllocaInst>(I))
2985-
return false;
2986-
2987-
// If the instruction was executed conditionally in the original scalar loop,
2988-
// predication is needed with a mask whose lanes are all possibly inactive.
2989-
if (blockNeedsPredication(I->getParent()))
2990-
return true;
2991-
2992-
// If we're not folding the tail by masking, predication is unnecessary.
2993-
if (!FoldTailByMasking)
2994-
return false;
2995-
2996-
// All that remain are instructions with side-effects originally executed in
2997-
// the loop unconditionally, but now execute under a tail-fold mask (only)
2998-
// having at least one active lane (the first). If the side-effects of the
2999-
// instruction are invariant, executing it w/o (the tail-folding) mask is safe
3000-
// - it will cause the same side-effects as when masked.
3001-
switch (I->getOpcode()) {
3002-
default:
3003-
llvm_unreachable(
3004-
"instruction should have been considered by earlier checks");
3005-
case Instruction::Call:
3006-
// Side-effects of a Call are assumed to be non-invariant, needing a
3007-
// (fold-tail) mask.
3008-
assert(isMasked(I) &&
3009-
"should have returned earlier for calls not needing a mask");
3010-
return true;
3011-
case Instruction::Load:
3012-
// If the address is loop invariant no predication is needed.
3013-
return !isInvariant(getLoadStorePointerOperand(I));
3014-
case Instruction::Store: {
3015-
// For stores, we need to prove both speculation safety (which follows from
3016-
// the same argument as loads), but also must prove the value being stored
3017-
// is correct. The easiest form of the later is to require that all values
3018-
// stored are the same.
3019-
return !(isInvariant(getLoadStorePointerOperand(I)) &&
3020-
TheLoop->isLoopInvariant(cast<StoreInst>(I)->getValueOperand()));
3021-
}
3022-
case Instruction::UDiv:
3023-
case Instruction::SDiv:
3024-
case Instruction::SRem:
3025-
case Instruction::URem:
3026-
// If the divisor is loop-invariant no predication is needed.
3027-
return !isInvariant(I->getOperand(1));
3028-
}
3029-
}
3030-
30312978
std::pair<InstructionCost, InstructionCost>
30322979
LoopVectorizationCostModel::getDivRemSpeculationCost(Instruction *I,
30332980
ElementCount VF) const {

0 commit comments

Comments
 (0)