authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-30 16:23:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-30 16:23:31-07:00
logfb7060d3c2e8ce4d7de5560adf8ec4a26fc5f6e8
tree3710af0870e1e1d43f44830d6ff3e662761e93ce
parent0c30799d4039c30f95eee29e2c2f8f604e8b9880

stage2: implement shl_exact and shr_exact

These produce an undefined value when one bits are shifted out. New AIR instruction: shr_exact.

13 files changed, 97 insertions(+), 62 deletions(-)

src/Air.zig+4
......@@ -179,6 +179,9 @@ pub const Inst = struct {
179179 /// Shift right. `>>`
180180 /// Uses the `bin_op` field.
181181 shr,
182 /// Shift right. The shift produces a poison value if it shifts out any non-zero bits.
183 /// Uses the `bin_op` field.
184 shr_exact,
182185 /// Shift left. `<<`
183186 /// Uses the `bin_op` field.
184187 shl,
......@@ -738,6 +741,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
738741 .ptr_add,
739742 .ptr_sub,
740743 .shr,
744 .shr_exact,
741745 .shl,
742746 .shl_exact,
743747 .shl_sat,
src/Liveness.zig+1
......@@ -261,6 +261,7 @@ fn analyzeInst(
261261 .shl_exact,
262262 .shl_sat,
263263 .shr,
264 .shr_exact,
264265 .atomic_store_unordered,
265266 .atomic_store_monotonic,
266267 .atomic_store_release,
src/Sema.zig+43-23
......@@ -666,7 +666,8 @@ fn analyzeBodyInner(
666666 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
667667 .ref => try sema.zirRef(block, inst),
668668 .ret_err_value_code => try sema.zirRetErrValueCode(block, inst),
669 .shr => try sema.zirShr(block, inst),
669 .shr => try sema.zirShr(block, inst, .shr),
670 .shr_exact => try sema.zirShr(block, inst, .shr_exact),
670671 .slice_end => try sema.zirSliceEnd(block, inst),
671672 .slice_sentinel => try sema.zirSliceSentinel(block, inst),
672673 .slice_start => try sema.zirSliceStart(block, inst),
......@@ -721,7 +722,6 @@ fn analyzeBodyInner(
721722 .pop_count => try sema.zirPopCount(block, inst),
722723 .byte_swap => try sema.zirByteSwap(block, inst),
723724 .bit_reverse => try sema.zirBitReverse(block, inst),
724 .shr_exact => try sema.zirShrExact(block, inst),
725725 .bit_offset_of => try sema.zirBitOffsetOf(block, inst),
726726 .offset_of => try sema.zirOffsetOf(block, inst),
727727 .cmpxchg_strong => try sema.zirCmpxchg(block, inst, .cmpxchg_strong),
......@@ -7472,18 +7472,30 @@ fn zirShl(
74727472 if (rhs_val.compareWithZero(.eq)) {
74737473 return sema.addConstant(lhs_ty, lhs_val);
74747474 }
7475 const target = sema.mod.getTarget();
74757476 const val = switch (air_tag) {
7476 .shl_exact => return sema.fail(block, lhs_src, "TODO implement Sema for comptime shl_exact", .{}),
7477 .shl_exact => val: {
7478 const shifted = try lhs_val.shl(rhs_val, sema.arena);
7479 if (lhs_ty.zigTypeTag() == .ComptimeInt) {
7480 break :val shifted;
7481 }
7482 const int_info = lhs_ty.intInfo(target);
7483 const truncated = try shifted.intTrunc(sema.arena, int_info.signedness, int_info.bits);
7484 if (truncated.compareHetero(.eq, shifted)) {
7485 break :val shifted;
7486 }
7487 return sema.addConstUndef(lhs_ty);
7488 },
74777489
74787490 .shl_sat => if (lhs_ty.zigTypeTag() == .ComptimeInt)
74797491 try lhs_val.shl(rhs_val, sema.arena)
74807492 else
7481 try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, sema.mod.getTarget()),
7493 try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, target),
74827494
74837495 .shl => if (lhs_ty.zigTypeTag() == .ComptimeInt)
74847496 try lhs_val.shl(rhs_val, sema.arena)
74857497 else
7486 try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, sema.mod.getTarget()),
7498 try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, target),
74877499
74887500 else => unreachable,
74897501 };
......@@ -7502,19 +7514,23 @@ fn zirShl(
75027514 return block.addBinOp(air_tag, lhs, rhs);
75037515}
75047516
7505fn zirShr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7517fn zirShr(
7518 sema: *Sema,
7519 block: *Block,
7520 inst: Zir.Inst.Index,
7521 air_tag: Air.Inst.Tag,
7522) CompileError!Air.Inst.Ref {
75067523 const tracy = trace(@src());
75077524 defer tracy.end();
75087525
75097526 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
7510 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
75117527 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
75127528 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
75137529 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
75147530 const lhs = sema.resolveInst(extra.lhs);
75157531 const rhs = sema.resolveInst(extra.rhs);
75167532
7517 if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| {
7533 const runtime_src = if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| rs: {
75187534 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
75197535 const lhs_ty = sema.typeOf(lhs);
75207536 if (lhs_val.isUndef() or rhs_val.isUndef()) {
......@@ -7524,19 +7540,29 @@ fn zirShr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
75247540 if (rhs_val.compareWithZero(.eq)) {
75257541 return sema.addConstant(lhs_ty, lhs_val);
75267542 }
7543 if (air_tag == .shr_exact) {
7544 // Detect if any ones would be shifted out.
7545 const bits = @intCast(u16, rhs_val.toUnsignedInt());
7546 const truncated = try lhs_val.intTrunc(sema.arena, .unsigned, bits);
7547 if (!truncated.compareWithZero(.eq)) {
7548 return sema.addConstUndef(lhs_ty);
7549 }
7550 }
75277551 const val = try lhs_val.shr(rhs_val, sema.arena);
75287552 return sema.addConstant(lhs_ty, val);
7553 } else {
7554 // Even if lhs is not comptime known, we can still deduce certain things based
7555 // on rhs.
7556 // If rhs is 0, return lhs without doing any calculations.
7557 if (rhs_val.compareWithZero(.eq)) {
7558 return lhs;
7559 }
7560 break :rs lhs_src;
75297561 }
7530 // Even if lhs is not comptime known, we can still deduce certain things based
7531 // on rhs.
7532 // If rhs is 0, return lhs without doing any calculations.
7533 else if (rhs_val.compareWithZero(.eq)) {
7534 return lhs;
7535 }
7536 }
7562 } else rhs_src;
75377563
7538 try sema.requireRuntimeBlock(block, src);
7539 return block.addBinOp(.shr, lhs, rhs);
7564 try sema.requireRuntimeBlock(block, runtime_src);
7565 return block.addBinOp(air_tag, lhs, rhs);
75407566}
75417567
75427568fn zirBitwise(
......@@ -11448,12 +11474,6 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1144811474 return sema.fail(block, src, "TODO: Sema.zirBitReverse", .{});
1144911475}
1145011476
11451fn zirShrExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
11452 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
11453 const src = inst_data.src();
11454 return sema.fail(block, src, "TODO: Sema.zirShrExact", .{});
11455}
11456
1145711477fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1145811478 const offset = try bitOffsetOf(sema, block, inst);
1145911479 return sema.addIntUnsigned(Type.comptime_int, offset);
src/arch/aarch64/CodeGen.zig+6-6
......@@ -535,12 +535,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
535535 .cmp_gt => try self.airCmp(inst, .gt),
536536 .cmp_neq => try self.airCmp(inst, .neq),
537537
538 .bool_and => try self.airBoolOp(inst),
539 .bool_or => try self.airBoolOp(inst),
540 .bit_and => try self.airBitAnd(inst),
541 .bit_or => try self.airBitOr(inst),
542 .xor => try self.airXor(inst),
543 .shr => try self.airShr(inst),
538 .bool_and => try self.airBoolOp(inst),
539 .bool_or => try self.airBoolOp(inst),
540 .bit_and => try self.airBitAnd(inst),
541 .bit_or => try self.airBitOr(inst),
542 .xor => try self.airXor(inst),
543 .shr, .shr_exact => try self.airShr(inst),
544544
545545 .alloc => try self.airAlloc(inst),
546546 .ret_ptr => try self.airRetPtr(inst),
src/arch/arm/CodeGen.zig+6-6
......@@ -527,12 +527,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
527527 .cmp_gt => try self.airCmp(inst, .gt),
528528 .cmp_neq => try self.airCmp(inst, .neq),
529529
530 .bool_and => try self.airBoolOp(inst),
531 .bool_or => try self.airBoolOp(inst),
532 .bit_and => try self.airBitAnd(inst),
533 .bit_or => try self.airBitOr(inst),
534 .xor => try self.airXor(inst),
535 .shr => try self.airShr(inst),
530 .bool_and => try self.airBoolOp(inst),
531 .bool_or => try self.airBoolOp(inst),
532 .bit_and => try self.airBitAnd(inst),
533 .bit_or => try self.airBitOr(inst),
534 .xor => try self.airXor(inst),
535 .shr, .shr_exact => try self.airShr(inst),
536536
537537 .alloc => try self.airAlloc(inst),
538538 .ret_ptr => try self.airRetPtr(inst),
src/arch/riscv64/CodeGen.zig+6-6
......@@ -514,12 +514,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
514514 .cmp_gt => try self.airCmp(inst, .gt),
515515 .cmp_neq => try self.airCmp(inst, .neq),
516516
517 .bool_and => try self.airBoolOp(inst),
518 .bool_or => try self.airBoolOp(inst),
519 .bit_and => try self.airBitAnd(inst),
520 .bit_or => try self.airBitOr(inst),
521 .xor => try self.airXor(inst),
522 .shr => try self.airShr(inst),
517 .bool_and => try self.airBoolOp(inst),
518 .bool_or => try self.airBoolOp(inst),
519 .bit_and => try self.airBitAnd(inst),
520 .bit_or => try self.airBitOr(inst),
521 .xor => try self.airXor(inst),
522 .shr, .shr_exact => try self.airShr(inst),
523523
524524 .alloc => try self.airAlloc(inst),
525525 .ret_ptr => try self.airRetPtr(inst),
src/arch/wasm/CodeGen.zig+2-3
......@@ -1442,8 +1442,8 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
14421442 .bool_and => self.airBinOp(inst, .@"and"),
14431443 .bool_or => self.airBinOp(inst, .@"or"),
14441444 .rem => self.airBinOp(inst, .rem),
1445 .shl => self.airBinOp(inst, .shl),
1446 .shr => self.airBinOp(inst, .shr),
1445 .shl, .shl_exact => self.airBinOp(inst, .shl),
1446 .shr, .shr_exact => self.airBinOp(inst, .shr),
14471447 .xor => self.airBinOp(inst, .xor),
14481448
14491449 .cmp_eq => self.airCmp(inst, .eq),
......@@ -1531,7 +1531,6 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
15311531 .max,
15321532 .min,
15331533 .assembly,
1534 .shl_exact,
15351534 .shl_sat,
15361535 .ret_addr,
15371536 .clz,
src/arch/x86_64/CodeGen.zig+6-6
......@@ -578,12 +578,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
578578 .cmp_gt => try self.airCmp(inst, .gt),
579579 .cmp_neq => try self.airCmp(inst, .neq),
580580
581 .bool_and => try self.airBoolOp(inst),
582 .bool_or => try self.airBoolOp(inst),
583 .bit_and => try self.airBitAnd(inst),
584 .bit_or => try self.airBitOr(inst),
585 .xor => try self.airXor(inst),
586 .shr => try self.airShr(inst),
581 .bool_and => try self.airBoolOp(inst),
582 .bool_or => try self.airBoolOp(inst),
583 .bit_and => try self.airBitAnd(inst),
584 .bit_or => try self.airBitOr(inst),
585 .xor => try self.airXor(inst),
586 .shr, .shr_exact => try self.airShr(inst),
587587
588588 .alloc => try self.airAlloc(inst),
589589 .ret_ptr => try self.airRetPtr(inst),
src/codegen/c.zig+1-1
......@@ -1470,7 +1470,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
14701470 .bit_and => try airBinOp(f, inst, " & "),
14711471 .bit_or => try airBinOp(f, inst, " | "),
14721472 .xor => try airBinOp(f, inst, " ^ "),
1473 .shr => try airBinOp(f, inst, " >> "),
1473 .shr, .shr_exact => try airBinOp(f, inst, " >> "),
14741474 .shl, .shl_exact => try airBinOp(f, inst, " << "),
14751475 .not => try airNot (f, inst),
14761476
src/codegen/llvm.zig+15-5
......@@ -2047,7 +2047,8 @@ pub const FuncGen = struct {
20472047 .bit_and, .bool_and => try self.airAnd(inst),
20482048 .bit_or, .bool_or => try self.airOr(inst),
20492049 .xor => try self.airXor(inst),
2050 .shr => try self.airShr(inst),
2050 .shr => try self.airShr(inst, false),
2051 .shr_exact => try self.airShr(inst, true),
20512052
20522053 .cmp_eq => try self.airCmp(inst, .eq),
20532054 .cmp_gt => try self.airCmp(inst, .gt),
......@@ -3633,7 +3634,7 @@ pub const FuncGen = struct {
36333634 return self.builder.buildUShlSat(lhs, casted_rhs, "");
36343635 }
36353636
3636 fn airShr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
3637 fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !?*const llvm.Value {
36373638 if (self.liveness.isUnused(inst))
36383639 return null;
36393640 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
......@@ -3645,11 +3646,20 @@ pub const FuncGen = struct {
36453646 self.builder.buildZExt(rhs, try self.dg.llvmType(lhs_type), "")
36463647 else
36473648 rhs;
3649 const is_signed_int = self.air.typeOfIndex(inst).isSignedInt();
36483650
3649 if (self.air.typeOfIndex(inst).isSignedInt()) {
3650 return self.builder.buildAShr(lhs, casted_rhs, "");
3651 if (is_exact) {
3652 if (is_signed_int) {
3653 return self.builder.buildAShrExact(lhs, casted_rhs, "");
3654 } else {
3655 return self.builder.buildLShrExact(lhs, casted_rhs, "");
3656 }
36513657 } else {
3652 return self.builder.buildLShr(lhs, casted_rhs, "");
3658 if (is_signed_int) {
3659 return self.builder.buildAShr(lhs, casted_rhs, "");
3660 } else {
3661 return self.builder.buildLShr(lhs, casted_rhs, "");
3662 }
36533663 }
36543664 }
36553665
src/codegen/llvm/bindings.zig+6
......@@ -548,6 +548,12 @@ pub const Builder = opaque {
548548 pub const buildAShr = LLVMBuildAShr;
549549 extern fn LLVMBuildAShr(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value;
550550
551 pub const buildLShrExact = ZigLLVMBuildLShrExact;
552 extern fn ZigLLVMBuildLShrExact(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value;
553
554 pub const buildAShrExact = ZigLLVMBuildAShrExact;
555 extern fn ZigLLVMBuildAShrExact(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value;
556
551557 pub const buildShl = LLVMBuildShl;
552558 extern fn LLVMBuildShl(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value;
553559
src/print_air.zig+1
......@@ -138,6 +138,7 @@ const Writer = struct {
138138 .shl_exact,
139139 .shl_sat,
140140 .shr,
141 .shr_exact,
141142 .set_union_tag,
142143 .min,
143144 .max,
test/behavior/math.zig-6
......@@ -736,8 +736,6 @@ fn testShlTrunc(x: u16) !void {
736736}
737737
738738test "exact shift left" {
739 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
740
741739 try testShlExact(0b00110101);
742740 comptime try testShlExact(0b00110101);
743741}
......@@ -747,8 +745,6 @@ fn testShlExact(x: u8) !void {
747745}
748746
749747test "exact shift right" {
750 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
751
752748 try testShrExact(0b10110100);
753749 comptime try testShrExact(0b10110100);
754750}
......@@ -758,8 +754,6 @@ fn testShrExact(x: u8) !void {
758754}
759755
760756test "shift left/right on u0 operand" {
761 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
762
763757 const S = struct {
764758 fn doTheTest() !void {
765759 var x: u0 = 0;