blob: 2fba24d99b30ab4afa2cfea71f333ca784fc130c [file] [log] [blame]
David Blaikie60310f22015-05-08 00:42:26 +00001#include "llvm/IR/Operator.h"
David Blaikie6f0d5222015-05-21 21:17:12 +00002#include "llvm/IR/GetElementPtrTypeIterator.h"
David Blaikie60310f22015-05-08 00:42:26 +00003#include "llvm/IR/Instructions.h"
4#include "llvm/IR/Type.h"
5
6#include "ConstantsContext.h"
7
8namespace llvm {
9Type *GEPOperator::getSourceElementType() const {
10 if (auto *I = dyn_cast<GetElementPtrInst>(this))
11 return I->getSourceElementType();
12 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
13}
David Blaikie6f0d5222015-05-21 21:17:12 +000014
Eduard Burtescu19eb0312016-01-19 17:28:00 +000015Type *GEPOperator::getResultElementType() const {
16 if (auto *I = dyn_cast<GetElementPtrInst>(this))
17 return I->getResultElementType();
18 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
19}
20
David Blaikie6f0d5222015-05-21 21:17:12 +000021bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
22 APInt &Offset) const {
23 assert(Offset.getBitWidth() ==
24 DL.getPointerSizeInBits(getPointerAddressSpace()) &&
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.
Peter Collingbourneab85225b2016-12-02 02:24:42 +000036 if (StructType *STy = GTI.getStructTypeOrNull()) {
David Blaikie6f0d5222015-05-21 21:17:12 +000037 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}
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}