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