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 TreeNodeInfoInitStoreLoc(tree->AsLclVarCommon());
253 // A GT_NOP is either a passthrough (if it is void, or if it has
254 // a child), but must be considered to produce a dummy value if it
255 // has a type but no child
257 if (tree->TypeGet() != TYP_VOID && tree->gtOp.gtOp1 == nullptr)
269 // TODO-ARM: Implement other type of intrinsics (round, sqrt and etc.)
270 // Both operand and its result must be of the same floating point type.
271 op1 = tree->gtOp.gtOp1;
272 assert(varTypeIsFloating(op1));
273 assert(op1->TypeGet() == tree->TypeGet());
275 switch (tree->gtIntrinsic.gtIntrinsicId)
277 case CORINFO_INTRINSIC_Abs:
278 case CORINFO_INTRINSIC_Sqrt:
283 NYI_ARM("Lowering::TreeNodeInfoInit for GT_INTRINSIC");
294 // Non-overflow casts to/from float/double are done using SSE2 instructions
295 // and that allow the source operand to be either a reg or memop. Given the
296 // fact that casts from small int to float/double are done as two-level casts,
297 // the source operand is always guaranteed to be of size 4 or 8 bytes.
298 var_types castToType = tree->CastToType();
299 GenTreePtr castOp = tree->gtCast.CastOp();
300 var_types castOpType = castOp->TypeGet();
301 if (tree->gtFlags & GTF_UNSIGNED)
303 castOpType = genUnsignedType(castOpType);
306 if (!tree->gtOverflow() && (varTypeIsFloating(castToType) || varTypeIsFloating(castOpType)))
308 // If converting to float/double, the operand must be 4 or 8 byte in size.
309 if (varTypeIsFloating(castToType))
311 unsigned opSize = genTypeSize(castOpType);
312 assert(opSize == 4 || opSize == 8);
317 if (varTypeIsLong(castOpType))
319 noway_assert(castOp->OperGet() == GT_LONG);
320 castOp->SetContained();
324 // FloatToIntCast needs a temporary register
325 if (varTypeIsFloating(castOpType) && varTypeIsIntOrI(tree))
327 info->setInternalCandidates(m_lsra, RBM_ALLFLOAT);
328 info->internalFloatCount = 1;
329 info->isInternalRegDelayFree = true;
334 // Get information about the cast.
335 getCastDescription(tree, &castInfo);
337 if (castInfo.requiresOverflowCheck)
339 var_types srcType = castOp->TypeGet();
340 emitAttr cmpSize = EA_ATTR(genTypeSize(srcType));
342 // If we cannot store data in an immediate for instructions,
343 // then we will need to reserve a temporary register.
345 if (!castInfo.signCheckOnly) // In case of only sign check, temp regs are not needeed.
347 if (castInfo.unsignedSource || castInfo.unsignedDest)
350 bool canStoreTypeMask = emitter::emitIns_valid_imm_for_alu(castInfo.typeMask);
351 if (!canStoreTypeMask)
353 info->internalIntCount = 1;
358 // For comparing against the max or min value
359 bool canStoreMaxValue =
360 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMax, INS_FLAGS_DONT_CARE);
361 bool canStoreMinValue =
362 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMin, INS_FLAGS_DONT_CARE);
364 if (!canStoreMaxValue || !canStoreMinValue)
366 info->internalIntCount = 1;
377 l->clearDstCount(tree->gtOp.gtOp1);
386 // This should never occur since switch nodes must not be visible at this
389 info->dstCount = 0; // To avoid getting uninit errors.
390 noway_assert(!"Switch must be lowered at this point");
398 case GT_SWITCH_TABLE:
406 noway_assert(!"We should never hit any assignment operator in lowering");
417 if (varTypeIsFloating(tree->TypeGet()))
419 // overflow operations aren't supported on float/double types.
420 assert(!tree->gtOverflow());
422 // No implicit conversions at this stage as the expectation is that
423 // everything is made explicit by adding casts.
424 assert(tree->gtOp.gtOp1->TypeGet() == tree->gtOp.gtOp2->TypeGet());
439 // Check and make op2 contained (if it is a containable immediate)
440 CheckImmedAndMakeContained(tree, tree->gtOp.gtOp2);
444 // this just turns into a compare of its child with an int
445 // + a conditional call
451 if (tree->gtOverflow())
453 // Need a register different from target reg to check for overflow.
454 info->internalIntCount = 1;
455 info->isInternalRegDelayFree = true;
484 if ((tree->gtLIRFlags & LIR::Flags::IsUnusedValue) != 0)
486 // An unused GT_LONG node needs to consume its sources.
501 if (tree->TypeGet() == TYP_FLOAT)
503 // An int register for float constant
504 info->internalIntCount = 1;
509 assert(tree->TypeGet() == TYP_DOUBLE);
511 // Two int registers for double constant
512 info->internalIntCount = 2;
517 TreeNodeInfoInitReturn(tree);
521 if (tree->TypeGet() == TYP_VOID)
528 assert(tree->TypeGet() == TYP_INT);
533 info->setSrcCandidates(l, RBM_INTRET);
534 tree->gtOp.gtOp1->gtLsraInfo.setSrcCandidates(l, RBM_INTRET);
538 case GT_ARR_BOUNDS_CHECK:
541 #endif // FEATURE_SIMD
543 // Consumes arrLen & index - has no result
550 // These must have been lowered to GT_ARR_INDEX
551 noway_assert(!"We should never see a GT_ARR_ELEM in lowering");
559 info->internalIntCount = 1;
560 info->isInternalRegDelayFree = true;
562 // For GT_ARR_INDEX, the lifetime of the arrObj must be extended because it is actually used multiple
563 // times while the result is being computed.
564 tree->AsArrIndex()->ArrObj()->gtLsraInfo.isDelayFree = true;
565 info->hasDelayFreeSrc = true;
569 // This consumes the offset, if any, the arrObj and the effective index,
570 // and produces the flattened offset for this dimension.
574 // we don't want to generate code for this
575 if (tree->gtArrOffs.gtOffset->IsIntegralConst(0))
577 MakeSrcContained(tree, tree->gtArrOffs.gtOffset);
581 // Here we simply need an internal register, which must be different
582 // from any of the operand's registers, but may be the same as targetReg.
583 info->internalIntCount = 1;
589 GenTreeAddrMode* lea = tree->AsAddrMode();
590 unsigned offset = lea->gtOffset;
592 // This LEA is instantiating an address, so we set up the srcCount and dstCount here.
604 // An internal register may be needed too; the logic here should be in sync with the
605 // genLeaInstruction()'s requirements for a such register.
606 if (lea->HasBase() && lea->HasIndex())
610 // We need a register when we have all three: base reg, index reg and a non-zero offset.
611 info->internalIntCount = 1;
614 else if (lea->HasBase())
616 if (!emitter::emitIns_valid_imm_for_add(offset, INS_FLAGS_DONT_CARE))
618 // We need a register when we have an offset that is too large to encode in the add instruction.
619 info->internalIntCount = 1;
641 TreeNodeInfoInitShiftRotate(tree);
651 TreeNodeInfoInitCmp(tree);
657 info->internalIntCount = 1;
661 TreeNodeInfoInitCall(tree->AsCall());
666 case GT_STORE_DYN_BLK:
667 LowerBlockStore(tree->AsBlk());
668 TreeNodeInfoInitBlockStore(tree->AsBlk());
672 // Always a passthrough of its child's value.
678 TreeNodeInfoInitLclHeap(tree);
685 GenTree* src = tree->gtOp.gtOp2;
687 if (compiler->codeGen->gcInfo.gcIsWriteBarrierAsgNode(tree))
689 TreeNodeInfoInitGCWriteBarrier(tree);
693 TreeNodeInfoInitIndir(tree);
700 info->isLocalDefUse = true;
701 // null check is an indirection on an addr
702 TreeNodeInfoInitIndir(tree);
708 TreeNodeInfoInitIndir(tree);
714 info->setDstCandidates(l, RBM_EXCEPTION_OBJECT);
719 // GT_CLS_VAR, by the time we reach the backend, must always
721 // It will produce a result of the type of the
722 // node, and use an internal register for the address.
725 assert((tree->gtFlags & (GTF_VAR_DEF | GTF_VAR_USEASG | GTF_VAR_USEDEF)) == 0);
726 info->internalIntCount = 1;
733 // This case currently only occurs for double types that are passed as TYP_LONG;
734 // actual long types would have been decomposed by now.
735 if (tree->TypeGet() == TYP_LONG)
744 // This case currently only occurs for double types that are passed as TYP_LONG;
745 // actual long types would have been decomposed by now.
746 if (tree->TypeGet() == TYP_LONG)
755 info->dstCount = info->srcCount;
761 _snprintf_s(message, _countof(message), _TRUNCATE, "NYI: Unimplemented node type %s",
762 GenTree::OpName(tree->OperGet()));
765 NYI_ARM("TreeNodeInfoInit default case");
768 case GT_LCL_FLD_ADDR:
770 case GT_LCL_VAR_ADDR:
772 case GT_CLS_VAR_ADDR:
777 case GT_PINVOKE_PROLOG:
780 case GT_MEMORYBARRIER:
782 case GT_PUTARG_SPLIT:
783 info->dstCount = tree->IsValue() ? 1 : 0;
784 if (kind & (GTK_CONST | GTK_LEAF))
788 else if (kind & (GTK_SMPOP))
790 if (tree->gtGetOp2IfPresent() != nullptr)
804 } // end switch (tree->OperGet())
806 // We need to be sure that we've set info->srcCount and info->dstCount appropriately
807 assert((info->dstCount < 2) || tree->IsMultiRegNode());
810 #endif // _TARGET_ARM_
812 #endif // !LEGACY_BACKEND