1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 XX Register Requirements for ARM XX
10 XX This encapsulates all the logic for setting register requirements for XX
11 XX the ARM architecture. XX
14 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
23 #ifndef LEGACY_BACKEND // This file is ONLY used for the RyuJIT backend that uses the linear scan register allocator
28 #include "sideeffects.h"
32 //------------------------------------------------------------------------
33 // TreeNodeInfoInitReturn: Set the NodeInfo for a GT_RETURN.
36 // tree - The node of interest
41 void Lowering::TreeNodeInfoInitReturn(GenTree* tree)
43 TreeNodeInfo* info = &(tree->gtLsraInfo);
44 LinearScan* l = m_lsra;
45 Compiler* compiler = comp;
47 if (tree->TypeGet() == TYP_LONG)
49 GenTree* op1 = tree->gtGetOp1();
50 noway_assert(op1->OperGet() == GT_LONG);
52 GenTree* loVal = op1->gtGetOp1();
53 GenTree* hiVal = op1->gtGetOp2();
55 loVal->gtLsraInfo.setSrcCandidates(l, RBM_LNGRET_LO);
56 hiVal->gtLsraInfo.setSrcCandidates(l, RBM_LNGRET_HI);
61 GenTree* op1 = tree->gtGetOp1();
62 regMaskTP useCandidates = RBM_NONE;
64 info->srcCount = (tree->TypeGet() == TYP_VOID) ? 0 : 1;
67 if (varTypeIsStruct(tree))
69 // op1 has to be either an lclvar or a multi-reg returning call
70 if (op1->OperGet() == GT_LCL_VAR)
72 GenTreeLclVarCommon* lclVarCommon = op1->AsLclVarCommon();
73 LclVarDsc* varDsc = &(compiler->lvaTable[lclVarCommon->gtLclNum]);
74 assert(varDsc->lvIsMultiRegRet);
76 // Mark var as contained if not enregistrable.
77 if (!varTypeIsEnregisterableStruct(op1))
79 MakeSrcContained(tree, op1);
84 noway_assert(op1->IsMultiRegCall());
86 ReturnTypeDesc* retTypeDesc = op1->AsCall()->GetReturnTypeDesc();
87 info->srcCount = retTypeDesc->GetReturnRegCount();
88 useCandidates = retTypeDesc->GetABIReturnRegs();
93 // Non-struct type return - determine useCandidates
94 switch (tree->TypeGet())
97 useCandidates = RBM_NONE;
100 useCandidates = RBM_FLOATRET;
103 useCandidates = RBM_DOUBLERET;
106 useCandidates = RBM_LNGRET;
109 useCandidates = RBM_INTRET;
114 if (useCandidates != RBM_NONE)
116 tree->gtOp.gtOp1->gtLsraInfo.setSrcCandidates(l, useCandidates);
121 void Lowering::TreeNodeInfoInitLclHeap(GenTree* tree)
123 TreeNodeInfo* info = &(tree->gtLsraInfo);
124 LinearScan* l = m_lsra;
125 Compiler* compiler = comp;
130 // Need a variable number of temp regs (see genLclHeap() in codegenarm.cpp):
131 // Here '-' means don't care.
133 // Size? Init Memory? # temp regs
135 // const and <=4 str instr - hasPspSym ? 1 : 0
136 // const and <PageSize No hasPspSym ? 1 : 0
137 // >4 ptr words Yes hasPspSym ? 2 : 1
138 // Non-const Yes hasPspSym ? 2 : 1
139 // Non-const No hasPspSym ? 2 : 1
142 #if FEATURE_EH_FUNCLETS
143 hasPspSym = (compiler->lvaPSPSym != BAD_VAR_NUM);
148 GenTreePtr size = tree->gtOp.gtOp1;
149 if (size->IsCnsIntOrI())
151 MakeSrcContained(tree, size);
153 size_t sizeVal = size->gtIntCon.gtIconVal;
156 info->internalIntCount = 0;
160 sizeVal = AlignUp(sizeVal, STACK_ALIGN);
161 size_t cntStackAlignedWidthItems = (sizeVal >> STACK_ALIGN_SHIFT);
163 // For small allocations up to 4 store instructions
164 if (cntStackAlignedWidthItems <= 4)
166 info->internalIntCount = 0;
168 else if (!compiler->info.compInitMem)
170 // No need to initialize allocated stack space.
171 if (sizeVal < compiler->eeGetPageSize())
173 info->internalIntCount = 0;
177 info->internalIntCount = 1;
182 info->internalIntCount = 1;
187 info->internalIntCount++;
193 // target (regCnt) + tmp + [psp]
194 info->internalIntCount = hasPspSym ? 2 : 1;
197 // If we are needed in temporary registers we should be sure that
198 // it's different from target (regCnt)
199 if (info->internalIntCount > 0)
201 info->isInternalRegDelayFree = true;
205 //------------------------------------------------------------------------
206 // TreeNodeInfoInit: Set the register requirements for RA.
209 // Takes care of annotating the register requirements
210 // for every TreeNodeInfo struct that maps to each tree node.
213 // LSRA has been initialized and there is a TreeNodeInfo node
214 // already allocated and initialized for every tree in the IR.
217 // Every TreeNodeInfo instance has the right annotations on register
218 // requirements needed by LSRA to build the Interval Table (source,
219 // destination and internal [temp] register counts).
221 void Lowering::TreeNodeInfoInit(GenTree* tree)
223 LinearScan* l = m_lsra;
224 Compiler* compiler = comp;
226 unsigned kind = tree->OperKind();
227 TreeNodeInfo* info = &(tree->gtLsraInfo);
228 RegisterType registerType = TypeGet(tree);
230 JITDUMP("TreeNodeInfoInit for: ");
233 switch (tree->OperGet())
238 case GT_STORE_LCL_FLD:
239 case GT_STORE_LCL_VAR:
240 if (varTypeIsLong(tree->gtGetOp1()))
249 LowerStoreLoc(tree->AsLclVarCommon());
250 TreeNodeInfoInitStoreLoc(tree->AsLclVarCommon());
254 // A GT_NOP is either a passthrough (if it is void, or if it has
255 // a child), but must be considered to produce a dummy value if it
256 // has a type but no child
258 if (tree->TypeGet() != TYP_VOID && tree->gtOp.gtOp1 == nullptr)
270 // TODO-ARM: Implement other type of intrinsics (round, sqrt and etc.)
271 // Both operand and its result must be of the same floating point type.
272 op1 = tree->gtOp.gtOp1;
273 assert(varTypeIsFloating(op1));
274 assert(op1->TypeGet() == tree->TypeGet());
276 switch (tree->gtIntrinsic.gtIntrinsicId)
278 case CORINFO_INTRINSIC_Abs:
279 case CORINFO_INTRINSIC_Sqrt:
284 NYI_ARM("Lowering::TreeNodeInfoInit for GT_INTRINSIC");
295 // Non-overflow casts to/from float/double are done using SSE2 instructions
296 // and that allow the source operand to be either a reg or memop. Given the
297 // fact that casts from small int to float/double are done as two-level casts,
298 // the source operand is always guaranteed to be of size 4 or 8 bytes.
299 var_types castToType = tree->CastToType();
300 GenTreePtr castOp = tree->gtCast.CastOp();
301 var_types castOpType = castOp->TypeGet();
302 if (tree->gtFlags & GTF_UNSIGNED)
304 castOpType = genUnsignedType(castOpType);
307 if (!tree->gtOverflow() && (varTypeIsFloating(castToType) || varTypeIsFloating(castOpType)))
309 // If converting to float/double, the operand must be 4 or 8 byte in size.
310 if (varTypeIsFloating(castToType))
312 unsigned opSize = genTypeSize(castOpType);
313 assert(opSize == 4 || opSize == 8);
318 if (varTypeIsLong(castOpType))
320 noway_assert(castOp->OperGet() == GT_LONG);
321 castOp->SetContained();
325 // FloatToIntCast needs a temporary register
326 if (varTypeIsFloating(castOpType) && varTypeIsIntOrI(tree))
328 info->setInternalCandidates(m_lsra, RBM_ALLFLOAT);
329 info->internalFloatCount = 1;
330 info->isInternalRegDelayFree = true;
335 // Get information about the cast.
336 getCastDescription(tree, &castInfo);
338 if (castInfo.requiresOverflowCheck)
340 var_types srcType = castOp->TypeGet();
341 emitAttr cmpSize = EA_ATTR(genTypeSize(srcType));
343 // If we cannot store data in an immediate for instructions,
344 // then we will need to reserve a temporary register.
346 if (!castInfo.signCheckOnly) // In case of only sign check, temp regs are not needeed.
348 if (castInfo.unsignedSource || castInfo.unsignedDest)
351 bool canStoreTypeMask = emitter::emitIns_valid_imm_for_alu(castInfo.typeMask);
352 if (!canStoreTypeMask)
354 info->internalIntCount = 1;
359 // For comparing against the max or min value
360 bool canStoreMaxValue =
361 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMax, INS_FLAGS_DONT_CARE);
362 bool canStoreMinValue =
363 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMin, INS_FLAGS_DONT_CARE);
365 if (!canStoreMaxValue || !canStoreMinValue)
367 info->internalIntCount = 1;
378 l->clearDstCount(tree->gtOp.gtOp1);
387 // This should never occur since switch nodes must not be visible at this
390 info->dstCount = 0; // To avoid getting uninit errors.
391 noway_assert(!"Switch must be lowered at this point");
399 case GT_SWITCH_TABLE:
407 noway_assert(!"We should never hit any assignment operator in lowering");
418 if (varTypeIsFloating(tree->TypeGet()))
420 // overflow operations aren't supported on float/double types.
421 assert(!tree->gtOverflow());
423 // No implicit conversions at this stage as the expectation is that
424 // everything is made explicit by adding casts.
425 assert(tree->gtOp.gtOp1->TypeGet() == tree->gtOp.gtOp2->TypeGet());
440 // Check and make op2 contained (if it is a containable immediate)
441 CheckImmedAndMakeContained(tree, tree->gtOp.gtOp2);
445 // this just turns into a compare of its child with an int
446 // + a conditional call
452 if (tree->gtOverflow())
454 // Need a register different from target reg to check for overflow.
455 info->internalIntCount = 1;
456 info->isInternalRegDelayFree = true;
485 if ((tree->gtLIRFlags & LIR::Flags::IsUnusedValue) != 0)
487 // An unused GT_LONG node needs to consume its sources.
502 if (tree->TypeGet() == TYP_FLOAT)
504 // An int register for float constant
505 info->internalIntCount = 1;
510 assert(tree->TypeGet() == TYP_DOUBLE);
512 // Two int registers for double constant
513 info->internalIntCount = 2;
518 TreeNodeInfoInitReturn(tree);
522 if (tree->TypeGet() == TYP_VOID)
529 assert(tree->TypeGet() == TYP_INT);
534 info->setSrcCandidates(l, RBM_INTRET);
535 tree->gtOp.gtOp1->gtLsraInfo.setSrcCandidates(l, RBM_INTRET);
539 case GT_ARR_BOUNDS_CHECK:
542 #endif // FEATURE_SIMD
544 // Consumes arrLen & index - has no result
551 // These must have been lowered to GT_ARR_INDEX
552 noway_assert(!"We should never see a GT_ARR_ELEM in lowering");
560 info->internalIntCount = 1;
561 info->isInternalRegDelayFree = true;
563 // For GT_ARR_INDEX, the lifetime of the arrObj must be extended because it is actually used multiple
564 // times while the result is being computed.
565 tree->AsArrIndex()->ArrObj()->gtLsraInfo.isDelayFree = true;
566 info->hasDelayFreeSrc = true;
570 // This consumes the offset, if any, the arrObj and the effective index,
571 // and produces the flattened offset for this dimension.
575 // we don't want to generate code for this
576 if (tree->gtArrOffs.gtOffset->IsIntegralConst(0))
578 MakeSrcContained(tree, tree->gtArrOffs.gtOffset);
582 // Here we simply need an internal register, which must be different
583 // from any of the operand's registers, but may be the same as targetReg.
584 info->internalIntCount = 1;
590 GenTreeAddrMode* lea = tree->AsAddrMode();
591 unsigned offset = lea->gtOffset;
593 // This LEA is instantiating an address, so we set up the srcCount and dstCount here.
605 // An internal register may be needed too; the logic here should be in sync with the
606 // genLeaInstruction()'s requirements for a such register.
607 if (lea->HasBase() && lea->HasIndex())
611 // We need a register when we have all three: base reg, index reg and a non-zero offset.
612 info->internalIntCount = 1;
615 else if (lea->HasBase())
617 if (!emitter::emitIns_valid_imm_for_add(offset, INS_FLAGS_DONT_CARE))
619 // We need a register when we have an offset that is too large to encode in the add instruction.
620 info->internalIntCount = 1;
642 TreeNodeInfoInitShiftRotate(tree);
652 TreeNodeInfoInitCmp(tree);
658 info->internalIntCount = 1;
662 TreeNodeInfoInitCall(tree->AsCall());
667 case GT_STORE_DYN_BLK:
668 LowerBlockStore(tree->AsBlk());
669 TreeNodeInfoInitBlockStore(tree->AsBlk());
673 // Always a passthrough of its child's value.
679 TreeNodeInfoInitLclHeap(tree);
686 GenTree* src = tree->gtOp.gtOp2;
688 if (compiler->codeGen->gcInfo.gcIsWriteBarrierAsgNode(tree))
690 TreeNodeInfoInitGCWriteBarrier(tree);
694 TreeNodeInfoInitIndir(tree);
701 info->isLocalDefUse = true;
702 // null check is an indirection on an addr
703 TreeNodeInfoInitIndir(tree);
709 TreeNodeInfoInitIndir(tree);
715 info->setDstCandidates(l, RBM_EXCEPTION_OBJECT);
720 // GT_CLS_VAR, by the time we reach the backend, must always
722 // It will produce a result of the type of the
723 // node, and use an internal register for the address.
726 assert((tree->gtFlags & (GTF_VAR_DEF | GTF_VAR_USEASG | GTF_VAR_USEDEF)) == 0);
727 info->internalIntCount = 1;
734 // This case currently only occurs for double types that are passed as TYP_LONG;
735 // actual long types would have been decomposed by now.
736 if (tree->TypeGet() == TYP_LONG)
745 // This case currently only occurs for double types that are passed as TYP_LONG;
746 // actual long types would have been decomposed by now.
747 if (tree->TypeGet() == TYP_LONG)
756 info->dstCount = info->srcCount;
762 _snprintf_s(message, _countof(message), _TRUNCATE, "NYI: Unimplemented node type %s",
763 GenTree::OpName(tree->OperGet()));
766 NYI_ARM("TreeNodeInfoInit default case");
769 case GT_LCL_FLD_ADDR:
771 case GT_LCL_VAR_ADDR:
773 case GT_CLS_VAR_ADDR:
778 case GT_PINVOKE_PROLOG:
781 case GT_MEMORYBARRIER:
783 case GT_PUTARG_SPLIT:
784 info->dstCount = tree->IsValue() ? 1 : 0;
785 if (kind & (GTK_CONST | GTK_LEAF))
789 else if (kind & (GTK_SMPOP))
791 if (tree->gtGetOp2IfPresent() != nullptr)
805 } // end switch (tree->OperGet())
807 // We need to be sure that we've set info->srcCount and info->dstCount appropriately
808 assert((info->dstCount < 2) || tree->IsMultiRegNode());
811 #endif // _TARGET_ARM_
813 #endif // !LEGACY_BACKEND