LLVM  8.0.1
Operator.cpp
Go to the documentation of this file.
1 //===-- Operator.cpp - Implement the LLVM operators -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the non-inline methods for the LLVM Operator classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/Operator.h"
15 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Type.h"
19 
20 #include "ConstantsContext.h"
21 
22 namespace llvm {
24  if (auto *I = dyn_cast<GetElementPtrInst>(this))
25  return I->getSourceElementType();
26  return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
27 }
28 
30  if (auto *I = dyn_cast<GetElementPtrInst>(this))
31  return I->getResultElementType();
32  return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
33 }
34 
36  APInt &Offset) const {
37  assert(Offset.getBitWidth() ==
39  "The offset bit width does not match DL specification.");
40 
41  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
42  GTI != GTE; ++GTI) {
43  ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
44  if (!OpC)
45  return false;
46  if (OpC->isZero())
47  continue;
48 
49  // Handle a struct index, which adds its field offset to the pointer.
50  if (StructType *STy = GTI.getStructTypeOrNull()) {
51  unsigned ElementIdx = OpC->getZExtValue();
52  const StructLayout *SL = DL.getStructLayout(STy);
53  Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
54  continue;
55  }
56 
57  // For array or vector indices, scale the index by the size of the type.
58  APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
59  Offset += Index * APInt(Offset.getBitWidth(),
60  DL.getTypeAllocSize(GTI.getIndexedType()));
61  }
62  return true;
63 }
64 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
Definition: DataLayout.h:373
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:588
gep_type_iterator gep_type_end(const User *GEP)
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:486
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1509
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:35
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:529
Class to represent struct types.
Definition: DerivedTypes.h:201
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:884
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
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:149
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Class for arbitrary precision integers.
Definition: APInt.h:70
Type * getResultElementType() const
Definition: Operator.cpp:29
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:436
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:551
#define I(x, y, z)
Definition: MD5.cpp:58
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
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:323
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Type * getSourceElementType() const
Definition: Operator.cpp:23
gep_type_iterator gep_type_begin(const User *GEP)