LLVM  9.0.0svn
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/IR/DataLayout.h"
19 
20 namespace llvm {
21 
22 /// Given a getelementptr instruction/constantexpr, emit the code necessary to
23 /// compute the offset from the base pointer (without adding in the base
24 /// pointer). Return the result as a signed integer of intptr size.
25 /// When NoAssumptions is true, no assumptions about index computation not
26 /// overflowing is made.
27 template <typename IRBuilderTy>
28 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
29  bool NoAssumptions = false) {
30  GEPOperator *GEPOp = cast<GEPOperator>(GEP);
31  Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
32  Value *Result = Constant::getNullValue(IntPtrTy);
33 
34  // If the GEP is inbounds, we know that none of the addressing operations will
35  // overflow in an unsigned sense.
36  bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
37 
38  // Build a mask for high order bits.
39  unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
40  uint64_t PtrSizeMask =
41  std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth);
42 
44  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
45  ++i, ++GTI) {
46  Value *Op = *i;
47  uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
48  if (Constant *OpC = dyn_cast<Constant>(Op)) {
49  if (OpC->isZeroValue())
50  continue;
51 
52  // Handle a struct index, which adds its field offset to the pointer.
53  if (StructType *STy = GTI.getStructTypeOrNull()) {
54  if (OpC->getType()->isVectorTy())
55  OpC = OpC->getSplatValue();
56 
57  uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
58  Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
59 
60  if (Size)
61  Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
62  GEP->getName()+".offs");
63  continue;
64  }
65 
66  Constant *Scale = ConstantInt::get(IntPtrTy, Size);
67  Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
68  Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
69  // Emit an add instruction.
70  Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
71  continue;
72  }
73  // Convert to correct type.
74  if (Op->getType() != IntPtrTy)
75  Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
76  if (Size != 1) {
77  // We'll let instcombine(mul) convert this to a shl if possible.
78  Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
79  GEP->getName()+".idx", isInBounds /*NUW*/);
80  }
81 
82  // Emit an add instruction.
83  Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
84  }
85  return Result;
86 }
87 
88 }
89 
90 #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H
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:28
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:607
Hexagon Common GEP
op_iterator op_begin()
Definition: User.h:229
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:274
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1622
Class to represent struct types.
Definition: DerivedTypes.h:232
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:457
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:769
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
op_iterator op_end()
Definition: User.h:231
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:631
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:461
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:576
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
uint32_t Size
Definition: Profile.cpp:46
LLVM Value Representation.
Definition: Value.h:72
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
gep_type_iterator gep_type_begin(const User *GEP)