Skip to content

Instantly share code, notes, and snippets.

@nickdesaulniers
Created October 10, 2023 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickdesaulniers/708e3feaa08d75964f38ef873f72f1f1 to your computer and use it in GitHub Desktop.
Save nickdesaulniers/708e3feaa08d75964f38ef873f72f1f1 to your computer and use it in GitHub Desktop.
struct foo {
long long x;
};
struct foo foo_factory (void);
struct foo div (struct foo, struct foo);
long long bar (void) {
return div(div(foo_factory(), foo_factory()), div(foo_factory(), foo_factory())).x;
}
; current codegen (well, llvm-14)
%struct.foo = type { i64 }
define dso_local i64 @bar() local_unnamed_addr #0 {
%1 = alloca %struct.foo, align 8
%2 = alloca %struct.foo, align 8
%3 = alloca %struct.foo, align 8
%4 = alloca %struct.foo, align 8
%5 = alloca %struct.foo, align 8
%6 = alloca %struct.foo, align 8
%7 = alloca %struct.foo, align 8
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %2) #2
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %3) #2
%8 = load i64, ptr %2, align 8
%9 = insertvalue [1 x i64] poison, i64 %8, 0
%10 = load i64, ptr %3, align 8
%11 = insertvalue [1 x i64] poison, i64 %10, 0
call void @div(ptr nonnull sret(%struct.foo) align 8 %1, [1 x i64] %9, [1 x i64] %11) #2
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %5) #2
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %6) #2
%12 = load i64, ptr %5, align 8
%13 = insertvalue [1 x i64] poison, i64 %12, 0
%14 = load i64, ptr %6, align 8
%15 = insertvalue [1 x i64] poison, i64 %14, 0
call void @div(ptr nonnull sret(%struct.foo) align 8 %4, [1 x i64] %13, [1 x i64] %15) #2
%16 = load i64, ptr %1, align 8
%17 = insertvalue [1 x i64] poison, i64 %16, 0
%18 = load i64, ptr %4, align 8
%19 = insertvalue [1 x i64] poison, i64 %18, 0
call void @div(ptr nonnull sret(%struct.foo) align 8 %7, [1 x i64] %17, [1 x i64] %19) #2
%20 = load i64, ptr %7, align 8
ret i64 %20
}
declare void @div(ptr sret(%struct.foo) align 8, [1 x i64], [1 x i64]) local_unnamed_addr #1
declare void @foo_factory(ptr sret(%struct.foo) align 8) local_unnamed_addr #1
attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="arm7tdmi" "target-features"="+armv4t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="arm7tdmi" "target-features"="+armv4t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
attributes #2 = { nounwind }
; optimal codegen
%struct.foo = type { i64 }
define dso_local i64 @bar() local_unnamed_addr #0 {
%1 = alloca %struct.foo, align 8
%2 = alloca %struct.foo, align 8
%3 = alloca %struct.foo, align 8
%4 = alloca %struct.foo, align 8
%5 = alloca %struct.foo, align 8
%6 = alloca %struct.foo, align 8
%7 = alloca %struct.foo, align 8
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2)
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %2) #2
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3)
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %3) #2
%8 = load i64, ptr %2, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2)
%9 = insertvalue [1 x i64] poison, i64 %8, 0
%10 = load i64, ptr %3, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3)
%11 = insertvalue [1 x i64] poison, i64 %10, 0
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1)
call void @div(ptr nonnull sret(%struct.foo) align 8 %1, [1 x i64] %9, [1 x i64] %11) #2
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5)
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %5) #2
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6)
call void @foo_factory(ptr nonnull sret(%struct.foo) align 8 %6) #2
%12 = load i64, ptr %5, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5)
%13 = insertvalue [1 x i64] poison, i64 %12, 0
%14 = load i64, ptr %6, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6)
%15 = insertvalue [1 x i64] poison, i64 %14, 0
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4)
call void @div(ptr nonnull sret(%struct.foo) align 8 %4, [1 x i64] %13, [1 x i64] %15) #2
%16 = load i64, ptr %1, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1)
%17 = insertvalue [1 x i64] poison, i64 %16, 0
%18 = load i64, ptr %4, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4)
%19 = insertvalue [1 x i64] poison, i64 %18, 0
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7)
call void @div(ptr nonnull sret(%struct.foo) align 8 %7, [1 x i64] %17, [1 x i64] %19) #2
%20 = load i64, ptr %7, align 8
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7)
ret i64 %20
}
declare void @div(ptr sret(%struct.foo) align 8, [1 x i64], [1 x i64]) local_unnamed_addr #1
declare void @foo_factory(ptr sret(%struct.foo) align 8) local_unnamed_addr #1
declare void @llvm.lifetime.start.p0(i64 %size, ptr nocapture %ptr)
declare void @llvm.lifetime.end.p0(i64 %size, ptr nocapture %ptr)
attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="arm7tdmi" "target-features"="+armv4t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="arm7tdmi" "target-features"="+armv4t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" }
attributes #2 = { nounwind }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment