@@ -82,6 +82,34 @@ static void printDepMatrix(CharMatrix &DepMatrix) {
8282}
8383#endif
8484
85+ static unsigned getAffectedLoopNum (const SCEV *Expr, ScalarEvolution &SE) {
86+ const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
87+ if (!AddRec)
88+ return 0 ;
89+
90+ const SCEV *Start = AddRec->getStart ();
91+ const SCEV *Step = AddRec->getStepRecurrence (SE);
92+ return getAffectedLoopNum (Start, SE) + getAffectedLoopNum (Step, SE) + 1 ;
93+ }
94+
95+ static bool hasConstantIndex (Instruction *Src, Instruction *Dst,
96+ ScalarEvolution *SE) {
97+ Value *PtrSrc = getLoadStorePointerOperand (Src);
98+ Value *PtrDst = getLoadStorePointerOperand (Dst);
99+ const SCEV *SrcSCEV = SE->getSCEV (PtrSrc);
100+ const SCEV *DstSCEV = SE->getSCEV (PtrDst);
101+
102+ unsigned SrcLoops = getAffectedLoopNum (SrcSCEV, *SE);
103+ unsigned DstLoops = getAffectedLoopNum (DstSCEV, *SE);
104+ // Loop interchange would need at least two loops. If the SCEV form
105+ // only has one loop or the loop numbers are not equal. There will be
106+ // at least one constant index.
107+ if (SrcLoops == 1 || DstLoops == 1 || SrcLoops != DstLoops)
108+ return true ;
109+
110+ return false ;
111+ }
112+
85113static bool populateDependencyMatrix (CharMatrix &DepMatrix, unsigned Level,
86114 Loop *L, DependenceInfo *DI,
87115 ScalarEvolution *SE) {
@@ -150,6 +178,13 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
150178 Direction = ' =' ;
151179 else
152180 Direction = ' *' ;
181+
182+ if (hasConstantIndex (Src, Dst, SE) &&
183+ (Direction == ' >' || Direction == ' <' )) {
184+ LLVM_DEBUG (dbgs () << " Has constant index with loop carried"
185+ << " dependencies inside loop\n " );
186+ return false ;
187+ }
153188 Dep.push_back (Direction);
154189 }
155190 }
0 commit comments