1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
4 ; This is the naive implementation of x86 BZHI/BEXTR instruction:
5 ; it takes input and bit count, and extracts low nbits up to bit width.
6 ; I.e. unlike shift it does not have any UB when nbits >= bitwidth.
7 ; Which means we don't need a while PHI here, simple select will do.
8 define i32 @extract_low_bits(i32 %input, i32 %nbits) {
9 ; CHECK-LABEL: @extract_low_bits(
11 ; CHECK-NEXT: [[SHOULD_MASK:%.*]] = icmp ult i32 [[NBITS:%.*]], 32
12 ; CHECK-NEXT: [[MASK_NOT:%.*]] = shl nsw i32 -1, [[NBITS]]
13 ; CHECK-NEXT: [[MASK:%.*]] = xor i32 [[MASK_NOT]], -1
14 ; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[MASK]], [[INPUT:%.*]]
15 ; CHECK-NEXT: [[RES:%.*]] = select i1 [[SHOULD_MASK]], i32 [[MASKED]], i32 [[INPUT]]
16 ; CHECK-NEXT: ret i32 [[RES]]
19 %should_mask = icmp ult i32 %nbits, 32
20 br i1 %should_mask, label %perform_masking, label %end
22 perform_masking: ; preds = %begin
23 %mask.not = shl nsw i32 -1, %nbits
24 %mask = xor i32 %mask.not, -1
25 %masked = and i32 %mask, %input
28 end: ; preds = %perform_masking, %begin
29 %res = phi i32 [ %masked, %perform_masking ], [ %input, %begin ]