Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/coreclr/jit/codegenwasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,17 @@ void CodeGen::genCodeForTreeNode(GenTree* treeNode)
genCodeForJTrue(treeNode->AsOp());
break;

case GT_WASM_SELECT:
{
GenTreeConditional* select = treeNode->AsConditional();
genConsumeRegs(select->gtOp1);
genConsumeRegs(select->gtOp2);
genConsumeRegs(select->gtCond);
GetEmitter()->emitIns(INS_select);
WasmProduceReg(treeNode);
break;
}

case GT_SWITCH:
genTableBasedSwitch(treeNode);
break;
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/jit/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4432,6 +4432,16 @@ GenTree::VisitResult GenTree::VisitOperandUses(TVisitor visitor)
return visitor(&cond->gtOp2);
}

#ifdef TARGET_WASM
case GT_WASM_SELECT:
{
GenTreeConditional* const select = this->AsConditional();
RETURN_IF_ABORT(visitor(&select->gtOp1));
RETURN_IF_ABORT(visitor(&select->gtOp2));
return visitor(&select->gtCond);
}
#endif

default:
{
assert(!"unhandled special node");
Expand Down
56 changes: 56 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8558,6 +8558,9 @@ bool GenTree::TryGetUse(GenTree* operand, GenTree*** pUse)
case GT_SELECT_NEG:
case GT_SELECT_INV:
case GT_SELECT_INC:
#endif
#ifdef TARGET_WASM
case GT_WASM_SELECT:
#endif
case GT_SELECT:
{
Expand Down Expand Up @@ -9334,6 +9337,15 @@ GenTree::VtablePtr GenTree::GetVtableForOper(genTreeOps oper)
}
break;

#ifdef TARGET_WASM
case GT_WASM_SELECT:
{
GenTreeConditional gt;
res = *reinterpret_cast<VtablePtr*>(&gt);
}
break;
#endif

// We don't need to handle GTSTRUCT_N for LclVarCommon, since all those allowed opers are specified
// in their proper subtype. Similarly for GenTreeIndir.

Expand Down Expand Up @@ -12433,6 +12445,13 @@ GenTreeUseEdgeIterator::GenTreeUseEdgeIterator(GenTree* node)
assert(*m_edge != nullptr);
m_advance = &GenTreeUseEdgeIterator::AdvanceConditional;
return;
#ifdef TARGET_WASM
case GT_WASM_SELECT:
m_edge = &m_node->AsConditional()->gtOp1;
assert(*m_edge != nullptr);
m_advance = &GenTreeUseEdgeIterator::AdvanceWasmSelect;
return;
#endif

// Binary nodes
default:
Expand Down Expand Up @@ -12551,6 +12570,31 @@ void GenTreeUseEdgeIterator::AdvanceConditional()
assert(*m_edge != nullptr);
}

#ifdef TARGET_WASM
//------------------------------------------------------------------------
// GenTreeUseEdgeIterator::AdvanceWasmSelect: produces the next operand in Wasm stack order.
//
void GenTreeUseEdgeIterator::AdvanceWasmSelect()
{
GenTreeConditional* const select = m_node->AsConditional();
switch (m_state)
{
case 0:
m_edge = &select->gtOp2;
m_state = 1;
break;
case 1:
m_edge = &select->gtCond;
m_advance = &GenTreeUseEdgeIterator::Terminate;
break;
default:
unreached();
}

assert(*m_edge != nullptr);
}
#endif // TARGET_WASM

