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 (match (BECount, m_scev_SpecificInt (0 )))
345
+ return false ;
346
346
347
347
SmallVector<BasicBlock *, 8 > ExitBlocks;
348
348
CurLoop->getUniqueExitBlocks (ExitBlocks);
@@ -805,20 +805,17 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
805
805
806
806
// Check if the stride matches the size of the memcpy. If so, then we know
807
807
// 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)
808
+ const APInt *StoreStrideValue, *LoadStrideValue;
809
+ if (!match (StoreEv->getOperand (1 ), m_scev_APInt (StoreStrideValue)) ||
810
+ !match (LoadEv->getOperand (1 ), m_scev_APInt (LoadStrideValue)))
813
811
return false ;
814
812
815
- APInt StoreStrideValue = ConstStoreStride->getAPInt ();
816
- APInt LoadStrideValue = ConstLoadStride->getAPInt ();
817
813
// Huge stride value - give up
818
- if (StoreStrideValue.getBitWidth () > 64 || LoadStrideValue.getBitWidth () > 64 )
814
+ if (StoreStrideValue->getBitWidth () > 64 ||
815
+ LoadStrideValue->getBitWidth () > 64 )
819
816
return false ;
820
817
821
- if (SizeInBytes != StoreStrideValue && SizeInBytes != -StoreStrideValue) {
818
+ if (SizeInBytes != * StoreStrideValue && SizeInBytes != -* StoreStrideValue) {
822
819
ORE.emit ([&]() {
823
820
return OptimizationRemarkMissed (DEBUG_TYPE, " SizeStrideUnequal" , MCI)
824
821
<< ore::NV (" Inst" , " memcpy" ) << " in "
@@ -829,8 +826,8 @@ bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
829
826
return false ;
830
827
}
831
828
832
- int64_t StoreStrideInt = StoreStrideValue. getSExtValue ();
833
- int64_t LoadStrideInt = LoadStrideValue. getSExtValue ();
829
+ int64_t StoreStrideInt = StoreStrideValue-> getSExtValue ();
830
+ int64_t LoadStrideInt = LoadStrideValue-> getSExtValue ();
834
831
// Check if the load stride matches the store stride.
835
832
if (StoreStrideInt != LoadStrideInt)
836
833
return false ;
@@ -879,15 +876,14 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
879
876
// we know that every byte is touched in the loop.
880
877
LLVM_DEBUG (dbgs () << " memset size is constant\n " );
881
878
uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength ())->getZExtValue ();
882
- const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev-> getOperand ( 1 )) ;
883
- if (!ConstStride )
879
+ const APInt *Stride ;
880
+ if (!match (Ev-> getOperand ( 1 ), m_scev_APInt (Stride)) )
884
881
return false ;
885
882
886
- APInt Stride = ConstStride->getAPInt ();
887
- if (SizeInBytes != Stride && SizeInBytes != -Stride)
883
+ if (SizeInBytes != *Stride && SizeInBytes != -*Stride)
888
884
return false ;
889
885
890
- IsNegStride = SizeInBytes == -Stride;
886
+ IsNegStride = SizeInBytes == -* Stride;
891
887
} else {
892
888
// Memset size is non-constant.
893
889
// Check if the pointer stride matches the memset size.
@@ -963,11 +959,11 @@ mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
963
959
964
960
// If the loop iterates a fixed number of times, we can refine the access size
965
961
// 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 ();
962
+ const APInt *BECst, *ConstSize ;
963
+ if ( match (BECount, m_scev_APInt (BECst)) &&
964
+ match (StoreSizeSCEV, m_scev_APInt ( ConstSize)) ) {
965
+ std::optional<uint64_t > BEInt = BECst->tryZExtValue ();
966
+ std::optional<uint64_t > SizeInt = ConstSize->tryZExtValue ();
971
967
// FIXME: Should this check for overflow?
972
968
if (BEInt && SizeInt)
973
969
AccessSize = LocationSize::precise ((*BEInt + 1 ) * *SizeInt);
@@ -1605,16 +1601,11 @@ class StrlenVerifier {
1605
1601
1606
1602
LLVM_DEBUG (dbgs () << " pointer load scev: " << *LoadEv << " \n " );
1607
1603
1608
- const SCEVConstant *Step =
1609
- dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence (*SE));
1610
- if (!Step)
1604
+ const APInt *Step;
1605
+ if (!match (LoadEv->getStepRecurrence (*SE), m_scev_APInt (Step)))
1611
1606
return false ;
1612
1607
1613
- unsigned StepSize = 0 ;
1614
- StepSizeCI = dyn_cast<ConstantInt>(Step->getValue ());
1615
- if (!StepSizeCI)
1616
- return false ;
1617
- StepSize = StepSizeCI->getZExtValue ();
1608
+ unsigned StepSize = Step->getZExtValue ();
1618
1609
1619
1610
// Verify that StepSize is consistent with platform char width.
1620
1611
OpWidth = OperandType->getIntegerBitWidth ();
@@ -3294,9 +3285,7 @@ bool LoopIdiomRecognize::recognizeShiftUntilZero() {
3294
3285
// Ok, transform appears worthwhile.
3295
3286
MadeChange = true ;
3296
3287
3297
- bool OffsetIsZero = false ;
3298
- if (auto *ExtraOffsetExprC = dyn_cast<SCEVConstant>(ExtraOffsetExpr))
3299
- OffsetIsZero = ExtraOffsetExprC->isZero ();
3288
+ bool OffsetIsZero = match (ExtraOffsetExpr, m_scev_SpecificInt (0 ));
3300
3289
3301
3290
// Step 1: Compute the loop's final IV value / trip count.
3302
3291
0 commit comments