thread-cmp.ll 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ; RUN: opt -S -jump-threading %s | FileCheck %s
  2. ; When simplify a branch based on LVI predicates, we should replace the
  3. ; comparison itself with a constant (when possible) in case it's otherwise used.
  4. define i32 @test(i32* %p) {
  5. ; CHECK-LABEL: @test
  6. ; CHECK: icmp eq
  7. ; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
  8. ; CHECK-NOT: icmp ne
  9. entry:
  10. %cmp = icmp eq i32* %p, null
  11. br i1 %cmp, label %is_null, label %not_null
  12. is_null:
  13. %cmp2 = icmp ne i32* %p, null
  14. br i1 %cmp2, label %exit1, label %exit2
  15. not_null:
  16. %cmp3 = icmp ne i32* %p, null
  17. br i1 %cmp3, label %exit1, label %exit2
  18. exit1:
  19. ret i32 0
  20. exit2:
  21. ret i32 1
  22. }
  23. declare void @use(i1)
  24. ; It would not be legal to replace %cmp2 (well, in this case it actually is,
  25. ; but that's a CSE problem, not a LVI/jump threading problem)
  26. define i32 @test_negative(i32* %p) {
  27. ; CHECK-LABEL: @test
  28. ; CHECK: icmp ne
  29. ; CHECK: icmp eq
  30. ; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
  31. ; CHECK-NOT: icmp ne
  32. entry:
  33. %cmp2 = icmp ne i32* %p, null
  34. call void @use(i1 %cmp2)
  35. %cmp = icmp eq i32* %p, null
  36. br i1 %cmp, label %is_null, label %not_null
  37. is_null:
  38. br i1 %cmp2, label %exit1, label %exit2
  39. not_null:
  40. br i1 %cmp2, label %exit1, label %exit2
  41. exit1:
  42. ret i32 0
  43. exit2:
  44. ret i32 1
  45. }
  46. ; In this case, we can remove cmp2 because it's otherwise unused
  47. define i32 @test2(i32* %p) {
  48. ; CHECK-LABEL: @test
  49. ; CHECK-LABEL: entry:
  50. ; CHECK-NEXT: icmp eq
  51. ; CHECK-NEXT: br i1 %cmp, label %exit2, label %exit1
  52. ; CHECK-NOT: icmp ne
  53. entry:
  54. %cmp2 = icmp ne i32* %p, null
  55. %cmp = icmp eq i32* %p, null
  56. br i1 %cmp, label %is_null, label %not_null
  57. is_null:
  58. br i1 %cmp2, label %exit1, label %exit2
  59. not_null:
  60. br i1 %cmp2, label %exit1, label %exit2
  61. exit1:
  62. ret i32 0
  63. exit2:
  64. ret i32 1
  65. }