Local.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===- Local.cpp - Functions to perform local transformations -------------===//
  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 family of functions perform various local transformations to the
  10. // program.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/Utils/Local.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/IR/DataLayout.h"
  16. #include "llvm/IR/GetElementPtrTypeIterator.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. using namespace llvm;
  19. Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
  20. User *GEP, bool NoAssumptions) {
  21. GEPOperator *GEPOp = cast<GEPOperator>(GEP);
  22. Type *IntIdxTy = DL.getIndexType(GEP->getType());
  23. Value *Result = nullptr;
  24. // If the GEP is inbounds, we know that none of the addressing operations will
  25. // overflow in a signed sense.
  26. bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
  27. // Build a mask for high order bits.
  28. unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth();
  29. uint64_t PtrSizeMask =
  30. std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth);
  31. gep_type_iterator GTI = gep_type_begin(GEP);
  32. for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
  33. ++i, ++GTI) {
  34. Value *Op = *i;
  35. uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
  36. Value *Offset;
  37. if (Constant *OpC = dyn_cast<Constant>(Op)) {
  38. if (OpC->isZeroValue())
  39. continue;
  40. // Handle a struct index, which adds its field offset to the pointer.
  41. if (StructType *STy = GTI.getStructTypeOrNull()) {
  42. uint64_t OpValue = OpC->getUniqueInteger().getZExtValue();
  43. Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
  44. if (!Size)
  45. continue;
  46. Offset = ConstantInt::get(IntIdxTy, Size);
  47. } else {
  48. // Splat the constant if needed.
  49. if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
  50. OpC = ConstantVector::getSplat(
  51. cast<VectorType>(IntIdxTy)->getElementCount(), OpC);
  52. Constant *Scale = ConstantInt::get(IntIdxTy, Size);
  53. Constant *OC =
  54. ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/);
  55. Offset =
  56. ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
  57. }
  58. } else {
  59. // Splat the index if needed.
  60. if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
  61. Op = Builder->CreateVectorSplat(
  62. cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
  63. // Convert to correct type.
  64. if (Op->getType() != IntIdxTy)
  65. Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
  66. if (Size != 1) {
  67. // We'll let instcombine(mul) convert this to a shl if possible.
  68. Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
  69. GEP->getName() + ".idx", false /*NUW*/,
  70. isInBounds /*NSW*/);
  71. }
  72. Offset = Op;
  73. }
  74. if (Result)
  75. Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
  76. false /*NUW*/, isInBounds /*NSW*/);
  77. else
  78. Result = Offset;
  79. }
  80. return Result ? Result : Constant::getNullValue(IntIdxTy);
  81. }