@@ -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+
14111462bool LoopVectorizationLegality::blockCanBePredicated (
14121463 BasicBlock *BB, SmallPtrSetImpl<Value *> &SafePtrs,
14131464 SmallPtrSetImpl<const Instruction *> &MaskedOp) const {
0 commit comments