aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/TargetData.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2005-03-13 19:04:41 +0000
committerChris Lattner <sabre@nondot.org>2005-03-13 19:04:41 +0000
commite7ea48cb612bc2fba1cc674fd6bff2bf3e6d5247 (patch)
treea2bb8830a27813af02f033129589c3d6c4b7503b /lib/Target/TargetData.cpp
parent977df767228a1754d17c8624c33138325e5dbf1b (diff)
add a StructLayout::getElementContainingOffset method.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20579 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Target/TargetData.cpp')
-rw-r--r--lib/Target/TargetData.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp
index fc58f57749..395f25ed67 100644
--- a/lib/Target/TargetData.cpp
+++ b/lib/Target/TargetData.cpp
@@ -22,6 +22,7 @@
#include "llvm/Constants.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/MathExtras.h"
+#include <algorithm>
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<uint64_t>::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
//===----------------------------------------------------------------------===//