1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -O1 -S -mattr=+lzcnt | FileCheck %s
4 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-unknown-linux-gnu"
7 ; This test ensures we are able to optimize the following loop to an llvm.abs
8 ; followed by an llvm.ctlz.
9 ; FIXME: LoopIdiom recongize is not forming llvm.ctlz.
11 ; int ctlz_zero_check(int n)
13 ; n = n >= 0 ? n : -n;
22 define i32 @ctlz_loop_with_abs(i32 %n) {
23 ; CHECK-LABEL: @ctlz_loop_with_abs(
25 ; CHECK-NEXT: [[TOBOOL_NOT1:%.*]] = icmp eq i32 [[N:%.*]], 0
26 ; CHECK-NEXT: br i1 [[TOBOOL_NOT1]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
27 ; CHECK: while.body.preheader:
28 ; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 @llvm.abs.i32(i32 [[N]], i1 true)
29 ; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
31 ; CHECK-NEXT: [[N_ADDR_03:%.*]] = phi i32 [ [[TMP1:%.*]], [[WHILE_BODY]] ], [ [[TMP0]], [[WHILE_BODY_PREHEADER]] ]
32 ; CHECK-NEXT: [[I_02:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 0, [[WHILE_BODY_PREHEADER]] ]
33 ; CHECK-NEXT: [[TMP1]] = lshr i32 [[N_ADDR_03]], 1
34 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I_02]], 1
35 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp samesign ult i32 [[N_ADDR_03]], 2
36 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[WHILE_END]], label [[WHILE_BODY]]
38 ; CHECK-NEXT: [[I_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC]], [[WHILE_BODY]] ]
39 ; CHECK-NEXT: ret i32 [[I_0_LCSSA]]
42 %cmp = icmp sge i32 %n, 0
43 br i1 %cmp, label %cond.true, label %cond.false
45 cond.true: ; preds = %entry
48 cond.false: ; preds = %entry
49 %sub = sub nsw i32 0, %n
52 cond.end: ; preds = %cond.false, %cond.true
53 %cond = phi i32 [ %n, %cond.true ], [ %sub, %cond.false ]
56 while.cond: ; preds = %while.body, %cond.end
57 %i.0 = phi i32 [ 0, %cond.end ], [ %inc, %while.body ]
58 %n.addr.0 = phi i32 [ %cond, %cond.end ], [ %shr, %while.body ]
59 %tobool = icmp ne i32 %n.addr.0, 0
60 br i1 %tobool, label %while.body, label %while.end
62 while.body: ; preds = %while.cond
63 %shr = ashr i32 %n.addr.0, 1
64 %inc = add nsw i32 %i.0, 1
67 while.end: ; preds = %while.cond