1 //===- PtrUseVisitor.cpp - InstVisitors over a pointers uses --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// Implementation of the pointer use visitors.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/PtrUseVisitor.h"
18 void detail::PtrUseVisitorBase::enqueueUsers(Instruction &I) {
19 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
21 if (VisitedUses.insert(&UI.getUse())) {
23 UseToVisit::UseAndIsOffsetKnownPair(&UI.getUse(), IsOffsetKnown),
26 Worklist.push_back(llvm_move(NewU));
31 bool detail::PtrUseVisitorBase::adjustOffsetForGEP(GetElementPtrInst &GEPI) {
35 for (gep_type_iterator GTI = gep_type_begin(GEPI), GTE = gep_type_end(GEPI);
37 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
43 // Handle a struct index, which adds its field offset to the pointer.
44 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
45 unsigned ElementIdx = OpC->getZExtValue();
46 const StructLayout *SL = DL.getStructLayout(STy);
47 Offset += APInt(Offset.getBitWidth(),
48 SL->getElementOffset(ElementIdx));
52 // For array or vector indices, scale the index by the size of the type.
53 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
54 Offset += Index * APInt(Offset.getBitWidth(),
55 DL.getTypeAllocSize(GTI.getIndexedType()));