1//===- utils.go - misc utils ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements misellaneous utilities for IR generation.
10//
11//===----------------------------------------------------------------------===//
12
13package irgen
14
15import (
16	"llvm.org/llgo/third_party/gotools/go/types"
17	"llvm.org/llvm/bindings/go/llvm"
18)
19
20func (fr *frame) loadOrNull(cond, ptr llvm.Value, ty types.Type) *govalue {
21	startbb := fr.builder.GetInsertBlock()
22	loadbb := llvm.AddBasicBlock(fr.function, "")
23	contbb := llvm.AddBasicBlock(fr.function, "")
24	fr.builder.CreateCondBr(cond, loadbb, contbb)
25
26	fr.builder.SetInsertPointAtEnd(loadbb)
27	llty := fr.types.ToLLVM(ty)
28	typedptr := fr.builder.CreateBitCast(ptr, llvm.PointerType(llty, 0), "")
29	loadedval := fr.builder.CreateLoad(typedptr, "")
30	fr.builder.CreateBr(contbb)
31
32	fr.builder.SetInsertPointAtEnd(contbb)
33	llv := fr.builder.CreatePHI(llty, "")
34	llv.AddIncoming(
35		[]llvm.Value{llvm.ConstNull(llty), loadedval},
36		[]llvm.BasicBlock{startbb, loadbb},
37	)
38	return newValue(llv, ty)
39}
40