2
0

Operator.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "llvm/IR/Operator.h"
  2. #include "llvm/IR/GetElementPtrTypeIterator.h"
  3. #include "llvm/IR/Instructions.h"
  4. #include "llvm/IR/Type.h"
  5. #include "ConstantsContext.h"
  6. namespace llvm {
  7. Type *GEPOperator::getSourceElementType() const {
  8. if (auto *I = dyn_cast<GetElementPtrInst>(this))
  9. return I->getSourceElementType();
  10. return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
  11. }
  12. bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
  13. APInt &Offset) const {
  14. assert(Offset.getBitWidth() ==
  15. DL.getPointerSizeInBits(getPointerAddressSpace()) &&
  16. "The offset must have exactly as many bits as our pointer.");
  17. for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
  18. GTI != GTE; ++GTI) {
  19. ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
  20. if (!OpC)
  21. return false;
  22. if (OpC->isZero())
  23. continue;
  24. // Handle a struct index, which adds its field offset to the pointer.
  25. if (StructType *STy = dyn_cast<StructType>(*GTI)) {
  26. unsigned ElementIdx = OpC->getZExtValue();
  27. const StructLayout *SL = DL.getStructLayout(STy);
  28. Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
  29. continue;
  30. }
  31. // For array or vector indices, scale the index by the size of the type.
  32. APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
  33. Offset += Index * APInt(Offset.getBitWidth(),
  34. DL.getTypeAllocSize(GTI.getIndexedType()));
  35. }
  36. return true;
  37. }
  38. }