1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 20:12:21 +02:00

Improved null conditional lhs cast location

This commit is contained in:
Brian Fiete 2021-06-29 11:58:33 -07:00
parent 4ef6723ac2
commit 096dc0aaa3
4 changed files with 40 additions and 16 deletions

View file

@ -20556,9 +20556,11 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
leftValue = mModule->LoadValue(leftValue);
auto prevBB = mModule->mBfIRBuilder->GetInsertBlock();
auto rhsBB = mModule->mBfIRBuilder->CreateBlock("nullc.rhs");
auto endBB = mModule->mBfIRBuilder->CreateBlock("nullc.end");
auto lhsBB = endBB;
auto endLhsBB = prevBB;
BfIRValue isNull;
if (leftValue.mType->IsFunction())
@ -20567,7 +20569,6 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
else
isNull = mModule->mBfIRBuilder->CreateIsNull(leftValue.mValue);
mModule->AddBasicBlock(rhsBB);
BfTypedValue rightValue;
if (assignTo != NULL)
@ -20591,7 +20592,8 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
}
else
{
mModule->mBfIRBuilder->SetInsertPoint(prevBB);
lhsBB = mModule->mBfIRBuilder->CreateBlock("nullc.lhs", true);
mModule->mBfIRBuilder->SetInsertPoint(lhsBB);
auto leftToRightValue = mModule->CastToValue(leftExpression, leftValue, rightValue.mType, BfCastFlags_SilentFail);
if (leftToRightValue)
@ -20606,6 +20608,8 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
leftValue = mModule->GetDefaultTypedValue(rightValue.mType);
}
mModule->mBfIRBuilder->CreateBr(endBB);
endLhsBB = mModule->mBfIRBuilder->GetInsertBlock();
mModule->mBfIRBuilder->SetInsertPoint(rhsBB);
}
}
@ -20618,7 +20622,7 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
// Actually add CondBr at start
mModule->mBfIRBuilder->SetInsertPoint(prevBB);
mModule->mBfIRBuilder->CreateCondBr(isNull, rhsBB, endBB);
mModule->mBfIRBuilder->CreateCondBr(isNull, rhsBB, lhsBB);
mModule->AddBasicBlock(endBB);
@ -20629,7 +20633,7 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
else
{
auto phi = mModule->mBfIRBuilder->CreatePhi(mModule->mBfIRBuilder->MapType(leftValue.mType), 2);
mModule->mBfIRBuilder->AddPhiIncoming(phi, leftValue.mValue, prevBB);
mModule->mBfIRBuilder->AddPhiIncoming(phi, leftValue.mValue, endLhsBB);
mModule->mBfIRBuilder->AddPhiIncoming(phi, rightValue.mValue, endRhsBB);
mResult = BfTypedValue(phi, leftValue.mType);
}

View file