//------------------------------------------------------------------------
// GenTreeUseEdgeIterator::AdvanceBinOp: produces the next operand of a binary node and advances the state.
//
Expand Down Expand Up @@ -15231,10 +15275,22 @@ void Compiler::gtDispTree(GenTree* tree,
break;

case GT_SELECT:
#ifdef TARGET_WASM
case GT_WASM_SELECT:
#endif
gtDispCommonEndLine(tree);

if (!topOnly)
{
#ifdef TARGET_WASM
if (tree->OperIs(GT_WASM_SELECT))
{
gtDispChild(tree->AsConditional()->gtOp1, indentStack, IIArc, childMsg, topOnly);
gtDispChild(tree->AsConditional()->gtOp2, indentStack, IIArc, childMsg, topOnly);
gtDispChild(tree->AsConditional()->gtCond, indentStack, IIArcBottom, childMsg, topOnly);
break;
}
#endif
gtDispChild(tree->AsConditional()->gtCond, indentStack, IIArc, childMsg, topOnly);
gtDispChild(tree->AsConditional()->gtOp1, indentStack, IIArc, childMsg, topOnly);
gtDispChild(tree->AsConditional()->gtOp2, indentStack, IIArcBottom, childMsg, topOnly);
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,9 @@ class GenTreeUseEdgeIterator final
void AdvanceFieldList();
void AdvancePhi();
void AdvanceConditional();
#ifdef TARGET_WASM
void AdvanceWasmSelect();
#endif

template <bool ReverseOperands>
void AdvanceBinOp();
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/gtlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ GTNODE(SWIFT_ERROR_RET , GenTreeOp ,0,1,GTK_BINOP|GTK_NOVALUE) // Retu
// Wasm specific nodes:
//-----------------------------------------------------------------------------

// Conditional select with operands in Wasm stack order: true value, false value, condition.
GTNODE(WASM_SELECT , GenTreeConditional ,0,0,GTK_SPECIAL|DBK_NOTHIR)
GTNODE(WASM_JEXCEPT , GenTree ,0,0,GTK_LEAF|GTK_NOVALUE|DBK_NOTHIR) // Special jump for Wasm exception handling
GTNODE(WASM_THROW_REF , GenTree ,0,0,GTK_LEAF|GTK_NOVALUE|DBK_NOTHIR) // Wasm rethrow host exception (exception is an implicit operand)

Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/gtstructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ GTSTRUCT_1(CmpXchg , GT_CMPXCHG)
#ifdef TARGET_ARM64
GTSTRUCT_N(Conditional , GT_SELECT, GT_SELECT_INC, GT_SELECT_INV, GT_SELECT_NEG)
GTSTRUCT_1(Bfm , GT_BFX)
#elif defined(TARGET_WASM)
GTSTRUCT_N(Conditional , GT_SELECT, GT_WASM_SELECT)
#else
GTSTRUCT_N(Conditional , GT_SELECT)
#endif //TARGET_ARM64
Expand Down
11 changes: 9 additions & 2 deletions src/coreclr/jit/ifconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,13 @@ bool OptIfConversionDsc::optIfConvert(int* pReachabilityBudget)

if (select->OperIs(GT_SELECT))
{
#ifndef TARGET_WASM
if (varTypeIsFloating(select))
{
JITDUMP("Abort: SELECT of type float\n");
return true;
}
#endif

#ifdef TARGET_RISCV64
// Without Zicond, riscv64 has no native lowering for GT_SELECT - the
Expand Down Expand Up @@ -673,7 +675,7 @@ bool OptIfConversionDsc::optIfConvert(int* pReachabilityBudget)
JITDUMP("Skipping if-conversion: branchy form fits in ~2 insns; Zicond would need 5+\n");
return true;
}
#elif !defined(TARGET_64BIT)
#elif !defined(TARGET_64BIT) && !defined(TARGET_WASM)
// Disallow 64-bit operands on 32-bit targets as the backend currently cannot
// handle contained relops efficiently after decomposition.
if (varTypeIsLong(select))
Expand All @@ -683,6 +685,11 @@ bool OptIfConversionDsc::optIfConvert(int* pReachabilityBudget)
}
#endif
}
else
{
// The comparison may now produce a value that must be normalized to 0 or 1.
m_cond->gtFlags &= ~GTF_RELOP_JMP_USED;
}

