[llvm-commits] [llvm] r157649 - /llvm/trunk/lib/Transforms/Scalar/BoundsChecking.cpp

Andrew Trick atrick at apple.com
Tue May 29 22:12:33 PDT 2012


On May 29, 2012, at 3:32 PM, Nuno Lopes <nunoplopes at sapo.pt> wrote:

> -  if ((GEP = dyn_cast<GEPOperator>(Ptr))) {
> +  // try to hoist the check if the instruction is inside a loop
> +  Value *LoopOffset = 0;
> +  if (Loop *L = LI->getLoopFor(Builder->GetInsertPoint()->getParent())) {
> +    const SCEV *PtrSCEV  = SE->getSCEVAtScope(Ptr, L->getParentLoop());
> +    const SCEV *BaseSCEV = SE->getPointerBase(PtrSCEV);
> +
> +    if (const SCEVUnknown *PointerBase = dyn_cast<SCEVUnknown>(BaseSCEV)) {
> +      Ptr = PointerBase->getValue()->stripPointerCasts();
> +      Instruction *InsertPoint = L->getLoopPreheader()->getFirstInsertionPt();
> +      Builder->SetInsertPoint(InsertPoint);
> +
> +      SCEVExpander Expander(*SE, "bounds-checking");
> +      const SCEV *OffsetSCEV = SE->getMinusSCEV(PtrSCEV, PointerBase);
> +      LoopOffset = Expander.expandCodeFor(OffsetSCEV, SizeTy, InsertPoint);
> +    }
> +  }
> +
> +  GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr);
> +  if (GEP) {

This is probably fine for now. It's a relatively safe use of SCEVExpander and the least effort approach, but generally I would like to encourage people to solve local rewrite problems with IRBuilder, not SCEVExpander, and build useful utilities for that purpose. Just because you use SCEV doesn't mean you have to use SCEVExpander. SCEVExpander may try to do more rewriting than you expect--it can create an indefinite number of new instructions. In general, it can create new phis, reassociate GEPs, drop "inbounds" and create ugly GEPS. Things I really hate to see before LSR! It also makes strong assumptions about valid input that the caller has to check first and is unable to bailout when it hits a strange case. Supporting SCEVExpander also imposes limitations on SCEV that would be nice to remove some day.

As an alternative, you can find your GEP's underlying phi and grab the value from the preheader. SCEV will tell you the difference between getSCEVAtScope and the preheader's expression, hopefully in terms of a constant or value+constant, and you should be able to materialize an inbounds gep. If any of these steps fail to find a simple solution, you can easily bailout. I'm glossing over the issues, but it would be nice to have a utility like this outside or alongside current SCEVExpander. I think most of the code in SCEVExpander is good, I just don't think the current design is well suited for use outside LSR.

-Andy


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120529/42ac931d/attachment.html>


More information about the llvm-commits mailing list