[LLVMdev] ScalarEvolution::getSVECAtScope

Thomas Jablin tjablin at cs.princeton.edu
Sat Apr 30 17:14:16 PDT 2011


Is it valid to query ScalarEvolution::getSCEVAtScope with a Value
which is an Instruction inside the provided Loop? If so, I believe
there is a bug in computeSCEVAtScope. Specifically:

04716     // If the scope is outside the addrec's loop, evaluate it by using the
04717     // loop exit value of the addrec.
04718     if (!AddRec->getLoop()->contains(L)) {
04719       // To evaluate this recurrence, we need to know how many
times the AddRec
04720       // loop iterates.  Compute this now.
04721       const SCEV *BackedgeTakenCount =
04722       if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
04724       // Then, evaluate the AddRec.
04725       return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
04726     }

The guard checks that !AddRec->getLoop()->contains(L), but not

Consequently, if getSCEVAtScope is called with GetElementPtrInst and a
Loop that contains it indirectly, the SCEV will be evaluated with
respect to the end of the inner Loop. For example:

L1 for(i to N)
L2  for(j to N)
     arrayidx = gep A, 0, j, i

getSCEVAtScope(arrayIdx, L1) will evaluate j as N.

If it is valid to call getSCEVAtScope with an outer loop, I believe
the guard should be:
04718     if (!AddRec->getLoop()->contains(L) &&
!L->contains(AddRec->getLoop())) {

If it is not valid, an assertion might be helpful.


More information about the llvm-dev mailing list