From 3434004dce348822eb56b38ee3e48e0a0f8dae08 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 13 Mar 2005 19:04:41 +0000 Subject: [PATCH] add a StructLayout::getElementContainingOffset method. llvm-svn: 20579 --- llvm/lib/Target/TargetData.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/llvm/lib/Target/TargetData.cpp b/llvm/lib/Target/TargetData.cpp index fc58f5774971..395f25ed671e 100644 --- a/llvm/lib/Target/TargetData.cpp +++ b/llvm/lib/Target/TargetData.cpp @@ -22,6 +22,7 @@ #include "llvm/Constants.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" +#include using namespace llvm; // Handle the Pass registration stuff necessary to use TargetData's. @@ -71,6 +72,22 @@ StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { StructSize = (StructSize/StructAlignment + 1) * StructAlignment; } + +/// getElementContainingOffset - Given a valid offset into the structure, +/// return the structure index that contains it. +unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { + std::vector::const_iterator SI = + std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(), + Offset); + assert(SI != MemberOffsets.begin() && "Offset not in structure type!"); + --SI; + assert(*SI <= Offset && "upper_bound didn't work"); + assert((SI == MemberOffsets.begin() || *(SI-1) < Offset) && + (SI+1 == MemberOffsets.end() || *(SI+1) > Offset) && + "Upper bound didn't work!"); + return SI-MemberOffsets.begin(); +} + //===----------------------------------------------------------------------===// // TargetData Class Implementation //===----------------------------------------------------------------------===//