Files
carbon-lang/toolchain/lowering/lowering_function_context.cpp
T
Richard Smith f7f73d42ca Split out a per-function lowering context from LoweringContext. (#2901)
This is being done in preparation for adding more state to per-function
lowering, to handle lowering functions containing more than one block.
2023-06-14 15:27:49 -07:00

35 lines
1.1 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/lowering/lowering_function_context.h"
#include "common/vlog.h"
#include "toolchain/semantics/semantics_ir.h"
#include "toolchain/semantics/semantics_node_kind.h"
namespace Carbon {
LoweringFunctionContext::LoweringFunctionContext(
LoweringContext& lowering_context, llvm::Function* function)
: lowering_context_(&lowering_context),
function_(function),
builder_(lowering_context.llvm_context()) {
builder_.SetInsertPoint(
llvm::BasicBlock::Create(llvm_context(), "entry", function_));
}
auto LoweringFunctionContext::GetLocalLoaded(SemanticsNodeId node_id)
-> llvm::Value* {
auto* value = GetLocal(node_id);
if (llvm::isa<llvm::AllocaInst, llvm::GetElementPtrInst>(value)) {
auto* load_type = GetType(semantics_ir().GetNode(node_id).type_id());
return builder().CreateLoad(load_type, value);
} else {
// No load is needed.
return value;
}
}
} // namespace Carbon