constant-fold-alias.ll 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. ; RUN: opt -S < %s -instcombine | FileCheck %s
  2. target datalayout = "e-p1:16:16-p2:32:32-p3:64:64"
  3. @G1 = global i32 42, align 1
  4. @G2 = global i32 42
  5. @G3 = global [4 x i8] zeroinitializer, align 1
  6. @A1 = alias bitcast (i8* getelementptr inbounds ([4 x i8], [4 x i8]* @G3, i32 0, i32 2) to i32*)
  7. @A2 = alias inttoptr (i64 and (i64 ptrtoint (i8* getelementptr inbounds ([4 x i8], [4 x i8]* @G3, i32 0, i32 3) to i64), i64 -4) to i32*)
  8. define i64 @f1() {
  9. ; This cannot be constant folded because G1 is underaligned.
  10. ; CHECK-LABEL: @f1(
  11. ; CHECK: ret i64 and
  12. ret i64 and (i64 ptrtoint (i32* @G1 to i64), i64 1)
  13. }
  14. define i64 @f2() {
  15. ; The preferred alignment for G2 allows this one to foled to zero.
  16. ; CHECK-LABEL: @f2(
  17. ; CHECK: ret i64 0
  18. ret i64 and (i64 ptrtoint (i32* @G2 to i64), i64 1)
  19. }
  20. define i64 @g1() {
  21. ; This cannot be constant folded because A1 aliases G3 which is underalaigned.
  22. ; CHECK-LABEL: @g1(
  23. ; CHECK: ret i64 and
  24. ret i64 and (i64 ptrtoint (i32* @A1 to i64), i64 1)
  25. }
  26. define i64 @g2() {
  27. ; While A2 also aliases G3 which is underaligned, the math of A2 forces a
  28. ; certain alignment allowing this to fold to zero.
  29. ; CHECK-LABEL: @g2(
  30. ; CHECK: ret i64 0
  31. ret i64 and (i64 ptrtoint (i32* @A2 to i64), i64 1)
  32. }