Skip to content

Commit

Permalink
[clang][Interp][NFC] Use direct Get{Local,Global} when possible
Browse files Browse the repository at this point in the history
When returning variable declaration values, try to get the value
directly instead of always going through a Pointer.
  • Loading branch information
tbaederr committed Nov 10, 2023
1 parent 86ef9d3 commit 7b1a058
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,27 +2119,33 @@ bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) {
if (!this->visitVarDecl(VD))
return false;

std::optional<PrimType> VarT = classify(VD->getType());
// Get a pointer to the variable
if (Context::shouldBeGloballyIndexed(VD)) {
auto GlobalIndex = P.getGlobal(VD);
assert(GlobalIndex); // visitVarDecl() didn't return false.
if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
return false;
if (VarT) {
if (!this->emitGetGlobal(*VarT, *GlobalIndex, VD))
return false;
} else {
if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
return false;
}
} else {
auto Local = Locals.find(VD);
assert(Local != Locals.end()); // Same here.
if (!this->emitGetPtrLocal(Local->second.Offset, VD))
return false;
if (VarT) {
if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
return false;
} else {
if (!this->emitGetPtrLocal(Local->second.Offset, VD))
return false;
}
}

// Return the value
if (std::optional<PrimType> VarT = classify(VD->getType())) {
if (!this->emitLoadPop(*VarT, VD))
return false;

if (VarT)
return this->emitRet(*VarT, VD);
}

return this->emitRetValue(VD);
}

Expand Down

0 comments on commit 7b1a058

Please sign in to comment.