Skip to content

Commit 080969e

Browse files
committed
Simplify BuildPointerToOffset to index at byte granularity rather
than using underlying element size
1 parent 05df435 commit 080969e

File tree

4 files changed

+9
-108
lines changed

4 files changed

+9
-108
lines changed

include/remill/BC/Compat/PointerType.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/Arch/SPARC32/Arch.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Decode.h"
2222
#include "remill/Arch/Instruction.h"
2323
#include "remill/Arch/Name.h"
24-
#include "remill/BC/Compat/PointerType.h"
2524
#include "remill/BC/ABI.h"
2625
#include "remill/BC/Util.h"
2726
#include "remill/OS/OS.h"

lib/Arch/SPARC64/Arch.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "remill/Arch/Instruction.h"
2323
#include "remill/Arch/Name.h"
2424
#include "remill/BC/ABI.h"
25-
#include "remill/BC/Compat/PointerType.h"
2625
#include "remill/BC/Util.h"
2726
#include "remill/OS/OS.h"
2827

lib/BC/Util.cpp

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include "remill/BC/Compat/DebugInfo.h"
5454
#include "remill/BC/Compat/GlobalValue.h"
5555
#include "remill/BC/Compat/IRReader.h"
56-
#include "remill/BC/Compat/PointerType.h"
5756
#include "remill/BC/Compat/ToolOutputFile.h"
5857
#include "remill/BC/Compat/VectorType.h"
5958
#include "remill/BC/Compat/Verifier.h"
@@ -2390,30 +2389,18 @@ BuildIndexes(const llvm::DataLayout &dl, llvm::Type *type, size_t offset,
23902389
// and to give access to a module for data layouts.
23912390
llvm::Value *BuildPointerToOffset(llvm::IRBuilder<> &ir, llvm::Value *ptr,
23922391
size_t dest_elem_offset,
2393-
llvm::Type *dest_ptr_type_) {
2394-
2395-
const auto block = ir.GetInsertBlock();
2396-
llvm::Module *module = nullptr;
2397-
if (block) {
2398-
module = block->getModule();
2399-
} else if (auto gv = llvm::dyn_cast<llvm::GlobalValue>(ptr); gv) {
2400-
module = gv->getParent();
2392+
llvm::Type *dest_ptr_type) {
24012393

24022394
// TODO(pag): Improve the API to take a `DataLayout`, perhaps.
2403-
} else {
2404-
LOG(FATAL) << "Unable to get the current module.";
2405-
}
2406-
24072395
auto &context = ptr->getContext();
24082396
const auto i32_type = llvm::Type::getInt32Ty(context);
24092397

2410-
const auto &dl = module->getDataLayout();
24112398
llvm::SmallVector<llvm::Value *, 16> indexes;
24122399

24132400
auto ptr_type = llvm::dyn_cast<llvm::PointerType>(ptr->getType());
24142401
CHECK_NOTNULL(ptr_type);
24152402
const auto dest_elem_ptr_type =
2416-
llvm::dyn_cast<llvm::PointerType>(dest_ptr_type_);
2403+
llvm::dyn_cast<llvm::PointerType>(dest_ptr_type);
24172404
CHECK_NOTNULL(dest_elem_ptr_type);
24182405
auto ptr_addr_space = ptr_type->getAddressSpace();
24192406
const auto dest_ptr_addr_space = dest_elem_ptr_type->getAddressSpace();
@@ -2433,73 +2420,17 @@ llvm::Value *BuildPointerToOffset(llvm::IRBuilder<> &ir, llvm::Value *ptr,
24332420
}
24342421
}
24352422

2436-
// NOTE(alex): Same here
2437-
const auto dest_elem_type = PointerElementType(dest_elem_ptr_type);
2438-
const auto ptr_elem_type = PointerElementType(ptr_type);
2439-
const auto ptr_elem_size = dl.getTypeAllocSize(ptr_elem_type);
2440-
const auto base_index = dest_elem_offset / ptr_elem_size;
2441-
2442-
indexes.push_back(llvm::ConstantInt::get(i32_type, base_index, false));
2443-
2444-
dest_elem_offset = dest_elem_offset % ptr_elem_size;
2423+
const auto i8_type = llvm::Type::getInt8Ty(context);
24452424

2446-
auto [reached_disp, indexed_type] =
2447-
BuildIndexes(dl, ptr_elem_type, 0, dest_elem_offset, indexes);
2448-
2449-
if (reached_disp) {
2425+
if (dest_elem_offset) {
2426+
indexes.push_back(
2427+
llvm::ConstantInt::get(i32_type, dest_elem_offset, false));
24502428
if (constant_ptr) {
2451-
if (base_index) {
2452-
constant_ptr = llvm::ConstantExpr::getGetElementPtr(
2453-
ptr_elem_type, constant_ptr, indexes);
2454-
} else {
2455-
constant_ptr = llvm::ConstantExpr::getGetElementPtr(
2456-
ptr_elem_type, constant_ptr, indexes, true,
2457-
(base_index * ptr_elem_size) + reached_disp);
2458-
}
2429+
constant_ptr =
2430+
llvm::ConstantExpr::getGetElementPtr(i8_type, constant_ptr, indexes);
24592431
ptr = constant_ptr;
24602432
} else {
2461-
if (base_index) {
2462-
ptr = ir.CreateGEP(ptr_elem_type, ptr, indexes);
2463-
} else {
2464-
ptr = ir.CreateInBoundsGEP(ptr_elem_type, ptr, indexes);
2465-
}
2466-
}
2467-
}
2468-
2469-
if (const auto diff = dest_elem_offset - reached_disp; diff) {
2470-
DCHECK_LE(diff, dest_elem_offset);
2471-
const auto i8_type = llvm::Type::getInt8Ty(context);
2472-
const auto i8_ptr_type =
2473-
llvm::PointerType::getInt8PtrTy(context, ptr_addr_space);
2474-
2475-
const auto dest_elem_size = dl.getTypeAllocSize(dest_elem_type);
2476-
if (diff % dest_elem_size) {
2477-
if (constant_ptr) {
2478-
constant_ptr =
2479-
llvm::ConstantExpr::getBitCast(constant_ptr, i8_ptr_type);
2480-
constant_ptr = llvm::ConstantExpr::getGetElementPtr(
2481-
i8_type, constant_ptr, llvm::ConstantInt::get(i32_type, diff));
2482-
return llvm::ConstantExpr::getBitCast(constant_ptr, dest_elem_ptr_type);
2483-
2484-
} else {
2485-
ptr = ir.CreateBitCast(ptr, i8_ptr_type);
2486-
ptr = ir.CreateGEP(i8_type, ptr,
2487-
llvm::ConstantInt::get(i32_type, diff, false));
2488-
return ir.CreateBitCast(ptr, dest_elem_ptr_type);
2489-
}
2490-
} else {
2491-
if (constant_ptr) {
2492-
constant_ptr =
2493-
llvm::ConstantExpr::getBitCast(constant_ptr, dest_elem_ptr_type);
2494-
return llvm::ConstantExpr::getGetElementPtr(
2495-
dest_elem_type, constant_ptr,
2496-
llvm::ConstantInt::get(i32_type, diff / dest_elem_size));
2497-
} else {
2498-
ptr = ir.CreateBitCast(ptr, dest_elem_ptr_type);
2499-
return ir.CreateGEP(
2500-
dest_elem_type, ptr,
2501-
llvm::ConstantInt::get(i32_type, diff / dest_elem_size, false));
2502-
}
2433+
ptr = ir.CreateGEP(i8_type, ptr, indexes);
25032434
}
25042435
}
25052436

0 commit comments

Comments
 (0)