LLVM  3.7.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  APInt &Offset) const {
17  assert(Offset.getBitWidth() ==
19  "The offset must have exactly as many bits as our pointer.");
20 
21  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
22  GTI != GTE; ++GTI) {
23  ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
24  if (!OpC)
25  return false;
26  if (OpC->isZero())
27  continue;
28 
29  // Handle a struct index, which adds its field offset to the pointer.
30  if (StructType *STy = dyn_cast<StructType>(*GTI)) {
31  unsigned ElementIdx = OpC->getZExtValue();
32  const StructLayout *SL = DL.getStructLayout(STy);
33  Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
34  continue;
35  }
36 
37  // For array or vector indices, scale the index by the size of the type.
38  APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
39  Offset += Index * APInt(Offset.getBitWidth(),
40  DL.getTypeAllocSize(GTI.getIndexedType()));
41  }
42  return true;
43 }
44 }
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:104
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:475
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:551
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:106
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
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:117
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:15
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:491
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1273
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:161
Class for arbitrary precision integers.
Definition: APInt.h:73
LLVM_ATTRIBUTE_UNUSED_RESULT 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:285
#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:329
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:406
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1023
gep_type_iterator gep_type_begin(const User *GEP)