/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/IR/Operator.cpp
Line | Count | Source |
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" |
16 | | #include "llvm/IR/GetElementPtrTypeIterator.h" |
17 | | #include "llvm/IR/Instructions.h" |
18 | | #include "llvm/IR/Type.h" |
19 | | |
20 | | #include "ConstantsContext.h" |
21 | | |
22 | | namespace llvm { |
23 | 406M | Type *GEPOperator::getSourceElementType() const { |
24 | 406M | if (auto *I = dyn_cast<GetElementPtrInst>(this)) |
25 | 334M | return I->getSourceElementType(); |
26 | 71.9M | return cast<GetElementPtrConstantExpr>(this)->getSourceElementType(); |
27 | 71.9M | } |
28 | | |
29 | 21.7M | Type *GEPOperator::getResultElementType() const { |
30 | 21.7M | if (auto *I = dyn_cast<GetElementPtrInst>(this)) |
31 | 46.1k | return I->getResultElementType(); |
32 | 21.7M | return cast<GetElementPtrConstantExpr>(this)->getResultElementType(); |
33 | 21.7M | } |
34 | | |
35 | | bool GEPOperator::accumulateConstantOffset(const DataLayout &DL, |
36 | 18.8M | APInt &Offset) const { |
37 | 18.8M | assert(Offset.getBitWidth() == |
38 | 18.8M | DL.getPointerSizeInBits(getPointerAddressSpace()) && |
39 | 18.8M | "The offset must have exactly as many bits as our pointer."); |
40 | 18.8M | |
41 | 18.8M | for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this); |
42 | 57.2M | GTI != GTE57.2M ; ++GTI38.3M ) { |
43 | 41.6M | ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); |
44 | 41.6M | if (!OpC) |
45 | 3.21M | return false; |
46 | 38.3M | if (38.3M OpC->isZero()38.3M ) |
47 | 17.6M | continue; |
48 | 20.7M | |
49 | 20.7M | // Handle a struct index, which adds its field offset to the pointer. |
50 | 20.7M | if (StructType *20.7M STy20.7M = GTI.getStructTypeOrNull()) { |
51 | 12.8M | unsigned ElementIdx = OpC->getZExtValue(); |
52 | 12.8M | const StructLayout *SL = DL.getStructLayout(STy); |
53 | 12.8M | Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)); |
54 | 12.8M | continue; |
55 | 12.8M | } |
56 | 7.86M | |
57 | 7.86M | // For array or vector indices, scale the index by the size of the type. |
58 | 7.86M | APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth()); |
59 | 7.86M | Offset += Index * APInt(Offset.getBitWidth(), |
60 | 7.86M | DL.getTypeAllocSize(GTI.getIndexedType())); |
61 | 7.86M | } |
62 | 15.6M | return true; |
63 | 18.8M | } |
64 | | } |