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