1 //===- Local.h - Functions to perform local transformations -----*- C++ -*-===// 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 #ifndef LLVM_ANALYSIS_UTILS_LOCAL_H 15 #define LLVM_ANALYSIS_UTILS_LOCAL_H 16 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/GetElementPtrTypeIterator.h" 20 21 namespace llvm { 22 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); 32 Type *IntIdxTy = DL.getIndexType(GEP->getType()); 33 Value *Result = Constant::getNullValue(IntIdxTy); 34 35 // If the GEP is inbounds, we know that none of the addressing operations will 36 // overflow in a signed sense. 37 bool isInBounds = GEPOp->isInBounds() && !NoAssumptions; 38 39 // Build a mask for high order bits. 40 unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth(); 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; 49 if (Constant *OpC = dyn_cast<Constant>(Op)) { 50 if (OpC->isZeroValue()) 51 continue; 52 53 // Handle a struct index, which adds its field offset to the pointer. 54 if (StructType *STy = GTI.getStructTypeOrNull()) { 55 uint64_t OpValue = OpC->getUniqueInteger().getZExtValue(); 56 Size = DL.getStructLayout(STy)->getElementOffset(OpValue); 57 58 if (Size) 59 Result = Builder->CreateAdd(Result, ConstantInt::get(IntIdxTy, Size), 60 GEP->getName()+".offs"); 61 continue; 62 } 63 64 // Splat the constant if needed. 65 if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy()) 66 OpC = ConstantVector::getSplat(IntIdxTy->getVectorNumElements(), OpC); 67 68 Constant *Scale = ConstantInt::get(IntIdxTy, Size); 69 Constant *OC = ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/); 70 Scale = 71 ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/); 72 // Emit an add instruction. 73 Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs"); 74 continue; 75 } 76 77 // Splat the index if needed. 78 if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy()) 79 Op = Builder->CreateVectorSplat(IntIdxTy->getVectorNumElements(), Op); 80 81 // Convert to correct type. 82 if (Op->getType() != IntIdxTy) 83 Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName()+".c"); 84 if (Size != 1) { 85 // We'll let instcombine(mul) convert this to a shl if possible. 86 Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size), 87 GEP->getName() + ".idx", false /*NUW*/, 88 isInBounds /*NSW*/); 89 } 90 91 // Emit an add instruction. 92 Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs"); 93 } 94 return Result; 95 } 96 97 } 98 99 #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H 100