32
32
#include " llvm/ADT/ArrayRef.h"
33
33
#include " llvm/ADT/DenseMap.h"
34
34
#include " llvm/ADT/MapVector.h"
35
- #include " llvm/ADT/STLExtras.h"
36
35
#include " llvm/ADT/SetVector.h"
37
36
#include " llvm/ADT/SmallPtrSet.h"
38
37
#include " llvm/ADT/SmallVector.h"
49
48
#include " llvm/Analysis/OptimizationRemarkEmitter.h"
50
49
#include " llvm/Analysis/ScalarEvolution.h"
51
50
#include " llvm/Analysis/ScalarEvolutionExpressions.h"
51
+ #include " llvm/Analysis/ScalarEvolutionPatternMatch.h"
52
52
#include " llvm/Analysis/TargetLibraryInfo.h"
53
53
#include " llvm/Analysis/TargetTransformInfo.h"
54
54
#include " llvm/Analysis/ValueTracking.h"
91
91
#include < vector>
92
92
93
93
using namespace llvm ;
94
+ using namespace SCEVPatternMatch ;
94
95
95
96
#define DEBUG_TYPE " loop-idiom"
96
97
@@ -340,9 +341,8 @@ bool LoopIdiomRecognize::runOnCountableLoop() {
340
341
341
342
// If this loop executes exactly one time, then it should be peeled, not
342
343
// optimized by this pass.
343
- if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
344
- if (BECst->getAPInt () == 0 )
345
- return false ;
344
+ if (BECount->isZero ())
345
+ return false ;
346
346
347
347
SmallVector<BasicBlock *, 8 > ExitBlocks;
348
348
CurLoop->getUniqueExitBlocks (ExitBlocks);
@@ -453,13 +453,10 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
453
453
// See if the pointer expression is an AddRec like {base,+,1} on the current
454
454
// loop, which indicates a strided store. If we have something else, it's a
455
455
// random store we can't handle.
456
- const SCEVAddRecExpr *StoreEv =
457
- dyn_cast<SCEVAddRecExpr>(SE->getSCEV (StorePtr));
458
- if (!StoreEv || StoreEv->getLoop () != CurLoop || !StoreEv->isAffine ())
459
- return LegalStoreKind::None;
460
-
461
- // Check to see if we have a constant stride.
462
- if (!isa<SCEVConstant>(StoreEv->getOperand (1 )))
456
+ const SCEV *StoreEv = SE->getSCEV (StorePtr);
457
+ const SCEVConstant *Stride;
458
+ if (!match (StoreEv, m_scev_AffineAddRec (m_SCEV (), m_SCEVConstant (Stride))) ||
459
+ cast<SCEVAddRecExpr>(StoreEv)->getLoop () != CurLoop)
463
460
return LegalStoreKind::None;
464
461
465
462
// See if the store can be turned into a memset.
@@ -494,9 +491,9 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
494
491
if (HasMemcpy && !DisableLIRP::Memcpy) {
495
492
// Check to see if the stride matches the size of the store. If so, then we
496
493
// know that every byte is touched in the loop.
497
- APInt Stride = getStoreStride (StoreEv);
498
494
unsigned StoreSize = DL->getTypeStoreSize (SI->getValueOperand ()->getType ());
499
- if (StoreSize != Stride && StoreSize != -Stride)
495
+ APInt StrideAP = Stride->getAPInt ();
496
+ if (StoreSize != StrideAP && StoreSize != -StrideAP)
500
497
return LegalStoreKind::None;
501
498
502
499
// The store must be feeding a non-volatile load.
@@ -512,13 +509,12 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
512
509
// See if the pointer expression is an AddRec like {base,+,1} on the current
513
510
// loop, which indicates a strided load. If we have something else, it's a
514
511
// random load we can't handle.
515
- const SCEVAddRecExpr *LoadEv =
516
- dyn_cast<SCEVAddRecExpr>(SE->getSCEV (LI->getPointerOperand ()));
517
- if (!LoadEv || LoadEv->getLoop () != CurLoop || !LoadEv->isAffine ())
518
- return LegalStoreKind::None;
512
+ const SCEV *LoadEv = SE->getSCEV (LI->getPointerOperand ());
519
513
520
514
// The store and load must share the same stride.
521
- if (StoreEv->getOperand (1 ) != LoadEv->getOperand (1 ))
515
+ if (!match (LoadEv,
516
+ m_scev_AffineAddRec (m_SCEV (), m_scev_Specific (Stride))) ||
517
+ cast<SCEVAddRecExpr>(LoadEv)->getLoop () != CurLoop)
522
518
return LegalStoreKind::None;
523
519
524
520
// Success. This store can be converted into a memcpy.
@@ -805,20 +801,17 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
805
801
806
802
// Check if the stride matches the size of the memcpy. If so, then we know
807
803
// that every byte is touched in the loop.
808
- const SCEVConstant *ConstStoreStride =
809
- dyn_cast<SCEVConstant>(StoreEv->getOperand (1 ));
810
- const SCEVConstant *ConstLoadStride =
811
- dyn_cast<SCEVConstant>(LoadEv->getOperand (1 ));
812
- if (!ConstStoreStride || !ConstLoadStride)
804
+ const APInt *StoreStrideValue, *LoadStrideValue;
805
+ if (!match (StoreEv->getOperand (1 ), m_scev_APInt (StoreStrideValue)) ||
806
+ !match (LoadEv->getOperand (1 ), m_scev_APInt (LoadStrideValue)))
813
807
return false ;
814
808
815
- APInt StoreStrideValue = ConstStoreStride->getAPInt ();
816
- APInt LoadStrideValue = ConstLoadStride->getAPInt ();
817
809
// Huge stride value - give up
818
- if (StoreStrideValue.getBitWidth () > 64 || LoadStrideValue.getBitWidth () > 64 )
810
+ if (StoreStrideValue->getBitWidth () > 64 ||
811
+ LoadStrideValue->getBitWidth () > 64 )
819
812
return false ;
820
813
821
- if (SizeInBytes != StoreStrideValue && SizeInBytes != -StoreStrideValue) {
814
+ if (SizeInBytes != * StoreStrideValue && SizeInBytes != -* StoreStrideValue) {
822
815
ORE.emit ([&]() {
823
816
return OptimizationRemarkMissed (DEBUG_TYPE, " SizeStrideUnequal" , MCI)
824
817
<< ore::NV (" Inst" , " memcpy" ) << " in "
@@ -829,8 +822,8 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
829
822
return false ;
830
823
}
831
824
832
- int64_t StoreStrideInt = StoreStrideValue. getSExtValue ();
833
- int64_t LoadStrideInt = LoadStrideValue. getSExtValue ();
825
+ int64_t StoreStrideInt = StoreStrideValue-> getSExtValue ();
826
+ int64_t LoadStrideInt = LoadStrideValue-> getSExtValue ();
834
827
// Check if the load stride matches the store stride.
835
828
if (StoreStrideInt != LoadStrideInt)
836
829
return false ;
@@ -857,15 +850,15 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
857
850
// See if the pointer expression is an AddRec like {base,+,1} on the current
858
851
// loop, which indicates a strided store. If we have something else, it's a
859
852
// random store we can't handle.
860
- const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV (Pointer));
861
- if (!Ev || Ev->getLoop () != CurLoop)
862
- return false ;
863
- if (!Ev->isAffine ()) {
853
+ const SCEV *Ev = SE->getSCEV (Pointer);
854
+ const SCEV *PointerStrideSCEV;
855
+ if (!match (Ev, m_scev_AffineAddRec (m_SCEV (), m_SCEV (PointerStrideSCEV)))) {
864
856
LLVM_DEBUG (dbgs () << " Pointer is not affine, abort\n " );
865
857
return false ;
866
858
}
859
+ if (cast<SCEVAddRecExpr>(Ev)->getLoop () != CurLoop)
860
+ return false ;
867
861
868
- const SCEV *PointerStrideSCEV = Ev->getOperand (1 );
869
862
const SCEV *MemsetSizeSCEV = SE->getSCEV (MSI->getLength ());
870
863
if (!PointerStrideSCEV || !MemsetSizeSCEV)
871
864
return false ;
@@ -879,15 +872,14 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
879
872
// we know that every byte is touched in the loop.
880
873
LLVM_DEBUG (dbgs () << " memset size is constant\n " );
881
874
uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength ())->getZExtValue ();
882
- const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev-> getOperand ( 1 )) ;
883
- if (!ConstStride )
875
+ const APInt *Stride ;
876
+ if (!match (PointerStrideSCEV, m_scev_APInt (Stride)) )
884
877
return false ;
885
878
886
- APInt Stride = ConstStride->getAPInt ();
887
- if (SizeInBytes != Stride && SizeInBytes != -Stride)
879
+ if (SizeInBytes != *Stride && SizeInBytes != -*Stride)
888
880
return false ;
889
881
890
- IsNegStride = SizeInBytes == -Stride;
882
+ IsNegStride = SizeInBytes == -* Stride;
891
883
} else {
892
884
// Memset size is non-constant.
893
885
// Check if the pointer stride matches the memset size.
@@ -944,8 +936,9 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
944
936
SmallPtrSet<Instruction *, 1 > MSIs;
945
937
MSIs.insert (MSI);
946
938
return processLoopStridedStore (Pointer, SE->getSCEV (MSI->getLength ()),
947
- MSI->getDestAlign (), SplatValue, MSI, MSIs, Ev,
948
- BECount, IsNegStride, /* IsLoopMemset=*/ true );
939
+ MSI->getDestAlign (), SplatValue, MSI, MSIs,
940
+ cast<SCEVAddRecExpr>(Ev), BECount, IsNegStride,
941
+ /* IsLoopMemset=*/ true );
949
942
}
950
943
951
944
// / mayLoopAccessLocation - Return true if the specified loop might access the
@@ -963,11 +956,11 @@ mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
963
956
964
957
// If the loop iterates a fixed number of times, we can refine the access size
965
958
// to be exactly the size of the memset, which is (BECount+1)*StoreSize
966
- const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount) ;
967
- const SCEVConstant *ConstSize = dyn_cast<SCEVConstant>(StoreSizeSCEV);
968
- if (BECst && ConstSize) {
969
- std::optional<uint64_t > BEInt = BECst->getAPInt (). tryZExtValue ();
970
- std::optional<uint64_t > SizeInt = ConstSize->getAPInt (). tryZExtValue ();
959
+ const APInt *BECst, *ConstSize ;
960
+ if ( match (BECount, m_scev_APInt (BECst)) &&
961
+ match (StoreSizeSCEV, m_scev_APInt ( ConstSize)) ) {
962
+ std::optional<uint64_t > BEInt = BECst->tryZExtValue ();
963
+ std::optional<uint64_t > SizeInt = ConstSize->tryZExtValue ();
971
964
// FIXME: Should this check for overflow?
972
965
if (BEInt && SizeInt)
973
966
AccessSize = LocationSize::precise ((*BEInt + 1 ) * *SizeInt);
@@ -1579,24 +1572,15 @@ class StrlenVerifier {
1579
1572
// See if the pointer expression is an AddRec with constant step a of form
1580
1573
// ({n,+,a}) where a is the width of the char type.
1581
1574
Value *IncPtr = LoopLoad->getPointerOperand ();
1582
- const SCEVAddRecExpr *LoadEv =
1583
- dyn_cast<SCEVAddRecExpr>(SE->getSCEV (IncPtr));
1584
- if (!LoadEv || LoadEv->getLoop () != CurLoop || !LoadEv->isAffine ())
1575
+ const SCEV *LoadEv = SE->getSCEV (IncPtr);
1576
+ const APInt *Step;
1577
+ if (!match (LoadEv,
1578
+ m_scev_AffineAddRec (m_SCEV (LoadBaseEv), m_scev_APInt (Step))))
1585
1579
return false ;
1586
- LoadBaseEv = LoadEv->getStart ();
1587
1580
1588
1581
LLVM_DEBUG (dbgs () << " pointer load scev: " << *LoadEv << " \n " );
1589
1582
1590
- const SCEVConstant *Step =
1591
- dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence (*SE));
1592
- if (!Step)
1593
- return false ;
1594
-
1595
- unsigned StepSize = 0 ;
1596
- StepSizeCI = dyn_cast<ConstantInt>(Step->getValue ());
1597
- if (!StepSizeCI)
1598
- return false ;
1599
- StepSize = StepSizeCI->getZExtValue ();
1583
+ unsigned StepSize = Step->getZExtValue ();
1600
1584
1601
1585
// Verify that StepSize is consistent with platform char width.
1602
1586
OpWidth = OperandType->getIntegerBitWidth ();
@@ -3277,9 +3261,7 @@ bool LoopIdiomRecognize::recognizeShiftUntilZero() {
3277
3261
// Ok, transform appears worthwhile.
3278
3262
MadeChange = true ;
3279
3263
3280
- bool OffsetIsZero = false ;
3281
- if (auto *ExtraOffsetExprC = dyn_cast<SCEVConstant>(ExtraOffsetExpr))
3282
- OffsetIsZero = ExtraOffsetExprC->isZero ();
3264
+ bool OffsetIsZero = ExtraOffsetExpr->isZero ();
3283
3265
3284
3266
// Step 1: Compute the loop's final IV value / trip count.
3285
3267
0 commit comments