TBAA fail on optimization, why?

hi,

i have a simple code like below, in wich variable "aaa" does not alias
to "bbb".
i use TBAA to specify this, please see the code.

then i ran this code thru LLVM optimization, and i expected that the
second "store" instruction is eliminated.
however, i am wrong: the second "store" instruction is still there
after optimization.

perhaps my TBAA setup is wrong somewhere? any hint, please?

thanks!
Jun

@aaa = external global i32
@bbb = external global i32

....
%0 = load i32* @aaa, !tbaa !1
%1 = load i32* @bbb, !tbaa !2
%2 = add i32 %0, %1
store i32 %2, i32* @aaa, !tbaa !2

; below line should be eliminated with optimization
store i32 %1, i32* @bbb, !tbaa !1 ; <==== WHY
optimization CANNOT ELIMINATE this insn?

...
!tbaa = !{!0, !1, !2}
!0 = metadata !{metadata !"tbaa_root"}
!1 = metadata !{metadata !"aaa", metadata !0}
!2 = metadata !{metadata !"bbb", metadata !0}

Hi Jun, did you tell "opt" to make use of TBAA? Also, please give complete IR
that people can use to reproduce, and instructions on how to reproduce (eg how
to run opt).

Ciao, Duncan.

%0 = load i32* @aaa, !tbaa !1
%1 = load i32* @bbb, !tbaa !2

store i32 %2, i32* @aaa, !tbaa !2
store i32 %1, i32* @bbb, !tbaa !1 ; <==== WHY

Why are you tagging each "store" with different classes against
corresponding "load(s)"?

store i32 %2, i32* @aaa, !tbaa !1
store i32 %1, i32* @bbb, !tbaa !2

I guess it would be more expected.

...Takumi

Hi Jun, did you tell "opt" to make use of TBAA? Also, please give complete
IR
that people can use to reproduce, and instructions on how to reproduce (eg
how
to run opt).

actually, i am still confused on which options should be given to
"opt" for it to use TBAA. any hint?

of course, we can simply use -O3, but i want to pinpoint exactly which
passes are necessary for TBAA optimization to work.

thanks,
Jun

Hi Jun,

Hi Jun, did you tell "opt" to make use of TBAA? Also, please give complete
IR
that people can use to reproduce, and instructions on how to reproduce (eg
how
to run opt).

actually, i am still confused on which options should be given to
"opt" for it to use TBAA. any hint?

-tbaa somewhere at the start.

of course, we can simply use -O3, but i want to pinpoint exactly which
passes are necessary for TBAA optimization to work.

The various alias analyses assume that you've run a certain minimum number of
optimizers on your bitcode, eg reg2mem.

Ciao, Duncan.

ok, below is my code. i run this through "opt" with option: "-basicaa
-mem2reg -tbaa"
however, the resulted bitcode file is still the same; no optimization is done.

meanwhile, the last "store" instruction is easily eliminated with "-O3" option.

so i am wondering which optimizations i should use to achieve the same
result. any hint?

thanks,
Jun

;;;;;;;;;;
@aaa = external global i32
@bbb = external global i32
@sss = external global i32

define void @testing() nounwind {
  %1 = load i32* @sss, align 4, !tbaa !2
  %2 = inttoptr i32 %1 to i32*
  %3 = load i32* @aaa, align 4, !tbaa !1
  store i32 %3, i32* %2, align 4, !tbaa !4
  store i32 %3, i32* @bbb, align 4, !tbaa !3
  store i32 %1, i32* @sss, align 4, !tbaa !2 ; <== O3 can REMOVE this
  ret void
}

!tbaa = !{!0, !1, !2, !3, !4}
!0 = metadata !{metadata !"tbaa_root"}
!1 = metadata !{metadata !"aaa", metadata !0, i32 0}
!2 = metadata !{metadata !"sss", metadata !0, i32 0}
!3 = metadata !{metadata !"bbb", metadata !0, i32 0}
!4 = metadata !{metadata !"mem", metadata !0, i32 0}

Hi Jun,

ok, below is my code. i run this through "opt" with option: "-basicaa
-mem2reg -tbaa"
however, the resulted bitcode file is still the same; no optimization is done.

that's because you didn't specify any optimization that uses alias analysis!
LLVM makes a distinction between analyses and transforms. Analyses, like tbaa,
are passes which look at your IR but don't modify it: instead they deduce
things about it (eg which memory accesses alias each other) and provide
that information to other passes. Transforms are passes that modify the IR,
i.e. these are the passes that do the actual optimizing. Consider the -dse
(dead store elimination pass). This is a transform pass, however it needs
to know which memory accesses alias each other. It doesn't compute this itself,
instead it requests this information from the alias analysis subsystem, which
queries any alias analysis passes that may have run. By specifying -tbaa you
have only done the analysis part, not the transform part.

meanwhile, the last "store" instruction is easily eliminated with "-O3" option.

Try -tbaa -basicaa mem2reg -dse

Ciao, Duncan.

Hi Jun,

ok, below is my code. i run this through "opt" with option: "-basicaa
-mem2reg -tbaa"
however, the resulted bitcode file is still the same; no optimization is
done.

that's because you didn't specify any optimization that uses alias analysis!
LLVM makes a distinction between analyses and transforms. Analyses, like
tbaa,
are passes which look at your IR but don't modify it: instead they deduce
things about it (eg which memory accesses alias each other) and provide
that information to other passes. Transforms are passes that modify the IR,
i.e. these are the passes that do the actual optimizing. Consider the -dse
(dead store elimination pass). This is a transform pass, however it needs
to know which memory accesses alias each other. It doesn't compute this
itself,
instead it requests this information from the alias analysis subsystem,
which
queries any alias analysis passes that may have run. By specifying -tbaa
you
have only done the analysis part, not the transform part.

thanks, now i got it.

meanwhile, the last "store" instruction is easily eliminated with "-O3"
option.

Try -tbaa -basicaa mem2reg -dse

this indeed works, thanks!!!

best,
Jun