LLVM  13.0.0git
Local.h
Go to the documentation of this file.
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/StringRef.h"
18 #include "llvm/IR/DataLayout.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 = nullptr;
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 
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  Value *Offset;
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()) {
56  uint64_t OpValue = OpC->getUniqueInteger().getZExtValue();
57  Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
58  if (!Size)
59  continue;
60 
61  Offset = ConstantInt::get(IntIdxTy, Size);
62  } else {
63  // Splat the constant if needed.
64  if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
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*/);
73  }
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);
79 
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;
90  }
91 
92  if (Result)
93  Result = Builder->CreateAdd(Result, Offset, GEP->getName().str()+".offs",
94  false /*NUW*/, isInBounds /*NSW*/);
95  else
96  Result = Offset;
97  }
98  return Result ? Result : Constant::getNullValue(IntIdxTy);
99 }
100 
101 }
102 
103 #endif // LLVM_ANALYSIS_UTILS_LOCAL_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::generic_gep_type_iterator
Definition: GetElementPtrTypeIterator.h:31
StringRef.h
GetElementPtrTypeIterator.h
llvm::Type::getScalarType
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:317
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::gep_type_begin
gep_type_iterator gep_type_begin(const User *GEP)
Definition: GetElementPtrTypeIterator.h:139
llvm::EmitGEPOffset
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.h:29
llvm::User
Definition: User.h:44
llvm::Type::isVectorTy
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:235
llvm::ConstantVector::getSplat
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1397
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:885
llvm::generic_gep_type_iterator::getStructTypeOrNull
StructType * getStructTypeOrNull() const
Definition: GetElementPtrTypeIterator.h:123
llvm::Type::getIntegerBitWidth
unsigned getIntegerBitWidth() const
Definition: DerivedTypes.h:96
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::generic_gep_type_iterator::getIndexedType
Type * getIndexedType() const
Definition: GetElementPtrTypeIterator.h:72
llvm::SystemZISD::OC
@ OC
Definition: SystemZISelLowering.h:128
llvm::GEPOperator
Definition: Operator.h:457
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:649
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:212
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::ConstantExpr::getIntegerCast
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2045
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:347
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:314
llvm::GEPOperator::isInBounds
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:474
llvm::ConstantExpr::getMul
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2686
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44