mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
02cc6923d0
The `EmitFieldAccesses` pass transforms `clift` by taking pointer-typed expressions computed via integerr arithmetic with type-safe field accesses and array accesses. The transformation is split in three main phases: 1) `PointerArithmetic` computation. 2) `BestTraversal` computation. 3) `FieldAccess` `clift` rewrite. The high level driver is implemented in the `EmitFieldAccesses` header and cpp, while the nested 3 phases are implemented respectively in `PointerArithmetic`, `BestTraversal` and `FieldAccessReplacement`. The `computerPointerArithmetic` phase is concerned with taking a pointer-typed `ExpressionOp`, called `PointerToReplace`, and expressing it in a `BasePointer+Offset` form. The `computeBestTraversal` phase is concerned with computing the best traversal of the type pointed to by `BasePointer`, that can be used to rewrite the pointer arithmetic in `clift` with just field accesses and array subscripts. The `replaceFieldAccess` phase takes the `Traversal` computed at the previous step, and actually rewrites in `clift` the `PointerToReplace` in terms of field accesses and array accesses w.r.t. the `BasePointer`.
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
#pragma once
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include <optional>
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "mlir/IR/Value.h"
|
|
|
|
#include "revng/Clift/Clift.h"
|
|
#include "revng/Support/Debug.h"
|
|
|
|
/// Represents a pointer-typed expression decomposed into a `BasePointer` and an
|
|
/// `Offset` expression. The offset is a constant `BaseOffset`, plus a linear
|
|
/// combination of strided terms, which capture array index patterns
|
|
struct PointerArithmetic {
|
|
|
|
/// Represents an `Index` with both a variable and a constant component,
|
|
/// e.g., `array[i+4]`
|
|
struct Index {
|
|
mlir::Value Variable;
|
|
llvm::APInt Constant;
|
|
};
|
|
|
|
/// Represents a strided term in the `PointerArithmetic`
|
|
struct StridedTerm {
|
|
llvm::APInt Stride;
|
|
Index Idx;
|
|
|
|
StridedTerm(llvm::APInt Stride, Index Idx) :
|
|
Stride(std::move(Stride)), Idx(std::move(Idx)) {}
|
|
|
|
void dump() const debug_function;
|
|
};
|
|
|
|
/// Represents the offset with possible strided terms in the
|
|
/// `PointerArithmetic`
|
|
struct OffsetExpression {
|
|
|
|
/// Represents the constant part of the `Offset`
|
|
llvm::APInt BaseOffset;
|
|
|
|
/// Holds the terms of the linear combination components of the `Offset`
|
|
llvm::SmallVector<StridedTerm> LinearCombination;
|
|
|
|
OffsetExpression(unsigned BitWidth);
|
|
OffsetExpression(llvm::APInt Offset);
|
|
|
|
void dump() const debug_function;
|
|
};
|
|
|
|
/// The base pointer the `PointerArithmetic` is expressed relative to
|
|
/// `BasePointer` object, which is where the root of the computation lies
|
|
mlir::Value BasePointer;
|
|
|
|
/// The offset w.r.t. the `BasePointer`
|
|
OffsetExpression Offset;
|
|
|
|
/// We define a `PointerArithmetic` with an empty `BasePointer` a _numeric_
|
|
bool isNumeric() const;
|
|
|
|
/// Check if this is an address (has base pointer) arithmetic
|
|
bool isAddress() const;
|
|
|
|
/// Verify the invariants for the strides contained in the PointerArithmetic
|
|
bool verify() const;
|
|
|
|
/// Dump method
|
|
void dump() const debug_function;
|
|
};
|
|
|
|
/// `PointerArithmetic` computation function entrypoint
|
|
std::optional<PointerArithmetic>
|
|
computePointerArithmetic(mlir::clift::ExpressionOpInterface PointerToReplace);
|