[llvm-dev] Possible to query type information from a malloc in optimized codes

Hück, Alexander via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 19 08:55:23 PDT 2018


Hello,


I am working on a pass that tries to extract type information from, say, all malloc statements in LLVM-IR (source language is C).

For debug code, this can be achieved by looking up the respective bitcast instruction and extracting the type from it.


However, in optimized code, the LLVM-IR omits these direct bitcasts in different scenarios (see example after the question).


My question now, is there any way to use, e.g., debug data or some use-def search to reliably extract the correct type information for such a malloc?



For one instance, consider the following C code:

  typedef struct {
    int nvars;
    int* vars;
  } struct_grid;

  void set(struct_grid* pgrid, int nvars, int* vars_n) {
    int* new_vars;
    new_vars = (int*)malloc(nvars * sizeof(int));
    for (int i = 0; i < nvars; i++) {
      new_vars[i] = vars_n[i];
    }
    pgrid->vars = new_vars;
  }


Compiled with -g, we get the expected bitcast. With optimizations, we get:

  %6 = tail call i8* @malloc(i64 %5) ; the malloc, no subsequent bitcast

  ...

  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %6, i8* %10, i64 %12, i32 4, i1 false)


Thus, the %6 is never casted, as it is directly put into the memcpy operation.


Only later, through some indirection when new_vars is assigned to pgrid->vars can we get the real type:

  %14 = getelementptr inbounds %struct.struct_grid, %struct.struct_grid* %0, i64 0, i32 1, !dbg !38
  %15 = bitcast i32** %14 to i8**, !dbg !39
  store i8* %6, i8** %15, align 8, !dbg !39, !tbaa !40
  ret void





Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180719/6bf6fedb/attachment.html>


More information about the llvm-dev mailing list