diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index f0c6e537..47f5f557 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -20555,10 +20555,12 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken, { leftValue = mModule->LoadValue(leftValue); - auto prevBB = mModule->mBfIRBuilder->GetInsertBlock(); - + 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) @@ -20583,7 +20584,7 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken, rightValue = mModule->LoadValue(rightValue); if (assignTo == NULL) - { + { auto rightToLeftValue = mModule->CastToValue(rightExpression, rightValue, leftValue.mType, BfCastFlags_SilentFail); if (rightToLeftValue) { @@ -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) @@ -20605,7 +20607,9 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken, mModule->TypeToString(leftValue.mType).c_str(), mModule->TypeToString(rightValue.mType).c_str()), opToken); leftValue = mModule->GetDefaultTypedValue(rightValue.mType); } - + + mModule->mBfIRBuilder->CreateBr(endBB); + endLhsBB = mModule->mBfIRBuilder->GetInsertBlock(); mModule->mBfIRBuilder->SetInsertPoint(rhsBB); } } @@ -20615,10 +20619,10 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken, mModule->mBfIRBuilder->CreateBr(endBB); auto endRhsBB = mModule->mBfIRBuilder->GetInsertBlock(); - + // 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); } diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 5fe1b758..688c2b7a 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -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; - useVal = mBfIRBuilder->CreateBitCast(val.mValue, mBfIRBuilder->MapTypeInstPtr(checkBaseType)); + + 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 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 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 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; } diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index ad002fe1..0b541a33 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -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); diff --git a/IDEHelper/Compiler/BfStmtEvaluator.cpp b/IDEHelper/Compiler/BfStmtEvaluator.cpp index cc9e893c..283b2861 100644 --- a/IDEHelper/Compiler/BfStmtEvaluator.cpp +++ b/IDEHelper/Compiler/BfStmtEvaluator.cpp @@ -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);