Commit Graph

47 Commits

Author SHA1 Message Date
Alessandro Di Federico 1ca8239b65 ImplicitCastElision: elide casts only on integrals
For comparison operators (==, !=, <, >, <=, >=) the elision is only
sound when both operands are of integral type; otherwise we'd be
changing the kind of comparison being performed.
2026-06-11 17:39:52 +02:00
Lauri Vasama e30a09644e Introduce hoist-variable-initializers pass 2026-05-25 09:00:41 +02:00
Lauri Vasama d3dcc70c0c Implement cast elision for variable initializers 2026-05-25 09:00:41 +02:00
Lauri Vasama 23e06b5212 Erase requires in tighten-variable-scopes 2026-05-25 09:00:41 +02:00
Roberto Bertolini 5dcd6334b3 Add Clift local variable scope tightening 2026-05-13 15:32:34 +02:00
Pietro Fezzardi 197bdd346d FieldAccessReplacement: fix type of array indices
When emitting the synthesized integer index for an access whose
LinearCombination contains a variable, use the variable's own type
for the constant `[0]` immediate (and for the rest of the index
arithmetic) instead of always using a generic pointer-sized integer.
With model-sized pointers in the picture the two integer widths can
differ, and using the wrong one trips Clift's type checking
downstream.
2026-05-08 11:37:09 +02:00
Lauri Vasama c7e3a12657 Add implicit cast elision pass 2026-05-04 10:49:07 +03:00
Lauri Vasama ed20fd5cd9 Collapse casts after legalization 2026-05-04 10:49:06 +03:00
Lauri Vasama ad0c466d6d Remove revng/Support/CTarget.h 2026-05-04 10:49:06 +03:00
Lauri Vasama 04fe27b8e9 Migrate Clift users to CDataModel 2026-05-04 10:49:03 +03:00
Lauri Vasama 7f2bf61d10 Move cast collapsing rewrite to a separate set 2026-04-30 15:09:16 +03:00
Lauri Vasama c151a2c7fd Improve cast collapsing expression rewrite 2026-04-30 15:09:16 +03:00
Lauri Vasama e5fb446430 Rename BooleanNegations.h to Expressions.h 2026-04-30 15:09:16 +03:00
Lauri Vasama a338194eb7 Move namespace mlir::clift out of namespace mlir 2026-04-27 12:48:24 +03:00
Andrea Gussoni 8a87c034a8 EmitFieldAccesses: re-run on indirection
Re-run the emit-field-accesses if any replacement propagates a type
through an IndirectionOp. This is needed to discover new rewriting
opportunities enabled by the newly typed pointers.
2026-04-14 09:50:48 +02:00
Andrea Gussoni 0e63c46930 EmitFieldAccesses: propagate through indirection
After a replacement, check if the replaced value feeds into an
IndirectionOp. If the "rich" pre-bitcast value has a ptr<ptr<T>>
type, create a new typed IndirectionOp and insert a fixup bitcast
for existing non-lvalue uses. Lvalue uses (assign LHS) stay on the
old indirection to preserve type and lvalue constraints.

Returns true if propagation occurred, signaling the EFA driver to
rerun and discover new rewriting opportunities.
2026-04-14 09:50:48 +02:00
Andrea Gussoni e0a9f4f484 EmitFieldAccesses: ignore qualifiers on Types 2026-04-14 09:24:48 +02:00
Lauri Vasama 8085a1e8e9 Improve integer literal emission 2026-04-08 12:31:45 +03:00
Lauri Vasama d35e54627c Add Clift ValueType interface
Value types is the set of non-array object types. These types are
assignable, can be used as function parameters, and as return types
(along with void), and in other scenarios where the type must have usual
value-like behaviour with no weirdness (such as array decay).
2026-04-08 12:28:45 +03:00
Lauri Vasama 80731d1a10 Migrate users to new Clift type casts 2026-04-08 12:27:32 +03:00
Lauri Vasama 29c935ac90 Replace Clift type category helpers with casts 2026-04-08 12:25:04 +03:00
Lauri Vasama 9ba90e9b81 Replace ValueType with mlir::Type where applicable
`mlir::Type` is now used as the default C++ type for representing all
MLIR types, even when `ValueType` would be a valid constraint, unless
doing so provides a clear benefit in that specific context.
2026-04-08 12:20:00 +03:00
Lauri Vasama f9a4cb6384 Split up clift::dealias
* `unwrapTypedefs` matches `dealias(T, /*IgnoreQualifiers=*/true)`.
* `collapseTypedefs` matches `dealias(T)`.
2026-04-08 12:18:42 +03:00
Lauri Vasama be8351a17d Split up Clift PrimitiveType
* PrimitiveType is now an interface.
* VoidKind is now VoidType.
* FloatKind is now FloatType.
* The integer kinds are now IntegerType with IntegerKind.
2026-04-08 12:18:10 +03:00
Lauri Vasama a2be1b476a Split up Clift CastOp
* DecayOp - array or function to pointer decay. Not shown in C.
* BitCastOp - bit-preserving conversion. Emitted as bit_cast if
  necessary.
