Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -372,27 +372,29 @@ def CIR_ObjSizeOp : CIR_Op<"objsize", [Pure]> {
//===----------------------------------------------------------------------===//

def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {

let summary = "Pointer subtraction arithmetic";
let description = [{
`cir.ptr_diff` performs a subtraction between two pointer types with the
same element type and produces a `cir::IntType` result.
The cir.ptr_diff operation computes the difference between two pointers that
have the same element type

The result reflects the ABI-defined size of the pointed-to type. For example,
subtracting two !cir.ptr<!u64i> values may yield 1, representing an 8-byte
difference. In contrast, for pointers to void or function types, a result of
8 corresponds to an 8-byte difference.

Note that the result considers the pointer size according to the ABI for
the pointee sizes, e.g. the subtraction between two `!cir.ptr<!u64i>` might
yield 1, meaning 8 bytes, whereas for `void` or function type pointees,
yielding 8 means 8 bytes.
Example:

```mlir
%7 = "cir.ptr_diff"(%0, %1) : !cir.ptr<!u64i> -> !u64i
%7 = cir.ptr_diff %0, %1 : !cir.ptr<!u64i> -> !u64i
```
}];

let results = (outs CIR_AnyFundamentalIntType:$result);
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);
let results = (outs CIR_AnyFundamentalIntType:$result);

let assemblyFormat = [{
`(` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `->` qualified(type($result)) attr-dict
$lhs `,` $rhs `:` qualified(type($lhs)) `->` qualified(type($result))
attr-dict
}];
}

Expand Down
4 changes: 2 additions & 2 deletions clang/test/CIR/CodeGen/ptrdiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ size_type size(unsigned long *_start, unsigned long *_finish) {
// CHECK: cir.func dso_local @_Z4sizePmS_(%arg0: !cir.ptr<!u64i>
// CHECK: %3 = cir.load{{.*}} %1 : !cir.ptr<!cir.ptr<!u64i>>, !cir.ptr<!u64i>
// CHECK: %4 = cir.load{{.*}} %0 : !cir.ptr<!cir.ptr<!u64i>>, !cir.ptr<!u64i>
// CHECK: %5 = cir.ptr_diff(%3, %4) : !cir.ptr<!u64i> -> !s64i
// CHECK: %5 = cir.ptr_diff %3, %4 : !cir.ptr<!u64i> -> !s64i
// CHECK: %6 = cir.cast integral %5 : !s64i -> !u64i

long add(char *a, char *b) {
return a - b + 1;
}

// CHECK: cir.func dso_local @_Z3addPcS_(%arg0: !cir.ptr<!s8i>
// %5 = cir.ptr_diff(%3, %4) : !cir.ptr<!s8i> -> !s64i
// %5 = cir.ptr_diff %3, %4 : !cir.ptr<!s8i> -> !s64i
// %6 = cir.const #cir.int<1> : !s32i
// %7 = cir.cast integral %6 : !s32i -> !s64i
// %8 = cir.binop(add, %5, %7) : !s64i
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/ptrdiff.cir
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module {
cir.func @foo(%arg0: !cir.ptr<!s32i>, %arg1: !cir.ptr<!s32i>) -> !s32i {
%1 = cir.ptr_diff(%arg0, %arg1) : !cir.ptr<!s32i> -> !u64i
%1 = cir.ptr_diff %arg0, %arg1 : !cir.ptr<!s32i> -> !u64i
%2 = cir.cast integral %1 : !u64i -> !s32i
cir.return %2 : !s32i
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Transforms/lib-opt-find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ unsigned char* test2(unsigned char* first, unsigned char* last, unsigned char v)
// CHECK: %[[pattern:.*]] = cir.cast integral %[[load_pattern:.*]] : !u8i -> !s32i

// CHECK-NOT: {{.*}} cir.call @_ZSt4findIPhhET_S1_S1_RKT0_(
// CHECK: %[[array_size:.*]] = cir.ptr_diff(%[[last]], %[[first]]) : !cir.ptr<!u8i> -> !u64i
// CHECK: %[[array_size:.*]] = cir.ptr_diff %[[last]], %[[first]] : !cir.ptr<!u8i> -> !u64i

// CHECK: %[[result_cast:.*]] = cir.libc.memchr(%[[cast_to_void]], %[[pattern]], %[[array_size]])
// CHECK: %[[memchr_res:.*]] = cir.cast bitcast %[[result_cast]] : !cir.ptr<!void> -> !cir.ptr<!u8i>
Expand Down
Loading