mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 20:18:11 +00:00
Lowering of Branch / BranchIf / BranchWithArg / BlockArg. (#2904)
This gives us complete lowering of `if` expressions plus `and` and `or`. Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
1 parent
c27f41c94b
commit
0d4d392d12
44 files changed
+453
-91
No files matched your search
@@ -14,9 +14,54 @@ 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_));
|
||||
builder_(lowering_context.llvm_context()) {}
|
||||
|
||||
auto LoweringFunctionContext::GetBlock(SemanticsNodeBlockId block_id)
|
||||
-> llvm::BasicBlock* {
|
||||
llvm::BasicBlock*& entry = blocks_[block_id];
|
||||
if (!entry) {
|
||||
entry = llvm::BasicBlock::Create(llvm_context(), "", function_);
|
||||
block_worklist().Push(block_id);
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
auto LoweringFunctionContext::TryToReuseBlock(SemanticsNodeBlockId block_id,
|
||||
llvm::BasicBlock* block) -> bool {
|
||||
if (!blocks_.insert({block_id, block}).second) {
|
||||
return false;
|
||||
}
|
||||
block_worklist().Push(block_id);
|
||||
if (block == synthetic_block_) {
|
||||
synthetic_block_ = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
auto LoweringFunctionContext::GetBlockArg(SemanticsNodeBlockId block_id,
|
||||
SemanticsTypeId type_id)
|
||||
-> llvm::PHINode* {
|
||||
llvm::BasicBlock* block = GetBlock(block_id);
|
||||
|
||||
// Find the existing phi, if any.
|
||||
auto phis = block->phis();
|
||||
if (!phis.empty()) {
|
||||
CARBON_CHECK(std::next(phis.begin()) == phis.end())
|
||||
<< "Expected at most one phi, found "
|
||||
<< std::distance(phis.begin(), phis.end());
|
||||
return &*phis.begin();
|
||||
}
|
||||
|
||||
// The number of predecessor slots to reserve.
|
||||
static constexpr unsigned NumReservedPredecessors = 2;
|
||||
auto* phi = llvm::PHINode::Create(GetType(type_id), NumReservedPredecessors);
|
||||
phi->insertInto(block, block->begin());
|
||||
return phi;
|
||||
}
|
||||
|
||||
auto LoweringFunctionContext::CreateSyntheticBlock() -> llvm::BasicBlock* {
|
||||
synthetic_block_ = llvm::BasicBlock::Create(llvm_context(), "", function_);
|
||||
return synthetic_block_;
|
||||
}
|
||||
|
||||
auto LoweringFunctionContext::GetLocalLoaded(SemanticsNodeId node_id)
|
||||
|
||||
Reference in new issue
Block a user