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.
This commit is contained in:
Richard Smith authored and GitHub committed 2023-06-14 15:27:49 -07:00
1 parent 70c6199496
commit f7f73d42ca
6 files changed
+173 -96

No files matched your search

@@ -0,0 +1,34 @@
// 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