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;
46 GenTree* op1 = tree->gtGetOp1();
48 assert(info->dstCount == 0);
49 if (tree->TypeGet() == TYP_LONG)
51 assert((op1->OperGet() == GT_LONG) && op1->isContained());
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);
60 regMaskTP useCandidates = RBM_NONE;
62 info->srcCount = ((tree->TypeGet() == TYP_VOID) || op1->isContained()) ? 0 : 1;
64 if (varTypeIsStruct(tree))
66 // op1 has to be either an lclvar or a multi-reg returning call
67 if (op1->OperGet() != GT_LCL_VAR)
69 noway_assert(op1->IsMultiRegCall());
71 ReturnTypeDesc* retTypeDesc = op1->AsCall()->GetReturnTypeDesc();
72 info->srcCount = retTypeDesc->GetReturnRegCount();
73 useCandidates = retTypeDesc->GetABIReturnRegs();
78 // Non-struct type return - determine useCandidates
79 switch (tree->TypeGet())
82 useCandidates = RBM_NONE;
85 useCandidates = RBM_FLOATRET;
88 useCandidates = RBM_DOUBLERET;
91 useCandidates = RBM_LNGRET;
94 useCandidates = RBM_INTRET;
99 if (useCandidates != RBM_NONE)
101 tree->gtOp.gtOp1->gtLsraInfo.setSrcCandidates(l, useCandidates);
106 void Lowering::TreeNodeInfoInitLclHeap(GenTree* tree)
108 TreeNodeInfo* info = &(tree->gtLsraInfo);
109 LinearScan* l = m_lsra;
110 Compiler* compiler = comp;
112 assert(info->dstCount == 1);
114 // Need a variable number of temp regs (see genLclHeap() in codegenarm.cpp):
115 // Here '-' means don't care.
117 // Size? Init Memory? # temp regs
119 // const and <=4 str instr - hasPspSym ? 1 : 0
120 // const and <PageSize No hasPspSym ? 1 : 0
121 // >4 ptr words Yes hasPspSym ? 2 : 1
122 // Non-const Yes hasPspSym ? 2 : 1
123 // Non-const No hasPspSym ? 2 : 1
126 #if FEATURE_EH_FUNCLETS
127 hasPspSym = (compiler->lvaPSPSym != BAD_VAR_NUM);
132 GenTreePtr size = tree->gtOp.gtOp1;
133 if (size->IsCnsIntOrI())
135 assert(size->isContained());
138 size_t sizeVal = size->gtIntCon.gtIconVal;
141 info->internalIntCount = 0;
145 sizeVal = AlignUp(sizeVal, STACK_ALIGN);
146 size_t cntStackAlignedWidthItems = (sizeVal >> STACK_ALIGN_SHIFT);
148 // For small allocations up to 4 store instructions
149 if (cntStackAlignedWidthItems <= 4)
151 info->internalIntCount = 0;
153 else if (!compiler->info.compInitMem)
155 // No need to initialize allocated stack space.
156 if (sizeVal < compiler->eeGetPageSize())
158 info->internalIntCount = 0;
162 info->internalIntCount = 1;
167 info->internalIntCount = 1;
172 info->internalIntCount++;
178 // target (regCnt) + tmp + [psp]
180 info->internalIntCount = hasPspSym ? 2 : 1;
183 // If we are needed in temporary registers we should be sure that
184 // it's different from target (regCnt)
185 if (info->internalIntCount > 0)
187 info->isInternalRegDelayFree = true;
191 //------------------------------------------------------------------------
192 // TreeNodeInfoInit: Set the register requirements for RA.
195 // Takes care of annotating the register requirements
196 // for every TreeNodeInfo struct that maps to each tree node.
199 // LSRA has been initialized and there is a TreeNodeInfo node
200 // already allocated and initialized for every tree in the IR.
203 // Every TreeNodeInfo instance has the right annotations on register
204 // requirements needed by LSRA to build the Interval Table (source,
205 // destination and internal [temp] register counts).
207 void Lowering::TreeNodeInfoInit(GenTree* tree)
209 LinearScan* l = m_lsra;
210 Compiler* compiler = comp;
212 unsigned kind = tree->OperKind();
213 TreeNodeInfo* info = &(tree->gtLsraInfo);
214 RegisterType registerType = TypeGet(tree);
216 if (tree->isContained())
219 assert(info->srcCount == 0);
223 // Set the default dstCount. This may be modified below.
224 info->dstCount = tree->IsValue() ? 1 : 0;
226 switch (tree->OperGet())
231 case GT_STORE_LCL_FLD:
232 case GT_STORE_LCL_VAR:
233 TreeNodeInfoInitStoreLoc(tree->AsLclVarCommon());
237 // A GT_NOP is either a passthrough (if it is void, or if it has
238 // a child), but must be considered to produce a dummy value if it
239 // has a type but no child
241 if (tree->TypeGet() != TYP_VOID && tree->gtOp.gtOp1 == nullptr)
243 assert(info->dstCount == 1);
247 assert(info->dstCount == 0);
253 // TODO-ARM: Implement other type of intrinsics (round, sqrt and etc.)
254 // Both operand and its result must be of the same floating point type.
255 op1 = tree->gtOp.gtOp1;
256 assert(varTypeIsFloating(op1));
257 assert(op1->TypeGet() == tree->TypeGet());
259 switch (tree->gtIntrinsic.gtIntrinsicId)
261 case CORINFO_INTRINSIC_Abs:
262 case CORINFO_INTRINSIC_Sqrt:
264 assert(info->dstCount == 1);
267 NYI_ARM("Lowering::TreeNodeInfoInit for GT_INTRINSIC");
276 assert(info->dstCount == 1);
278 // Non-overflow casts to/from float/double are done using SSE2 instructions
279 // and that allow the source operand to be either a reg or memop. Given the
280 // fact that casts from small int to float/double are done as two-level casts,
281 // the source operand is always guaranteed to be of size 4 or 8 bytes.
282 var_types castToType = tree->CastToType();
283 GenTreePtr castOp = tree->gtCast.CastOp();
284 var_types castOpType = castOp->TypeGet();
285 if (tree->gtFlags & GTF_UNSIGNED)
287 castOpType = genUnsignedType(castOpType);
290 if (!tree->gtOverflow() && (varTypeIsFloating(castToType) || varTypeIsFloating(castOpType)))
292 // If converting to float/double, the operand must be 4 or 8 byte in size.
293 if (varTypeIsFloating(castToType))
295 unsigned opSize = genTypeSize(castOpType);
296 assert(opSize == 4 || opSize == 8);
301 if (varTypeIsLong(castOpType))
303 assert((castOp->OperGet() == GT_LONG) && castOp->isContained());
307 // FloatToIntCast needs a temporary register
308 if (varTypeIsFloating(castOpType) && varTypeIsIntOrI(tree))
310 info->setInternalCandidates(m_lsra, RBM_ALLFLOAT);
311 info->internalFloatCount = 1;
312 info->isInternalRegDelayFree = true;
317 // Get information about the cast.
318 getCastDescription(tree, &castInfo);
320 if (castInfo.requiresOverflowCheck)
322 var_types srcType = castOp->TypeGet();
323 emitAttr cmpSize = EA_ATTR(genTypeSize(srcType));
325 // If we cannot store data in an immediate for instructions,
326 // then we will need to reserve a temporary register.
328 if (!castInfo.signCheckOnly) // In case of only sign check, temp regs are not needeed.
330 if (castInfo.unsignedSource || castInfo.unsignedDest)
333 bool canStoreTypeMask = emitter::emitIns_valid_imm_for_alu(castInfo.typeMask);
334 if (!canStoreTypeMask)
336 info->internalIntCount = 1;
341 // For comparing against the max or min value
342 bool canStoreMaxValue =
343 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMax, INS_FLAGS_DONT_CARE);
344 bool canStoreMinValue =
345 emitter::emitIns_valid_imm_for_cmp(castInfo.typeMin, INS_FLAGS_DONT_CARE);
347 if (!canStoreMaxValue || !canStoreMinValue)
349 info->internalIntCount = 1;
359 assert(info->dstCount == 0);
364 assert(info->dstCount == 0);
368 // This should never occur since switch nodes must not be visible at this
371 noway_assert(!"Switch must be lowered at this point");
376 assert(info->dstCount == 1);
379 case GT_SWITCH_TABLE:
381 assert(info->dstCount == 0);
387 noway_assert(!"We should never hit any assignment operator in lowering");
397 if (varTypeIsFloating(tree->TypeGet()))
399 // overflow operations aren't supported on float/double types.
400 assert(!tree->gtOverflow());
402 // No implicit conversions at this stage as the expectation is that
403 // everything is made explicit by adding casts.
404 assert(tree->gtOp.gtOp1->TypeGet() == tree->gtOp.gtOp2->TypeGet());
407 assert(info->dstCount == 1);
417 info->srcCount = tree->gtOp.gtOp2->isContained() ? 1 : 2;
418 assert(info->dstCount == 1);
422 // this just turns into a compare of its child with an int
423 // + a conditional call
425 assert(info->dstCount == 0);
429 if (tree->gtOverflow())
431 // Need a register different from target reg to check for overflow.
432 info->internalIntCount = 1;
433 info->isInternalRegDelayFree = true;
442 assert(info->dstCount == 1);
458 assert(info->dstCount == 0);
462 if (tree->IsUnusedValue())
464 // An unused GT_LONG node needs to consume its sources.
473 assert(info->dstCount == 0);
478 assert(info->dstCount == 1);
479 if (tree->TypeGet() == TYP_FLOAT)
481 // An int register for float constant
482 info->internalIntCount = 1;
487 assert(tree->TypeGet() == TYP_DOUBLE);
489 // Two int registers for double constant
490 info->internalIntCount = 2;
495 TreeNodeInfoInitReturn(tree);
499 assert(info->dstCount == 0);
500 if (tree->TypeGet() == TYP_VOID)
506 assert(tree->TypeGet() == TYP_INT);
509 info->setSrcCandidates(l, RBM_INTRET);
510 tree->gtOp.gtOp1->gtLsraInfo.setSrcCandidates(l, RBM_INTRET);
514 case GT_ARR_BOUNDS_CHECK:
517 #endif // FEATURE_SIMD
519 // Consumes arrLen & index - has no result
521 assert(info->dstCount == 0);
526 // These must have been lowered to GT_ARR_INDEX
527 noway_assert(!"We should never see a GT_ARR_ELEM in lowering");
529 assert(info->dstCount == 0);
534 assert(info->dstCount == 1);
535 info->internalIntCount = 1;
536 info->isInternalRegDelayFree = true;
538 // For GT_ARR_INDEX, the lifetime of the arrObj must be extended because it is actually used multiple
539 // times while the result is being computed.
540 tree->AsArrIndex()->ArrObj()->gtLsraInfo.isDelayFree = true;
541 info->hasDelayFreeSrc = true;
545 // This consumes the offset, if any, the arrObj and the effective index,
546 // and produces the flattened offset for this dimension.
547 assert(info->dstCount == 1);
549 if (tree->gtArrOffs.gtOffset->isContained())
555 // Here we simply need an internal register, which must be different
556 // from any of the operand's registers, but may be the same as targetReg.
557 info->internalIntCount = 1;
564 GenTreeAddrMode* lea = tree->AsAddrMode();
565 int offset = lea->Offset();
567 // This LEA is instantiating an address, so we set up the srcCount and dstCount here.
577 assert(info->dstCount == 1);
579 // An internal register may be needed too; the logic here should be in sync with the
580 // genLeaInstruction()'s requirements for a such register.
581 if (lea->HasBase() && lea->HasIndex())
585 // We need a register when we have all three: base reg, index reg and a non-zero offset.
586 info->internalIntCount = 1;
589 else if (lea->HasBase())
591 if (!emitter::emitIns_valid_imm_for_add(offset, INS_FLAGS_DONT_CARE))
593 // We need a register when we have an offset that is too large to encode in the add instruction.
594 info->internalIntCount = 1;
602 assert(info->dstCount == 1);
607 assert(info->dstCount == 1);
616 TreeNodeInfoInitShiftRotate(tree);
626 TreeNodeInfoInitCmp(tree);
631 assert(info->dstCount == 1);
632 info->internalIntCount = 1;
636 TreeNodeInfoInitCall(tree->AsCall());
641 case GT_STORE_DYN_BLK:
642 LowerBlockStore(tree->AsBlk());
643 TreeNodeInfoInitBlockStore(tree->AsBlk());
647 // Always a passthrough of its child's value.
648 assert(!"INIT_VAL should always be contained");
652 TreeNodeInfoInitLclHeap(tree);
657 assert(info->dstCount == 0);
658 GenTree* src = tree->gtOp.gtOp2;
660 if (compiler->codeGen->gcInfo.gcIsWriteBarrierAsgNode(tree))
662 TreeNodeInfoInitGCWriteBarrier(tree);
666 TreeNodeInfoInitIndir(tree->AsIndir());
667 // No contained source on ARM.
668 assert(!src->isContained());
674 assert(info->dstCount == 0);
676 info->isLocalDefUse = true;
677 // null check is an indirection on an addr
678 TreeNodeInfoInitIndir(tree->AsIndir());
682 assert(info->dstCount == 1);
684 TreeNodeInfoInitIndir(tree->AsIndir());
689 assert(info->dstCount == 1);
690 info->setDstCandidates(l, RBM_EXCEPTION_OBJECT);
695 // GT_CLS_VAR, by the time we reach the backend, must always
697 // It will produce a result of the type of the
698 // node, and use an internal register for the address.
700 assert(info->dstCount == 1);
701 assert((tree->gtFlags & (GTF_VAR_DEF | GTF_VAR_USEASG)) == 0);
702 info->internalIntCount = 1;
708 // This case currently only occurs for double types that are passed as TYP_LONG;
709 // actual long types would have been decomposed by now.
710 if (tree->TypeGet() == TYP_LONG)
717 assert(info->dstCount == 1);
721 case GT_PUTARG_SPLIT:
722 TreeNodeInfoInitPutArgSplit(tree->AsPutArgSplit());
726 TreeNodeInfoInitPutArgStk(tree->AsPutArgStk());
730 TreeNodeInfoInitPutArgReg(tree->AsUnOp());
736 _snprintf_s(message, _countof(message), _TRUNCATE, "NYI: Unimplemented node type %s",
737 GenTree::OpName(tree->OperGet()));
740 NYI_ARM("TreeNodeInfoInit default case");
743 case GT_LCL_FLD_ADDR:
745 case GT_LCL_VAR_ADDR:
747 case GT_CLS_VAR_ADDR:
751 case GT_PINVOKE_PROLOG:
754 case GT_MEMORYBARRIER:
756 assert(info->dstCount == (tree->IsValue() ? 1 : 0));
757 if (kind & (GTK_CONST | GTK_LEAF))
761 else if (kind & (GTK_SMPOP))
763 if (tree->gtGetOp2IfPresent() != nullptr)
781 info->internalIntCount = 1;
783 } // end switch (tree->OperGet())
785 // We need to be sure that we've set info->srcCount and info->dstCount appropriately
786 assert((info->dstCount < 2) || tree->IsMultiRegNode());
789 #endif // _TARGET_ARM_
791 #endif // !LEGACY_BACKEND