@ -1915,7 +1915,7 @@ BfIRValue BfModule::CreateAllocaInst(BfTypeInstance* typeInst, bool addLifetime,
return allocaInst;
}
void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* refNode, BfScopeData* scopeData, bool condAlloca, bool mayEscape)
void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* refNode, BfScopeData* scopeData, bool condAlloca, bool mayEscape, BfIRBlock valBlock)
{
//This was removed because we want the alloc to be added to the __deferred list if it's actually a "stack"
// 'stack' in a head scopeData is really the same as 'scopeData', so use the simpler scopeData handling
@ -1939,7 +1939,15 @@ void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* r
{
bool isDynAlloc = (scopeData != NULL) && (mCurMethodState->mCurScope->IsDyn(scopeData));
BfIRValue useVal = val.mValue;
BfIRBlock prevBlock = mBfIRBuilder->GetInsertBlock();
if (valBlock)
mBfIRBuilder->SetInsertPoint(valBlock);
useVal = mBfIRBuilder->CreateBitCast(val.mValue, mBfIRBuilder->MapTypeInstPtr(checkBaseType));
if (!useVal.IsConst())
mBfIRBuilder->ClearDebugLocation(useVal);
if (valBlock)
mBfIRBuilder->SetInsertPoint(prevBlock);
if (isDynAlloc)
{
@ -1950,7 +1958,9 @@ void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* r
BF_ASSERT(!IsTargetingBeefBackend());
BF_ASSERT(!isDynAlloc);
auto valPtr = CreateAlloca(checkBaseType);
mBfIRBuilder->ClearDebugLocation_Last();
mBfIRBuilder->CreateStore(useVal, valPtr);
mBfIRBuilder->ClearDebugLocation_Last();
useVal = valPtr;
}
@ -1994,7 +2004,10 @@ void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* r
{
SizedArray<BfIRValue, 1> llvmArgs;
if (IsTargetingBeefBackend())
{
llvmArgs.push_back(mBfIRBuilder->CreateBitCast(val.mValue, mBfIRBuilder->MapType(nullPtrType)));
//mBfIRBuilder->ClearDebugLocation_Last();
}
else
llvmArgs.push_back(val.mValue);
llvmArgs.push_back(GetConstValue(val.mType->mSize));
@ -2023,6 +2036,7 @@ void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* r
{
SizedArray<BfIRValue, 1> llvmArgs;
llvmArgs.push_back(mBfIRBuilder->CreateBitCast(val.mValue, mBfIRBuilder->MapType(nullPtrType)));
//mBfIRBuilder->ClearDebugLocation_Last();
llvmArgs.push_back(clearSize);
AddDeferredCall(dtorMethodInstance, llvmArgs, scopeData, refNode, true);
}
@ -2052,6 +2066,7 @@ void BfModule::AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* r
{
SizedArray<BfIRValue, 1> llvmArgs;
llvmArgs.push_back(mBfIRBuilder->CreateBitCast(val.mValue, mBfIRBuilder->MapType(nullPtrType)));
//mBfIRBuilder->ClearDebugLocation_Last();
AddDeferredCall(dtorMethodInstance, llvmArgs, scopeData, refNode, true);
}
}
@ -8829,13 +8844,14 @@ BfIRValue BfModule::AllocFromType(BfType* type, const BfAllocTarget& allocTarget
auto allocaInst = mBfIRBuilder->CreateAlloca(mBfIRBuilder->MapType(byteType), sizeValue);
if (!isDynAlloc)
mBfIRBuilder->ClearDebugLocation(allocaInst);
auto allocaBlock = mBfIRBuilder->GetInsertBlock();
mBfIRBuilder->SetAllocaAlignment(allocaInst, allocAlign);
if (!isDynAlloc)
mBfIRBuilder->SetInsertPoint(prevBlock);
auto typedVal = BfTypedValue(mBfIRBuilder->CreateBitCast(allocaInst, mBfIRBuilder->MapType(arrayType)), arrayType);
mBfIRBuilder->ClearDebugLocation_Last();
if (!isDynAlloc)
mBfIRBuilder->SetInsertPoint(prevBlock);
if (!noDtorCall)
AddStackAlloc(typedVal, BfIRValue(), NULL, scopeData, false, true);
AddStackAlloc(typedVal, BfIRValue(), NULL, scopeData, false, true, allocaBlock);
InitTypeInst(typedVal, scopeData, zeroMemory, sizeValue);
return typedVal.mValue;
}

View file

@ -1573,7 +1573,7 @@ public:
void NewScopeState(bool createLexicalBlock = true, bool flushValueScope = true); // returns prev scope data
BfIRValue CreateAlloca(BfType* type, bool addLifetime = true, const char* name = NULL, BfIRValue arraySize = BfIRValue());
BfIRValue CreateAllocaInst(BfTypeInstance* typeInst, bool addLifetime = true, const char* name = NULL);
void AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* refNode, BfScopeData* scope, bool condAlloca = false, bool mayEscape = false);
void AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* refNode, BfScopeData* scope, bool condAlloca = false, bool mayEscape = false, BfIRBlock valBlock = BfIRBlock());
void RestoreScoreState_LocalVariables();
void RestoreScopeState();
void MarkDynStack(BfScopeData* scope);

View file

@ -83,11 +83,15 @@ bool BfModule::AddDeferredCallEntry(BfDeferredCallEntry* deferredCallEntry, BfSc
auto prevInsertBlock = mBfIRBuilder->GetInsertBlock();
mBfIRBuilder->SetInsertPoint(mCurMethodState->mIRHeadBlock);
auto allocaInst = mBfIRBuilder->CreateAlloca(origParamTypes[paramIdx]);
mBfIRBuilder->ClearDebugLocation(allocaInst);
mBfIRBuilder->ClearDebugLocation_Last();
mBfIRBuilder->SetInsertPoint(prevInsertBlock);
if (WantsLifetimes())
{
mBfIRBuilder->CreateLifetimeStart(allocaInst);
mBfIRBuilder->ClearDebugLocation_Last();
}
mBfIRBuilder->CreateStore(scopeArg, allocaInst);
mBfIRBuilder->ClearDebugLocation_Last();
deferredCallEntry->mScopeArgs[paramIdx] = allocaInst;
if (WantsLifetimes())
scopeData->mDeferredLifetimeEnds.push_back(allocaInst);