LLVM  4.0.0
Operator.cpp
Go to the documentation of this file.
1 #include "llvm/IR/Operator.h"
3 #include "llvm/IR/Instructions.h"
4 #include "llvm/IR/Type.h"
5 
6 #include "ConstantsContext.h"
7 
8 namespace llvm {
10  if (auto *I = dyn_cast<GetElementPtrInst>(this))
11  return I->getSourceElementType();
12  return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
13 }
14 
16  if (auto *I = dyn_cast<GetElementPtrInst>(this))
17  return I->getResultElementType();
18  return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
19 }
20 
22  APInt &Offset) const {
23  assert(Offset.getBitWidth() ==
25  "The offset must have exactly as many bits as our pointer.");
26 
27  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
28  GTI != GTE; ++GTI) {
29  ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
30  if (!OpC)
31  return false;
32  if (OpC->isZero())
33  continue;
34 
35  // Handle a struct index, which adds its field offset to the pointer.
36  if (StructType *STy = GTI.getStructTypeOrNull()) {
37  unsigned ElementIdx = OpC->getZExtValue();
38  const StructLayout *SL = DL.getStructLayout(STy);
39  Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
40  continue;
41  }
42 
43  // For array or vector indices, scale the index by the size of the type.
44  APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
45  Offset += Index * APInt(Offset.getBitWidth(),
46  DL.getTypeAllocSize(GTI.getIndexedType()));
47  }
48  return true;
49 }
50 }
Type * getSourceElementType() const
Definition: Operator.cpp:9
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
gep_type_iterator gep_type_end(const User *GEP)
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:496
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:566
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:143
Class to represent struct types.
Definition: DerivedTypes.h:199
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:154
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:21
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:517
Type * getResultElementType() const
Definition: Operator.cpp:15
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
uint32_t Offset
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1255
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:408
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:198
Class for arbitrary precision integers.
Definition: APInt.h:77
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:349
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:420
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1007
gep_type_iterator gep_type_begin(const User *GEP)