// Use the SELECT as the source of the Then STORE/RETURN.
m_thenOperation.node->AddAllEffectsFlags(select);
Expand Down Expand Up @@ -1038,7 +1045,7 @@ PhaseStatus Compiler::optIfConversion()
assert(!fgSsaValid);
optReachableBitVecTraits = nullptr;

#if defined(TARGET_ARM64) || defined(TARGET_XARCH) || defined(TARGET_RISCV64)
#if defined(TARGET_ARM64) || defined(TARGET_XARCH) || defined(TARGET_RISCV64) || defined(TARGET_WASM)
// Reverse iterate through the blocks.
BasicBlock* block = fgLastBB;

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/instrswasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ INST(deferred_call, "deferred.call", 0, IF_DEFERRED_CALL, 0x10)
INST(deferred_return_call, "deferred.return_call", 0, IF_DEFERRED_CALL, 0x12)

INST(drop, "drop", 0, IF_OPCODE, 0x1A)
INST(select, "select", 0, IF_OPCODE, 0x1B)
INST(try_table, "try_table", 0, IF_TRY_TABLE, 0x1F)

INST(local_get, "local.get", 0, IF_ULEB128, 0x20)
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4827,6 +4827,10 @@ GenTree* Lowering::LowerSelect(GenTreeConditional* select)
DISPTREERANGE(BlockRange(), select);
JITDUMP("\n");

#ifdef TARGET_WASM
TryMoveWasmSelectCondition(select);
#endif

// Do not transform GT_SELECT with GTF_SET_FLAGS into GT_SELECTCC; this
// node is used by decomposition on x86.
// TODO-CQ: If we allowed multiple nodes to consume the same CPU flags then
Expand Down Expand Up @@ -4860,6 +4864,11 @@ GenTree* Lowering::LowerSelect(GenTreeConditional* select)
}
#endif

#ifdef TARGET_WASM
assert(newSelect == nullptr);
select->SetOper(GT_WASM_SELECT);
#endif

return newSelect != nullptr ? newSelect->gtNext : select->gtNext;
}

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Lowering final : public Phase
#ifdef TARGET_WASM
void AddWasmPortableEntryPointArg(GenTreeCall* call, GenTree* value);
void LowerPEPCall(GenTreeCall* call);
void TryMoveWasmSelectCondition(GenTreeConditional* select);
#endif
void MovePutArgNodesUpToCall(GenTreeCall* call);
void MovePutArgUpToCall(GenTreeCall* call, GenTree* node);
Expand Down
37 changes: 37 additions & 0 deletions src/coreclr/jit/lowerwasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,43 @@ void Lowering::ContainCheckSelect(GenTreeOp* node)
{
}

//------------------------------------------------------------------------
// TryMoveWasmSelectCondition: Move the condition tree to the top of the
// Wasm value stack when it can be evaluated after both value operands.
//
void Lowering::TryMoveWasmSelectCondition(GenTreeConditional* select)
{
GenTree* condition = select->gtCond;
if (condition->gtNext == select)
{
return;
}

bool isClosed;
LIR::ReadOnlyRange range = BlockRange().GetTreeRange(condition, &isClosed);
if (!isClosed)
{
return;
}

for (GenTree* node : range)
{
if ((node->gtLIRFlags & LIR::Flags::MultiplyUsed) != 0)
{
return;
}
}

if (!IsRangeInvariantInRange(range.FirstNode(), range.LastNode(), select, nullptr))
{
return;
}

JITDUMP("Moving select condition to Wasm stack order\n");
LIR::Range conditionRange = BlockRange().Remove(std::move(range));
BlockRange().InsertBefore(select, std::move(conditionRange));
}

//------------------------------------------------------------------------
// AfterLowerBlocks: stackify the nodes in all blocks.
//
Expand Down
Loading
Loading