提交 fbb54514 编写于 作者: A Alireza Habibi

Capture stackalloc size expression only if needed

上级 542daef6
......@@ -1290,10 +1290,10 @@ private BoundExpression MakeNullableHasValue(SyntaxNode syntax, BoundExpression
BoundExpression rightNeverNull = NullableAlwaysHasValue(loweredRight);
BoundExpression boundTempX = leftNeverNull ?? loweredLeft;
boundTempX = CaptureNullableOperandInTempIfNeeded(boundTempX, sideeffects, locals);
boundTempX = CaptureExpressionInTempIfNeeded(boundTempX, sideeffects, locals);
BoundExpression boundTempY = rightNeverNull ?? loweredRight;
boundTempY = CaptureNullableOperandInTempIfNeeded(boundTempY, sideeffects, locals);
boundTempY = CaptureExpressionInTempIfNeeded(boundTempY, sideeffects, locals);
BoundExpression callX_GetValueOrDefault = MakeOptimizedGetValueOrDefault(syntax, boundTempX);
BoundExpression callY_GetValueOrDefault = MakeOptimizedGetValueOrDefault(syntax, boundTempY);
......@@ -1330,7 +1330,7 @@ private BoundExpression MakeNullableHasValue(SyntaxNode syntax, BoundExpression
type: type);
}
private BoundExpression CaptureNullableOperandInTempIfNeeded(BoundExpression operand, ArrayBuilder<BoundExpression> sideeffects, ArrayBuilder<LocalSymbol> locals)
private BoundExpression CaptureExpressionInTempIfNeeded(BoundExpression operand, ArrayBuilder<BoundExpression> sideeffects, ArrayBuilder<LocalSymbol> locals)
{
if (CanChangeValueBetweenReads(operand))
{
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
using System.Diagnostics;
namespace Microsoft.CodeAnalysis.CSharp
{
......@@ -35,7 +35,9 @@ public override BoundNode VisitStackAllocArrayCreation(BoundStackAllocArrayCreat
else if (type.OriginalDefinition == _compilation.GetWellKnownType(WellKnownType.System_Span_T))
{
var spanType = (NamedTypeSymbol)stackAllocNode.Type;
var countTemp = _factory.StoreToTemp(rewrittenCount, out BoundAssignmentOperator countTempAssignment);
var sideEffects = ArrayBuilder<BoundExpression>.GetInstance();
var locals = ArrayBuilder<LocalSymbol>.GetInstance();
var countTemp = CaptureExpressionInTempIfNeeded(rewrittenCount, sideEffects, locals);
var stackSize = RewriteStackAllocCountToSize(countTemp, elementType);
stackAllocNode = new BoundConvertedStackAllocExpression(stackAllocNode.Syntax, elementType, stackSize, spanType);
......@@ -44,8 +46,8 @@ public override BoundNode VisitStackAllocArrayCreation(BoundStackAllocArrayCreat
return new BoundSequence(
syntax: stackAllocNode.Syntax,
locals: ImmutableArray.Create(countTemp.LocalSymbol),
sideEffects: ImmutableArray.Create<BoundExpression>(countTempAssignment),
locals: locals.ToImmutableAndFree(),
sideEffects: sideEffects.ToImmutableAndFree(),
value: ctorCall,
type: spanType);
}
......
......@@ -1077,24 +1077,19 @@ public static void Main()
CompileAndVerify(comp, expectedOutput: "10", verify: Verification.Fails).VerifyIL("Test.Main", @"
{
// Code size 29 (0x1d)
// Code size 26 (0x1a)
.maxstack 2
.locals init (System.Span<int> V_0, //x
int V_1)
IL_0000: ldc.i4.s 10
IL_0002: stloc.1
IL_0003: ldloc.1
IL_0004: conv.u
IL_0005: ldc.i4.4
IL_0006: mul.ovf.un
IL_0007: localloc
IL_0009: ldloc.1
IL_000a: newobj ""System.Span<int>..ctor(void*, int)""
IL_000f: stloc.0
IL_0010: ldloca.s V_0
IL_0012: call ""int System.Span<int>.Length.get""
IL_0017: call ""void System.Console.WriteLine(int)""
IL_001c: ret
.locals init (System.Span<int> V_0) //x
IL_0000: ldc.i4.s 40
IL_0002: conv.u
IL_0003: localloc
IL_0005: ldc.i4.s 10
IL_0007: newobj ""System.Span<int>..ctor(void*, int)""
IL_000c: stloc.0
IL_000d: ldloca.s V_0
IL_000f: call ""int System.Span<int>.Length.get""
IL_0014: call ""void System.Console.WriteLine(int)""
IL_0019: ret
}");
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册