LLVM  6.0.0svn
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 must have exactly as many bits as our pointer.");
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:109
Compute iterated dominance frontiers using a linear time algorithm.
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:562
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:468
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:344
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
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:491
Class to represent struct types.
Definition: DerivedTypes.h:201
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:891
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:69
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:403
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:513
#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)