Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Local.h - Functions to perform local transformations -----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This family of functions perform various local transformations to the |
| 10 | // program. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_ANALYSIS_UTILS_LOCAL_H |
| 15 | #define LLVM_ANALYSIS_UTILS_LOCAL_H |
| 16 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | /// Given a getelementptr instruction/constantexpr, emit the code necessary to |
| 24 | /// compute the offset from the base pointer (without adding in the base |
| 25 | /// pointer). Return the result as a signed integer of intptr size. |
| 26 | /// When NoAssumptions is true, no assumptions about index computation not |
| 27 | /// overflowing is made. |
| 28 | template <typename IRBuilderTy> |
| 29 | Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, |
| 30 | bool NoAssumptions = false) { |
| 31 | GEPOperator *GEPOp = cast<GEPOperator>(GEP); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 32 | Type *IntIdxTy = DL.getIndexType(GEP->getType()); |
| 33 | Value *Result = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | |
| 35 | // If the GEP is inbounds, we know that none of the addressing operations will |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 36 | // overflow in a signed sense. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | bool isInBounds = GEPOp->isInBounds() && !NoAssumptions; |
| 38 | |
| 39 | // Build a mask for high order bits. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 40 | unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | uint64_t PtrSizeMask = |
| 42 | std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth); |
| 43 | |
| 44 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 45 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 46 | ++i, ++GTI) { |
| 47 | Value *Op = *i; |
| 48 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 49 | Value *Offset; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 51 | if (OpC->isZeroValue()) |
| 52 | continue; |
| 53 | |
| 54 | // Handle a struct index, which adds its field offset to the pointer. |
| 55 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 56 | uint64_t OpValue = OpC->getUniqueInteger().getZExtValue(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | Size = DL.getStructLayout(STy)->getElementOffset(OpValue); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 58 | if (!Size) |
| 59 | continue; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 60 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 61 | Offset = ConstantInt::get(IntIdxTy, Size); |
| 62 | } else { |
| 63 | // Splat the constant if needed. |
| 64 | if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy()) |
| 65 | OpC = ConstantVector::getSplat( |
| 66 | cast<VectorType>(IntIdxTy)->getElementCount(), OpC); |
| 67 | |
| 68 | Constant *Scale = ConstantInt::get(IntIdxTy, Size); |
| 69 | Constant *OC = |
| 70 | ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/); |
| 71 | Offset = |
| 72 | ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 74 | } else { |
| 75 | // Splat the index if needed. |
| 76 | if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy()) |
| 77 | Op = Builder->CreateVectorSplat( |
| 78 | cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 80 | // Convert to correct type. |
| 81 | if (Op->getType() != IntIdxTy) |
| 82 | Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName().str()+".c"); |
| 83 | if (Size != 1) { |
| 84 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 85 | Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size), |
| 86 | GEP->getName().str() + ".idx", false /*NUW*/, |
| 87 | isInBounds /*NSW*/); |
| 88 | } |
| 89 | Offset = Op; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 92 | if (Result) |
| 93 | Result = Builder->CreateAdd(Result, Offset, GEP->getName().str()+".offs", |
| 94 | false /*NUW*/, isInBounds /*NSW*/); |
| 95 | else |
| 96 | Result = Offset; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 98 | return Result ? Result : Constant::getNullValue(IntIdxTy); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 101 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | |
| 103 | #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H |