ldc/gen/variable_lifetime.h
Johan Engelen ef0719f36b
Implement -femit-local-var-lifetime which adds local (stack) variable… (#4395)
Implement -femit-local-var-lifetime which adds local (stack) variable lifetime annotation to LLVM IR, which enables sharing stack space for variables whose lifetimes do not overlap.
Resolves issue #2227

This is not enabled by default yet, to prevent miscompilation due to bugs (should be enabled in future for optimization levels > 0, and when sanitizers are enabled).
2023-06-01 22:45:56 +00:00

56 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//===-- gen/variable_lifetime.h - -------------------------------*- C++ -*-===//
//
// LDC the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Codegen for local variable lifetime: llvm.lifetime.start abd
// llvm.lifetime.end.
//
//===----------------------------------------------------------------------===//
#pragma once
#include <vector>
#include <utility>
namespace llvm {
class Function;
class Type;
class Value;
}
struct IRState;
struct LocalVariableLifetimeAnnotator {
struct LocalVariableScope {
std::vector<std::pair<llvm::Value *, llvm::Value *>> variables;
};
/// Stack of scopes, each scope can have multiple variables.
std::vector<LocalVariableScope> scopes;
/// Cache the llvm types and intrinsics used for codegen.
llvm::Function *lifetimeStartFunction = nullptr;
llvm::Function *lifetimeEndFunction = nullptr;
llvm::Type *allocaType = nullptr;
llvm::Function *getLLVMLifetimeStartFn();
llvm::Function *getLLVMLifetimeEndFn();
IRState &irs;
public:
LocalVariableLifetimeAnnotator(IRState &irs);
/// Opens a new scope.
void pushScope();
/// Closes current scope and emits end-of-lifetime annotation for all
/// variables in current scope.
void popScope();
/// Register a new local variable for lifetime annotation.
void addLocalVariable(llvm::Value *address, llvm::Value *size);
};