1bdd1243dSDimitry Andric //===- Local.cpp - Functions to perform local transformations -------------===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric // 9bdd1243dSDimitry Andric // This family of functions perform various local transformations to the 10bdd1243dSDimitry Andric // program. 11bdd1243dSDimitry Andric // 12bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 13bdd1243dSDimitry Andric 14bdd1243dSDimitry Andric #include "llvm/Analysis/Utils/Local.h" 15bdd1243dSDimitry Andric #include "llvm/ADT/Twine.h" 16bdd1243dSDimitry Andric #include "llvm/IR/DataLayout.h" 17bdd1243dSDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h" 18bdd1243dSDimitry Andric #include "llvm/IR/IRBuilder.h" 19bdd1243dSDimitry Andric 20bdd1243dSDimitry Andric using namespace llvm; 21bdd1243dSDimitry Andric 22bdd1243dSDimitry Andric Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, 23bdd1243dSDimitry Andric User *GEP, bool NoAssumptions) { 24bdd1243dSDimitry Andric GEPOperator *GEPOp = cast<GEPOperator>(GEP); 25bdd1243dSDimitry Andric Type *IntIdxTy = DL.getIndexType(GEP->getType()); 26bdd1243dSDimitry Andric Value *Result = nullptr; 27bdd1243dSDimitry Andric 28*0fca6ea1SDimitry Andric // nusw implies nsw for the offset arithmetic. 29*0fca6ea1SDimitry Andric bool NSW = GEPOp->hasNoUnsignedSignedWrap() && !NoAssumptions; 30*0fca6ea1SDimitry Andric bool NUW = GEPOp->hasNoUnsignedWrap() && !NoAssumptions; 315f757f3fSDimitry Andric auto AddOffset = [&](Value *Offset) { 325f757f3fSDimitry Andric if (Result) 335f757f3fSDimitry Andric Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs", 34*0fca6ea1SDimitry Andric NUW, NSW); 355f757f3fSDimitry Andric else 365f757f3fSDimitry Andric Result = Offset; 375f757f3fSDimitry Andric }; 38bdd1243dSDimitry Andric 39bdd1243dSDimitry Andric gep_type_iterator GTI = gep_type_begin(GEP); 40bdd1243dSDimitry Andric for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; 41bdd1243dSDimitry Andric ++i, ++GTI) { 42bdd1243dSDimitry Andric Value *Op = *i; 43bdd1243dSDimitry Andric if (Constant *OpC = dyn_cast<Constant>(Op)) { 44bdd1243dSDimitry Andric if (OpC->isZeroValue()) 45bdd1243dSDimitry Andric continue; 46bdd1243dSDimitry Andric 47bdd1243dSDimitry Andric // Handle a struct index, which adds its field offset to the pointer. 48bdd1243dSDimitry Andric if (StructType *STy = GTI.getStructTypeOrNull()) { 49bdd1243dSDimitry Andric uint64_t OpValue = OpC->getUniqueInteger().getZExtValue(); 505f757f3fSDimitry Andric uint64_t Size = DL.getStructLayout(STy)->getElementOffset(OpValue); 51bdd1243dSDimitry Andric if (!Size) 52bdd1243dSDimitry Andric continue; 53bdd1243dSDimitry Andric 545f757f3fSDimitry Andric AddOffset(ConstantInt::get(IntIdxTy, Size)); 555f757f3fSDimitry Andric continue; 56bdd1243dSDimitry Andric } 575f757f3fSDimitry Andric } 585f757f3fSDimitry Andric 59bdd1243dSDimitry Andric // Splat the index if needed. 60bdd1243dSDimitry Andric if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy()) 61bdd1243dSDimitry Andric Op = Builder->CreateVectorSplat( 625f757f3fSDimitry Andric cast<VectorType>(IntIdxTy)->getElementCount(), Op); 63bdd1243dSDimitry Andric 64bdd1243dSDimitry Andric // Convert to correct type. 65bdd1243dSDimitry Andric if (Op->getType() != IntIdxTy) 66bdd1243dSDimitry Andric Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c"); 671db9f3b2SDimitry Andric TypeSize TSize = GTI.getSequentialElementStride(DL); 685f757f3fSDimitry Andric if (TSize != TypeSize::getFixed(1)) { 695f757f3fSDimitry Andric Value *Scale = Builder->CreateTypeSize(IntIdxTy->getScalarType(), TSize); 705f757f3fSDimitry Andric if (IntIdxTy->isVectorTy()) 715f757f3fSDimitry Andric Scale = Builder->CreateVectorSplat( 725f757f3fSDimitry Andric cast<VectorType>(IntIdxTy)->getElementCount(), Scale); 73bdd1243dSDimitry Andric // We'll let instcombine(mul) convert this to a shl if possible. 74*0fca6ea1SDimitry Andric Op = Builder->CreateMul(Op, Scale, GEP->getName() + ".idx", NUW, NSW); 75bdd1243dSDimitry Andric } 765f757f3fSDimitry Andric AddOffset(Op); 77bdd1243dSDimitry Andric } 78bdd1243dSDimitry Andric return Result ? Result : Constant::getNullValue(IntIdxTy); 79bdd1243dSDimitry Andric } 80