<div dir="ltr">Ahh, that makes much more sense then; thanks for the detailed illustration. :)<div><br></div><div>Though, in your example:</div><div><br></div><div><span style="font-size:12.8px">*** IR Dump After Partially inline calls to library functions ***</span><br style="font-size:12.8px"><span style="font-size:12.8px">; Function Attrs: nounwind readnone uwtable</span><br style="font-size:12.8px"><span style="font-size:12.8px">define i64 @foo(i32 %flag) #0 {</span><br style="font-size:12.8px"><span style="font-size:12.8px">entry:</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %chararray = alloca [30 x i8], align 16</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %chararray2 = alloca [10 x i8], align 1</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %0 = getelementptr inbounds [30 x i8], [30 x i8]* %chararray, i64 0, i64 0</span><br style="font-size:12.8px"><span style="font-size:12.8px">  call void @llvm.lifetime.start(i64 30, i8* %0) #5</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %1 = getelementptr inbounds [10 x i8], [10 x i8]* %chararray2, i64 0, i64 0</span><br style="font-size:12.8px"><span style="font-size:12.8px">  call void @llvm.lifetime.start(i64 10, i8* %1) #5</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %tobool = icmp eq i32 %flag, 0</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %cptr.0 = select i1 %tobool, i8* %0, i8* %1</span><br style="font-size:12.8px"><span style="font-size:12.8px">  %2 = call i64 @llvm.objectsize.i64.p0i8.i32(</span><span style="font-size:12.8px">i8* %cptr.0, i1 true, i32 10)</span><br style="font-size:12.8px"><span style="font-size:12.8px">  call void @llvm.lifetime.end(i64 10, i8* %1) #5</span><br style="font-size:12.8px"><span style="font-size:12.8px">  call void @llvm.lifetime.end(i64 30, i8* %0) #5</span><br style="font-size:12.8px"><span style="font-size:12.8px">  ret i64 %2</span><br style="font-size:12.8px"><span style="font-size:12.8px">}</span><br></div><div><br></div><div>Why is the third argument to @llvm.objectsize necessary? It seems that we have enough information to just compute "10" during CGP. Are there cases where we wouldn't be able to do this?</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 17, 2016 at 10:01 AM, Duncan P. N. Exon Smith via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Okay, good.<br>
<br>
FTR, 0 *is* a correct value.  You want to fix it to get a more accurate<br>
number, but 0 is always correct for type 2.<br>
<div><div><br>
> On 2016-Mar-17, at 04:10, Strahinja Petrovic <<a href="mailto:strahinja.petrovic@rt-rk.com" target="_blank">strahinja.petrovic@rt-rk.com</a>> wrote:<br>
><br>
> I made a mistake here, I get zero same as you. I want to fix it to get correct value.<br>
><br>
> On 16.03.2016. 19:28, Duncan P. N. Exon Smith wrote:<br>
>>> On 2016-Mar-16, at 09:39, Strahinja Petrovic via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>><br>
>>> Optimizer doesn't know how to calculate the object size when it finds condition that cannot be eliminated. There is example:<br>
>>><br>
>>> -----------------------------------------------<br>
>>> #include<stdlib.h><br>
>>> #define STATIC_BUF_SIZE 10<br>
>>> #define LARGER_BUF_SIZE 30<br>
>>><br>
>>> size_t foo(int flag) {<br>
>>>  char *cptr;<br>
>>>  char chararray[LARGER_BUF_SIZE];<br>
>>>  char chararray2[STATIC_BUF_SIZE];<br>
>>>  if(flag)<br>
>>>    cptr = chararray2;<br>
>>>   else<br>
>>>    cptr = chararray;<br>
>>><br>
>>>  return  __builtin_object_size(cptr, 2);<br>
>>> }<br>
>>><br>
>>> int main() {<br>
>>>  size_t ret;<br>
>>>  ret = foo(0);<br>
>>>  printf("\n%d\n", ret);<br>
>>>  return 0;<br>
>>> }<br>
>>> ----------------------------------------------<br>
>>> If you try to compile this example with clang (trunk version) with option -fno-inline the result will be -1.<br>
>> I get `0` when I run this, which is correct because you're asking<br>
>> for type "2".  `-1` would be a fairly major bug.  Was this a typo<br>
>> on your part, or can you somehow reproduce `-1`?<br>
>><br>
>>> Without option -fno-inline result will be correct (30). When foo function is inlined into main, condition is eliminated and compiler knows to calculate correct object size. Compiler should be able to calculate object size in both cases (with/without inlining foo function). In case when condition can't be eliminated compiler should calculate object size depending on second argument of __builtin_object_size function (taking minimum or maximum value from condition). In this example, the result should be 10 with -fno-inline.<br>
>>><br>
>>> If I replace the llvm.objectsize with the constant in foo() depending on the second argument, the result will be correct with -fno-inline (10), but incorrect without the flag. This is because foo() is inlined, the condition can be eliminated, and the result should be 30.<br>
>>><br>
>>> I resolved this problem by adding third argument in llvm.objectsize intrinsic. When I calculate the result based on condition, I put it in the third argument. If there is no inlining, condition will not get eliminated, and this will be the final result. When there is inlining, condition will be  eliminated after inlining and the llvm.objectsize will be replaced with a constant.<br>
>>> With this approach, I get the correct result in both cases (with and without -fno-inline).<br>
>>><br>
>>> I would like to have a discussion about this approach because I am changing the IR (because of adding third argument to __builtin_object_size function). Do you have some comments ?<br>
>>><br>
>>> _______________________________________________<br>
>>> LLVM Developers mailing list<br>
>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
><br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div></div>