[llvm] [llubi] Add support for GEP/alloca/inttoptr (PR #181861)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 01:11:04 PST 2026


================
@@ -0,0 +1,130 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: llubi --verbose < %s 2>&1 | FileCheck %s
+
+target datalayout = "e-p:64:64:64:32"
+
+%struct = type { i64, [2 x i32], i64 }
+
+define void @main() {
+  %alloc = alloca i32
+  %alloc_struct = alloca %struct
+
+  %gep = getelementptr i8, ptr %alloc, i64 3
+  %gep_poison_base = getelementptr i8, ptr poison, i64 1
+  %gep_poison_idx = getelementptr i8, ptr %alloc, i64 poison
+  ; The indices are first converted to offsets in the pointer’s index type.
+  ; The index itself is sign-extended or truncated, and then multiplied by the type allocation size
+  %gep_trunc_idx = getelementptr i32, ptr %alloc, i128 18446744073709551616
+  %gep_sext_idx = getelementptr i32, ptr %alloc, i8 -1
+  ; The offsets are then added to the low bits of the base address up to the index type width,
+  ; with silently-wrapping two’s complement arithmetic.
+  %large_address = inttoptr i64 -1 to ptr
+  %gep_update_idx_bits = getelementptr i32, ptr %large_address, i64 1
+  %gep_struct = getelementptr %struct, ptr %alloc_struct, i64 0, i32 1, i32 1
+  %gep_scalable_vec = getelementptr <vscale x 4 x i32>, ptr null, i64 4
+
+  %gep_vec_idx = getelementptr [2 x i32], ptr %alloc, i64 1, <2 x i64> <i64 0, i64 2>
+  %ptr_vec_insert = insertelement <2 x ptr> poison, ptr %alloc, i32 0
+  %ptr_vec_splat = shufflevector <2 x ptr> %ptr_vec_insert, <2 x ptr> poison, <2 x i32> zeroinitializer
+  %gep_vec_ptr = getelementptr i32, <2 x ptr> %ptr_vec_splat, i64 1
+  %gep_vec_ptr_vec_idx = getelementptr i32, <2 x ptr> %ptr_vec_splat, <2 x i64> <i64 0, i64 2>
+
+  ; Note that getelementptr with all-zero indices is always considered to be inbounds, even if the base
+  ; pointer does not point to an allocated object. As a corollary, the only pointer in bounds of the null
+  ; pointer in the default address space is the null pointer itself.
+  %gep_inbounds_valid_null = getelementptr inbounds i8, ptr null, i64 0
+  %gep_inbounds_invalid_null = getelementptr inbounds i8, ptr null, i64 1
+  ; The base pointer has an in bounds address of the allocated object that it is based on.
+  ; This means that it points into that allocated object, or to its end.
+  %gep_inbounds_valid1 = getelementptr inbounds i8, ptr %alloc, i64 3
+  %gep_inbounds_valid2 = getelementptr inbounds i8, ptr %alloc, i64 4
+  %gep_inbounds_invalid1 = getelementptr inbounds i8, ptr %alloc, i64 -1
+  %gep_inbounds_invalid2 = getelementptr inbounds i8, ptr %alloc, i64 5
+  ; During the successive addition of offsets to the address, the resulting pointer
+  ; must remain in bounds of the allocated object at each step.
+  %gep_inbounds_invalid3 = getelementptr inbounds %struct, ptr %alloc_struct, i64 1, i32 1, i32 -2
----------------
antoniofrighetto wrote:

Maybe nit: should we also test `inbounds` on a deallocated object?

https://github.com/llvm/llvm-project/pull/181861


More information about the llvm-commits mailing list