[llvm] r283165 - Make GlobalsAA ignore dead constant expressions.

Oliver Stannard via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 21 07:27:06 PDT 2016


Hi Eli,

I've found a codegen regression (using csmith), and bisected it back to this commit. Here's the minimal reproducer for it:

  @c = internal global i32 1, align 4
  @d = hidden global i32* @c, align 4
  @f = hidden global i32* @c, align 4
  @e = hidden global i32 0, align 4
  @.str = private unnamed_addr constant [15 x i8] c"checksum = %X\0A\00", align 1

  define void @main() {
  entry:
    %0 = load i32, i32* @c, align 4
    store i32 %0, i32* @e, align 4
    %1 = load i32*, i32** @d, align 4
    store i32 2, i32* %1, align 4
    %2 = load i32, i32* @c, align 4
    %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0), i32 %2) #2
    ret void
  }

  declare i32 @printf(i8* nocapture readonly, ...)

This gets miscompiled when globals-aa is used in combination with instcombine:

  $ /work/llvm/build/bin/opt -globals-aa -instcombine < test.ll -S 
  ; ModuleID = '<stdin>'
  source_filename = "<stdin>"

  @c = internal global i32 1, align 4
  @d = hidden global i32* @c, align 8
  @f = hidden global i32* @c, align 4
  @e = hidden global i32 0, align 4
  @.str = private unnamed_addr constant [15 x i8] c"checksum = %X\0A\00", align 1

  define void @main() {
  entry:
    %0 = load i32, i32* @c, align 4
    store i32 %0, i32* @e, align 4
    %1 = load i32*, i32** @d, align 8
    store i32 2, i32* %1, align 4
    %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
    ret void
  }

  declare i32 @printf(i8* nocapture readonly, ...)

The second parameter to the printf call has been replaced with %0, which is the value loaded from @c before @c was modified by the store through pointer @d/%1.

Sorry it's taken me so long to report this, it had been masked by some other issues until recently.

Regards,
Oliver

> -----Original Message-----
> From: llvm-commits [mailto:llvm-commits-bounces at lists.llvm.org] On Behalf
> Of Eli Friedman via llvm-commits
> Sent: 04 October 2016 01:04
> To: llvm-commits at lists.llvm.org
> Subject: [llvm] r283165 - Make GlobalsAA ignore dead constant expressions.
> 
> Author: efriedma
> Date: Mon Oct  3 19:03:55 2016
> New Revision: 283165
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=283165&view=rev
> Log:
> Make GlobalsAA ignore dead constant expressions.
> 
> Slightly improves the precision of GlobalsAA in certain situations, and
> makes the behavior of optimization passes more predictable.
> 
> Differential Revision: https://reviews.llvm.org/D24104
> 
> 
> Added:
>     llvm/trunk/test/Analysis/GlobalsModRef/dead-uses.ll
> Modified:
>     llvm/trunk/lib/Analysis/GlobalsModRef.cpp
> 
> Modified: llvm/trunk/lib/Analysis/GlobalsModRef.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/llvm/trunk/lib/Analysis/GlobalsModRef.cpp?rev=283165&r1=283164&r2=
> 283165&view=diff
> ==========================================================================
> ====
> --- llvm/trunk/lib/Analysis/GlobalsModRef.cpp (original)
> +++ llvm/trunk/lib/Analysis/GlobalsModRef.cpp Mon Oct  3 19:03:55 2016
> @@ -366,6 +366,8 @@ bool GlobalsAAResult::AnalyzeUsesOfPoint
>      } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
>        if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
>          return true; // Allow comparison against null.
> +    } else if (Constant *C = dyn_cast<Constant>(I)) {
> +      return C->isConstantUsed();
>      } else {
>        return true;
>      }
> 
> Added: llvm/trunk/test/Analysis/GlobalsModRef/dead-uses.ll
> URL: http://llvm.org/viewvc/llvm-
> project/llvm/trunk/test/Analysis/GlobalsModRef/dead-
> uses.ll?rev=283165&view=auto
> ==========================================================================
> ====
> --- llvm/trunk/test/Analysis/GlobalsModRef/dead-uses.ll (added)
> +++ llvm/trunk/test/Analysis/GlobalsModRef/dead-uses.ll Mon Oct  3
> 19:03:55 2016
> @@ -0,0 +1,54 @@
> +; RUN: opt < %s -instcombine -globals-aa -licm -S | FileCheck %s
> +
> +; Make sure -globals-aa ignores dead uses of globals.
> +
> + at a = internal global i32 0, align 4
> + at c = common global i32 0, align 4
> +
> +; Function Attrs: nounwind
> +define i32 @g() {
> +; Make sure the load of @a is hoisted.
> +; CHECK-LABEL: define i32 @g()
> +; CHECK: entry:
> +; CHECK-NEXT: load i32, i32* @a, align 4
> +; CHECK-NEXT: br label %for.cond
> +entry:
> +  br label %for.cond
> +
> +for.cond:                                         ; preds = %for.inc,
> %entry
> +  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
> +  %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ]
> +  %cmp = icmp slt i32 %i.0, 1000
> +  br i1 %cmp, label %for.body, label %for.end
> +
> +for.body:                                         ; preds = %for.cond
> +  %0 = load i32, i32* @a, align 4
> +  %add = add nsw i32 %sum.0, %0
> +  call void @f()
> +  br label %for.inc
> +
> +for.inc:                                          ; preds = %for.body
> +  %inc = add nsw i32 %i.0, 1
> +  br label %for.cond
> +
> +for.end:                                          ; preds = %for.cond
> +  ret i32 %sum.0
> +}
> +
> +; Function Attrs: nounwind
> +define internal void @f() {
> +entry:
> +  %tobool = icmp ne i32 0, 0
> +  br i1 %tobool, label %if.then, label %if.end
> +
> +if.then:                                          ; preds = %entry
> +  store i32 ptrtoint (i32* @a to i32), i32* @c, align 4
> +  br label %if.end
> +
> +if.end:                                           ; preds = %if.then,
> %entry
> +  %0 = load i32, i32* @c, align 4
> +  %inc = add nsw i32 %0, 1
> +  store i32 %inc, i32* @c, align 4
> +  ret void
> +}
> +
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.ll
Type: application/octet-stream
Size: 605 bytes
Desc: test.ll
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161021/88d319d2/attachment.obj>


More information about the llvm-commits mailing list