blob: 5b4c7524b672e40491fbd8617e19e53a2ee4c307 [file] [log] [blame]
Craig Topperc98f8832017-03-20 05:08:38 +00001//===-- 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
David Blaikie60310f22015-05-08 00:42:26 +000014#include "llvm/IR/Operator.h"
Craig Topperb5c2bfa2017-03-20 05:08:41 +000015#include "llvm/IR/DataLayout.h"
David Blaikie6f0d5222015-05-21 21:17:12 +000016#include "llvm/IR/GetElementPtrTypeIterator.h"
David Blaikie60310f22015-05-08 00:42:26 +000017#include "llvm/IR/Instructions.h"
18#include "llvm/IR/Type.h"
19
20#include "ConstantsContext.h"
21
22namespace llvm {
23Type *GEPOperator::getSourceElementType() const {
24 if (auto *I = dyn_cast<GetElementPtrInst>(this))
25 return I->getSourceElementType();
26 return cast<GetElementPtrConstantExpr>(this)->getSourceElementType();
27}
David Blaikie6f0d5222015-05-21 21:17:12 +000028
Eduard Burtescu19eb0312016-01-19 17:28:00 +000029Type *GEPOperator::getResultElementType() const {
30 if (auto *I = dyn_cast<GetElementPtrInst>(this))
31 return I->getResultElementType();
32 return cast<GetElementPtrConstantExpr>(this)->getResultElementType();
33}
34
David Blaikie6f0d5222015-05-21 21:17:12 +000035bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
36 APInt &Offset) const {
37 assert(Offset.getBitWidth() ==
Elena Demikhovsky945b7e52018-02-14 06:58:08 +000038 DL.getIndexSizeInBits(getPointerAddressSpace()) &&
39 "The offset bit width does not match DL specification.");
David Blaikie6f0d5222015-05-21 21:17:12 +000040
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.
Peter Collingbourneab85225b2016-12-02 02:24:42 +000050 if (StructType *STy = GTI.getStructTypeOrNull()) {
David Blaikie6f0d5222015-05-21 21:17:12 +000051 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}
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}