[llvm-commits] [llvm] r169852 - in /llvm/trunk: include/llvm/Analysis/PtrUseVisitor.h include/llvm/Instructions.h lib/Analysis/PtrUseVisitor.cpp lib/VMCore/Instructions.cpp

Duncan Sands baldrick at free.fr
Tue Dec 11 03:55:20 PST 2012


Hi Chandler,

 > +  /// \brief Accumulate the constant address offset of this GEP if possible.
> +  ///
> +  /// This routine accepts an APInt into which it will accumulate the constant
> +  /// offset of this GEP if the GEP is in fact constant. If the GEP is not
> +  /// all-constant, it returns false and the value of the offset APInt is
> +  /// undefined (it is *not* preserved!). The APInt passed into this routine
> +  /// must be at least as wide

here you say "at least as wide", but...

  as the IntPtr type for the address space of
> +  /// the base GEP pointer.
> +  bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
> +
>     // Methods for support type inquiry through isa, cast, and dyn_cast:
>     static inline bool classof(const Instruction *I) {
>       return (I->getOpcode() == Instruction::GetElementPtr);



> @@ -1423,6 +1425,37 @@
>     return cast<GEPOperator>(this)->isInBounds();
>   }
>
> +bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
> +                                                 APInt &Offset) const {

[This interface can't work for vector-GEPS, as it would need to return a vector
of offsets]

> +  assert(Offset.getBitWidth() ==
> +         DL.getPointerSizeInBits(getPointerAddressSpace()) &&
> +         "The offset must have exactly as many bits as our pointer.");

...here you check that the widths are exactly the same.

> +
> +  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
> +       GTI != GTE; ++GTI) {
> +    ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
> +    if (!OpC)
> +      return false;
> +    if (OpC->isZero())
> +      continue;
> +
> +    // Handle a struct index, which adds its field offset to the pointer.
> +    if (StructType *STy = dyn_cast<StructType>(*GTI)) {
> +      unsigned ElementIdx = OpC->getZExtValue();

This will fail on vector-gep, you should use Constant::getUniqueInteger instead.

> +      const StructLayout *SL = DL.getStructLayout(STy);
> +      Offset += APInt(Offset.getBitWidth(),
> +                      SL->getElementOffset(ElementIdx));
> +      continue;
> +    }
> +
> +    // For array or vector indices, scale the index by the size of the type.
> +    APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
> +    Offset += Index * APInt(Offset.getBitWidth(),
> +                            DL.getTypeAllocSize(GTI.getIndexedType()));
> +  }
> +  return true;
> +}
> +

Ciao, Duncan.




More information about the llvm-commits mailing list