[cfe-dev] Evaluate constant condition of if statement has no effect

John McCall via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 22 10:30:45 PDT 2017


> On Aug 21, 2017, at 12:28 AM, Frozen via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> Simple Example:
> 
> int main()
> {
>   int x;
>   if (x || 1) {}
> }
> 
> Clang can not evaluate this condition and will emit IR like this:
> 
> define signext i32 @main() #0 {
> entry:
>   %retval = alloca i32, align 4
>   %x = alloca i32, align 4
>   store i32 0, i32* %retval, align 4
>   %0 = load i32, i32* %x, align 4
>   %tobool = icmp ne i32 %0, 0
>   br i1 %tobool, label %if.then, label %lor.lhs.false
> 
> lor.lhs.false:                                    ; preds = %entry
>   br i1 true, label %if.then, label %if.end
> 
> if.then:                                          ; preds = %lor.lhs.false, %entry
>   br label %if.end
> 
> if.end:                                           ; preds = %if.then, %lor.lhs.false
>   %1 = load i32, i32* %retval, align 4
>   ret i32 %1
> }
> 
> However, when we swap the position of LHS and RHS(i.e. if (1 || x), Clang can recognize it:
> 
> define signext i32 @main() #0 {
> entry:
>   %x = alloca i32, align 4
>   ret i32 0
> }
> 
> I also find the root issue and propose one potential solution in comment 2 of this link: https://bugs.llvm.org/show_bug.cgi?id=34229 <https://bugs.llvm.org/show_bug.cgi?id=34229>
> 
> Any idea?

There will always be some example of something that we generate less efficiently at -O0 than we could.  Why is this example specifically worth optimizing?

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170822/06792d54/attachment.html>


More information about the cfe-dev mailing list