+
+define i32 @ctlz_bsr(i32 %n) {
+entry:
+; Don't generate any xors when a 'ctlz' intrinsic is actually used to compute
+; the most significant bit, which is what 'bsr' does natively.
+; CHECK-LABEL: ctlz_bsr:
+; CHECK: bsrl
+; CHECK-NOT: xorl
+; CHECK: ret
+ %ctlz = call i32 @llvm.ctlz.i32(i32 %n, i1 true)
+ %bsr = xor i32 %ctlz, 31
+ ret i32 %bsr
+}
+
+define i32 @ctlz_bsr_cmov(i32 %n) {
+entry:
+; Same as ctlz_bsr, but ensure this happens even when there is a potential
+; zero.
+; CHECK-LABEL: ctlz_bsr_cmov:
+; CHECK: bsrl
+; CHECK-NOT: xorl
+; CHECK: ret
+ %ctlz = call i32 @llvm.ctlz.i32(i32 %n, i1 false)
+ %bsr = xor i32 %ctlz, 31
+ ret i32 %bsr
+}