Skip to content

Commit

Permalink
Optimization regarding memuse and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 17, 2025
1 parent 726a93a commit ab9a76b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,9 @@ class TernaryExprNode final : public ExprNode {
LogicalOrExprNode *condition = nullptr;
LogicalOrExprNode *trueExpr = nullptr;
LogicalOrExprNode *falseExpr = nullptr;
Function *trueCalledCopyCtor = nullptr;
Function *falseCalledCopyCtor = nullptr;
Function *calledCopyCtor = nullptr;
bool trueSideCallsCopyCtor = false;
bool falseSideCallsCopyCtor = false;
bool isShortened = false;
};

Expand Down
18 changes: 9 additions & 9 deletions src/irgenerator/GenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ std::any IRGenerator::visitTernaryExpr(const TernaryExprNode *node) {
switchToBlock(condTrue);
llvm::Value *trueValue = nullptr;
llvm::Value* truePtr = nullptr;
if (node->falseCalledCopyCtor) { // both sides or only the false side needs copy ctor call
if (node->falseSideCallsCopyCtor) { // both sides or only the false side needs copy ctor call
truePtr = resolveAddress(trueNode);
} else if (node->trueCalledCopyCtor) { // only true side needs copy ctor call
} else if (node->trueSideCallsCopyCtor) { // only true side needs copy ctor call
llvm::Value* originalPtr = resolveAddress(trueNode);
truePtr = insertAlloca(trueNode->getEvaluatedSymbolType(manIdx).toLLVMType(sourceFile));
generateCtorOrDtorCall(truePtr, node->trueCalledCopyCtor, {originalPtr});
generateCtorOrDtorCall(truePtr, node->calledCopyCtor, {originalPtr});
} else { // neither true nor false side need copy ctor call
trueValue = resolveValue(trueNode);
}
Expand All @@ -135,12 +135,12 @@ std::any IRGenerator::visitTernaryExpr(const TernaryExprNode *node) {
switchToBlock(condFalse);
llvm::Value *falseValue = nullptr;
llvm::Value *falsePtr = nullptr;
if (node->trueCalledCopyCtor) { // both sides or only the true side needs copy ctor call
if (node->trueSideCallsCopyCtor) { // both sides or only the true side needs copy ctor call
falsePtr = resolveAddress(falseNode);
} else if (node->falseCalledCopyCtor) { // only false side needs copy ctor call
} else if (node->falseSideCallsCopyCtor) { // only false side needs copy ctor call
llvm::Value* originalPtr = resolveAddress(falseNode);
falsePtr = insertAlloca(falseNode->getEvaluatedSymbolType(manIdx).toLLVMType(sourceFile));
generateCtorOrDtorCall(falsePtr, node->falseCalledCopyCtor, {originalPtr});
generateCtorOrDtorCall(falsePtr, node->calledCopyCtor, {originalPtr});
} else { // neither true nor false side need copy ctor call
falseValue = resolveValue(falseNode);
}
Expand All @@ -149,13 +149,13 @@ std::any IRGenerator::visitTernaryExpr(const TernaryExprNode *node) {
// Fill the exit block
switchToBlock(condExit);
llvm::Type *resultType = node->getEvaluatedSymbolType(manIdx).toLLVMType(sourceFile);
if (node->trueCalledCopyCtor || node->falseCalledCopyCtor) { // at least one side needs copy ctor call
if (node->trueSideCallsCopyCtor || node->falseSideCallsCopyCtor) { // at least one side needs copy ctor call
llvm::PHINode* phiInst = builder.CreatePHI(builder.getPtrTy(), 2, "cond.result");
phiInst->addIncoming(truePtr, condTrue);
phiInst->addIncoming(falsePtr, condFalse);
if (node->trueCalledCopyCtor && node->falseCalledCopyCtor) { // both sides need copy ctor call
if (node->trueSideCallsCopyCtor && node->falseSideCallsCopyCtor) { // both sides need copy ctor call
resultPtr = insertAlloca(resultType);
generateCtorOrDtorCall(resultPtr, node->trueCalledCopyCtor, {phiInst});
generateCtorOrDtorCall(resultPtr, node->calledCopyCtor, {phiInst});
} else {
resultPtr = phiInst;
}
Expand Down
8 changes: 6 additions & 2 deletions src/typechecker/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,13 +996,13 @@ std::any TypeChecker::visitTernaryExpr(TernaryExprNode *node) {
if (removeAnonymousSymbolTrueSide) {
currentScope->symbolTable.deleteAnonymous(trueEntry->name);
} else if (trueEntry && !trueEntry->anonymous && !trueTypeModified.isTriviallyCopyable(node)) {
node->trueCalledCopyCtor = matchCopyCtor(trueTypeModified, node);
node->trueSideCallsCopyCtor = true;
}
const bool removeAnonymousSymbolFalseSide = falseEntry && falseEntry->anonymous;
if (removeAnonymousSymbolFalseSide) {
currentScope->symbolTable.deleteAnonymous(falseEntry->name);
} else if (falseEntry && !falseEntry->anonymous && !falseTypeModified.isTriviallyCopyable(node)) {
node->falseCalledCopyCtor = matchCopyCtor(falseTypeModified, node);
node->falseSideCallsCopyCtor = true;
}

// Create a new anonymous symbol for the result if required
Expand All @@ -1011,6 +1011,10 @@ std::any TypeChecker::visitTernaryExpr(TernaryExprNode *node) {
if (removeAnonymousSymbolTrueSide || removeAnonymousSymbolFalseSide)
anonymousSymbol = currentScope->symbolTable.insertAnonymous(returnType, node);

// Lookup copy ctor if at least one side needs it
if (node->trueSideCallsCopyCtor || node->falseSideCallsCopyCtor)
node->calledCopyCtor = matchCopyCtor(trueTypeModified, node);

return ExprResult{node->setEvaluatedSymbolType(trueType, manIdx), anonymousSymbol};
}

Expand Down

0 comments on commit ab9a76b

Please sign in to comment.