Skip to content

avoid bounds check when slicing from [0..] #1839

@andrewrk

Description

@andrewrk
export fn entry() void {
    var z = []u32{1234};
    var y: []const u32 = z[0..];
    var x = @sliceToBytes(y);
}
define void @entry() #2 !dbg !41 {
Entry:
  %0 = alloca %"[]u32", align 8
  %1 = alloca %"[]u8", align 8
  %z = alloca [1 x i32], align 4
  %y = alloca %"[]u32", align 8
  %x = alloca %"[]u8", align 8
  %2 = bitcast [1 x i32]* %z to i8*, !dbg !60
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %2, i8* align 4 bitcast ([1 x i32]* @0 to i8*), i64 4, i1 false), !dbg !60
  call void @llvm.dbg.declare(metadata [1 x i32]* %z, metadata !45, metadata !DIExpression()), !dbg !60
  br i1 true, label %BoundsCheckOk, label %BoundsCheckFail, !dbg !61

BoundsCheckFail:                                  ; preds = %Entry
  tail call fastcc void @panic(%"[]u8"* @3, %StackTrace* null), !dbg !61
  unreachable, !dbg !61

BoundsCheckOk:                                    ; preds = %Entry
  %3 = getelementptr inbounds %"[]u32", %"[]u32"* %0, i32 0, i32 0, !dbg !61
  %4 = getelementptr inbounds [1 x i32], [1 x i32]* %z, i64 0, i64 0, !dbg !61
...

The bounds check can always be elided, even in debug mode, when slicing from 0 with inferred len.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions