dont-recompute.ll 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ; RUN: opt < %s -indvars -S | FileCheck %s
  2. ; This tests that the IV is not recomputed outside of the loop when it is known
  3. ; to be computed by the loop and used in the loop any way. In the example below
  4. ; although a's value can be computed outside of the loop, there is no benefit
  5. ; in doing so as it has to be computed by the loop anyway.
  6. ;
  7. ; extern void func(unsigned val);
  8. ;
  9. ; void test(unsigned m)
  10. ; {
  11. ; unsigned a = 0;
  12. ;
  13. ; for (int i=0; i<186; i++) {
  14. ; a += m;
  15. ; func(a);
  16. ; }
  17. ;
  18. ; func(a);
  19. ; }
  20. declare void @func(i32)
  21. ; CHECK-LABEL: @test(
  22. define void @test(i32 %m) nounwind uwtable {
  23. entry:
  24. br label %for.body
  25. for.body: ; preds = %for.body, %entry
  26. %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  27. %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
  28. %add = add i32 %a.05, %m
  29. ; CHECK: tail call void @func(i32 %add)
  30. tail call void @func(i32 %add)
  31. %inc = add nsw i32 %i.06, 1
  32. %exitcond = icmp eq i32 %inc, 186
  33. br i1 %exitcond, label %for.end, label %for.body
  34. for.end: ; preds = %for.body
  35. ; CHECK: for.end:
  36. ; CHECK-NOT: mul i32 %m, 186
  37. ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
  38. ; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
  39. tail call void @func(i32 %add)
  40. ret void
  41. }
  42. ; CHECK-LABEL: @test2(
  43. define i32 @test2(i32 %m) nounwind uwtable {
  44. entry:
  45. br label %for.body
  46. for.body: ; preds = %for.body, %entry
  47. %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  48. %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
  49. %add = add i32 %a.05, %m
  50. ; CHECK: tail call void @func(i32 %add)
  51. tail call void @func(i32 %add)
  52. %inc = add nsw i32 %i.06, 1
  53. %exitcond = icmp eq i32 %inc, 186
  54. br i1 %exitcond, label %for.end, label %for.body
  55. for.end: ; preds = %for.body
  56. ; CHECK: for.end:
  57. ; CHECK-NOT: mul i32 %m, 186
  58. ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
  59. ; CHECK-NEXT: ret i32 %add.lcssa
  60. ret i32 %add
  61. }