pre-compare.ll 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ; RUN: opt -gvn -S < %s | FileCheck %s
  2. ; C source:
  3. ;
  4. ; void f(int x) {
  5. ; if (x != 1)
  6. ; puts (x == 2 ? "a" : "b");
  7. ; for (;;) {
  8. ; puts("step 1");
  9. ; if (x == 2)
  10. ; continue;
  11. ; printf("step 2: %d\n", x);
  12. ; }
  13. ; }
  14. ;
  15. ; If we PRE %cmp3, CodeGenPrepare won't be able to sink the compare down to its
  16. ; uses, and we are forced to keep both %x and %cmp3 in registers in the loop.
  17. ;
  18. ; It is just as cheap to recompute the icmp against %x as it is to compare a
  19. ; GPR against 0. On x86-64, the br i1 %cmp3 becomes:
  20. ;
  21. ; testb %r12b, %r12b
  22. ; jne LBB0_3
  23. ;
  24. ; The sunk icmp is:
  25. ;
  26. ; cmpl $2, %ebx
  27. ; je LBB0_3
  28. ;
  29. ; This is just as good, and it doesn't require a separate register.
  30. ;
  31. ; CHECK-NOT: phi i1
  32. @.str = private unnamed_addr constant [2 x i8] c"a\00", align 1
  33. @.str1 = private unnamed_addr constant [2 x i8] c"b\00", align 1
  34. @.str2 = private unnamed_addr constant [7 x i8] c"step 1\00", align 1
  35. @.str3 = private unnamed_addr constant [12 x i8] c"step 2: %d\0A\00", align 1
  36. define void @f(i32 %x) noreturn nounwind uwtable ssp {
  37. entry:
  38. %cmp = icmp eq i32 %x, 1
  39. br i1 %cmp, label %for.cond.preheader, label %if.then
  40. if.then: ; preds = %entry
  41. %cmp1 = icmp eq i32 %x, 2
  42. %cond = select i1 %cmp1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str1, i64 0, i64 0)
  43. %call = tail call i32 @puts(i8* %cond) nounwind
  44. br label %for.cond.preheader
  45. for.cond.preheader: ; preds = %entry, %if.then
  46. %cmp3 = icmp eq i32 %x, 2
  47. br label %for.cond
  48. for.cond: ; preds = %for.cond.backedge, %for.cond.preheader
  49. %call2 = tail call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str2, i64 0, i64 0)) nounwind
  50. br i1 %cmp3, label %for.cond.backedge, label %if.end5
  51. if.end5: ; preds = %for.cond
  52. %call6 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str3, i64 0, i64 0), i32 %x) nounwind
  53. br label %for.cond.backedge
  54. for.cond.backedge: ; preds = %if.end5, %for.cond
  55. br label %for.cond
  56. }
  57. declare i32 @puts(i8* nocapture) nounwind
  58. declare i32 @printf(i8* nocapture, ...) nounwind