* ExtendOp - zero- or sign-extending conversion from any integer type to
  any wider integer type.
* TruncateOp - truncating conversion from any integer type to any
  narrower integer type.
* PtrResizeOp - resizes a pointer without changing its pointee type.
2026-04-08 12:11:54 +03:00
Lauri Vasama 89fd0ee909 Use getObjectSize in EmitFieldAccesses 2026-04-08 11:13:44 +03:00
Andrea Gussoni fab35aaa24 EmitFieldAccesses: unwrap typedef in subscript
Use a `bitcast` to unwrap a `typedef` passed as `Pointer` operand to a
`subscript`, which requires it to be a raw `PointerType`.
2026-03-30 16:03:37 +02:00
Andrea Gussoni 82945de055 EmitFieldAccesses: support pointer as array
Introduce the support to decouple the `BaseType` from `BasePointer`
pointee type.

This enable us to wrap pointee typees into an implicit array. This
enables pointer arithmetic rewriting of `*(p + i)` as `p[i]`.
2026-03-30 16:03:37 +02:00
Andrea Gussoni 5d023b32a4 EmitFieldAccesses: no assumption on composePtrAdd
We now don't assume that the `PointerOperand` of a `ptr_add` leads to a
`Address` PA, nor that the `OffsetOperand` leads to a `Numeric` PA.

We now behave as a simple `composeAdd`, keeping the special `Offset`
scaling by the `PointeeSize` of the `PointerOperand`.
2026-03-19 17:31:42 +01:00
Andrea Gussoni 6eaa1c39ad EmitFieldAccesses: no leaf on intermediate bitcast
Remove the special casing for `composeBitcast`, where we created a
`leaf` PA in case the traversal of the upward tree did only provide a
`Numeric` PA.

This was introduced as a workaround for handling `composePtrAdd` when
the `PointerOperand` was a `Numeric` PA.
2026-03-19 16:49:37 +01:00
Andrea Gussoni 02cc6923d0 EmitFieldAccesses: implement pass
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`.
2026-03-18 17:25:13 +01:00
Andrea Gussoni 4952346620 Clift: reject TypePunned for ArrayType
Reject the `assignTypePunnedConstraint` DDR `Expressions.td` rewrite
when the `DstType` is an `ArrayType`, to avoid an invalid rewrite that
would violate the assumption that `bitcast`s do not admit a `ArrayType`
as `result` type.
2026-03-18 17:25:13 +01:00
Lauri Vasama 21a51af54f Add immediate radix deduction pass 2025-12-05 19:53:41 +02:00
Lauri Vasama 33450e9306 Add terminal-branch-complement-hoisting pass 2025-12-05 19:53:41 +02:00
Lauri Vasama 73d7a77442 Add expression rewrite pass 2025-12-05 19:53:41 +02:00
Lauri Vasama 0f04483517 Add statement rewrite pass 2025-12-05 19:53:41 +02:00
Lauri Vasama 448276915e Add label-merging pass 2025-12-05 19:53:41 +02:00
Lauri Vasama f2911da190 Add trivial-return-elimination pass 2025-12-05 19:53:41 +02:00
Lauri Vasama 58216deafd Add RewriteHelpers.h
Contains helpers for statement rewriting.
2025-12-05 19:53:41 +02:00
Lauri Vasama c835baf65a Add PatternRewriter.h
Contains APIs from upstream LLVM that are not yet present in the current
version, as well as helpers not present in upstream.

See upstream commit 42c31d8302ff8f716601df9c276a5cd9ace6b158.
2025-12-05 19:53:41 +02:00
Lauri Vasama ba945442d6 Add BlockStatementOp 2025-12-05 15:01:14 +02:00
Lauri Vasama 332fe51aa0 Fix pointer arithmetic legalization
Resizes the integer types involved in pointer arithmetic along with the
pointer types.
2025-12-05 15:01:14 +02:00
Lauri Vasama b43df10c4b Improve Clift legalization
Casts are no longer emitted for immediates in boolean-tested contexts.
2025-12-05 15:01:14 +02:00
Lauri Vasama fbc1563b43 Add loop detection pass 2025-11-12 15:28:54 +02:00
Lauri Vasama b24f61db40 Improve legalization
Legalization now emits fewer casts depending on context.
2025-11-12 15:28:54 +02:00
Lauri Vasama 7980e57fb5 Add Clift C legalization pass 2025-10-21 09:15:40 +02:00
Lauri Vasama 22d6f70ee2 Clift directory structure overhaul 2025-10-13 15:08:20 +02:00