lowering_function_context.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lowering/lowering_function_context.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/semantics/semantics_ir.h"
  7. #include "toolchain/semantics/semantics_node_kind.h"
  8. namespace Carbon {
  9. LoweringFunctionContext::LoweringFunctionContext(
  10. LoweringContext& lowering_context, llvm::Function* function)
  11. : lowering_context_(&lowering_context),
  12. function_(function),
  13. builder_(lowering_context.llvm_context()) {
  14. builder_.SetInsertPoint(
  15. llvm::BasicBlock::Create(llvm_context(), "entry", function_));
  16. }
  17. auto LoweringFunctionContext::GetLocalLoaded(SemanticsNodeId node_id)
  18. -> llvm::Value* {
  19. auto* value = GetLocal(node_id);
  20. if (llvm::isa<llvm::AllocaInst, llvm::GetElementPtrInst>(value)) {
  21. auto* load_type = GetType(semantics_ir().GetNode(node_id).type_id());
  22. return builder().CreateLoad(load_type, value);
  23. } else {
  24. // No load is needed.
  25. return value;
  26. }
  27. }
  28. } // namespace Carbon