Operator.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===-- Operator.cpp - Implement the LLVM operators -----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the non-inline methods for the LLVM Operator classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/Operator.h"
  13. #include "llvm/IR/DataLayout.h"
  14. #include "llvm/IR/GetElementPtrTypeIterator.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/Type.h"
  17. #include "ConstantsContext.h"
  18. namespace llvm {
  19. Type *GEPOperator::getSourceElementType() const {
  20. if (auto *I = dyn_cast<GetElementPtrInst>(this))
  21. return I->getSourceElementType();
  22. return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
  23. }
  24. Type *GEPOperator::getResultElementType() const {
  25. if (auto *I = dyn_cast<GetElementPtrInst>(this))
  26. return I->getResultElementType();
  27. return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
  28. }
  29. bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
  30. APInt &Offset) const {
  31. assert(Offset.getBitWidth() ==
  32. DL.getIndexSizeInBits(getPointerAddressSpace()) &&
  33. "The offset bit width does not match DL specification.");
  34. for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
  35. GTI != GTE; ++GTI) {
  36. ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
  37. if (!OpC)
  38. return false;
  39. if (OpC->isZero())
  40. continue;
  41. // Handle a struct index, which adds its field offset to the pointer.
  42. if (StructType *STy = GTI.getStructTypeOrNull()) {
  43. unsigned ElementIdx = OpC->getZExtValue();
  44. const StructLayout *SL = DL.getStructLayout(STy);
  45. Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
  46. continue;
  47. }
  48. // For array or vector indices, scale the index by the size of the type.
  49. APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
  50. Offset += Index * APInt(Offset.getBitWidth(),
  51. DL.getTypeAllocSize(GTI.getIndexedType()));
  52. }
  53. return true;
  54. }
  55. }