<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p>Hello,</p>
<p><br>
</p>
<p>I am working on a pass that tries to extract type information from, say, all malloc statements in LLVM-IR (source language is C).<br>
</p>
<p>For debug code, this can be achieved by looking up the respective bitcast instruction and extracting the type from it.</p>
<p><br>
</p>
<p>However, in optimized code, the LLVM-IR omits these direct bitcasts in different scenarios (see example after the question).</p>
<p><br>
</p>
<p><span>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?</span><br>
</p>
<p><br>
</p>
<p><br>
</p>
<p>For one instance, consider the following C code:</p>
<p></p>
<div>  typedef struct {<br>
    int nvars;<br>
    int* vars;<br>
  } struct_grid;<br>
<br>
  void set(struct_grid* pgrid, int nvars, int* vars_n) {<br>
    int* new_vars;<br>
    new_vars = (int*)malloc(nvars * sizeof(int));<br>
    for (int i = 0; i < nvars; i++) {<br>
      new_vars[i] = vars_n[i];<br>
    }<br>
    pgrid->vars = new_vars;<br>
  }<br>
<br>
</div>
<p></p>
<p>Compiled with -g, we get the expected bitcast. With optimizations, we get:<br>
</p>
<p><span>  %6 = tail call i8* @malloc(i64 %5) ; the malloc, no subsequent bitcast</span></p>
<p>  ...</p>
<p><span>  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %6, i8* %10, i64 %12, i32 4, i1 false)</span></p>
<p><br>
</p>
<p>Thus, the %6 is never casted, as it is directly put into the memcpy operation.
<br>
</p>
<p><br>
</p>
<p>Only later, through some indirection when new_vars is assigned to pgrid->vars can we get the real type:</p>
<p></p>
<div>  %14 = getelementptr inbounds %struct.struct_grid, %struct.struct_grid* %0, i64 0, i32 1, !dbg !38<br>
  %15 = bitcast i32** %14 to i8**, !dbg !39<br>
  store i8* %6, i8** %15, align 8, !dbg !39, !tbaa !40</div>
<div>  ret void</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<p></p>
<p><br>
</p>
<p>Thanks in advance.<br>
</p>
</div>
</body>
</html>