diff --git a/transform/allocs.go b/transform/allocs.go index f21dde12..9cf15a1d 100644 --- a/transform/allocs.go +++ b/transform/allocs.go @@ -43,6 +43,18 @@ func OptimizeAllocs(mod llvm.Module) { continue } + if size == 0 { + // If the size is 0, the pointer is allowed to alias other + // zero-sized pointers. Use the pointer to the global that would + // also be returned by runtime.alloc. + zeroSizedAlloc := mod.NamedGlobal("runtime.zeroSizedAlloc") + if !zeroSizedAlloc.IsNil() { + heapalloc.ReplaceAllUsesWith(zeroSizedAlloc) + heapalloc.EraseFromParentAsInstruction() + } + continue + } + // In general the pattern is: // %0 = call i8* @runtime.alloc(i32 %size) // %1 = bitcast i8* %0 to type* diff --git a/transform/testdata/allocs.ll b/transform/testdata/allocs.ll index 0511d79d..42932f4f 100644 --- a/transform/testdata/allocs.ll +++ b/transform/testdata/allocs.ll @@ -1,6 +1,8 @@ target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" target triple = "armv7m-none-eabi" +@runtime.zeroSizedAlloc = internal global i8 0, align 1 + declare nonnull i8* @runtime.alloc(i32) ; Test allocating a single int (i32) that should be allocated on the stack. @@ -68,6 +70,14 @@ end: ret void } +; Test a zero-sized allocation. +define void @testZeroSizedAlloc() { + %1 = call i8* @runtime.alloc(i32 0) + %2 = bitcast i8* %1 to i32* + %3 = call i32* @noescapeIntPtr(i32* %2) + ret void +} + declare i32* @escapeIntPtr(i32*) declare i32* @noescapeIntPtr(i32* nocapture) diff --git a/transform/testdata/allocs.out.ll b/transform/testdata/allocs.out.ll index eacf62b2..eaddd464 100644 --- a/transform/testdata/allocs.out.ll +++ b/transform/testdata/allocs.out.ll @@ -1,6 +1,8 @@ target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" target triple = "armv7m-none-eabi" +@runtime.zeroSizedAlloc = internal global i8 0, align 1 + declare nonnull i8* @runtime.alloc(i32) define void @testInt() { @@ -66,6 +68,12 @@ end: ; preds = %loop ret void } +define void @testZeroSizedAlloc() { + %1 = bitcast i8* @runtime.zeroSizedAlloc to i32* + %2 = call i32* @noescapeIntPtr(i32* %1) + ret void +} + declare i32* @escapeIntPtr(i32*) declare i32* @noescapeIntPtr(i32* nocapture)