LLVM  10.0.0svn
Operator.cpp
Go to the documentation of this file.
1 //===-- Operator.cpp - Implement the LLVM operators -----------------------===//
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 file implements the non-inline methods for the LLVM Operator classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Operator.h"
14 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/Type.h"
18 
19 #include "ConstantsContext.h"
20 
21 namespace llvm {
23  if (auto *I = dyn_cast<GetElementPtrInst>(this))
24  return I->getSourceElementType();
25  return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
26 }
27 
29  if (auto *I = dyn_cast<GetElementPtrInst>(this))
30  return I->getResultElementType();
31  return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
32 }
33 
35  APInt &Offset) const {
36  assert(Offset.getBitWidth() ==
38  "The offset bit width does not match DL specification.");
39 
40  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
41  GTI != GTE; ++GTI) {
42  ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
43  if (!OpC)
44  return false;
45  if (OpC->isZero())
46  continue;
47 
48  // Handle a struct index, which adds its field offset to the pointer.
49  if (StructType *STy = GTI.getStructTypeOrNull()) {
50  unsigned ElementIdx = OpC->getZExtValue();
51  const StructLayout *SL = DL.getStructLayout(STy);
52  Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
53  continue;
54  }
55 
56  // For array or vector indices, scale the index by the size of the type.
57  APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
58  Offset += Index * APInt(Offset.getBitWidth(),
59  DL.getTypeAllocSize(GTI.getIndexedType()));
60  }
61  return true;
62 }
63 }
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:402
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:613
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:501
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1517
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:34
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:585
Class to represent struct types.
Definition: DerivedTypes.h:238
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:938
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:137
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:148
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:486
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
Class for arbitrary precision integers.
Definition: APInt.h:69
Type * getResultElementType() const
Definition: Operator.cpp:28
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:607
#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:192
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:332
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Type * getSourceElementType() const
Definition: Operator.cpp:22
gep_type_iterator gep_type_begin(const User *GEP)