| 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 | |
| 14 | #include "llvm/Analysis/Utils/Local.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 18 | #include "llvm/IR/IRBuilder.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, |
| 23 | User *GEP, bool NoAssumptions) { |
| 24 | GEPOperator *GEPOp = cast<GEPOperator>(Val: GEP); |
| 25 | Type *IntIdxTy = DL.getIndexType(PtrTy: GEP->getType()); |
| 26 | Value *Result = nullptr; |
| 27 | |
| 28 | // nusw implies nsw for the offset arithmetic. |
| 29 | bool NSW = GEPOp->hasNoUnsignedSignedWrap() && !NoAssumptions; |
| 30 | bool NUW = GEPOp->hasNoUnsignedWrap() && !NoAssumptions; |
| 31 | auto AddOffset = [&](Value *Offset) { |
| 32 | if (Result) |
| 33 | Result = Builder->CreateAdd(LHS: Result, RHS: Offset, Name: GEP->getName() + ".offs" , |
| 34 | HasNUW: NUW, HasNSW: NSW); |
| 35 | else |
| 36 | Result = Offset; |
| 37 | }; |
| 38 | |
| 39 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 40 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 41 | ++i, ++GTI) { |
| 42 | Value *Op = *i; |
| 43 | if (Constant *OpC = dyn_cast<Constant>(Val: Op)) { |
| 44 | if (OpC->isZeroValue()) |
| 45 | continue; |
| 46 | |
| 47 | // Handle a struct index, which adds its field offset to the pointer. |
| 48 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
| 49 | uint64_t OpValue = OpC->getUniqueInteger().getZExtValue(); |
| 50 | uint64_t Size = DL.getStructLayout(Ty: STy)->getElementOffset(Idx: OpValue); |
| 51 | if (!Size) |
| 52 | continue; |
| 53 | |
| 54 | AddOffset(ConstantInt::get(Ty: IntIdxTy, V: Size)); |
| 55 | continue; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Splat the index if needed. |
| 60 | if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy()) |
| 61 | Op = Builder->CreateVectorSplat( |
| 62 | EC: cast<VectorType>(Val: IntIdxTy)->getElementCount(), V: Op); |
| 63 | |
| 64 | // Convert to correct type. |
| 65 | if (Op->getType() != IntIdxTy) |
| 66 | Op = Builder->CreateIntCast(V: Op, DestTy: IntIdxTy, isSigned: true, Name: Op->getName() + ".c" ); |
| 67 | TypeSize TSize = GTI.getSequentialElementStride(DL); |
| 68 | if (TSize != TypeSize::getFixed(ExactSize: 1)) { |
| 69 | Value *Scale = Builder->CreateTypeSize(Ty: IntIdxTy->getScalarType(), Size: TSize); |
| 70 | if (IntIdxTy->isVectorTy()) |
| 71 | Scale = Builder->CreateVectorSplat( |
| 72 | EC: cast<VectorType>(Val: IntIdxTy)->getElementCount(), V: Scale); |
| 73 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 74 | Op = Builder->CreateMul(LHS: Op, RHS: Scale, Name: GEP->getName() + ".idx" , HasNUW: NUW, HasNSW: NSW); |
| 75 | } |
| 76 | AddOffset(Op); |
| 77 | } |
| 78 | return Result ? Result : Constant::getNullValue(Ty: IntIdxTy); |
| 79 | } |
| 80 | |