2 // Licensed to the .NET Foundation under one or more agreements.
3 // The .NET Foundation licenses this file to you under the MIT license.
4 // See the LICENSE file in the project root for more information.
6 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
11 XX Imports the given method and converts it to semantic trees XX
13 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
24 #define Verify(cond, msg) \
29 verRaiseVerifyExceptionIfNeeded(INDEBUG(msg) DEBUGARG(__FILE__) DEBUGARG(__LINE__)); \
33 #define VerifyOrReturn(cond, msg) \
38 verRaiseVerifyExceptionIfNeeded(INDEBUG(msg) DEBUGARG(__FILE__) DEBUGARG(__LINE__)); \
43 #define VerifyOrReturnSpeculative(cond, msg, speculative) \
57 verRaiseVerifyExceptionIfNeeded(INDEBUG(msg) DEBUGARG(__FILE__) DEBUGARG(__LINE__)); \
63 /*****************************************************************************/
65 void Compiler::impInit()
69 impTreeList = nullptr;
70 impTreeLast = nullptr;
71 impInlinedCodeSize = 0;
75 /*****************************************************************************
77 * Pushes the given tree on the stack.
80 void Compiler::impPushOnStack(GenTreePtr tree, typeInfo ti)
82 /* Check for overflow. If inlining, we may be using a bigger stack */
84 if ((verCurrentState.esStackDepth >= info.compMaxStack) &&
85 (verCurrentState.esStackDepth >= impStkSize || ((compCurBB->bbFlags & BBF_IMPORTED) == 0)))
87 BADCODE("stack overflow");
91 // If we are pushing a struct, make certain we know the precise type!
92 if (tree->TypeGet() == TYP_STRUCT)
94 assert(ti.IsType(TI_STRUCT));
95 CORINFO_CLASS_HANDLE clsHnd = ti.GetClassHandle();
96 assert(clsHnd != NO_CLASS_HANDLE);
99 if (tiVerificationNeeded && !ti.IsDead())
101 assert(typeInfo::AreEquivalent(NormaliseForStack(ti), ti)); // types are normalized
103 // The ti type is consistent with the tree type.
106 // On 64-bit systems, nodes whose "proper" type is "native int" get labeled TYP_LONG.
107 // In the verification type system, we always transform "native int" to "TI_INT".
108 // Ideally, we would keep track of which nodes labeled "TYP_LONG" are really "native int", but
109 // attempts to do that have proved too difficult. Instead, we'll assume that in checks like this,
110 // when there's a mismatch, it's because of this reason -- the typeInfo::AreEquivalentModuloNativeInt
111 // method used in the last disjunct allows exactly this mismatch.
112 assert(ti.IsDead() || ti.IsByRef() && (tree->TypeGet() == TYP_I_IMPL || tree->TypeGet() == TYP_BYREF) ||
113 ti.IsUnboxedGenericTypeVar() && tree->TypeGet() == TYP_REF ||
114 ti.IsObjRef() && tree->TypeGet() == TYP_REF || ti.IsMethod() && tree->TypeGet() == TYP_I_IMPL ||
115 ti.IsType(TI_STRUCT) && tree->TypeGet() != TYP_REF ||
116 typeInfo::AreEquivalentModuloNativeInt(NormaliseForStack(ti),
117 NormaliseForStack(typeInfo(tree->TypeGet()))));
119 // If it is a struct type, make certain we normalized the primitive types
120 assert(!ti.IsType(TI_STRUCT) ||
121 info.compCompHnd->getTypeForPrimitiveValueClass(ti.GetClassHandle()) == CORINFO_TYPE_UNDEF);
125 if (VERBOSE && tiVerificationNeeded)
128 printf(TI_DUMP_PADDING);
129 printf("About to push to stack: ");
132 #endif // VERBOSE_VERIFY
136 verCurrentState.esStack[verCurrentState.esStackDepth].seTypeInfo = ti;
137 verCurrentState.esStack[verCurrentState.esStackDepth++].val = tree;
139 if ((tree->gtType == TYP_LONG) && (compLongUsed == false))
143 else if (((tree->gtType == TYP_FLOAT) || (tree->gtType == TYP_DOUBLE)) && (compFloatingPointUsed == false))
145 compFloatingPointUsed = true;
149 inline void Compiler::impPushNullObjRefOnStack()
151 impPushOnStack(gtNewIconNode(0, TYP_REF), typeInfo(TI_NULL));
154 // This method gets called when we run into unverifiable code
155 // (and we are verifying the method)
157 inline void Compiler::verRaiseVerifyExceptionIfNeeded(INDEBUG(const char* msg) DEBUGARG(const char* file)
158 DEBUGARG(unsigned line))
160 // Remember that the code is not verifiable
161 // Note that the method may yet pass canSkipMethodVerification(),
162 // and so the presence of unverifiable code may not be an issue.
163 tiIsVerifiableCode = FALSE;
166 const char* tail = strrchr(file, '\\');
172 if (JitConfig.JitBreakOnUnsafeCode())
174 assert(!"Unsafe code detected");
178 JITLOG((LL_INFO10000, "Detected unsafe code: %s:%d : %s, while compiling %s opcode %s, IL offset %x\n", file, line,
179 msg, info.compFullName, impCurOpcName, impCurOpcOffs));
181 if (verNeedsVerification() || compIsForImportOnly())
183 JITLOG((LL_ERROR, "Verification failure: %s:%d : %s, while compiling %s opcode %s, IL offset %x\n", file, line,
184 msg, info.compFullName, impCurOpcName, impCurOpcOffs));
185 verRaiseVerifyException(INDEBUG(msg) DEBUGARG(file) DEBUGARG(line));
189 inline void DECLSPEC_NORETURN Compiler::verRaiseVerifyException(INDEBUG(const char* msg) DEBUGARG(const char* file)
190 DEBUGARG(unsigned line))
192 JITLOG((LL_ERROR, "Verification failure: %s:%d : %s, while compiling %s opcode %s, IL offset %x\n", file, line,
193 msg, info.compFullName, impCurOpcName, impCurOpcOffs));
196 // BreakIfDebuggerPresent();
197 if (getBreakOnBadCode())
199 assert(!"Typechecking error");
203 RaiseException(SEH_VERIFICATION_EXCEPTION, EXCEPTION_NONCONTINUABLE, 0, nullptr);
207 // helper function that will tell us if the IL instruction at the addr passed
208 // by param consumes an address at the top of the stack. We use it to save
210 bool Compiler::impILConsumesAddr(const BYTE* codeAddr, CORINFO_METHOD_HANDLE fncHandle, CORINFO_MODULE_HANDLE scpHandle)
212 assert(!compIsForInlining());
216 opcode = (OPCODE)getU1LittleEndian(codeAddr);
220 // case CEE_LDFLDA: We're taking this one out as if you have a sequence
226 // of a primitivelike struct, you end up after morphing with addr of a local
227 // that's not marked as addrtaken, which is wrong. Also ldflda is usually used
228 // for structs that contain other structs, which isnt a case we handle very
229 // well now for other reasons.
233 // We won't collapse small fields. This is probably not the right place to have this
234 // check, but we're only using the function for this purpose, and is easy to factor
235 // out if we need to do so.
237 CORINFO_RESOLVED_TOKEN resolvedToken;
238 impResolveToken(codeAddr + sizeof(__int8), &resolvedToken, CORINFO_TOKENKIND_Field);
240 CORINFO_CLASS_HANDLE clsHnd;
241 var_types lclTyp = JITtype2varType(info.compCompHnd->getFieldType(resolvedToken.hField, &clsHnd));
243 // Preserve 'small' int types
244 if (!varTypeIsSmall(lclTyp))
246 lclTyp = genActualType(lclTyp);
249 if (varTypeIsSmall(lclTyp))
263 void Compiler::impResolveToken(const BYTE* addr, CORINFO_RESOLVED_TOKEN* pResolvedToken, CorInfoTokenKind kind)
265 pResolvedToken->tokenContext = impTokenLookupContextHandle;
266 pResolvedToken->tokenScope = info.compScopeHnd;
267 pResolvedToken->token = getU4LittleEndian(addr);
268 pResolvedToken->tokenType = kind;
270 if (!tiVerificationNeeded)
272 info.compCompHnd->resolveToken(pResolvedToken);
276 Verify(eeTryResolveToken(pResolvedToken), "Token resolution failed");
280 /*****************************************************************************
282 * Pop one tree from the stack.
285 StackEntry Compiler::impPopStack()
287 if (verCurrentState.esStackDepth == 0)
289 BADCODE("stack underflow");
294 if (VERBOSE && tiVerificationNeeded)
297 printf(TI_DUMP_PADDING);
298 printf("About to pop from the stack: ");
299 const typeInfo& ti = verCurrentState.esStack[verCurrentState.esStackDepth - 1].seTypeInfo;
302 #endif // VERBOSE_VERIFY
305 return verCurrentState.esStack[--verCurrentState.esStackDepth];
308 /*****************************************************************************
310 * Peep at n'th (0-based) tree on the top of the stack.
313 StackEntry& Compiler::impStackTop(unsigned n)
315 if (verCurrentState.esStackDepth <= n)
317 BADCODE("stack underflow");
320 return verCurrentState.esStack[verCurrentState.esStackDepth - n - 1];
323 unsigned Compiler::impStackHeight()
325 return verCurrentState.esStackDepth;
328 /*****************************************************************************
329 * Some of the trees are spilled specially. While unspilling them, or
330 * making a copy, these need to be handled specially. The function
331 * enumerates the operators possible after spilling.
334 #ifdef DEBUG // only used in asserts
335 static bool impValidSpilledStackEntry(GenTreePtr tree)
337 if (tree->gtOper == GT_LCL_VAR)
342 if (tree->OperIsConst())
351 /*****************************************************************************
353 * The following logic is used to save/restore stack contents.
354 * If 'copy' is true, then we make a copy of the trees on the stack. These
355 * have to all be cloneable/spilled values.
358 void Compiler::impSaveStackState(SavedStack* savePtr, bool copy)
360 savePtr->ssDepth = verCurrentState.esStackDepth;
362 if (verCurrentState.esStackDepth)
364 savePtr->ssTrees = new (this, CMK_ImpStack) StackEntry[verCurrentState.esStackDepth];
365 size_t saveSize = verCurrentState.esStackDepth * sizeof(*savePtr->ssTrees);
369 StackEntry* table = savePtr->ssTrees;
371 /* Make a fresh copy of all the stack entries */
373 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++, table++)
375 table->seTypeInfo = verCurrentState.esStack[level].seTypeInfo;
376 GenTreePtr tree = verCurrentState.esStack[level].val;
378 assert(impValidSpilledStackEntry(tree));
380 switch (tree->gtOper)
387 table->val = gtCloneExpr(tree);
391 assert(!"Bad oper - Not covered by impValidSpilledStackEntry()");
398 memcpy(savePtr->ssTrees, verCurrentState.esStack, saveSize);
403 void Compiler::impRestoreStackState(SavedStack* savePtr)
405 verCurrentState.esStackDepth = savePtr->ssDepth;
407 if (verCurrentState.esStackDepth)
409 memcpy(verCurrentState.esStack, savePtr->ssTrees,
410 verCurrentState.esStackDepth * sizeof(*verCurrentState.esStack));
414 /*****************************************************************************
416 * Get the tree list started for a new basic block.
418 inline void Compiler::impBeginTreeList()
420 assert(impTreeList == nullptr && impTreeLast == nullptr);
422 impTreeList = impTreeLast = new (this, GT_BEG_STMTS) GenTree(GT_BEG_STMTS, TYP_VOID);
425 /*****************************************************************************
427 * Store the given start and end stmt in the given basic block. This is
428 * mostly called by impEndTreeList(BasicBlock *block). It is called
429 * directly only for handling CEE_LEAVEs out of finally-protected try's.
432 inline void Compiler::impEndTreeList(BasicBlock* block, GenTreePtr firstStmt, GenTreePtr lastStmt)
434 assert(firstStmt->gtOper == GT_STMT);
435 assert(lastStmt->gtOper == GT_STMT);
437 /* Make the list circular, so that we can easily walk it backwards */
439 firstStmt->gtPrev = lastStmt;
441 /* Store the tree list in the basic block */
443 block->bbTreeList = firstStmt;
445 /* The block should not already be marked as imported */
446 assert((block->bbFlags & BBF_IMPORTED) == 0);
448 block->bbFlags |= BBF_IMPORTED;
451 /*****************************************************************************
453 * Store the current tree list in the given basic block.
456 inline void Compiler::impEndTreeList(BasicBlock* block)
458 assert(impTreeList->gtOper == GT_BEG_STMTS);
460 GenTreePtr firstTree = impTreeList->gtNext;
464 /* The block should not already be marked as imported */
465 assert((block->bbFlags & BBF_IMPORTED) == 0);
467 // Empty block. Just mark it as imported
468 block->bbFlags |= BBF_IMPORTED;
472 // Ignore the GT_BEG_STMTS
473 assert(firstTree->gtPrev == impTreeList);
475 impEndTreeList(block, firstTree, impTreeLast);
479 if (impLastILoffsStmt != nullptr)
481 impLastILoffsStmt->gtStmt.gtStmtLastILoffs = compIsForInlining() ? BAD_IL_OFFSET : impCurOpcOffs;
482 impLastILoffsStmt = nullptr;
485 impTreeList = impTreeLast = nullptr;
489 /*****************************************************************************
491 * Check that storing the given tree doesnt mess up the semantic order. Note
492 * that this has only limited value as we can only check [0..chkLevel).
495 inline void Compiler::impAppendStmtCheck(GenTreePtr stmt, unsigned chkLevel)
500 assert(stmt->gtOper == GT_STMT);
502 if (chkLevel == (unsigned)CHECK_SPILL_ALL)
504 chkLevel = verCurrentState.esStackDepth;
507 if (verCurrentState.esStackDepth == 0 || chkLevel == 0 || chkLevel == (unsigned)CHECK_SPILL_NONE)
512 GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
514 // Calls can only be appended if there are no GTF_GLOB_EFFECT on the stack
516 if (tree->gtFlags & GTF_CALL)
518 for (unsigned level = 0; level < chkLevel; level++)
520 assert((verCurrentState.esStack[level].val->gtFlags & GTF_GLOB_EFFECT) == 0);
524 if (tree->gtOper == GT_ASG)
526 // For an assignment to a local variable, all references of that
527 // variable have to be spilled. If it is aliased, all calls and
528 // indirect accesses have to be spilled
530 if (tree->gtOp.gtOp1->gtOper == GT_LCL_VAR)
532 unsigned lclNum = tree->gtOp.gtOp1->gtLclVarCommon.gtLclNum;
533 for (unsigned level = 0; level < chkLevel; level++)
535 assert(!gtHasRef(verCurrentState.esStack[level].val, lclNum, false));
536 assert(!lvaTable[lclNum].lvAddrExposed ||
537 (verCurrentState.esStack[level].val->gtFlags & GTF_SIDE_EFFECT) == 0);
541 // If the access may be to global memory, all side effects have to be spilled.
543 else if (tree->gtOp.gtOp1->gtFlags & GTF_GLOB_REF)
545 for (unsigned level = 0; level < chkLevel; level++)
547 assert((verCurrentState.esStack[level].val->gtFlags & GTF_GLOB_REF) == 0);
554 /*****************************************************************************
556 * Append the given GT_STMT node to the current block's tree list.
557 * [0..chkLevel) is the portion of the stack which we will check for
558 * interference with stmt and spill if needed.
561 inline void Compiler::impAppendStmt(GenTreePtr stmt, unsigned chkLevel)
563 assert(stmt->gtOper == GT_STMT);
564 noway_assert(impTreeLast != nullptr);
566 /* If the statement being appended has any side-effects, check the stack
567 to see if anything needs to be spilled to preserve correct ordering. */
569 GenTreePtr expr = stmt->gtStmt.gtStmtExpr;
570 unsigned flags = expr->gtFlags & GTF_GLOB_EFFECT;
572 // Assignment to (unaliased) locals don't count as a side-effect as
573 // we handle them specially using impSpillLclRefs(). Temp locals should
576 if ((expr->gtOper == GT_ASG) && (expr->gtOp.gtOp1->gtOper == GT_LCL_VAR) &&
577 !(expr->gtOp.gtOp1->gtFlags & GTF_GLOB_REF) && !gtHasLocalsWithAddrOp(expr->gtOp.gtOp2))
579 unsigned op2Flags = expr->gtOp.gtOp2->gtFlags & GTF_GLOB_EFFECT;
580 assert(flags == (op2Flags | GTF_ASG));
584 if (chkLevel == (unsigned)CHECK_SPILL_ALL)
586 chkLevel = verCurrentState.esStackDepth;
589 if (chkLevel && chkLevel != (unsigned)CHECK_SPILL_NONE)
591 assert(chkLevel <= verCurrentState.esStackDepth);
595 // If there is a call, we have to spill global refs
596 bool spillGlobEffects = (flags & GTF_CALL) ? true : false;
598 if (expr->gtOper == GT_ASG)
600 GenTree* lhs = expr->gtGetOp1();
601 // If we are assigning to a global ref, we have to spill global refs on stack.
602 // TODO-1stClassStructs: Previously, spillGlobEffects was set to true for
603 // GT_INITBLK and GT_COPYBLK, but this is overly conservative, and should be
604 // revisited. (Note that it was NOT set to true for GT_COPYOBJ.)
605 if (!expr->OperIsBlkOp())
607 // If we are assigning to a global ref, we have to spill global refs on stack
608 if ((lhs->gtFlags & GTF_GLOB_REF) != 0)
610 spillGlobEffects = true;
613 else if ((lhs->OperIsBlk() && !lhs->AsBlk()->HasGCPtr()) ||
614 ((lhs->OperGet() == GT_LCL_VAR) &&
615 (lvaTable[lhs->AsLclVarCommon()->gtLclNum].lvStructGcCount == 0)))
617 spillGlobEffects = true;
621 impSpillSideEffects(spillGlobEffects, chkLevel DEBUGARG("impAppendStmt"));
625 impSpillSpecialSideEff();
629 impAppendStmtCheck(stmt, chkLevel);
631 /* Point 'prev' at the previous node, so that we can walk backwards */
633 stmt->gtPrev = impTreeLast;
635 /* Append the expression statement to the list */
637 impTreeLast->gtNext = stmt;
641 impMarkContiguousSIMDFieldAssignments(stmt);
644 /* Once we set impCurStmtOffs in an appended tree, we are ready to
645 report the following offsets. So reset impCurStmtOffs */
647 if (impTreeLast->gtStmt.gtStmtILoffsx == impCurStmtOffs)
649 impCurStmtOffsSet(BAD_IL_OFFSET);
653 if (impLastILoffsStmt == nullptr)
655 impLastILoffsStmt = stmt;
666 /*****************************************************************************
668 * Insert the given GT_STMT "stmt" before GT_STMT "stmtBefore"
671 inline void Compiler::impInsertStmtBefore(GenTreePtr stmt, GenTreePtr stmtBefore)
673 assert(stmt->gtOper == GT_STMT);
674 assert(stmtBefore->gtOper == GT_STMT);
676 GenTreePtr stmtPrev = stmtBefore->gtPrev;
677 stmt->gtPrev = stmtPrev;
678 stmt->gtNext = stmtBefore;
679 stmtPrev->gtNext = stmt;
680 stmtBefore->gtPrev = stmt;
683 /*****************************************************************************
685 * Append the given expression tree to the current block's tree list.
686 * Return the newly created statement.
689 GenTreePtr Compiler::impAppendTree(GenTreePtr tree, unsigned chkLevel, IL_OFFSETX offset)
693 /* Allocate an 'expression statement' node */
695 GenTreePtr expr = gtNewStmt(tree, offset);
697 /* Append the statement to the current block's stmt list */
699 impAppendStmt(expr, chkLevel);
704 /*****************************************************************************
706 * Insert the given exression tree before GT_STMT "stmtBefore"
709 void Compiler::impInsertTreeBefore(GenTreePtr tree, IL_OFFSETX offset, GenTreePtr stmtBefore)
711 assert(stmtBefore->gtOper == GT_STMT);
713 /* Allocate an 'expression statement' node */
715 GenTreePtr expr = gtNewStmt(tree, offset);
717 /* Append the statement to the current block's stmt list */
719 impInsertStmtBefore(expr, stmtBefore);
722 /*****************************************************************************
724 * Append an assignment of the given value to a temp to the current tree list.
725 * curLevel is the stack level for which the spill to the temp is being done.
728 void Compiler::impAssignTempGen(unsigned tmp,
731 GenTreePtr* pAfterStmt, /* = NULL */
732 IL_OFFSETX ilOffset, /* = BAD_IL_OFFSET */
733 BasicBlock* block /* = NULL */
736 GenTreePtr asg = gtNewTempAssign(tmp, val);
738 if (!asg->IsNothingNode())
742 GenTreePtr asgStmt = gtNewStmt(asg, ilOffset);
743 *pAfterStmt = fgInsertStmtAfter(block, *pAfterStmt, asgStmt);
747 impAppendTree(asg, curLevel, impCurStmtOffs);
752 /*****************************************************************************
753 * same as above, but handle the valueclass case too
756 void Compiler::impAssignTempGen(unsigned tmpNum,
758 CORINFO_CLASS_HANDLE structType,
760 GenTreePtr* pAfterStmt, /* = NULL */
761 IL_OFFSETX ilOffset, /* = BAD_IL_OFFSET */
762 BasicBlock* block /* = NULL */
767 if (varTypeIsStruct(val))
769 assert(tmpNum < lvaCount);
770 assert(structType != NO_CLASS_HANDLE);
772 // if the method is non-verifiable the assert is not true
773 // so at least ignore it in the case when verification is turned on
774 // since any block that tries to use the temp would have failed verification.
775 var_types varType = lvaTable[tmpNum].lvType;
776 assert(tiVerificationNeeded || varType == TYP_UNDEF || varTypeIsStruct(varType));
777 lvaSetStruct(tmpNum, structType, false);
779 // Now, set the type of the struct value. Note that lvaSetStruct may modify the type
780 // of the lclVar to a specialized type (e.g. TYP_SIMD), based on the handle (structType)
781 // that has been passed in for the value being assigned to the temp, in which case we
782 // need to set 'val' to that same type.
783 // Note also that if we always normalized the types of any node that might be a struct
784 // type, this would not be necessary - but that requires additional JIT/EE interface
785 // calls that may not actually be required - e.g. if we only access a field of a struct.
787 val->gtType = lvaTable[tmpNum].lvType;
789 GenTreePtr dst = gtNewLclvNode(tmpNum, val->gtType);
790 asg = impAssignStruct(dst, val, structType, curLevel, pAfterStmt, block);
794 asg = gtNewTempAssign(tmpNum, val);
797 if (!asg->IsNothingNode())
801 GenTreePtr asgStmt = gtNewStmt(asg, ilOffset);
802 *pAfterStmt = fgInsertStmtAfter(block, *pAfterStmt, asgStmt);
806 impAppendTree(asg, curLevel, impCurStmtOffs);
811 /*****************************************************************************
813 * Pop the given number of values from the stack and return a list node with
815 * The 'prefixTree' argument may optionally contain an argument
816 * list that is prepended to the list returned from this function.
818 * The notion of prepended is a bit misleading in that the list is backwards
819 * from the way I would expect: The first element popped is at the end of
820 * the returned list, and prefixTree is 'before' that, meaning closer to
821 * the end of the list. To get to prefixTree, you have to walk to the
824 * For ARG_ORDER_R2L prefixTree is only used to insert extra arguments, as
825 * such we reverse its meaning such that returnValue has a reversed
826 * prefixTree at the head of the list.
829 GenTreeArgList* Compiler::impPopList(unsigned count, CORINFO_SIG_INFO* sig, GenTreeArgList* prefixTree)
831 assert(sig == nullptr || count == sig->numArgs);
833 CORINFO_CLASS_HANDLE structType;
834 GenTreeArgList* treeList;
836 if (Target::g_tgtArgOrder == Target::ARG_ORDER_R2L)
842 treeList = prefixTree;
847 StackEntry se = impPopStack();
848 typeInfo ti = se.seTypeInfo;
849 GenTreePtr temp = se.val;
851 if (varTypeIsStruct(temp))
853 // Morph trees that aren't already OBJs or MKREFANY to be OBJs
854 assert(ti.IsType(TI_STRUCT));
855 structType = ti.GetClassHandleForValueClass();
859 printf("Calling impNormStructVal on:\n");
863 temp = impNormStructVal(temp, structType, (unsigned)CHECK_SPILL_ALL);
867 printf("resulting tree:\n");
873 /* NOTE: we defer bashing the type for I_IMPL to fgMorphArgs */
874 treeList = gtNewListNode(temp, treeList);
879 if (sig->retTypeSigClass != nullptr && sig->retType != CORINFO_TYPE_CLASS &&
880 sig->retType != CORINFO_TYPE_BYREF && sig->retType != CORINFO_TYPE_PTR && sig->retType != CORINFO_TYPE_VAR)
882 // Make sure that all valuetypes (including enums) that we push are loaded.
883 // This is to guarantee that if a GC is triggerred from the prestub of this methods,
884 // all valuetypes in the method signature are already loaded.
885 // We need to be able to find the size of the valuetypes, but we cannot
886 // do a class-load from within GC.
887 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(sig->retTypeSigClass);
890 CORINFO_ARG_LIST_HANDLE argLst = sig->args;
891 CORINFO_CLASS_HANDLE argClass;
892 CORINFO_CLASS_HANDLE argRealClass;
893 GenTreeArgList* args;
895 for (args = treeList, count = sig->numArgs; count > 0; args = args->Rest(), count--)
897 PREFIX_ASSUME(args != nullptr);
899 CorInfoType corType = strip(info.compCompHnd->getArgType(sig, argLst, &argClass));
901 // insert implied casts (from float to double or double to float)
903 if (corType == CORINFO_TYPE_DOUBLE && args->Current()->TypeGet() == TYP_FLOAT)
905 args->Current() = gtNewCastNode(TYP_DOUBLE, args->Current(), TYP_DOUBLE);
907 else if (corType == CORINFO_TYPE_FLOAT && args->Current()->TypeGet() == TYP_DOUBLE)
909 args->Current() = gtNewCastNode(TYP_FLOAT, args->Current(), TYP_FLOAT);
912 // insert any widening or narrowing casts for backwards compatibility
914 args->Current() = impImplicitIorI4Cast(args->Current(), JITtype2varType(corType));
916 if (corType != CORINFO_TYPE_CLASS && corType != CORINFO_TYPE_BYREF && corType != CORINFO_TYPE_PTR &&
917 corType != CORINFO_TYPE_VAR && (argRealClass = info.compCompHnd->getArgClass(sig, argLst)) != nullptr)
919 // Everett MC++ could generate IL with a mismatched valuetypes. It used to work with Everett JIT,
920 // but it stopped working in Whidbey when we have started passing simple valuetypes as underlying
922 // We will try to adjust for this case here to avoid breaking customers code (see VSW 485789 for
924 if (corType == CORINFO_TYPE_VALUECLASS && !varTypeIsStruct(args->Current()))
926 args->Current() = impNormStructVal(args->Current(), argRealClass, (unsigned)CHECK_SPILL_ALL, true);
929 // Make sure that all valuetypes (including enums) that we push are loaded.
930 // This is to guarantee that if a GC is triggered from the prestub of this methods,
931 // all valuetypes in the method signature are already loaded.
932 // We need to be able to find the size of the valuetypes, but we cannot
933 // do a class-load from within GC.
934 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(argRealClass);
937 argLst = info.compCompHnd->getArgNext(argLst);
941 if (Target::g_tgtArgOrder == Target::ARG_ORDER_R2L)
943 // Prepend the prefixTree
945 // Simple in-place reversal to place treeList
946 // at the end of a reversed prefixTree
947 while (prefixTree != nullptr)
949 GenTreeArgList* next = prefixTree->Rest();
950 prefixTree->Rest() = treeList;
951 treeList = prefixTree;
958 /*****************************************************************************
960 * Pop the given number of values from the stack in reverse order (STDCALL/CDECL etc.)
961 * The first "skipReverseCount" items are not reversed.
964 GenTreeArgList* Compiler::impPopRevList(unsigned count, CORINFO_SIG_INFO* sig, unsigned skipReverseCount)
967 assert(skipReverseCount <= count);
969 GenTreeArgList* list = impPopList(count, sig);
972 if (list == nullptr || skipReverseCount == count)
977 GenTreeArgList* ptr = nullptr; // Initialized to the first node that needs to be reversed
978 GenTreeArgList* lastSkipNode = nullptr; // Will be set to the last node that does not need to be reversed
980 if (skipReverseCount == 0)
987 // Get to the first node that needs to be reversed
988 for (unsigned i = 0; i < skipReverseCount - 1; i++)
990 lastSkipNode = lastSkipNode->Rest();
993 PREFIX_ASSUME(lastSkipNode != nullptr);
994 ptr = lastSkipNode->Rest();
997 GenTreeArgList* reversedList = nullptr;
1001 GenTreeArgList* tmp = ptr->Rest();
1002 ptr->Rest() = reversedList;
1005 } while (ptr != nullptr);
1007 if (skipReverseCount)
1009 lastSkipNode->Rest() = reversedList;
1014 return reversedList;
1018 /*****************************************************************************
1019 Assign (copy) the structure from 'src' to 'dest'. The structure is a value
1020 class of type 'clsHnd'. It returns the tree that should be appended to the
1021 statement list that represents the assignment.
1022 Temp assignments may be appended to impTreeList if spilling is necessary.
1023 curLevel is the stack level for which a spill may be being done.
1026 GenTreePtr Compiler::impAssignStruct(GenTreePtr dest,
1028 CORINFO_CLASS_HANDLE structHnd,
1030 GenTreePtr* pAfterStmt, /* = NULL */
1031 BasicBlock* block /* = NULL */
1034 assert(varTypeIsStruct(dest));
1036 while (dest->gtOper == GT_COMMA)
1038 assert(varTypeIsStruct(dest->gtOp.gtOp2)); // Second thing is the struct
1040 // Append all the op1 of GT_COMMA trees before we evaluate op2 of the GT_COMMA tree.
1043 *pAfterStmt = fgInsertStmtAfter(block, *pAfterStmt, gtNewStmt(dest->gtOp.gtOp1, impCurStmtOffs));
1047 impAppendTree(dest->gtOp.gtOp1, curLevel, impCurStmtOffs); // do the side effect
1050 // set dest to the second thing
1051 dest = dest->gtOp.gtOp2;
1054 assert(dest->gtOper == GT_LCL_VAR || dest->gtOper == GT_RETURN || dest->gtOper == GT_FIELD ||
1055 dest->gtOper == GT_IND || dest->gtOper == GT_OBJ || dest->gtOper == GT_INDEX);
1057 if (dest->OperGet() == GT_LCL_VAR && src->OperGet() == GT_LCL_VAR &&
1058 src->gtLclVarCommon.gtLclNum == dest->gtLclVarCommon.gtLclNum)
1061 return gtNewNothingNode();
1064 // TODO-1stClassStructs: Avoid creating an address if it is not needed,
1065 // or re-creating a Blk node if it is.
1066 GenTreePtr destAddr;
1068 if (dest->gtOper == GT_IND || dest->OperIsBlk())
1070 destAddr = dest->gtOp.gtOp1;
1074 destAddr = gtNewOperNode(GT_ADDR, TYP_BYREF, dest);
1077 return (impAssignStructPtr(destAddr, src, structHnd, curLevel, pAfterStmt, block));
1080 /*****************************************************************************/
1082 GenTreePtr Compiler::impAssignStructPtr(GenTreePtr destAddr,
1084 CORINFO_CLASS_HANDLE structHnd,
1086 GenTreePtr* pAfterStmt, /* = NULL */
1087 BasicBlock* block /* = NULL */
1091 GenTreePtr dest = nullptr;
1092 unsigned destFlags = 0;
1094 #if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1095 assert(varTypeIsStruct(src) || (src->gtOper == GT_ADDR && src->TypeGet() == TYP_BYREF));
1096 // TODO-ARM-BUG: Does ARM need this?
1097 // TODO-ARM64-BUG: Does ARM64 need this?
1098 assert(src->gtOper == GT_LCL_VAR || src->gtOper == GT_FIELD || src->gtOper == GT_IND || src->gtOper == GT_OBJ ||
1099 src->gtOper == GT_CALL || src->gtOper == GT_MKREFANY || src->gtOper == GT_RET_EXPR ||
1100 src->gtOper == GT_COMMA || src->gtOper == GT_ADDR ||
1101 (src->TypeGet() != TYP_STRUCT &&
1102 (GenTree::OperIsSIMD(src->gtOper) || src->OperIsSimdHWIntrinsic() || src->gtOper == GT_LCL_FLD)));
1103 #else // !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1104 assert(varTypeIsStruct(src));
1106 assert(src->gtOper == GT_LCL_VAR || src->gtOper == GT_FIELD || src->gtOper == GT_IND || src->gtOper == GT_OBJ ||
1107 src->gtOper == GT_CALL || src->gtOper == GT_MKREFANY || src->gtOper == GT_RET_EXPR ||
1108 src->gtOper == GT_COMMA ||
1109 (src->TypeGet() != TYP_STRUCT &&
1110 (GenTree::OperIsSIMD(src->gtOper) || src->OperIsSimdHWIntrinsic() || src->gtOper == GT_LCL_FLD)));
1111 #endif // !defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1112 if (destAddr->OperGet() == GT_ADDR)
1114 GenTree* destNode = destAddr->gtGetOp1();
1115 // If the actual destination is a local (for non-LEGACY_BACKEND), or already a block node, or is a node that
1116 // will be morphed, don't insert an OBJ(ADDR).
1117 if (destNode->gtOper == GT_INDEX || destNode->OperIsBlk()
1118 #ifndef LEGACY_BACKEND
1119 || ((destNode->OperGet() == GT_LCL_VAR) && (destNode->TypeGet() == src->TypeGet()))
1120 #endif // !LEGACY_BACKEND
1125 destType = destNode->TypeGet();
1129 destType = src->TypeGet();
1132 var_types asgType = src->TypeGet();
1134 if (src->gtOper == GT_CALL)
1136 if (src->AsCall()->TreatAsHasRetBufArg(this))
1138 // Case of call returning a struct via hidden retbuf arg
1140 // insert the return value buffer into the argument list as first byref parameter
1141 src->gtCall.gtCallArgs = gtNewListNode(destAddr, src->gtCall.gtCallArgs);
1143 // now returns void, not a struct
1144 src->gtType = TYP_VOID;
1146 // return the morphed call node
1151 // Case of call returning a struct in one or more registers.
1153 var_types returnType = (var_types)src->gtCall.gtReturnType;
1155 // We won't use a return buffer, so change the type of src->gtType to 'returnType'
1156 src->gtType = genActualType(returnType);
1158 // First we try to change this to "LclVar/LclFld = call"
1160 if ((destAddr->gtOper == GT_ADDR) && (destAddr->gtOp.gtOp1->gtOper == GT_LCL_VAR))
1162 // If it is a multi-reg struct return, don't change the oper to GT_LCL_FLD.
1163 // That is, the IR will be of the form lclVar = call for multi-reg return
1165 GenTreePtr lcl = destAddr->gtOp.gtOp1;
1166 if (src->AsCall()->HasMultiRegRetVal())
1168 // Mark the struct LclVar as used in a MultiReg return context
1169 // which currently makes it non promotable.
1170 // TODO-1stClassStructs: Eliminate this pessimization when we can more generally
1171 // handle multireg returns.
1172 lcl->gtFlags |= GTF_DONT_CSE;
1173 lvaTable[lcl->gtLclVarCommon.gtLclNum].lvIsMultiRegRet = true;
1175 else // The call result is not a multireg return
1177 // We change this to a GT_LCL_FLD (from a GT_ADDR of a GT_LCL_VAR)
1178 lcl->ChangeOper(GT_LCL_FLD);
1179 fgLclFldAssign(lcl->gtLclVarCommon.gtLclNum);
1182 lcl->gtType = src->gtType;
1183 asgType = src->gtType;
1186 #if defined(_TARGET_ARM_)
1187 // TODO-Cleanup: This should have been taken care of in the above HasMultiRegRetVal() case,
1188 // but that method has not been updadted to include ARM.
1189 impMarkLclDstNotPromotable(lcl->gtLclVarCommon.gtLclNum, src, structHnd);
1190 lcl->gtFlags |= GTF_DONT_CSE;
1191 #elif defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
1192 // Not allowed for FEATURE_CORCLR which is the only SKU available for System V OSs.
1193 assert(!src->gtCall.IsVarargs() && "varargs not allowed for System V OSs.");
1195 // Make the struct non promotable. The eightbytes could contain multiple fields.
1196 // TODO-1stClassStructs: Eliminate this pessimization when we can more generally
1197 // handle multireg returns.
1198 // TODO-Cleanup: Why is this needed here? This seems that it will set this even for
1199 // non-multireg returns.
1200 lcl->gtFlags |= GTF_DONT_CSE;
1201 lvaTable[lcl->gtLclVarCommon.gtLclNum].lvIsMultiRegRet = true;
1204 else // we don't have a GT_ADDR of a GT_LCL_VAR
1206 // !!! The destination could be on stack. !!!
1207 // This flag will let us choose the correct write barrier.
1208 asgType = returnType;
1209 destFlags = GTF_IND_TGTANYWHERE;
1213 else if (src->gtOper == GT_RET_EXPR)
1215 GenTreeCall* call = src->gtRetExpr.gtInlineCandidate->AsCall();
1216 noway_assert(call->gtOper == GT_CALL);
1218 if (call->HasRetBufArg())
1220 // insert the return value buffer into the argument list as first byref parameter
1221 call->gtCallArgs = gtNewListNode(destAddr, call->gtCallArgs);
1223 // now returns void, not a struct
1224 src->gtType = TYP_VOID;
1225 call->gtType = TYP_VOID;
1227 // We already have appended the write to 'dest' GT_CALL's args
1228 // So now we just return an empty node (pruning the GT_RET_EXPR)
1233 // Case of inline method returning a struct in one or more registers.
1235 var_types returnType = (var_types)call->gtReturnType;
1237 // We won't need a return buffer
1238 asgType = returnType;
1239 src->gtType = genActualType(returnType);
1240 call->gtType = src->gtType;
1242 // If we've changed the type, and it no longer matches a local destination,
1243 // we must use an indirection.
1244 if ((dest != nullptr) && (dest->OperGet() == GT_LCL_VAR) && (dest->TypeGet() != asgType))
1249 // !!! The destination could be on stack. !!!
1250 // This flag will let us choose the correct write barrier.
1251 destFlags = GTF_IND_TGTANYWHERE;
1254 else if (src->OperIsBlk())
1256 asgType = impNormStructType(structHnd);
1257 if (src->gtOper == GT_OBJ)
1259 assert(src->gtObj.gtClass == structHnd);
1262 else if (src->gtOper == GT_INDEX)
1264 asgType = impNormStructType(structHnd);
1265 assert(src->gtIndex.gtStructElemClass == structHnd);
1267 else if (src->gtOper == GT_MKREFANY)
1269 // Since we are assigning the result of a GT_MKREFANY,
1270 // "destAddr" must point to a refany.
1272 GenTreePtr destAddrClone;
1274 impCloneExpr(destAddr, &destAddrClone, structHnd, curLevel, pAfterStmt DEBUGARG("MKREFANY assignment"));
1276 assert(offsetof(CORINFO_RefAny, dataPtr) == 0);
1277 assert(destAddr->gtType == TYP_I_IMPL || destAddr->gtType == TYP_BYREF);
1278 GetZeroOffsetFieldMap()->Set(destAddr, GetFieldSeqStore()->CreateSingleton(GetRefanyDataField()));
1279 GenTreePtr ptrSlot = gtNewOperNode(GT_IND, TYP_I_IMPL, destAddr);
1280 GenTreeIntCon* typeFieldOffset = gtNewIconNode(offsetof(CORINFO_RefAny, type), TYP_I_IMPL);
1281 typeFieldOffset->gtFieldSeq = GetFieldSeqStore()->CreateSingleton(GetRefanyTypeField());
1282 GenTreePtr typeSlot =
1283 gtNewOperNode(GT_IND, TYP_I_IMPL, gtNewOperNode(GT_ADD, destAddr->gtType, destAddrClone, typeFieldOffset));
1285 // append the assign of the pointer value
1286 GenTreePtr asg = gtNewAssignNode(ptrSlot, src->gtOp.gtOp1);
1289 *pAfterStmt = fgInsertStmtAfter(block, *pAfterStmt, gtNewStmt(asg, impCurStmtOffs));
1293 impAppendTree(asg, curLevel, impCurStmtOffs);
1296 // return the assign of the type value, to be appended
1297 return gtNewAssignNode(typeSlot, src->gtOp.gtOp2);
1299 else if (src->gtOper == GT_COMMA)
1301 // The second thing is the struct or its address.
1302 assert(varTypeIsStruct(src->gtOp.gtOp2) || src->gtOp.gtOp2->gtType == TYP_BYREF);
1305 *pAfterStmt = fgInsertStmtAfter(block, *pAfterStmt, gtNewStmt(src->gtOp.gtOp1, impCurStmtOffs));
1309 impAppendTree(src->gtOp.gtOp1, curLevel, impCurStmtOffs); // do the side effect
1312 // Evaluate the second thing using recursion.
1313 return impAssignStructPtr(destAddr, src->gtOp.gtOp2, structHnd, curLevel, pAfterStmt, block);
1315 else if (src->IsLocal())
1317 asgType = src->TypeGet();
1319 else if (asgType == TYP_STRUCT)
1321 asgType = impNormStructType(structHnd);
1322 src->gtType = asgType;
1323 #ifdef LEGACY_BACKEND
1324 if (asgType == TYP_STRUCT)
1326 GenTree* srcAddr = gtNewOperNode(GT_ADDR, TYP_BYREF, src);
1327 src = gtNewOperNode(GT_IND, TYP_STRUCT, srcAddr);
1331 if (dest == nullptr)
1333 // TODO-1stClassStructs: We shouldn't really need a block node as the destination
1334 // if this is a known struct type.
1335 if (asgType == TYP_STRUCT)
1337 dest = gtNewObjNode(structHnd, destAddr);
1338 gtSetObjGcInfo(dest->AsObj());
1339 // Although an obj as a call argument was always assumed to be a globRef
1340 // (which is itself overly conservative), that is not true of the operands
1341 // of a block assignment.
1342 dest->gtFlags &= ~GTF_GLOB_REF;
1343 dest->gtFlags |= (destAddr->gtFlags & GTF_GLOB_REF);
1345 else if (varTypeIsStruct(asgType))
1347 dest = new (this, GT_BLK) GenTreeBlk(GT_BLK, asgType, destAddr, genTypeSize(asgType));
1351 dest = gtNewOperNode(GT_IND, asgType, destAddr);
1356 dest->gtType = asgType;
1359 dest->gtFlags |= destFlags;
1360 destFlags = dest->gtFlags;
1362 // return an assignment node, to be appended
1363 GenTree* asgNode = gtNewAssignNode(dest, src);
1364 gtBlockOpInit(asgNode, dest, src, false);
1366 // TODO-1stClassStructs: Clean up the settings of GTF_DONT_CSE on the lhs
1368 if ((destFlags & GTF_DONT_CSE) == 0)
1370 dest->gtFlags &= ~(GTF_DONT_CSE);
1375 /*****************************************************************************
1376 Given a struct value, and the class handle for that structure, return
1377 the expression for the address for that structure value.
1379 willDeref - does the caller guarantee to dereference the pointer.
1382 GenTreePtr Compiler::impGetStructAddr(GenTreePtr structVal,
1383 CORINFO_CLASS_HANDLE structHnd,
1387 assert(varTypeIsStruct(structVal) || eeIsValueClass(structHnd));
1389 var_types type = structVal->TypeGet();
1391 genTreeOps oper = structVal->gtOper;
1393 if (oper == GT_OBJ && willDeref)
1395 assert(structVal->gtObj.gtClass == structHnd);
1396 return (structVal->gtObj.Addr());
1398 else if (oper == GT_CALL || oper == GT_RET_EXPR || oper == GT_OBJ || oper == GT_MKREFANY)
1400 unsigned tmpNum = lvaGrabTemp(true DEBUGARG("struct address for call/obj"));
1402 impAssignTempGen(tmpNum, structVal, structHnd, curLevel);
1404 // The 'return value' is now the temp itself
1406 type = genActualType(lvaTable[tmpNum].TypeGet());
1407 GenTreePtr temp = gtNewLclvNode(tmpNum, type);
1408 temp = gtNewOperNode(GT_ADDR, TYP_BYREF, temp);
1411 else if (oper == GT_COMMA)
1413 assert(structVal->gtOp.gtOp2->gtType == type); // Second thing is the struct
1415 GenTreePtr oldTreeLast = impTreeLast;
1416 structVal->gtOp.gtOp2 = impGetStructAddr(structVal->gtOp.gtOp2, structHnd, curLevel, willDeref);
1417 structVal->gtType = TYP_BYREF;
1419 if (oldTreeLast != impTreeLast)
1421 // Some temp assignment statement was placed on the statement list
1422 // for Op2, but that would be out of order with op1, so we need to
1423 // spill op1 onto the statement list after whatever was last
1424 // before we recursed on Op2 (i.e. before whatever Op2 appended).
1425 impInsertTreeBefore(structVal->gtOp.gtOp1, impCurStmtOffs, oldTreeLast->gtNext);
1426 structVal->gtOp.gtOp1 = gtNewNothingNode();
1432 return (gtNewOperNode(GT_ADDR, TYP_BYREF, structVal));
1435 //------------------------------------------------------------------------
1436 // impNormStructType: Given a (known to be) struct class handle structHnd, normalize its type,
1437 // and optionally determine the GC layout of the struct.
1440 // structHnd - The class handle for the struct type of interest.
1441 // gcLayout - (optional, default nullptr) - a BYTE pointer, allocated by the caller,
1442 // into which the gcLayout will be written.
1443 // pNumGCVars - (optional, default nullptr) - if non-null, a pointer to an unsigned,
1444 // which will be set to the number of GC fields in the struct.
1445 // pSimdBaseType - (optional, default nullptr) - if non-null, and the struct is a SIMD
1446 // type, set to the SIMD base type
1449 // The JIT type for the struct (e.g. TYP_STRUCT, or TYP_SIMD*).
1450 // The gcLayout will be returned using the pointers provided by the caller, if non-null.
1451 // It may also modify the compFloatingPointUsed flag if the type is a SIMD type.
1454 // The caller must set gcLayout to nullptr OR ensure that it is large enough
1455 // (see ICorStaticInfo::getClassGClayout in corinfo.h).
1458 // Normalizing the type involves examining the struct type to determine if it should
1459 // be modified to one that is handled specially by the JIT, possibly being a candidate
1460 // for full enregistration, e.g. TYP_SIMD16.
1462 var_types Compiler::impNormStructType(CORINFO_CLASS_HANDLE structHnd,
1464 unsigned* pNumGCVars,
1465 var_types* pSimdBaseType)
1467 assert(structHnd != NO_CLASS_HANDLE);
1469 const DWORD structFlags = info.compCompHnd->getClassAttribs(structHnd);
1470 var_types structType = TYP_STRUCT;
1472 // On coreclr the check for GC includes a "may" to account for the special
1473 // ByRef like span structs. The added check for "CONTAINS_STACK_PTR" is the particular bit.
1474 // When this is set the struct will contain a ByRef that could be a GC pointer or a native
1476 const bool mayContainGCPtrs =
1477 ((structFlags & CORINFO_FLG_CONTAINS_STACK_PTR) != 0 || ((structFlags & CORINFO_FLG_CONTAINS_GC_PTR) != 0));
1480 // Check to see if this is a SIMD type.
1481 if (featureSIMD && !mayContainGCPtrs)
1483 unsigned originalSize = info.compCompHnd->getClassSize(structHnd);
1485 if ((originalSize >= minSIMDStructBytes()) && (originalSize <= maxSIMDStructBytes()))
1487 unsigned int sizeBytes;
1488 var_types simdBaseType = getBaseTypeAndSizeOfSIMDType(structHnd, &sizeBytes);
1489 if (simdBaseType != TYP_UNKNOWN)
1491 assert(sizeBytes == originalSize);
1492 structType = getSIMDTypeForSize(sizeBytes);
1493 if (pSimdBaseType != nullptr)
1495 *pSimdBaseType = simdBaseType;
1497 // Also indicate that we use floating point registers.
1498 compFloatingPointUsed = true;
1502 #endif // FEATURE_SIMD
1504 // Fetch GC layout info if requested
1505 if (gcLayout != nullptr)
1507 unsigned numGCVars = info.compCompHnd->getClassGClayout(structHnd, gcLayout);
1509 // Verify that the quick test up above via the class attributes gave a
1510 // safe view of the type's GCness.
1512 // Note there are cases where mayContainGCPtrs is true but getClassGClayout
1513 // does not report any gc fields.
1515 assert(mayContainGCPtrs || (numGCVars == 0));
1517 if (pNumGCVars != nullptr)
1519 *pNumGCVars = numGCVars;
1524 // Can't safely ask for number of GC pointers without also
1525 // asking for layout.
1526 assert(pNumGCVars == nullptr);
1532 //****************************************************************************
1533 // Given TYP_STRUCT value 'structVal', make sure it is 'canonical', that is
1534 // it is either an OBJ or a MKREFANY node, or a node (e.g. GT_INDEX) that will be morphed.
1536 GenTreePtr Compiler::impNormStructVal(GenTreePtr structVal,
1537 CORINFO_CLASS_HANDLE structHnd,
1539 bool forceNormalization /*=false*/)
1541 assert(forceNormalization || varTypeIsStruct(structVal));
1542 assert(structHnd != NO_CLASS_HANDLE);
1543 var_types structType = structVal->TypeGet();
1544 bool makeTemp = false;
1545 if (structType == TYP_STRUCT)
1547 structType = impNormStructType(structHnd);
1549 bool alreadyNormalized = false;
1550 GenTreeLclVarCommon* structLcl = nullptr;
1552 genTreeOps oper = structVal->OperGet();
1555 // GT_RETURN and GT_MKREFANY don't capture the handle.
1559 alreadyNormalized = true;
1563 structVal->gtCall.gtRetClsHnd = structHnd;
1568 structVal->gtRetExpr.gtRetClsHnd = structHnd;
1573 structVal->gtArgPlace.gtArgPlaceClsHnd = structHnd;
1577 // This will be transformed to an OBJ later.
1578 alreadyNormalized = true;
1579 structVal->gtIndex.gtStructElemClass = structHnd;
1580 structVal->gtIndex.gtIndElemSize = info.compCompHnd->getClassSize(structHnd);
1584 // Wrap it in a GT_OBJ.
1585 structVal->gtType = structType;
1586 structVal = gtNewObjNode(structHnd, gtNewOperNode(GT_ADDR, TYP_BYREF, structVal));
1591 structLcl = structVal->AsLclVarCommon();
1592 // Wrap it in a GT_OBJ.
1593 structVal = gtNewObjNode(structHnd, gtNewOperNode(GT_ADDR, TYP_BYREF, structVal));
1600 // These should already have the appropriate type.
1601 assert(structVal->gtType == structType);
1602 alreadyNormalized = true;
1606 assert(structVal->gtType == structType);
1607 structVal = gtNewObjNode(structHnd, structVal->gtGetOp1());
1608 alreadyNormalized = true;
1613 assert(varTypeIsSIMD(structVal) && (structVal->gtType == structType));
1615 #endif // FEATURE_SIMD
1616 #if FEATURE_HW_INTRINSICS
1617 case GT_HWIntrinsic:
1618 assert(varTypeIsSIMD(structVal) && (structVal->gtType == structType));
1624 // The second thing could either be a block node or a GT_FIELD or a GT_SIMD or a GT_COMMA node.
1625 GenTree* blockNode = structVal->gtOp.gtOp2;
1626 assert(blockNode->gtType == structType);
1628 // Is this GT_COMMA(op1, GT_COMMA())?
1629 GenTree* parent = structVal;
1630 if (blockNode->OperGet() == GT_COMMA)
1632 // Find the last node in the comma chain.
1635 assert(blockNode->gtType == structType);
1637 blockNode = blockNode->gtOp.gtOp2;
1638 } while (blockNode->OperGet() == GT_COMMA);
1641 if (blockNode->OperGet() == GT_FIELD)
1643 // If we have a GT_FIELD then wrap it in a GT_OBJ.
1644 blockNode = gtNewObjNode(structHnd, gtNewOperNode(GT_ADDR, TYP_BYREF, blockNode));
1648 if (blockNode->OperGet() == GT_SIMD)
1650 parent->gtOp.gtOp2 = impNormStructVal(blockNode, structHnd, curLevel, forceNormalization);
1651 alreadyNormalized = true;
1655 #if FEATURE_HW_INTRINSICS
1656 if (blockNode->OperGet() == GT_HWIntrinsic && blockNode->AsHWIntrinsic()->isSIMD())
1658 parent->gtOp.gtOp2 = impNormStructVal(blockNode, structHnd, curLevel, forceNormalization);
1659 alreadyNormalized = true;
1664 noway_assert(blockNode->OperIsBlk());
1666 // Sink the GT_COMMA below the blockNode addr.
1667 // That is GT_COMMA(op1, op2=blockNode) is tranformed into
1668 // blockNode(GT_COMMA(TYP_BYREF, op1, op2's op1)).
1670 // In case of a chained GT_COMMA case, we sink the last
1671 // GT_COMMA below the blockNode addr.
1672 GenTree* blockNodeAddr = blockNode->gtOp.gtOp1;
1673 assert(blockNodeAddr->gtType == TYP_BYREF);
1674 GenTree* commaNode = parent;
1675 commaNode->gtType = TYP_BYREF;
1676 commaNode->gtOp.gtOp2 = blockNodeAddr;
1677 blockNode->gtOp.gtOp1 = commaNode;
1678 if (parent == structVal)
1680 structVal = blockNode;
1682 alreadyNormalized = true;
1688 noway_assert(!"Unexpected node in impNormStructVal()");
1691 structVal->gtType = structType;
1692 GenTree* structObj = structVal;
1694 if (!alreadyNormalized || forceNormalization)
1698 unsigned tmpNum = lvaGrabTemp(true DEBUGARG("struct address for call/obj"));
1700 impAssignTempGen(tmpNum, structVal, structHnd, curLevel);
1702 // The structVal is now the temp itself
1704 structLcl = gtNewLclvNode(tmpNum, structType)->AsLclVarCommon();
1705 // TODO-1stClassStructs: Avoid always wrapping in GT_OBJ.
1706 structObj = gtNewObjNode(structHnd, gtNewOperNode(GT_ADDR, TYP_BYREF, structLcl));
1708 else if (varTypeIsStruct(structType) && !structVal->OperIsBlk())
1710 // Wrap it in a GT_OBJ
1711 structObj = gtNewObjNode(structHnd, gtNewOperNode(GT_ADDR, TYP_BYREF, structVal));
1715 if (structLcl != nullptr)
1717 // A OBJ on a ADDR(LCL_VAR) can never raise an exception
1718 // so we don't set GTF_EXCEPT here.
1719 if (!lvaIsImplicitByRefLocal(structLcl->gtLclNum))
1721 structObj->gtFlags &= ~GTF_GLOB_REF;
1726 // In general a OBJ is an indirection and could raise an exception.
1727 structObj->gtFlags |= GTF_EXCEPT;
1732 /******************************************************************************/
1733 // Given a type token, generate code that will evaluate to the correct
1734 // handle representation of that token (type handle, field handle, or method handle)
1736 // For most cases, the handle is determined at compile-time, and the code
1737 // generated is simply an embedded handle.
1739 // Run-time lookup is required if the enclosing method is shared between instantiations
1740 // and the token refers to formal type parameters whose instantiation is not known
1743 GenTreePtr Compiler::impTokenToHandle(CORINFO_RESOLVED_TOKEN* pResolvedToken,
1744 BOOL* pRuntimeLookup /* = NULL */,
1745 BOOL mustRestoreHandle /* = FALSE */,
1746 BOOL importParent /* = FALSE */)
1748 assert(!fgGlobalMorph);
1750 CORINFO_GENERICHANDLE_RESULT embedInfo;
1751 info.compCompHnd->embedGenericHandle(pResolvedToken, importParent, &embedInfo);
1755 *pRuntimeLookup = embedInfo.lookup.lookupKind.needsRuntimeLookup;
1758 if (mustRestoreHandle && !embedInfo.lookup.lookupKind.needsRuntimeLookup)
1760 switch (embedInfo.handleType)
1762 case CORINFO_HANDLETYPE_CLASS:
1763 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun((CORINFO_CLASS_HANDLE)embedInfo.compileTimeHandle);
1766 case CORINFO_HANDLETYPE_METHOD:
1767 info.compCompHnd->methodMustBeLoadedBeforeCodeIsRun((CORINFO_METHOD_HANDLE)embedInfo.compileTimeHandle);
1770 case CORINFO_HANDLETYPE_FIELD:
1771 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(
1772 info.compCompHnd->getFieldClass((CORINFO_FIELD_HANDLE)embedInfo.compileTimeHandle));
1780 // Generate the full lookup tree. May be null if we're abandoning an inline attempt.
1781 GenTree* result = impLookupToTree(pResolvedToken, &embedInfo.lookup, gtTokenToIconFlags(pResolvedToken->token),
1782 embedInfo.compileTimeHandle);
1784 // If we have a result and it requires runtime lookup, wrap it in a runtime lookup node.
1785 if ((result != nullptr) && embedInfo.lookup.lookupKind.needsRuntimeLookup)
1787 result = gtNewRuntimeLookup(embedInfo.compileTimeHandle, embedInfo.handleType, result);
1793 GenTreePtr Compiler::impLookupToTree(CORINFO_RESOLVED_TOKEN* pResolvedToken,
1794 CORINFO_LOOKUP* pLookup,
1795 unsigned handleFlags,
1796 void* compileTimeHandle)
1798 if (!pLookup->lookupKind.needsRuntimeLookup)
1800 // No runtime lookup is required.
1801 // Access is direct or memory-indirect (of a fixed address) reference
1803 CORINFO_GENERIC_HANDLE handle = nullptr;
1804 void* pIndirection = nullptr;
1805 assert(pLookup->constLookup.accessType != IAT_PPVALUE);
1807 if (pLookup->constLookup.accessType == IAT_VALUE)
1809 handle = pLookup->constLookup.handle;
1811 else if (pLookup->constLookup.accessType == IAT_PVALUE)
1813 pIndirection = pLookup->constLookup.addr;
1815 return gtNewIconEmbHndNode(handle, pIndirection, handleFlags, compileTimeHandle);
1817 else if (compIsForInlining())
1819 // Don't import runtime lookups when inlining
1820 // Inlining has to be aborted in such a case
1821 compInlineResult->NoteFatal(InlineObservation::CALLSITE_GENERIC_DICTIONARY_LOOKUP);
1826 // Need to use dictionary-based access which depends on the typeContext
1827 // which is only available at runtime, not at compile-time.
1829 return impRuntimeLookupToTree(pResolvedToken, pLookup, compileTimeHandle);
1833 #ifdef FEATURE_READYTORUN_COMPILER
1834 GenTreePtr Compiler::impReadyToRunLookupToTree(CORINFO_CONST_LOOKUP* pLookup,
1835 unsigned handleFlags,
1836 void* compileTimeHandle)
1838 CORINFO_GENERIC_HANDLE handle = nullptr;
1839 void* pIndirection = nullptr;
1840 assert(pLookup->accessType != IAT_PPVALUE);
1842 if (pLookup->accessType == IAT_VALUE)
1844 handle = pLookup->handle;
1846 else if (pLookup->accessType == IAT_PVALUE)
1848 pIndirection = pLookup->addr;
1850 return gtNewIconEmbHndNode(handle, pIndirection, handleFlags, compileTimeHandle);
1853 GenTreeCall* Compiler::impReadyToRunHelperToTree(
1854 CORINFO_RESOLVED_TOKEN* pResolvedToken,
1855 CorInfoHelpFunc helper,
1857 GenTreeArgList* args /* =NULL*/,
1858 CORINFO_LOOKUP_KIND* pGenericLookupKind /* =NULL. Only used with generics */)
1860 CORINFO_CONST_LOOKUP lookup;
1861 if (!info.compCompHnd->getReadyToRunHelper(pResolvedToken, pGenericLookupKind, helper, &lookup))
1866 GenTreeCall* op1 = gtNewHelperCallNode(helper, type, args);
1868 op1->setEntryPoint(lookup);
1874 GenTreePtr Compiler::impMethodPointer(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_CALL_INFO* pCallInfo)
1876 GenTreePtr op1 = nullptr;
1878 switch (pCallInfo->kind)
1881 op1 = new (this, GT_FTN_ADDR) GenTreeFptrVal(TYP_I_IMPL, pCallInfo->hMethod);
1883 #ifdef FEATURE_READYTORUN_COMPILER
1884 if (opts.IsReadyToRun())
1886 op1->gtFptrVal.gtEntryPoint = pCallInfo->codePointerLookup.constLookup;
1890 op1->gtFptrVal.gtEntryPoint.addr = nullptr;
1895 case CORINFO_CALL_CODE_POINTER:
1896 if (compIsForInlining())
1898 // Don't import runtime lookups when inlining
1899 // Inlining has to be aborted in such a case
1900 compInlineResult->NoteFatal(InlineObservation::CALLSITE_GENERIC_DICTIONARY_LOOKUP);
1904 op1 = impLookupToTree(pResolvedToken, &pCallInfo->codePointerLookup, GTF_ICON_FTN_ADDR, pCallInfo->hMethod);
1908 noway_assert(!"unknown call kind");
1915 //------------------------------------------------------------------------
1916 // getRuntimeContextTree: find pointer to context for runtime lookup.
1919 // kind - lookup kind.
1922 // Return GenTree pointer to generic shared context.
1925 // Reports about generic context using.
1927 GenTreePtr Compiler::getRuntimeContextTree(CORINFO_RUNTIME_LOOKUP_KIND kind)
1929 GenTreePtr ctxTree = nullptr;
1931 // Collectible types requires that for shared generic code, if we use the generic context parameter
1932 // that we report it. (This is a conservative approach, we could detect some cases particularly when the
1933 // context parameter is this that we don't need the eager reporting logic.)
1934 lvaGenericsContextUseCount++;
1936 if (kind == CORINFO_LOOKUP_THISOBJ)
1939 ctxTree = gtNewLclvNode(info.compThisArg, TYP_REF);
1941 // Vtable pointer of this object
1942 ctxTree = gtNewOperNode(GT_IND, TYP_I_IMPL, ctxTree);
1943 ctxTree->gtFlags |= GTF_EXCEPT; // Null-pointer exception
1944 ctxTree->gtFlags |= GTF_IND_INVARIANT;
1948 assert(kind == CORINFO_LOOKUP_METHODPARAM || kind == CORINFO_LOOKUP_CLASSPARAM);
1950 ctxTree = gtNewLclvNode(info.compTypeCtxtArg, TYP_I_IMPL); // Exact method descriptor as passed in as last arg
1955 /*****************************************************************************/
1956 /* Import a dictionary lookup to access a handle in code shared between
1957 generic instantiations.
1958 The lookup depends on the typeContext which is only available at
1959 runtime, and not at compile-time.
1960 pLookup->token1 and pLookup->token2 specify the handle that is needed.
1963 1. pLookup->indirections == CORINFO_USEHELPER : Call a helper passing it the
1964 instantiation-specific handle, and the tokens to lookup the handle.
1965 2. pLookup->indirections != CORINFO_USEHELPER :
1966 2a. pLookup->testForNull == false : Dereference the instantiation-specific handle
1968 2b. pLookup->testForNull == true : Dereference the instantiation-specific handle.
1969 If it is non-NULL, it is the handle required. Else, call a helper
1970 to lookup the handle.
1973 GenTreePtr Compiler::impRuntimeLookupToTree(CORINFO_RESOLVED_TOKEN* pResolvedToken,
1974 CORINFO_LOOKUP* pLookup,
1975 void* compileTimeHandle)
1978 // This method can only be called from the importer instance of the Compiler.
1979 // In other word, it cannot be called by the instance of the Compiler for the inlinee.
1980 assert(!compIsForInlining());
1982 GenTreePtr ctxTree = getRuntimeContextTree(pLookup->lookupKind.runtimeLookupKind);
1984 CORINFO_RUNTIME_LOOKUP* pRuntimeLookup = &pLookup->runtimeLookup;
1985 // It's available only via the run-time helper function
1986 if (pRuntimeLookup->indirections == CORINFO_USEHELPER)
1988 #ifdef FEATURE_READYTORUN_COMPILER
1989 if (opts.IsReadyToRun())
1991 return impReadyToRunHelperToTree(pResolvedToken, CORINFO_HELP_READYTORUN_GENERIC_HANDLE, TYP_I_IMPL,
1992 gtNewArgList(ctxTree), &pLookup->lookupKind);
1996 gtNewIconEmbHndNode(pRuntimeLookup->signature, nullptr, GTF_ICON_TOKEN_HDL, compileTimeHandle);
1997 GenTreeArgList* helperArgs = gtNewArgList(ctxTree, argNode);
1999 return gtNewHelperCallNode(pRuntimeLookup->helper, TYP_I_IMPL, helperArgs);
2003 GenTreePtr slotPtrTree = ctxTree;
2005 if (pRuntimeLookup->testForNull)
2007 slotPtrTree = impCloneExpr(ctxTree, &ctxTree, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
2008 nullptr DEBUGARG("impRuntimeLookup slot"));
2011 GenTreePtr indOffTree = nullptr;
2013 // Applied repeated indirections
2014 for (WORD i = 0; i < pRuntimeLookup->indirections; i++)
2016 if ((i == 1 && pRuntimeLookup->indirectFirstOffset) || (i == 2 && pRuntimeLookup->indirectSecondOffset))
2018 indOffTree = impCloneExpr(slotPtrTree, &slotPtrTree, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
2019 nullptr DEBUGARG("impRuntimeLookup indirectOffset"));
2024 slotPtrTree = gtNewOperNode(GT_IND, TYP_I_IMPL, slotPtrTree);
2025 slotPtrTree->gtFlags |= GTF_IND_NONFAULTING;
2026 slotPtrTree->gtFlags |= GTF_IND_INVARIANT;
2029 if ((i == 1 && pRuntimeLookup->indirectFirstOffset) || (i == 2 && pRuntimeLookup->indirectSecondOffset))
2031 slotPtrTree = gtNewOperNode(GT_ADD, TYP_I_IMPL, indOffTree, slotPtrTree);
2034 if (pRuntimeLookup->offsets[i] != 0)
2037 gtNewOperNode(GT_ADD, TYP_I_IMPL, slotPtrTree, gtNewIconNode(pRuntimeLookup->offsets[i], TYP_I_IMPL));
2041 // No null test required
2042 if (!pRuntimeLookup->testForNull)
2044 if (pRuntimeLookup->indirections == 0)
2049 slotPtrTree = gtNewOperNode(GT_IND, TYP_I_IMPL, slotPtrTree);
2050 slotPtrTree->gtFlags |= GTF_IND_NONFAULTING;
2052 if (!pRuntimeLookup->testForFixup)
2057 impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("bubbling QMark0"));
2059 unsigned slotLclNum = lvaGrabTemp(true DEBUGARG("impRuntimeLookup test"));
2060 impAssignTempGen(slotLclNum, slotPtrTree, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL, nullptr, impCurStmtOffs);
2062 GenTree* slot = gtNewLclvNode(slotLclNum, TYP_I_IMPL);
2063 // downcast the pointer to a TYP_INT on 64-bit targets
2064 slot = impImplicitIorI4Cast(slot, TYP_INT);
2065 // Use a GT_AND to check for the lowest bit and indirect if it is set
2066 GenTree* test = gtNewOperNode(GT_AND, TYP_INT, slot, gtNewIconNode(1));
2067 GenTree* relop = gtNewOperNode(GT_EQ, TYP_INT, test, gtNewIconNode(0));
2068 relop->gtFlags |= GTF_RELOP_QMARK;
2070 // slot = GT_IND(slot - 1)
2071 slot = gtNewLclvNode(slotLclNum, TYP_I_IMPL);
2072 GenTree* add = gtNewOperNode(GT_ADD, TYP_I_IMPL, slot, gtNewIconNode(-1, TYP_I_IMPL));
2073 GenTree* indir = gtNewOperNode(GT_IND, TYP_I_IMPL, add);
2074 indir->gtFlags |= GTF_IND_NONFAULTING;
2075 indir->gtFlags |= GTF_IND_INVARIANT;
2077 slot = gtNewLclvNode(slotLclNum, TYP_I_IMPL);
2078 GenTree* asg = gtNewAssignNode(slot, indir);
2079 GenTree* colon = new (this, GT_COLON) GenTreeColon(TYP_VOID, gtNewNothingNode(), asg);
2080 GenTree* qmark = gtNewQmarkNode(TYP_VOID, relop, colon);
2081 impAppendTree(qmark, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
2083 return gtNewLclvNode(slotLclNum, TYP_I_IMPL);
2086 assert(pRuntimeLookup->indirections != 0);
2088 impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("bubbling QMark1"));
2090 // Extract the handle
2091 GenTreePtr handle = gtNewOperNode(GT_IND, TYP_I_IMPL, slotPtrTree);
2092 handle->gtFlags |= GTF_IND_NONFAULTING;
2094 GenTreePtr handleCopy = impCloneExpr(handle, &handle, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
2095 nullptr DEBUGARG("impRuntimeLookup typehandle"));
2098 GenTree* argNode = gtNewIconEmbHndNode(pRuntimeLookup->signature, nullptr, GTF_ICON_TOKEN_HDL, compileTimeHandle);
2100 GenTreeArgList* helperArgs = gtNewArgList(ctxTree, argNode);
2101 GenTreePtr helperCall = gtNewHelperCallNode(pRuntimeLookup->helper, TYP_I_IMPL, helperArgs);
2103 // Check for null and possibly call helper
2104 GenTreePtr relop = gtNewOperNode(GT_NE, TYP_INT, handle, gtNewIconNode(0, TYP_I_IMPL));
2105 relop->gtFlags |= GTF_RELOP_QMARK;
2107 GenTreePtr colon = new (this, GT_COLON) GenTreeColon(TYP_I_IMPL,
2108 gtNewNothingNode(), // do nothing if nonnull
2111 GenTreePtr qmark = gtNewQmarkNode(TYP_I_IMPL, relop, colon);
2114 if (handleCopy->IsLocal())
2116 tmp = handleCopy->gtLclVarCommon.gtLclNum;
2120 tmp = lvaGrabTemp(true DEBUGARG("spilling QMark1"));
2123 impAssignTempGen(tmp, qmark, (unsigned)CHECK_SPILL_NONE);
2124 return gtNewLclvNode(tmp, TYP_I_IMPL);
2127 /******************************************************************************
2128 * Spills the stack at verCurrentState.esStack[level] and replaces it with a temp.
2129 * If tnum!=BAD_VAR_NUM, the temp var used to replace the tree is tnum,
2130 * else, grab a new temp.
2131 * For structs (which can be pushed on the stack using obj, etc),
2132 * special handling is needed
2135 struct RecursiveGuard
2140 m_pAddress = nullptr;
2147 *m_pAddress = false;
2151 void Init(bool* pAddress, bool bInitialize)
2153 assert(pAddress && *pAddress == false && "Recursive guard violation");
2154 m_pAddress = pAddress;
2166 bool Compiler::impSpillStackEntry(unsigned level,
2170 bool bAssertOnRecursion,
2177 RecursiveGuard guard;
2178 guard.Init(&impNestedStackSpill, bAssertOnRecursion);
2181 GenTreePtr tree = verCurrentState.esStack[level].val;
2183 /* Allocate a temp if we haven't been asked to use a particular one */
2185 if (tiVerificationNeeded)
2187 // Ignore bad temp requests (they will happen with bad code and will be
2188 // catched when importing the destblock)
2189 if ((tnum != BAD_VAR_NUM && tnum >= lvaCount) && verNeedsVerification())
2196 if (tnum != BAD_VAR_NUM && (tnum >= lvaCount))
2202 bool isNewTemp = false;
2204 if (tnum == BAD_VAR_NUM)
2206 tnum = lvaGrabTemp(true DEBUGARG(reason));
2209 else if (tiVerificationNeeded && lvaTable[tnum].TypeGet() != TYP_UNDEF)
2211 // if verification is needed and tnum's type is incompatible with
2212 // type on that stack, we grab a new temp. This is safe since
2213 // we will throw a verification exception in the dest block.
2215 var_types valTyp = tree->TypeGet();
2216 var_types dstTyp = lvaTable[tnum].TypeGet();
2218 // if the two types are different, we return. This will only happen with bad code and will
2219 // be catched when importing the destblock. We still allow int/byrefs and float/double differences.
2220 if ((genActualType(valTyp) != genActualType(dstTyp)) &&
2222 #ifndef _TARGET_64BIT_
2223 (valTyp == TYP_I_IMPL && dstTyp == TYP_BYREF) || (valTyp == TYP_BYREF && dstTyp == TYP_I_IMPL) ||
2224 #endif // !_TARGET_64BIT_
2225 (varTypeIsFloating(dstTyp) && varTypeIsFloating(valTyp))))
2227 if (verNeedsVerification())
2234 /* Assign the spilled entry to the temp */
2235 impAssignTempGen(tnum, tree, verCurrentState.esStack[level].seTypeInfo.GetClassHandle(), level);
2237 // If temp is newly introduced and a ref type, grab what type info we can.
2238 if (isNewTemp && (lvaTable[tnum].lvType == TYP_REF))
2240 CORINFO_CLASS_HANDLE stkHnd = verCurrentState.esStack[level].seTypeInfo.GetClassHandle();
2241 lvaSetClass(tnum, tree, stkHnd);
2244 // The tree type may be modified by impAssignTempGen, so use the type of the lclVar.
2245 var_types type = genActualType(lvaTable[tnum].TypeGet());
2246 GenTreePtr temp = gtNewLclvNode(tnum, type);
2247 verCurrentState.esStack[level].val = temp;
2252 /*****************************************************************************
2254 * Ensure that the stack has only spilled values
2257 void Compiler::impSpillStackEnsure(bool spillLeaves)
2259 assert(!spillLeaves || opts.compDbgCode);
2261 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
2263 GenTreePtr tree = verCurrentState.esStack[level].val;
2265 if (!spillLeaves && tree->OperIsLeaf())
2270 // Temps introduced by the importer itself don't need to be spilled
2272 bool isTempLcl = (tree->OperGet() == GT_LCL_VAR) && (tree->gtLclVarCommon.gtLclNum >= info.compLocalsCount);
2279 impSpillStackEntry(level, BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impSpillStackEnsure"));
2283 void Compiler::impSpillEvalStack()
2285 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
2287 impSpillStackEntry(level, BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impSpillEvalStack"));
2291 /*****************************************************************************
2293 * If the stack contains any trees with side effects in them, assign those
2294 * trees to temps and append the assignments to the statement list.
2295 * On return the stack is guaranteed to be empty.
2298 inline void Compiler::impEvalSideEffects()
2300 impSpillSideEffects(false, (unsigned)CHECK_SPILL_ALL DEBUGARG("impEvalSideEffects"));
2301 verCurrentState.esStackDepth = 0;
2304 /*****************************************************************************
2306 * If the stack contains any trees with side effects in them, assign those
2307 * trees to temps and replace them on the stack with refs to their temps.
2308 * [0..chkLevel) is the portion of the stack which will be checked and spilled.
2311 inline void Compiler::impSpillSideEffects(bool spillGlobEffects, unsigned chkLevel DEBUGARG(const char* reason))
2313 assert(chkLevel != (unsigned)CHECK_SPILL_NONE);
2315 /* Before we make any appends to the tree list we must spill the
2316 * "special" side effects (GTF_ORDER_SIDEEFF on a GT_CATCH_ARG) */
2318 impSpillSpecialSideEff();
2320 if (chkLevel == (unsigned)CHECK_SPILL_ALL)
2322 chkLevel = verCurrentState.esStackDepth;
2325 assert(chkLevel <= verCurrentState.esStackDepth);
2327 unsigned spillFlags = spillGlobEffects ? GTF_GLOB_EFFECT : GTF_SIDE_EFFECT;
2329 for (unsigned i = 0; i < chkLevel; i++)
2331 GenTreePtr tree = verCurrentState.esStack[i].val;
2333 GenTreePtr lclVarTree;
2335 if ((tree->gtFlags & spillFlags) != 0 ||
2336 (spillGlobEffects && // Only consider the following when spillGlobEffects == TRUE
2337 !impIsAddressInLocal(tree, &lclVarTree) && // No need to spill the GT_ADDR node on a local.
2338 gtHasLocalsWithAddrOp(tree))) // Spill if we still see GT_LCL_VAR that contains lvHasLdAddrOp or
2339 // lvAddrTaken flag.
2341 impSpillStackEntry(i, BAD_VAR_NUM DEBUGARG(false) DEBUGARG(reason));
2346 /*****************************************************************************
2348 * If the stack contains any trees with special side effects in them, assign
2349 * those trees to temps and replace them on the stack with refs to their temps.
2352 inline void Compiler::impSpillSpecialSideEff()
2354 // Only exception objects need to be carefully handled
2356 if (!compCurBB->bbCatchTyp)
2361 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
2363 GenTreePtr tree = verCurrentState.esStack[level].val;
2364 // Make sure if we have an exception object in the sub tree we spill ourselves.
2365 if (gtHasCatchArg(tree))
2367 impSpillStackEntry(level, BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impSpillSpecialSideEff"));
2372 /*****************************************************************************
2374 * Spill all stack references to value classes (TYP_STRUCT nodes)
2377 void Compiler::impSpillValueClasses()
2379 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
2381 GenTreePtr tree = verCurrentState.esStack[level].val;
2383 if (fgWalkTreePre(&tree, impFindValueClasses) == WALK_ABORT)
2385 // Tree walk was aborted, which means that we found a
2386 // value class on the stack. Need to spill that
2389 impSpillStackEntry(level, BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impSpillValueClasses"));
2394 /*****************************************************************************
2396 * Callback that checks if a tree node is TYP_STRUCT
2399 Compiler::fgWalkResult Compiler::impFindValueClasses(GenTreePtr* pTree, fgWalkData* data)
2401 fgWalkResult walkResult = WALK_CONTINUE;
2403 if ((*pTree)->gtType == TYP_STRUCT)
2405 // Abort the walk and indicate that we found a value class
2407 walkResult = WALK_ABORT;
2413 /*****************************************************************************
2415 * If the stack contains any trees with references to local #lclNum, assign
2416 * those trees to temps and replace their place on the stack with refs to
2420 void Compiler::impSpillLclRefs(ssize_t lclNum)
2422 /* Before we make any appends to the tree list we must spill the
2423 * "special" side effects (GTF_ORDER_SIDEEFF) - GT_CATCH_ARG */
2425 impSpillSpecialSideEff();
2427 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
2429 GenTreePtr tree = verCurrentState.esStack[level].val;
2431 /* If the tree may throw an exception, and the block has a handler,
2432 then we need to spill assignments to the local if the local is
2433 live on entry to the handler.
2434 Just spill 'em all without considering the liveness */
2436 bool xcptnCaught = ehBlockHasExnFlowDsc(compCurBB) && (tree->gtFlags & (GTF_CALL | GTF_EXCEPT));
2438 /* Skip the tree if it doesn't have an affected reference,
2439 unless xcptnCaught */
2441 if (xcptnCaught || gtHasRef(tree, lclNum, false))
2443 impSpillStackEntry(level, BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impSpillLclRefs"));
2448 /*****************************************************************************
2450 * Push catch arg onto the stack.
2451 * If there are jumps to the beginning of the handler, insert basic block
2452 * and spill catch arg to a temp. Update the handler block if necessary.
2454 * Returns the basic block of the actual handler.
2457 BasicBlock* Compiler::impPushCatchArgOnStack(BasicBlock* hndBlk, CORINFO_CLASS_HANDLE clsHnd, bool isSingleBlockFilter)
2459 // Do not inject the basic block twice on reimport. This should be
2460 // hit only under JIT stress. See if the block is the one we injected.
2461 // Note that EH canonicalization can inject internal blocks here. We might
2462 // be able to re-use such a block (but we don't, right now).
2463 if ((hndBlk->bbFlags & (BBF_IMPORTED | BBF_INTERNAL | BBF_DONT_REMOVE | BBF_HAS_LABEL | BBF_JMP_TARGET)) ==
2464 (BBF_IMPORTED | BBF_INTERNAL | BBF_DONT_REMOVE | BBF_HAS_LABEL | BBF_JMP_TARGET))
2466 GenTreePtr tree = hndBlk->bbTreeList;
2468 if (tree != nullptr && tree->gtOper == GT_STMT)
2470 tree = tree->gtStmt.gtStmtExpr;
2471 assert(tree != nullptr);
2473 if ((tree->gtOper == GT_ASG) && (tree->gtOp.gtOp1->gtOper == GT_LCL_VAR) &&
2474 (tree->gtOp.gtOp2->gtOper == GT_CATCH_ARG))
2476 tree = gtNewLclvNode(tree->gtOp.gtOp1->gtLclVarCommon.gtLclNum, TYP_REF);
2478 impPushOnStack(tree, typeInfo(TI_REF, clsHnd));
2480 return hndBlk->bbNext;
2484 // If we get here, it must have been some other kind of internal block. It's possible that
2485 // someone prepended something to our injected block, but that's unlikely.
2488 /* Push the exception address value on the stack */
2489 GenTreePtr arg = new (this, GT_CATCH_ARG) GenTree(GT_CATCH_ARG, TYP_REF);
2491 /* Mark the node as having a side-effect - i.e. cannot be
2492 * moved around since it is tied to a fixed location (EAX) */
2493 arg->gtFlags |= GTF_ORDER_SIDEEFF;
2495 #if defined(JIT32_GCENCODER)
2496 const bool forceInsertNewBlock = isSingleBlockFilter || compStressCompile(STRESS_CATCH_ARG, 5);
2498 const bool forceInsertNewBlock = compStressCompile(STRESS_CATCH_ARG, 5);
2499 #endif // defined(JIT32_GCENCODER)
2501 /* Spill GT_CATCH_ARG to a temp if there are jumps to the beginning of the handler */
2502 if (hndBlk->bbRefs > 1 || forceInsertNewBlock)
2504 if (hndBlk->bbRefs == 1)
2509 /* Create extra basic block for the spill */
2510 BasicBlock* newBlk = fgNewBBbefore(BBJ_NONE, hndBlk, /* extendRegion */ true);
2511 newBlk->bbFlags |= BBF_IMPORTED | BBF_DONT_REMOVE | BBF_HAS_LABEL | BBF_JMP_TARGET;
2512 newBlk->setBBWeight(hndBlk->bbWeight);
2513 newBlk->bbCodeOffs = hndBlk->bbCodeOffs;
2515 /* Account for the new link we are about to create */
2518 /* Spill into a temp */
2519 unsigned tempNum = lvaGrabTemp(false DEBUGARG("SpillCatchArg"));
2520 lvaTable[tempNum].lvType = TYP_REF;
2521 arg = gtNewTempAssign(tempNum, arg);
2523 hndBlk->bbStkTempsIn = tempNum;
2525 /* Report the debug info. impImportBlockCode won't treat
2526 * the actual handler as exception block and thus won't do it for us. */
2527 if (info.compStmtOffsetsImplicit & ICorDebugInfo::CALL_SITE_BOUNDARIES)
2529 impCurStmtOffs = newBlk->bbCodeOffs | IL_OFFSETX_STKBIT;
2530 arg = gtNewStmt(arg, impCurStmtOffs);
2533 fgInsertStmtAtEnd(newBlk, arg);
2535 arg = gtNewLclvNode(tempNum, TYP_REF);
2538 impPushOnStack(arg, typeInfo(TI_REF, clsHnd));
2543 /*****************************************************************************
2545 * Given a tree, clone it. *pClone is set to the cloned tree.
2546 * Returns the original tree if the cloning was easy,
2547 * else returns the temp to which the tree had to be spilled to.
2548 * If the tree has side-effects, it will be spilled to a temp.
2551 GenTreePtr Compiler::impCloneExpr(GenTreePtr tree,
2553 CORINFO_CLASS_HANDLE structHnd,
2555 GenTreePtr* pAfterStmt DEBUGARG(const char* reason))
2557 if (!(tree->gtFlags & GTF_GLOB_EFFECT))
2559 GenTreePtr clone = gtClone(tree, true);
2568 /* Store the operand in a temp and return the temp */
2570 unsigned temp = lvaGrabTemp(true DEBUGARG(reason));
2572 // impAssignTempGen() may change tree->gtType to TYP_VOID for calls which
2573 // return a struct type. It also may modify the struct type to a more
2574 // specialized type (e.g. a SIMD type). So we will get the type from
2575 // the lclVar AFTER calling impAssignTempGen().
2577 impAssignTempGen(temp, tree, structHnd, curLevel, pAfterStmt, impCurStmtOffs);
2578 var_types type = genActualType(lvaTable[temp].TypeGet());
2580 *pClone = gtNewLclvNode(temp, type);
2581 return gtNewLclvNode(temp, type);
2584 /*****************************************************************************
2585 * Remember the IL offset (including stack-empty info) for the trees we will
2589 inline void Compiler::impCurStmtOffsSet(IL_OFFSET offs)
2591 if (compIsForInlining())
2593 GenTreePtr callStmt = impInlineInfo->iciStmt;
2594 assert(callStmt->gtOper == GT_STMT);
2595 impCurStmtOffs = callStmt->gtStmt.gtStmtILoffsx;
2599 assert(offs == BAD_IL_OFFSET || (offs & IL_OFFSETX_BITS) == 0);
2600 IL_OFFSETX stkBit = (verCurrentState.esStackDepth > 0) ? IL_OFFSETX_STKBIT : 0;
2601 impCurStmtOffs = offs | stkBit;
2605 /*****************************************************************************
2606 * Returns current IL offset with stack-empty and call-instruction info incorporated
2608 inline IL_OFFSETX Compiler::impCurILOffset(IL_OFFSET offs, bool callInstruction)
2610 if (compIsForInlining())
2612 return BAD_IL_OFFSET;
2616 assert(offs == BAD_IL_OFFSET || (offs & IL_OFFSETX_BITS) == 0);
2617 IL_OFFSETX stkBit = (verCurrentState.esStackDepth > 0) ? IL_OFFSETX_STKBIT : 0;
2618 IL_OFFSETX callInstructionBit = callInstruction ? IL_OFFSETX_CALLINSTRUCTIONBIT : 0;
2619 return offs | stkBit | callInstructionBit;
2623 //------------------------------------------------------------------------
2624 // impCanSpillNow: check is it possible to spill all values from eeStack to local variables.
2627 // prevOpcode - last importer opcode
2630 // true if it is legal, false if it could be a sequence that we do not want to divide.
2631 bool Compiler::impCanSpillNow(OPCODE prevOpcode)
2633 // Don't spill after ldtoken, newarr and newobj, because it could be a part of the InitializeArray sequence.
2634 // Avoid breaking up to guarantee that impInitializeArrayIntrinsic can succeed.
2635 return (prevOpcode != CEE_LDTOKEN) && (prevOpcode != CEE_NEWARR) && (prevOpcode != CEE_NEWOBJ);
2638 /*****************************************************************************
2640 * Remember the instr offset for the statements
2642 * When we do impAppendTree(tree), we can't set tree->gtStmtLastILoffs to
2643 * impCurOpcOffs, if the append was done because of a partial stack spill,
2644 * as some of the trees corresponding to code up to impCurOpcOffs might
2645 * still be sitting on the stack.
2646 * So we delay marking of gtStmtLastILoffs until impNoteLastILoffs().
2647 * This should be called when an opcode finally/explicitly causes
2648 * impAppendTree(tree) to be called (as opposed to being called because of
2649 * a spill caused by the opcode)
2654 void Compiler::impNoteLastILoffs()
2656 if (impLastILoffsStmt == nullptr)
2658 // We should have added a statement for the current basic block
2659 // Is this assert correct ?
2661 assert(impTreeLast);
2662 assert(impTreeLast->gtOper == GT_STMT);
2664 impTreeLast->gtStmt.gtStmtLastILoffs = compIsForInlining() ? BAD_IL_OFFSET : impCurOpcOffs;
2668 impLastILoffsStmt->gtStmt.gtStmtLastILoffs = compIsForInlining() ? BAD_IL_OFFSET : impCurOpcOffs;
2669 impLastILoffsStmt = nullptr;
2675 /*****************************************************************************
2676 * We don't create any GenTree (excluding spills) for a branch.
2677 * For debugging info, we need a placeholder so that we can note
2678 * the IL offset in gtStmt.gtStmtOffs. So append an empty statement.
2681 void Compiler::impNoteBranchOffs()
2683 if (opts.compDbgCode)
2685 impAppendTree(gtNewNothingNode(), (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
2689 /*****************************************************************************
2690 * Locate the next stmt boundary for which we need to record info.
2691 * We will have to spill the stack at such boundaries if it is not
2693 * Returns the next stmt boundary (after the start of the block)
2696 unsigned Compiler::impInitBlockLineInfo()
2698 /* Assume the block does not correspond with any IL offset. This prevents
2699 us from reporting extra offsets. Extra mappings can cause confusing
2700 stepping, especially if the extra mapping is a jump-target, and the
2701 debugger does not ignore extra mappings, but instead rewinds to the
2702 nearest known offset */
2704 impCurStmtOffsSet(BAD_IL_OFFSET);
2706 if (compIsForInlining())
2711 IL_OFFSET blockOffs = compCurBB->bbCodeOffs;
2713 if ((verCurrentState.esStackDepth == 0) && (info.compStmtOffsetsImplicit & ICorDebugInfo::STACK_EMPTY_BOUNDARIES))
2715 impCurStmtOffsSet(blockOffs);
2718 if (false && (info.compStmtOffsetsImplicit & ICorDebugInfo::CALL_SITE_BOUNDARIES))
2720 impCurStmtOffsSet(blockOffs);
2723 /* Always report IL offset 0 or some tests get confused.
2724 Probably a good idea anyways */
2728 impCurStmtOffsSet(blockOffs);
2731 if (!info.compStmtOffsetsCount)
2736 /* Find the lowest explicit stmt boundary within the block */
2738 /* Start looking at an entry that is based on our instr offset */
2740 unsigned index = (info.compStmtOffsetsCount * blockOffs) / info.compILCodeSize;
2742 if (index >= info.compStmtOffsetsCount)
2744 index = info.compStmtOffsetsCount - 1;
2747 /* If we've guessed too far, back up */
2749 while (index > 0 && info.compStmtOffsets[index - 1] >= blockOffs)
2754 /* If we guessed short, advance ahead */
2756 while (info.compStmtOffsets[index] < blockOffs)
2760 if (index == info.compStmtOffsetsCount)
2762 return info.compStmtOffsetsCount;
2766 assert(index < info.compStmtOffsetsCount);
2768 if (info.compStmtOffsets[index] == blockOffs)
2770 /* There is an explicit boundary for the start of this basic block.
2771 So we will start with bbCodeOffs. Else we will wait until we
2772 get to the next explicit boundary */
2774 impCurStmtOffsSet(blockOffs);
2782 /*****************************************************************************/
2784 static inline bool impOpcodeIsCallOpcode(OPCODE opcode)
2798 /*****************************************************************************/
2800 static inline bool impOpcodeIsCallSiteBoundary(OPCODE opcode)
2817 /*****************************************************************************/
2819 // One might think it is worth caching these values, but results indicate
2821 // In addition, caching them causes SuperPMI to be unable to completely
2822 // encapsulate an individual method context.
2823 CORINFO_CLASS_HANDLE Compiler::impGetRefAnyClass()
2825 CORINFO_CLASS_HANDLE refAnyClass = info.compCompHnd->getBuiltinClass(CLASSID_TYPED_BYREF);
2826 assert(refAnyClass != (CORINFO_CLASS_HANDLE) nullptr);
2830 CORINFO_CLASS_HANDLE Compiler::impGetTypeHandleClass()
2832 CORINFO_CLASS_HANDLE typeHandleClass = info.compCompHnd->getBuiltinClass(CLASSID_TYPE_HANDLE);
2833 assert(typeHandleClass != (CORINFO_CLASS_HANDLE) nullptr);
2834 return typeHandleClass;
2837 CORINFO_CLASS_HANDLE Compiler::impGetRuntimeArgumentHandle()
2839 CORINFO_CLASS_HANDLE argIteratorClass = info.compCompHnd->getBuiltinClass(CLASSID_ARGUMENT_HANDLE);
2840 assert(argIteratorClass != (CORINFO_CLASS_HANDLE) nullptr);
2841 return argIteratorClass;
2844 CORINFO_CLASS_HANDLE Compiler::impGetStringClass()
2846 CORINFO_CLASS_HANDLE stringClass = info.compCompHnd->getBuiltinClass(CLASSID_STRING);
2847 assert(stringClass != (CORINFO_CLASS_HANDLE) nullptr);
2851 CORINFO_CLASS_HANDLE Compiler::impGetObjectClass()
2853 CORINFO_CLASS_HANDLE objectClass = info.compCompHnd->getBuiltinClass(CLASSID_SYSTEM_OBJECT);
2854 assert(objectClass != (CORINFO_CLASS_HANDLE) nullptr);
2858 /*****************************************************************************
2859 * "&var" can be used either as TYP_BYREF or TYP_I_IMPL, but we
2860 * set its type to TYP_BYREF when we create it. We know if it can be
2861 * changed to TYP_I_IMPL only at the point where we use it
2865 void Compiler::impBashVarAddrsToI(GenTreePtr tree1, GenTreePtr tree2)
2867 if (tree1->IsVarAddr())
2869 tree1->gtType = TYP_I_IMPL;
2872 if (tree2 && tree2->IsVarAddr())
2874 tree2->gtType = TYP_I_IMPL;
2878 /*****************************************************************************
2879 * TYP_INT and TYP_I_IMPL can be used almost interchangeably, but we want
2880 * to make that an explicit cast in our trees, so any implicit casts that
2881 * exist in the IL (at least on 64-bit where TYP_I_IMPL != TYP_INT) are
2882 * turned into explicit casts here.
2883 * We also allow an implicit conversion of a ldnull into a TYP_I_IMPL(0)
2886 GenTreePtr Compiler::impImplicitIorI4Cast(GenTreePtr tree, var_types dstTyp)
2888 var_types currType = genActualType(tree->gtType);
2889 var_types wantedType = genActualType(dstTyp);
2891 if (wantedType != currType)
2893 // Automatic upcast for a GT_CNS_INT into TYP_I_IMPL
2894 if ((tree->OperGet() == GT_CNS_INT) && varTypeIsI(dstTyp))
2896 if (!varTypeIsI(tree->gtType) || ((tree->gtType == TYP_REF) && (tree->gtIntCon.gtIconVal == 0)))
2898 tree->gtType = TYP_I_IMPL;
2901 #ifdef _TARGET_64BIT_
2902 else if (varTypeIsI(wantedType) && (currType == TYP_INT))
2904 // Note that this allows TYP_INT to be cast to a TYP_I_IMPL when wantedType is a TYP_BYREF or TYP_REF
2905 tree = gtNewCastNode(TYP_I_IMPL, tree, TYP_I_IMPL);
2907 else if ((wantedType == TYP_INT) && varTypeIsI(currType))
2909 // Note that this allows TYP_BYREF or TYP_REF to be cast to a TYP_INT
2910 tree = gtNewCastNode(TYP_INT, tree, TYP_INT);
2912 #endif // _TARGET_64BIT_
2918 /*****************************************************************************
2919 * TYP_FLOAT and TYP_DOUBLE can be used almost interchangeably in some cases,
2920 * but we want to make that an explicit cast in our trees, so any implicit casts
2921 * that exist in the IL are turned into explicit casts here.
2924 GenTreePtr Compiler::impImplicitR4orR8Cast(GenTreePtr tree, var_types dstTyp)
2926 #ifndef LEGACY_BACKEND
2927 if (varTypeIsFloating(tree) && varTypeIsFloating(dstTyp) && (dstTyp != tree->gtType))
2929 tree = gtNewCastNode(dstTyp, tree, dstTyp);
2931 #endif // !LEGACY_BACKEND
2936 //------------------------------------------------------------------------
2937 // impInitializeArrayIntrinsic: Attempts to replace a call to InitializeArray
2938 // with a GT_COPYBLK node.
2941 // sig - The InitializeArray signature.
2944 // A pointer to the newly created GT_COPYBLK node if the replacement succeeds or
2945 // nullptr otherwise.
2948 // The function recognizes the following IL pattern:
2949 // ldc <length> or a list of ldc <lower bound>/<length>
2952 // ldtoken <field handle>
2953 // call InitializeArray
2954 // The lower bounds need not be constant except when the array rank is 1.
2955 // The function recognizes all kinds of arrays thus enabling a small runtime
2956 // such as CoreRT to skip providing an implementation for InitializeArray.
2958 GenTreePtr Compiler::impInitializeArrayIntrinsic(CORINFO_SIG_INFO* sig)
2960 assert(sig->numArgs == 2);
2962 GenTreePtr fieldTokenNode = impStackTop(0).val;
2963 GenTreePtr arrayLocalNode = impStackTop(1).val;
2966 // Verify that the field token is known and valid. Note that It's also
2967 // possible for the token to come from reflection, in which case we cannot do
2968 // the optimization and must therefore revert to calling the helper. You can
2969 // see an example of this in bvt\DynIL\initarray2.exe (in Main).
2972 // Check to see if the ldtoken helper call is what we see here.
2973 if (fieldTokenNode->gtOper != GT_CALL || (fieldTokenNode->gtCall.gtCallType != CT_HELPER) ||
2974 (fieldTokenNode->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_FIELDDESC_TO_STUBRUNTIMEFIELD)))
2979 // Strip helper call away
2980 fieldTokenNode = fieldTokenNode->gtCall.gtCallArgs->Current();
2982 if (fieldTokenNode->gtOper == GT_IND)
2984 fieldTokenNode = fieldTokenNode->gtOp.gtOp1;
2987 // Check for constant
2988 if (fieldTokenNode->gtOper != GT_CNS_INT)
2993 CORINFO_FIELD_HANDLE fieldToken = (CORINFO_FIELD_HANDLE)fieldTokenNode->gtIntCon.gtCompileTimeHandle;
2994 if (!fieldTokenNode->IsIconHandle(GTF_ICON_FIELD_HDL) || (fieldToken == nullptr))
3000 // We need to get the number of elements in the array and the size of each element.
3001 // We verify that the newarr statement is exactly what we expect it to be.
3002 // If it's not then we just return NULL and we don't optimize this call
3006 // It is possible the we don't have any statements in the block yet
3008 if (impTreeLast->gtOper != GT_STMT)
3010 assert(impTreeLast->gtOper == GT_BEG_STMTS);
3015 // We start by looking at the last statement, making sure it's an assignment, and
3016 // that the target of the assignment is the array passed to InitializeArray.
3018 GenTreePtr arrayAssignment = impTreeLast->gtStmt.gtStmtExpr;
3019 if ((arrayAssignment->gtOper != GT_ASG) || (arrayAssignment->gtOp.gtOp1->gtOper != GT_LCL_VAR) ||
3020 (arrayLocalNode->gtOper != GT_LCL_VAR) ||
3021 (arrayAssignment->gtOp.gtOp1->gtLclVarCommon.gtLclNum != arrayLocalNode->gtLclVarCommon.gtLclNum))
3027 // Make sure that the object being assigned is a helper call.
3030 GenTreePtr newArrayCall = arrayAssignment->gtOp.gtOp2;
3031 if ((newArrayCall->gtOper != GT_CALL) || (newArrayCall->gtCall.gtCallType != CT_HELPER))
3037 // Verify that it is one of the new array helpers.
3040 bool isMDArray = false;
3042 if (newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEWARR_1_DIRECT) &&
3043 newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEWARR_1_OBJ) &&
3044 newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEWARR_1_VC) &&
3045 newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEWARR_1_ALIGN8)
3046 #ifdef FEATURE_READYTORUN_COMPILER
3047 && newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEWARR_1_R2R_DIRECT) &&
3048 newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_READYTORUN_NEWARR_1)
3052 if (newArrayCall->gtCall.gtCallMethHnd != eeFindHelper(CORINFO_HELP_NEW_MDARR_NONVARARG))
3060 CORINFO_CLASS_HANDLE arrayClsHnd = (CORINFO_CLASS_HANDLE)newArrayCall->gtCall.compileTimeHelperArgumentHandle;
3063 // Make sure we found a compile time handle to the array
3072 S_UINT32 numElements;
3076 rank = info.compCompHnd->getArrayRank(arrayClsHnd);
3083 GenTreeArgList* tokenArg = newArrayCall->gtCall.gtCallArgs;
3084 assert(tokenArg != nullptr);
3085 GenTreeArgList* numArgsArg = tokenArg->Rest();
3086 assert(numArgsArg != nullptr);
3087 GenTreeArgList* argsArg = numArgsArg->Rest();
3088 assert(argsArg != nullptr);
3091 // The number of arguments should be a constant between 1 and 64. The rank can't be 0
3092 // so at least one length must be present and the rank can't exceed 32 so there can
3093 // be at most 64 arguments - 32 lengths and 32 lower bounds.
3096 if ((!numArgsArg->Current()->IsCnsIntOrI()) || (numArgsArg->Current()->AsIntCon()->IconValue() < 1) ||
3097 (numArgsArg->Current()->AsIntCon()->IconValue() > 64))
3102 unsigned numArgs = static_cast<unsigned>(numArgsArg->Current()->AsIntCon()->IconValue());
3103 bool lowerBoundsSpecified;
3105 if (numArgs == rank * 2)
3107 lowerBoundsSpecified = true;
3109 else if (numArgs == rank)
3111 lowerBoundsSpecified = false;
3114 // If the rank is 1 and a lower bound isn't specified then the runtime creates
3115 // a SDArray. Note that even if a lower bound is specified it can be 0 and then
3116 // we get a SDArray as well, see the for loop below.
3130 // The rank is known to be at least 1 so we can start with numElements being 1
3131 // to avoid the need to special case the first dimension.
3134 numElements = S_UINT32(1);
3138 static bool IsArgsFieldInit(GenTree* tree, unsigned index, unsigned lvaNewObjArrayArgs)
3140 return (tree->OperGet() == GT_ASG) && IsArgsFieldIndir(tree->gtGetOp1(), index, lvaNewObjArrayArgs) &&
3141 IsArgsAddr(tree->gtGetOp1()->gtGetOp1()->gtGetOp1(), lvaNewObjArrayArgs);
3144 static bool IsArgsFieldIndir(GenTree* tree, unsigned index, unsigned lvaNewObjArrayArgs)
3146 return (tree->OperGet() == GT_IND) && (tree->gtGetOp1()->OperGet() == GT_ADD) &&
3147 (tree->gtGetOp1()->gtGetOp2()->IsIntegralConst(sizeof(INT32) * index)) &&
3148 IsArgsAddr(tree->gtGetOp1()->gtGetOp1(), lvaNewObjArrayArgs);
3151 static bool IsArgsAddr(GenTree* tree, unsigned lvaNewObjArrayArgs)
3153 return (tree->OperGet() == GT_ADDR) && (tree->gtGetOp1()->OperGet() == GT_LCL_VAR) &&
3154 (tree->gtGetOp1()->AsLclVar()->GetLclNum() == lvaNewObjArrayArgs);
3157 static bool IsComma(GenTree* tree)
3159 return (tree != nullptr) && (tree->OperGet() == GT_COMMA);
3163 unsigned argIndex = 0;
3166 for (comma = argsArg->Current(); Match::IsComma(comma); comma = comma->gtGetOp2())
3168 if (lowerBoundsSpecified)
3171 // In general lower bounds can be ignored because they're not needed to
3172 // calculate the total number of elements. But for single dimensional arrays
3173 // we need to know if the lower bound is 0 because in this case the runtime
3174 // creates a SDArray and this affects the way the array data offset is calculated.
3179 GenTree* lowerBoundAssign = comma->gtGetOp1();
3180 assert(Match::IsArgsFieldInit(lowerBoundAssign, argIndex, lvaNewObjArrayArgs));
3181 GenTree* lowerBoundNode = lowerBoundAssign->gtGetOp2();
3183 if (lowerBoundNode->IsIntegralConst(0))
3189 comma = comma->gtGetOp2();
3193 GenTree* lengthNodeAssign = comma->gtGetOp1();
3194 assert(Match::IsArgsFieldInit(lengthNodeAssign, argIndex, lvaNewObjArrayArgs));
3195 GenTree* lengthNode = lengthNodeAssign->gtGetOp2();
3197 if (!lengthNode->IsCnsIntOrI())
3202 numElements *= S_SIZE_T(lengthNode->AsIntCon()->IconValue());
3206 assert((comma != nullptr) && Match::IsArgsAddr(comma, lvaNewObjArrayArgs));
3208 if (argIndex != numArgs)
3216 // Make sure there are exactly two arguments: the array class and
3217 // the number of elements.
3220 GenTreePtr arrayLengthNode;
3222 GenTreeArgList* args = newArrayCall->gtCall.gtCallArgs;
3223 #ifdef FEATURE_READYTORUN_COMPILER
3224 if (newArrayCall->gtCall.gtCallMethHnd == eeFindHelper(CORINFO_HELP_READYTORUN_NEWARR_1))
3226 // Array length is 1st argument for readytorun helper
3227 arrayLengthNode = args->Current();
3232 // Array length is 2nd argument for regular helper
3233 arrayLengthNode = args->Rest()->Current();
3237 // Make sure that the number of elements look valid.
3239 if (arrayLengthNode->gtOper != GT_CNS_INT)
3244 numElements = S_SIZE_T(arrayLengthNode->gtIntCon.gtIconVal);
3246 if (!info.compCompHnd->isSDArray(arrayClsHnd))
3252 CORINFO_CLASS_HANDLE elemClsHnd;
3253 var_types elementType = JITtype2varType(info.compCompHnd->getChildType(arrayClsHnd, &elemClsHnd));
3256 // Note that genTypeSize will return zero for non primitive types, which is exactly
3257 // what we want (size will then be 0, and we will catch this in the conditional below).
3258 // Note that we don't expect this to fail for valid binaries, so we assert in the
3259 // non-verification case (the verification case should not assert but rather correctly
3260 // handle bad binaries). This assert is not guarding any specific invariant, but rather
3261 // saying that we don't expect this to happen, and if it is hit, we need to investigate
3265 S_UINT32 elemSize(genTypeSize(elementType));
3266 S_UINT32 size = elemSize * S_UINT32(numElements);
3268 if (size.IsOverflow())
3273 if ((size.Value() == 0) || (varTypeIsGC(elementType)))
3275 assert(verNeedsVerification());
3279 void* initData = info.compCompHnd->getArrayInitializationData(fieldToken, size.Value());
3286 // At this point we are ready to commit to implementing the InitializeArray
3287 // intrinsic using a struct assignment. Pop the arguments from the stack and
3288 // return the struct assignment node.
3294 const unsigned blkSize = size.Value();
3295 unsigned dataOffset;
3299 dataOffset = eeGetMDArrayDataOffset(elementType, rank);
3303 dataOffset = eeGetArrayDataOffset(elementType);
3306 GenTreePtr dst = gtNewOperNode(GT_ADD, TYP_BYREF, arrayLocalNode, gtNewIconNode(dataOffset, TYP_I_IMPL));
3307 GenTreePtr blk = gtNewBlockVal(dst, blkSize);
3308 GenTreePtr src = gtNewIndOfIconHandleNode(TYP_STRUCT, (size_t)initData, GTF_ICON_STATIC_HDL, false);
3310 return gtNewBlkOpNode(blk, // dst
3317 //------------------------------------------------------------------------
3318 // impIntrinsic: possibly expand intrinsic call into alternate IR sequence
3321 // newobjThis - for constructor calls, the tree for the newly allocated object
3322 // clsHnd - handle for the intrinsic method's class
3323 // method - handle for the intrinsic method
3324 // sig - signature of the intrinsic method
3325 // methodFlags - CORINFO_FLG_XXX flags of the intrinsic method
3326 // memberRef - the token for the intrinsic method
3327 // readonlyCall - true if call has a readonly prefix
3328 // tailCall - true if call is in tail position
3329 // pConstrainedResolvedToken -- resolved token for constrained call, or nullptr
3330 // if call is not constrained
3331 // constraintCallThisTransform -- this transform to apply for a constrained call
3332 // pIntrinsicID [OUT] -- intrinsic ID (see enumeration in corinfo.h)
3333 // for "traditional" jit intrinsics
3334 // isSpecialIntrinsic [OUT] -- set true if intrinsic expansion is a call
3335 // that is amenable to special downstream optimization opportunities
3338 // IR tree to use in place of the call, or nullptr if the jit should treat
3339 // the intrinsic call like a normal call.
3341 // pIntrinsicID set to non-illegal value if the call is recognized as a
3342 // traditional jit intrinsic, even if the intrinsic is not expaned.
3344 // isSpecial set true if the expansion is subject to special
3345 // optimizations later in the jit processing
3348 // On success the IR tree may be a call to a different method or an inline
3349 // sequence. If it is a call, then the intrinsic processing here is responsible
3350 // for handling all the special cases, as upon return to impImportCall
3351 // expanded intrinsics bypass most of the normal call processing.
3353 // Intrinsics are generally not recognized in minopts and debug codegen.
3355 // However, certain traditional intrinsics are identifed as "must expand"
3356 // if there is no fallback implmentation to invoke; these must be handled
3357 // in all codegen modes.
3359 // New style intrinsics (where the fallback implementation is in IL) are
3360 // identified as "must expand" if they are invoked from within their
3361 // own method bodies.
3364 GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
3365 CORINFO_CLASS_HANDLE clsHnd,
3366 CORINFO_METHOD_HANDLE method,
3367 CORINFO_SIG_INFO* sig,
3368 unsigned methodFlags,
3372 CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
3373 CORINFO_THIS_TRANSFORM constraintCallThisTransform,
3374 CorInfoIntrinsics* pIntrinsicID,
3375 bool* isSpecialIntrinsic)
3377 assert((methodFlags & (CORINFO_FLG_INTRINSIC | CORINFO_FLG_JIT_INTRINSIC)) != 0);
3379 bool mustExpand = false;
3380 bool isSpecial = false;
3381 CorInfoIntrinsics intrinsicID = CORINFO_INTRINSIC_Illegal;
3382 NamedIntrinsic ni = NI_Illegal;
3384 if ((methodFlags & CORINFO_FLG_INTRINSIC) != 0)
3386 intrinsicID = info.compCompHnd->getIntrinsicID(method, &mustExpand);
3389 if ((methodFlags & CORINFO_FLG_JIT_INTRINSIC) != 0)
3391 // The recursive calls to Jit intrinsics are must-expand by convention.
3392 mustExpand = mustExpand || gtIsRecursiveCall(method);
3394 if (intrinsicID == CORINFO_INTRINSIC_Illegal)
3396 ni = lookupNamedIntrinsic(method);
3398 #if FEATURE_HW_INTRINSICS
3399 #ifdef _TARGET_XARCH_
3400 if (ni > NI_HW_INTRINSIC_START && ni < NI_HW_INTRINSIC_END)
3402 return impX86HWIntrinsic(ni, method, sig, mustExpand);
3404 #endif // _TARGET_XARCH_
3405 #endif // FEATURE_HW_INTRINSICS
3409 *pIntrinsicID = intrinsicID;
3411 #ifndef _TARGET_ARM_
3412 genTreeOps interlockedOperator;
3415 if (intrinsicID == CORINFO_INTRINSIC_StubHelpers_GetStubContext)
3417 // must be done regardless of DbgCode and MinOpts
3418 return gtNewLclvNode(lvaStubArgumentVar, TYP_I_IMPL);
3420 #ifdef _TARGET_64BIT_
3421 if (intrinsicID == CORINFO_INTRINSIC_StubHelpers_GetStubContextAddr)
3423 // must be done regardless of DbgCode and MinOpts
3424 return gtNewOperNode(GT_ADDR, TYP_I_IMPL, gtNewLclvNode(lvaStubArgumentVar, TYP_I_IMPL));
3427 assert(intrinsicID != CORINFO_INTRINSIC_StubHelpers_GetStubContextAddr);
3430 GenTreePtr retNode = nullptr;
3432 // Under debug and minopts, only expand what is required.
3433 if (!mustExpand && (opts.compDbgCode || opts.MinOpts()))
3435 *pIntrinsicID = CORINFO_INTRINSIC_Illegal;
3439 var_types callType = JITtype2varType(sig->retType);
3441 /* First do the intrinsics which are always smaller than a call */
3443 switch (intrinsicID)
3445 GenTreePtr op1, op2;
3447 case CORINFO_INTRINSIC_Sin:
3448 case CORINFO_INTRINSIC_Cbrt:
3449 case CORINFO_INTRINSIC_Sqrt:
3450 case CORINFO_INTRINSIC_Abs:
3451 case CORINFO_INTRINSIC_Cos:
3452 case CORINFO_INTRINSIC_Round:
3453 case CORINFO_INTRINSIC_Cosh:
3454 case CORINFO_INTRINSIC_Sinh:
3455 case CORINFO_INTRINSIC_Tan:
3456 case CORINFO_INTRINSIC_Tanh:
3457 case CORINFO_INTRINSIC_Asin:
3458 case CORINFO_INTRINSIC_Asinh:
3459 case CORINFO_INTRINSIC_Acos:
3460 case CORINFO_INTRINSIC_Acosh:
3461 case CORINFO_INTRINSIC_Atan:
3462 case CORINFO_INTRINSIC_Atan2:
3463 case CORINFO_INTRINSIC_Atanh:
3464 case CORINFO_INTRINSIC_Log10:
3465 case CORINFO_INTRINSIC_Pow:
3466 case CORINFO_INTRINSIC_Exp:
3467 case CORINFO_INTRINSIC_Ceiling:
3468 case CORINFO_INTRINSIC_Floor:
3469 retNode = impMathIntrinsic(method, sig, callType, intrinsicID, tailCall);
3472 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3473 // TODO-ARM-CQ: reenable treating Interlocked operation as intrinsic
3474 case CORINFO_INTRINSIC_InterlockedAdd32:
3475 interlockedOperator = GT_LOCKADD;
3476 goto InterlockedBinOpCommon;
3477 case CORINFO_INTRINSIC_InterlockedXAdd32:
3478 interlockedOperator = GT_XADD;
3479 goto InterlockedBinOpCommon;
3480 case CORINFO_INTRINSIC_InterlockedXchg32:
3481 interlockedOperator = GT_XCHG;
3482 goto InterlockedBinOpCommon;
3484 #ifdef _TARGET_64BIT_
3485 case CORINFO_INTRINSIC_InterlockedAdd64:
3486 interlockedOperator = GT_LOCKADD;
3487 goto InterlockedBinOpCommon;
3488 case CORINFO_INTRINSIC_InterlockedXAdd64:
3489 interlockedOperator = GT_XADD;
3490 goto InterlockedBinOpCommon;
3491 case CORINFO_INTRINSIC_InterlockedXchg64:
3492 interlockedOperator = GT_XCHG;
3493 goto InterlockedBinOpCommon;
3494 #endif // _TARGET_AMD64_
3496 InterlockedBinOpCommon:
3497 assert(callType != TYP_STRUCT);
3498 assert(sig->numArgs == 2);
3500 op2 = impPopStack().val;
3501 op1 = impPopStack().val;
3507 // field (for example)
3509 // In the case where the first argument is the address of a local, we might
3510 // want to make this *not* make the var address-taken -- but atomic instructions
3511 // on a local are probably pretty useless anyway, so we probably don't care.
3513 op1 = gtNewOperNode(interlockedOperator, genActualType(callType), op1, op2);
3514 op1->gtFlags |= GTF_GLOB_REF | GTF_ASG;
3517 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3519 case CORINFO_INTRINSIC_MemoryBarrier:
3521 assert(sig->numArgs == 0);
3523 op1 = new (this, GT_MEMORYBARRIER) GenTree(GT_MEMORYBARRIER, TYP_VOID);
3524 op1->gtFlags |= GTF_GLOB_REF | GTF_ASG;
3528 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3529 // TODO-ARM-CQ: reenable treating InterlockedCmpXchg32 operation as intrinsic
3530 case CORINFO_INTRINSIC_InterlockedCmpXchg32:
3531 #ifdef _TARGET_64BIT_
3532 case CORINFO_INTRINSIC_InterlockedCmpXchg64:
3535 assert(callType != TYP_STRUCT);
3536 assert(sig->numArgs == 3);
3539 op3 = impPopStack().val; // comparand
3540 op2 = impPopStack().val; // value
3541 op1 = impPopStack().val; // location
3543 GenTreePtr node = new (this, GT_CMPXCHG) GenTreeCmpXchg(genActualType(callType), op1, op2, op3);
3545 node->gtCmpXchg.gtOpLocation->gtFlags |= GTF_DONT_CSE;
3549 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3551 case CORINFO_INTRINSIC_StringLength:
3552 op1 = impPopStack().val;
3553 if (!opts.MinOpts() && !opts.compDbgCode)
3555 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, op1, offsetof(CORINFO_String, stringLen));
3560 /* Create the expression "*(str_addr + stringLengthOffset)" */
3561 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1,
3562 gtNewIconNode(offsetof(CORINFO_String, stringLen), TYP_I_IMPL));
3563 op1 = gtNewOperNode(GT_IND, TYP_INT, op1);
3566 // Getting the length of a null string should throw
3567 op1->gtFlags |= GTF_EXCEPT;
3572 case CORINFO_INTRINSIC_StringGetChar:
3573 op2 = impPopStack().val;
3574 op1 = impPopStack().val;
3575 op1 = gtNewIndexRef(TYP_USHORT, op1, op2);
3576 op1->gtFlags |= GTF_INX_STRING_LAYOUT;
3580 case CORINFO_INTRINSIC_InitializeArray:
3581 retNode = impInitializeArrayIntrinsic(sig);
3584 case CORINFO_INTRINSIC_Array_Address:
3585 case CORINFO_INTRINSIC_Array_Get:
3586 case CORINFO_INTRINSIC_Array_Set:
3587 retNode = impArrayAccessIntrinsic(clsHnd, sig, memberRef, readonlyCall, intrinsicID);
3590 case CORINFO_INTRINSIC_GetTypeFromHandle:
3591 op1 = impStackTop(0).val;
3592 if (op1->gtOper == GT_CALL && (op1->gtCall.gtCallType == CT_HELPER) &&
3593 gtIsTypeHandleToRuntimeTypeHelper(op1->AsCall()))
3595 op1 = impPopStack().val;
3596 // Change call to return RuntimeType directly.
3597 op1->gtType = TYP_REF;
3600 // Call the regular function.
3603 case CORINFO_INTRINSIC_RTH_GetValueInternal:
3604 op1 = impStackTop(0).val;
3605 if (op1->gtOper == GT_CALL && (op1->gtCall.gtCallType == CT_HELPER) &&
3606 gtIsTypeHandleToRuntimeTypeHelper(op1->AsCall()))
3609 // Helper-RuntimeTypeHandle -> TreeToGetNativeTypeHandle
3612 // TreeToGetNativeTypeHandle
3614 // Remove call to helper and return the native TypeHandle pointer that was the parameter
3617 op1 = impPopStack().val;
3619 // Get native TypeHandle argument to old helper
3620 op1 = op1->gtCall.gtCallArgs;
3621 assert(op1->OperIsList());
3622 assert(op1->gtOp.gtOp2 == nullptr);
3623 op1 = op1->gtOp.gtOp1;
3626 // Call the regular function.
3629 #ifndef LEGACY_BACKEND
3630 case CORINFO_INTRINSIC_Object_GetType:
3632 JITDUMP("\n impIntrinsic: call to Object.GetType\n");
3633 op1 = impStackTop(0).val;
3635 // If we're calling GetType on a boxed value, just get the type directly.
3636 if (op1->IsBoxedValue())
3638 JITDUMP("Attempting to optimize box(...).getType() to direct type construction\n");
3640 // Try and clean up the box. Obtain the handle we
3641 // were going to pass to the newobj.
3642 GenTree* boxTypeHandle = gtTryRemoveBoxUpstreamEffects(op1, BR_REMOVE_AND_NARROW_WANT_TYPE_HANDLE);
3644 if (boxTypeHandle != nullptr)
3646 // Note we don't need to play the TYP_STRUCT games here like
3647 // do for LDTOKEN since the return value of this operator is Type,
3648 // not RuntimeTypeHandle.
3650 GenTreeArgList* helperArgs = gtNewArgList(boxTypeHandle);
3651 GenTree* runtimeType =
3652 gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, helperArgs);
3653 retNode = runtimeType;
3657 // If we have a constrained callvirt with a "box this" transform
3658 // we know we have a value class and hence an exact type.
3660 // If so, instead of boxing and then extracting the type, just
3661 // construct the type directly.
3662 if ((retNode == nullptr) && (pConstrainedResolvedToken != nullptr) &&
3663 (constraintCallThisTransform == CORINFO_BOX_THIS))
3665 // Ensure this is one of the is simple box cases (in particular, rule out nullables).
3666 const CorInfoHelpFunc boxHelper = info.compCompHnd->getBoxHelper(pConstrainedResolvedToken->hClass);
3667 const bool isSafeToOptimize = (boxHelper == CORINFO_HELP_BOX);
3669 if (isSafeToOptimize)
3671 JITDUMP("Optimizing constrained box-this obj.getType() to direct type construction\n");
3673 GenTree* typeHandleOp =
3674 impTokenToHandle(pConstrainedResolvedToken, nullptr, TRUE /* mustRestoreHandle */);
3675 GenTreeArgList* helperArgs = gtNewArgList(typeHandleOp);
3676 GenTree* runtimeType =
3677 gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, helperArgs);
3678 retNode = runtimeType;
3683 if (retNode != nullptr)
3685 JITDUMP("Optimized result for call to GetType is\n");
3688 gtDispTree(retNode);
3693 // Else expand as an intrinsic, unless the call is constrained,
3694 // in which case we defer expansion to allow impImportCall do the
3695 // special constraint processing.
3696 if ((retNode == nullptr) && (pConstrainedResolvedToken == nullptr))
3698 JITDUMP("Expanding as special intrinsic\n");
3700 op1 = new (this, GT_INTRINSIC) GenTreeIntrinsic(genActualType(callType), op1, intrinsicID, method);
3702 // Set the CALL flag to indicate that the operator is implemented by a call.
3703 // Set also the EXCEPTION flag because the native implementation of
3704 // CORINFO_INTRINSIC_Object_GetType intrinsic can throw NullReferenceException.
3705 op1->gtFlags |= (GTF_CALL | GTF_EXCEPT);
3707 // Might be further optimizable, so arrange to leave a mark behind
3711 if (retNode == nullptr)
3713 JITDUMP("Leaving as normal call\n");
3714 // Might be further optimizable, so arrange to leave a mark behind
3722 // Implement ByReference Ctor. This wraps the assignment of the ref into a byref-like field
3723 // in a value type. The canonical example of this is Span<T>. In effect this is just a
3724 // substitution. The parameter byref will be assigned into the newly allocated object.
3725 case CORINFO_INTRINSIC_ByReference_Ctor:
3727 // Remove call to constructor and directly assign the byref passed
3728 // to the call to the first slot of the ByReference struct.
3729 op1 = impPopStack().val;
3730 GenTreePtr thisptr = newobjThis;
3731 CORINFO_FIELD_HANDLE fldHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
3732 GenTreePtr field = gtNewFieldRef(TYP_BYREF, fldHnd, thisptr, 0, false);
3733 GenTreePtr assign = gtNewAssignNode(field, op1);
3734 GenTreePtr byReferenceStruct = gtCloneExpr(thisptr->gtGetOp1());
3735 assert(byReferenceStruct != nullptr);
3736 impPushOnStack(byReferenceStruct, typeInfo(TI_STRUCT, clsHnd));
3740 // Implement ptr value getter for ByReference struct.
3741 case CORINFO_INTRINSIC_ByReference_Value:
3743 op1 = impPopStack().val;
3744 CORINFO_FIELD_HANDLE fldHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
3745 GenTreePtr field = gtNewFieldRef(TYP_BYREF, fldHnd, op1, 0, false);
3749 case CORINFO_INTRINSIC_Span_GetItem:
3750 case CORINFO_INTRINSIC_ReadOnlySpan_GetItem:
3752 // Have index, stack pointer-to Span<T> s on the stack. Expand to:
3756 // BoundsCheck(index, s->_length)
3757 // s->_pointer + index * sizeof(T)
3759 // For ReadOnlySpan<T> -- same expansion, as it now returns a readonly ref
3761 // Signature should show one class type parameter, which
3762 // we need to examine.
3763 assert(sig->sigInst.classInstCount == 1);
3764 CORINFO_CLASS_HANDLE spanElemHnd = sig->sigInst.classInst[0];
3765 const unsigned elemSize = info.compCompHnd->getClassSize(spanElemHnd);
3766 assert(elemSize > 0);
3768 const bool isReadOnly = (intrinsicID == CORINFO_INTRINSIC_ReadOnlySpan_GetItem);
3770 JITDUMP("\nimpIntrinsic: Expanding %sSpan<T>.get_Item, T=%s, sizeof(T)=%u\n", isReadOnly ? "ReadOnly" : "",
3771 info.compCompHnd->getClassName(spanElemHnd), elemSize);
3773 GenTreePtr index = impPopStack().val;
3774 GenTreePtr ptrToSpan = impPopStack().val;
3775 GenTreePtr indexClone = nullptr;
3776 GenTreePtr ptrToSpanClone = nullptr;
3781 printf("with ptr-to-span\n");
3782 gtDispTree(ptrToSpan);
3783 printf("and index\n");
3786 #endif // defined(DEBUG)
3788 // We need to use both index and ptr-to-span twice, so clone or spill.
3789 index = impCloneExpr(index, &indexClone, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
3790 nullptr DEBUGARG("Span.get_Item index"));
3791 ptrToSpan = impCloneExpr(ptrToSpan, &ptrToSpanClone, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
3792 nullptr DEBUGARG("Span.get_Item ptrToSpan"));
3795 CORINFO_FIELD_HANDLE lengthHnd = info.compCompHnd->getFieldInClass(clsHnd, 1);
3796 const unsigned lengthOffset = info.compCompHnd->getFieldOffset(lengthHnd);
3797 GenTreePtr length = gtNewFieldRef(TYP_INT, lengthHnd, ptrToSpan, lengthOffset, false);
3798 GenTreePtr boundsCheck = new (this, GT_ARR_BOUNDS_CHECK)
3799 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, length, SCK_RNGCHK_FAIL);
3802 GenTreePtr indexIntPtr = impImplicitIorI4Cast(indexClone, TYP_I_IMPL);
3803 GenTreePtr sizeofNode = gtNewIconNode(elemSize);
3804 GenTreePtr mulNode = gtNewOperNode(GT_MUL, TYP_I_IMPL, indexIntPtr, sizeofNode);
3805 CORINFO_FIELD_HANDLE ptrHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
3806 const unsigned ptrOffset = info.compCompHnd->getFieldOffset(ptrHnd);
3807 GenTreePtr data = gtNewFieldRef(TYP_BYREF, ptrHnd, ptrToSpanClone, ptrOffset, false);
3808 GenTreePtr result = gtNewOperNode(GT_ADD, TYP_BYREF, data, mulNode);
3811 var_types resultType = JITtype2varType(sig->retType);
3812 assert(resultType == result->TypeGet());
3813 retNode = gtNewOperNode(GT_COMMA, resultType, boundsCheck, result);
3818 case CORINFO_INTRINSIC_GetRawHandle:
3820 noway_assert(IsTargetAbi(CORINFO_CORERT_ABI)); // Only CoreRT supports it.
3821 CORINFO_RESOLVED_TOKEN resolvedToken;
3822 resolvedToken.tokenContext = MAKE_METHODCONTEXT(info.compMethodHnd);
3823 resolvedToken.tokenScope = info.compScopeHnd;
3824 resolvedToken.token = memberRef;
3825 resolvedToken.tokenType = CORINFO_TOKENKIND_Method;
3827 CORINFO_GENERICHANDLE_RESULT embedInfo;
3828 info.compCompHnd->expandRawHandleIntrinsic(&resolvedToken, &embedInfo);
3830 GenTreePtr rawHandle = impLookupToTree(&resolvedToken, &embedInfo.lookup, gtTokenToIconFlags(memberRef),
3831 embedInfo.compileTimeHandle);
3832 if (rawHandle == nullptr)
3837 noway_assert(genTypeSize(rawHandle->TypeGet()) == genTypeSize(TYP_I_IMPL));
3839 unsigned rawHandleSlot = lvaGrabTemp(true DEBUGARG("rawHandle"));
3840 impAssignTempGen(rawHandleSlot, rawHandle, clsHnd, (unsigned)CHECK_SPILL_NONE);
3842 GenTreePtr lclVar = gtNewLclvNode(rawHandleSlot, TYP_I_IMPL);
3843 GenTreePtr lclVarAddr = gtNewOperNode(GT_ADDR, TYP_I_IMPL, lclVar);
3844 var_types resultType = JITtype2varType(sig->retType);
3845 retNode = gtNewOperNode(GT_IND, resultType, lclVarAddr);
3850 case CORINFO_INTRINSIC_TypeEQ:
3851 case CORINFO_INTRINSIC_TypeNEQ:
3853 JITDUMP("Importing Type.op_*Equality intrinsic\n");
3854 op1 = impStackTop(1).val;
3855 op2 = impStackTop(0).val;
3856 GenTree* optTree = gtFoldTypeEqualityCall(intrinsicID, op1, op2);
3857 if (optTree != nullptr)
3859 // Success, clean up the evaluation stack.
3863 // See if we can optimize even further, to a handle compare.
3864 optTree = gtFoldTypeCompare(optTree);
3866 // See if we can now fold a handle compare to a constant.
3867 optTree = gtFoldExpr(optTree);
3873 // Retry optimizing these later
3879 case CORINFO_INTRINSIC_GetCurrentManagedThread:
3880 case CORINFO_INTRINSIC_GetManagedThreadId:
3882 // Retry optimizing these during morph
3888 /* Unknown intrinsic */
3889 intrinsicID = CORINFO_INTRINSIC_Illegal;
3893 // Look for new-style jit intrinsics by name
3894 if (ni != NI_Illegal)
3896 assert(retNode == nullptr);
3899 case NI_System_Enum_HasFlag:
3901 GenTree* thisOp = impStackTop(1).val;
3902 GenTree* flagOp = impStackTop(0).val;
3903 GenTree* optTree = gtOptimizeEnumHasFlag(thisOp, flagOp);
3905 if (optTree != nullptr)
3907 // Optimization successful. Pop the stack for real.
3914 // Retry optimizing this during morph.
3921 case NI_MathF_Round:
3924 // Math.Round and MathF.Round used to be a traditional JIT intrinsic. In order
3925 // to simplify the transition, we will just treat it as if it was still the
3926 // old intrinsic, CORINFO_INTRINSIC_Round. This should end up flowing properly
3929 retNode = impMathIntrinsic(method, sig, callType, CORINFO_INTRINSIC_Round, tailCall);
3933 case NI_System_Collections_Generic_EqualityComparer_get_Default:
3935 // Flag for later handling during devirtualization.
3947 if (retNode == nullptr)
3949 NO_WAY("JIT must expand the intrinsic!");
3953 // Optionally report if this intrinsic is special
3954 // (that is, potentially re-optimizable during morph).
3955 if (isSpecialIntrinsic != nullptr)
3957 *isSpecialIntrinsic = isSpecial;
3963 GenTree* Compiler::impMathIntrinsic(CORINFO_METHOD_HANDLE method,
3964 CORINFO_SIG_INFO* sig,
3966 CorInfoIntrinsics intrinsicID,
3972 assert(callType != TYP_STRUCT);
3973 assert((intrinsicID == CORINFO_INTRINSIC_Sin) || intrinsicID == CORINFO_INTRINSIC_Cbrt ||
3974 (intrinsicID == CORINFO_INTRINSIC_Sqrt) || (intrinsicID == CORINFO_INTRINSIC_Abs) ||
3975 (intrinsicID == CORINFO_INTRINSIC_Cos) || (intrinsicID == CORINFO_INTRINSIC_Round) ||
3976 (intrinsicID == CORINFO_INTRINSIC_Cosh) || (intrinsicID == CORINFO_INTRINSIC_Sinh) ||
3977 (intrinsicID == CORINFO_INTRINSIC_Tan) || (intrinsicID == CORINFO_INTRINSIC_Tanh) ||
3978 (intrinsicID == CORINFO_INTRINSIC_Asin) || (intrinsicID == CORINFO_INTRINSIC_Asinh) ||
3979 (intrinsicID == CORINFO_INTRINSIC_Acos) || (intrinsicID == CORINFO_INTRINSIC_Acosh) ||
3980 (intrinsicID == CORINFO_INTRINSIC_Atan) || (intrinsicID == CORINFO_INTRINSIC_Atan2) ||
3981 (intrinsicID == CORINFO_INTRINSIC_Atanh) || (intrinsicID == CORINFO_INTRINSIC_Log10) ||
3982 (intrinsicID == CORINFO_INTRINSIC_Pow) || (intrinsicID == CORINFO_INTRINSIC_Exp) ||
3983 (intrinsicID == CORINFO_INTRINSIC_Ceiling) || (intrinsicID == CORINFO_INTRINSIC_Floor));
3987 #if defined(LEGACY_BACKEND)
3988 if (IsTargetIntrinsic(intrinsicID))
3989 #elif !defined(_TARGET_X86_)
3990 // Intrinsics that are not implemented directly by target instructions will
3991 // be re-materialized as users calls in rationalizer. For prefixed tail calls,
3992 // don't do this optimization, because
3993 // a) For back compatibility reasons on desktop.Net 4.6 / 4.6.1
3994 // b) It will be non-trivial task or too late to re-materialize a surviving
3995 // tail prefixed GT_INTRINSIC as tail call in rationalizer.
3996 if (!IsIntrinsicImplementedByUserCall(intrinsicID) || !tailCall)
3998 // On x86 RyuJIT, importing intrinsics that are implemented as user calls can cause incorrect calculation
3999 // of the depth of the stack if these intrinsics are used as arguments to another call. This causes bad
4000 // code generation for certain EH constructs.
4001 if (!IsIntrinsicImplementedByUserCall(intrinsicID))
4004 switch (sig->numArgs)
4007 op1 = impPopStack().val;
4009 #if FEATURE_X87_DOUBLES
4011 // X87 stack doesn't differentiate between float/double
4012 // so it doesn't need a cast, but everybody else does
4013 // Just double check it is at least a FP type
4014 noway_assert(varTypeIsFloating(op1));
4016 #else // FEATURE_X87_DOUBLES
4018 if (op1->TypeGet() != callType)
4020 op1 = gtNewCastNode(callType, op1, callType);
4023 #endif // FEATURE_X87_DOUBLES
4025 op1 = new (this, GT_INTRINSIC) GenTreeIntrinsic(genActualType(callType), op1, intrinsicID, method);
4029 op2 = impPopStack().val;
4030 op1 = impPopStack().val;
4032 #if FEATURE_X87_DOUBLES
4034 // X87 stack doesn't differentiate between float/double
4035 // so it doesn't need a cast, but everybody else does
4036 // Just double check it is at least a FP type
4037 noway_assert(varTypeIsFloating(op2));
4038 noway_assert(varTypeIsFloating(op1));
4040 #else // FEATURE_X87_DOUBLES
4042 if (op2->TypeGet() != callType)
4044 op2 = gtNewCastNode(callType, op2, callType);
4046 if (op1->TypeGet() != callType)
4048 op1 = gtNewCastNode(callType, op1, callType);
4051 #endif // FEATURE_X87_DOUBLES
4053 op1 = new (this, GT_INTRINSIC) GenTreeIntrinsic(genActualType(callType), op1, op2, intrinsicID, method);
4057 NO_WAY("Unsupported number of args for Math Instrinsic");
4060 #ifndef LEGACY_BACKEND
4061 if (IsIntrinsicImplementedByUserCall(intrinsicID))
4063 op1->gtFlags |= GTF_CALL;
4071 //------------------------------------------------------------------------
4072 // lookupNamedIntrinsic: map method to jit named intrinsic value
4075 // method -- method handle for method
4078 // Id for the named intrinsic, or Illegal if none.
4081 // method should have CORINFO_FLG_JIT_INTRINSIC set in its attributes,
4082 // otherwise it is not a named jit intrinsic.
4085 NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
4087 NamedIntrinsic result = NI_Illegal;
4089 const char* className = nullptr;
4090 const char* namespaceName = nullptr;
4091 const char* methodName = info.compCompHnd->getMethodNameFromMetadata(method, &className, &namespaceName);
4093 if ((namespaceName == nullptr) || (className == nullptr) || (methodName == nullptr))
4098 if (strcmp(namespaceName, "System") == 0)
4100 if ((strcmp(className, "Enum") == 0) && (strcmp(methodName, "HasFlag") == 0))
4102 result = NI_System_Enum_HasFlag;
4104 else if ((strcmp(className, "MathF") == 0) && (strcmp(methodName, "Round") == 0))
4106 result = NI_MathF_Round;
4108 else if ((strcmp(className, "Math") == 0) && (strcmp(methodName, "Round") == 0))
4110 result = NI_Math_Round;
4113 else if (strcmp(namespaceName, "System.Collections.Generic") == 0)
4115 if ((strcmp(className, "EqualityComparer`1") == 0) && (strcmp(methodName, "get_Default") == 0))
4117 result = NI_System_Collections_Generic_EqualityComparer_get_Default;
4121 #if FEATURE_HW_INTRINSICS && defined(_TARGET_XARCH_)
4122 if ((namespaceName != nullptr) && strcmp(namespaceName, "System.Runtime.Intrinsics.X86") == 0)
4124 InstructionSet isa = lookupHWIntrinsicISA(className);
4125 result = lookupHWIntrinsic(methodName, isa);
4127 #endif // FEATURE_HW_INTRINSICS
4131 /*****************************************************************************/
4133 GenTreePtr Compiler::impArrayAccessIntrinsic(
4134 CORINFO_CLASS_HANDLE clsHnd, CORINFO_SIG_INFO* sig, int memberRef, bool readonlyCall, CorInfoIntrinsics intrinsicID)
4136 /* If we are generating SMALL_CODE, we don't want to use intrinsics for
4137 the following, as it generates fatter code.
4140 if (compCodeOpt() == SMALL_CODE)
4145 /* These intrinsics generate fatter (but faster) code and are only
4146 done if we don't need SMALL_CODE */
4148 unsigned rank = (intrinsicID == CORINFO_INTRINSIC_Array_Set) ? (sig->numArgs - 1) : sig->numArgs;
4150 // The rank 1 case is special because it has to handle two array formats
4151 // we will simply not do that case
4152 if (rank > GT_ARR_MAX_RANK || rank <= 1)
4157 CORINFO_CLASS_HANDLE arrElemClsHnd = nullptr;
4158 var_types elemType = JITtype2varType(info.compCompHnd->getChildType(clsHnd, &arrElemClsHnd));
4160 // For the ref case, we will only be able to inline if the types match
4161 // (verifier checks for this, we don't care for the nonverified case and the
4162 // type is final (so we don't need to do the cast)
4163 if ((intrinsicID != CORINFO_INTRINSIC_Array_Get) && !readonlyCall && varTypeIsGC(elemType))
4165 // Get the call site signature
4166 CORINFO_SIG_INFO LocalSig;
4167 eeGetCallSiteSig(memberRef, info.compScopeHnd, impTokenLookupContextHandle, &LocalSig);
4168 assert(LocalSig.hasThis());
4170 CORINFO_CLASS_HANDLE actualElemClsHnd;
4172 if (intrinsicID == CORINFO_INTRINSIC_Array_Set)
4174 // Fetch the last argument, the one that indicates the type we are setting.
4175 CORINFO_ARG_LIST_HANDLE argType = LocalSig.args;
4176 for (unsigned r = 0; r < rank; r++)
4178 argType = info.compCompHnd->getArgNext(argType);
4181 typeInfo argInfo = verParseArgSigToTypeInfo(&LocalSig, argType);
4182 actualElemClsHnd = argInfo.GetClassHandle();
4186 assert(intrinsicID == CORINFO_INTRINSIC_Array_Address);
4188 // Fetch the return type
4189 typeInfo retInfo = verMakeTypeInfo(LocalSig.retType, LocalSig.retTypeClass);
4190 assert(retInfo.IsByRef());
4191 actualElemClsHnd = retInfo.GetClassHandle();
4194 // if it's not final, we can't do the optimization
4195 if (!(info.compCompHnd->getClassAttribs(actualElemClsHnd) & CORINFO_FLG_FINAL))
4201 unsigned arrayElemSize;
4202 if (elemType == TYP_STRUCT)
4204 assert(arrElemClsHnd);
4206 arrayElemSize = info.compCompHnd->getClassSize(arrElemClsHnd);
4210 arrayElemSize = genTypeSize(elemType);
4213 if ((unsigned char)arrayElemSize != arrayElemSize)
4215 // arrayElemSize would be truncated as an unsigned char.
4216 // This means the array element is too large. Don't do the optimization.
4220 GenTreePtr val = nullptr;
4222 if (intrinsicID == CORINFO_INTRINSIC_Array_Set)
4224 // Assignment of a struct is more work, and there are more gets than sets.
4225 if (elemType == TYP_STRUCT)
4230 val = impPopStack().val;
4231 assert(genActualType(elemType) == genActualType(val->gtType) ||
4232 (elemType == TYP_FLOAT && val->gtType == TYP_DOUBLE) ||
4233 (elemType == TYP_INT && val->gtType == TYP_BYREF) ||
4234 (elemType == TYP_DOUBLE && val->gtType == TYP_FLOAT));
4237 noway_assert((unsigned char)GT_ARR_MAX_RANK == GT_ARR_MAX_RANK);
4239 GenTreePtr inds[GT_ARR_MAX_RANK];
4240 for (unsigned k = rank; k > 0; k--)
4242 inds[k - 1] = impPopStack().val;
4245 GenTreePtr arr = impPopStack().val;
4246 assert(arr->gtType == TYP_REF);
4248 GenTreePtr arrElem =
4249 new (this, GT_ARR_ELEM) GenTreeArrElem(TYP_BYREF, arr, static_cast<unsigned char>(rank),
4250 static_cast<unsigned char>(arrayElemSize), elemType, &inds[0]);
4252 if (intrinsicID != CORINFO_INTRINSIC_Array_Address)
4254 arrElem = gtNewOperNode(GT_IND, elemType, arrElem);
4257 if (intrinsicID == CORINFO_INTRINSIC_Array_Set)
4259 assert(val != nullptr);
4260 return gtNewAssignNode(arrElem, val);
4268 BOOL Compiler::verMergeEntryStates(BasicBlock* block, bool* changed)
4272 // do some basic checks first
4273 if (block->bbStackDepthOnEntry() != verCurrentState.esStackDepth)
4278 if (verCurrentState.esStackDepth > 0)
4280 // merge stack types
4281 StackEntry* parentStack = block->bbStackOnEntry();
4282 StackEntry* childStack = verCurrentState.esStack;
4284 for (i = 0; i < verCurrentState.esStackDepth; i++, parentStack++, childStack++)
4286 if (tiMergeToCommonParent(&parentStack->seTypeInfo, &childStack->seTypeInfo, changed) == FALSE)
4293 // merge initialization status of this ptr
4295 if (verTrackObjCtorInitState)
4297 // If we're tracking the CtorInitState, then it must not be unknown in the current state.
4298 assert(verCurrentState.thisInitialized != TIS_Bottom);
4300 // If the successor block's thisInit state is unknown, copy it from the current state.
4301 if (block->bbThisOnEntry() == TIS_Bottom)
4304 verSetThisInit(block, verCurrentState.thisInitialized);
4306 else if (verCurrentState.thisInitialized != block->bbThisOnEntry())
4308 if (block->bbThisOnEntry() != TIS_Top)
4311 verSetThisInit(block, TIS_Top);
4313 if (block->bbFlags & BBF_FAILED_VERIFICATION)
4315 // The block is bad. Control can flow through the block to any handler that catches the
4316 // verification exception, but the importer ignores bad blocks and therefore won't model
4317 // this flow in the normal way. To complete the merge into the bad block, the new state
4318 // needs to be manually pushed to the handlers that may be reached after the verification
4319 // exception occurs.
4321 // Usually, the new state was already propagated to the relevant handlers while processing
4322 // the predecessors of the bad block. The exception is when the bad block is at the start
4323 // of a try region, meaning it is protected by additional handlers that do not protect its
4326 if (block->hasTryIndex() && ((block->bbFlags & BBF_TRY_BEG) != 0))
4328 // Push TIS_Top to the handlers that protect the bad block. Note that this can cause
4329 // recursive calls back into this code path (if successors of the current bad block are
4330 // also bad blocks).
4332 ThisInitState origTIS = verCurrentState.thisInitialized;
4333 verCurrentState.thisInitialized = TIS_Top;
4334 impVerifyEHBlock(block, true);
4335 verCurrentState.thisInitialized = origTIS;
4343 assert(verCurrentState.thisInitialized == TIS_Bottom && block->bbThisOnEntry() == TIS_Bottom);
4349 /*****************************************************************************
4350 * 'logMsg' is true if a log message needs to be logged. false if the caller has
4351 * already logged it (presumably in a more detailed fashion than done here)
4352 * 'bVerificationException' is true for a verification exception, false for a
4353 * "call unauthorized by host" exception.
4356 void Compiler::verConvertBBToThrowVerificationException(BasicBlock* block DEBUGARG(bool logMsg))
4358 block->bbJumpKind = BBJ_THROW;
4359 block->bbFlags |= BBF_FAILED_VERIFICATION;
4361 impCurStmtOffsSet(block->bbCodeOffs);
4364 // we need this since BeginTreeList asserts otherwise
4365 impTreeList = impTreeLast = nullptr;
4366 block->bbFlags &= ~BBF_IMPORTED;
4370 JITLOG((LL_ERROR, "Verification failure: while compiling %s near IL offset %x..%xh \n", info.compFullName,
4371 block->bbCodeOffs, block->bbCodeOffsEnd));
4374 printf("\n\nVerification failure: %s near IL %xh \n", info.compFullName, block->bbCodeOffs);
4378 if (JitConfig.DebugBreakOnVerificationFailure())
4386 // if the stack is non-empty evaluate all the side-effects
4387 if (verCurrentState.esStackDepth > 0)
4389 impEvalSideEffects();
4391 assert(verCurrentState.esStackDepth == 0);
4394 gtNewHelperCallNode(CORINFO_HELP_VERIFICATION, TYP_VOID, gtNewArgList(gtNewIconNode(block->bbCodeOffs)));
4395 // verCurrentState.esStackDepth = 0;
4396 impAppendTree(op1, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
4398 // The inliner is not able to handle methods that require throw block, so
4399 // make sure this methods never gets inlined.
4400 info.compCompHnd->setMethodAttribs(info.compMethodHnd, CORINFO_FLG_BAD_INLINEE);
4403 /*****************************************************************************
4406 void Compiler::verHandleVerificationFailure(BasicBlock* block DEBUGARG(bool logMsg))
4409 // In AMD64, for historical reasons involving design limitations of JIT64, the VM has a
4410 // slightly different mechanism in which it calls the JIT to perform IL verification:
4411 // in the case of transparent methods the VM calls for a predicate IsVerifiable()
4412 // that consists of calling the JIT with the IMPORT_ONLY flag and with the IL verify flag on.
4413 // If the JIT determines the method is not verifiable, it should raise the exception to the VM and let
4414 // it bubble up until reported by the runtime. Currently in RyuJIT, this method doesn't bubble
4415 // up the exception, instead it embeds a throw inside the offending basic block and lets this
4416 // to fail upon runtime of the jitted method.
4418 // For AMD64 we don't want this behavior when the JIT has been called only for verification (i.e.
4419 // with the IMPORT_ONLY and IL Verification flag set) because this won't actually generate code,
4420 // just try to find out whether to fail this method before even actually jitting it. So, in case
4421 // we detect these two conditions, instead of generating a throw statement inside the offending
4422 // basic block, we immediately fail to JIT and notify the VM to make the IsVerifiable() predicate
4423 // to return false and make RyuJIT behave the same way JIT64 does.
4425 // The rationale behind this workaround is to avoid modifying the VM and maintain compatibility between JIT64 and
4426 // RyuJIT for the time being until we completely replace JIT64.
4427 // TODO-ARM64-Cleanup: We probably want to actually modify the VM in the future to avoid the unnecesary two passes.
4429 // In AMD64 we must make sure we're behaving the same way as JIT64, meaning we should only raise the verification
4430 // exception if we are only importing and verifying. The method verNeedsVerification() can also modify the
4431 // tiVerificationNeeded flag in the case it determines it can 'skip verification' during importation and defer it
4432 // to a runtime check. That's why we must assert one or the other (since the flag tiVerificationNeeded can
4433 // be turned off during importation).
4434 CLANG_FORMAT_COMMENT_ANCHOR;
4436 #ifdef _TARGET_64BIT_
4439 bool canSkipVerificationResult =
4440 info.compCompHnd->canSkipMethodVerification(info.compMethodHnd) != CORINFO_VERIFICATION_CANNOT_SKIP;
4441 assert(tiVerificationNeeded || canSkipVerificationResult);
4444 // Add the non verifiable flag to the compiler
4445 if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IMPORT_ONLY))
4447 tiIsVerifiableCode = FALSE;
4449 #endif //_TARGET_64BIT_
4450 verResetCurrentState(block, &verCurrentState);
4451 verConvertBBToThrowVerificationException(block DEBUGARG(logMsg));
4454 impNoteLastILoffs(); // Remember at which BC offset the tree was finished
4458 /******************************************************************************/
4459 typeInfo Compiler::verMakeTypeInfo(CorInfoType ciType, CORINFO_CLASS_HANDLE clsHnd)
4461 assert(ciType < CORINFO_TYPE_COUNT);
4466 case CORINFO_TYPE_STRING:
4467 case CORINFO_TYPE_CLASS:
4468 tiResult = verMakeTypeInfo(clsHnd);
4469 if (!tiResult.IsType(TI_REF))
4470 { // type must be consistent with element type
4475 #ifdef _TARGET_64BIT_
4476 case CORINFO_TYPE_NATIVEINT:
4477 case CORINFO_TYPE_NATIVEUINT:
4480 // If we have more precise information, use it
4481 return verMakeTypeInfo(clsHnd);
4485 return typeInfo::nativeInt();
4488 #endif // _TARGET_64BIT_
4490 case CORINFO_TYPE_VALUECLASS:
4491 case CORINFO_TYPE_REFANY:
4492 tiResult = verMakeTypeInfo(clsHnd);
4493 // type must be constant with element type;
4494 if (!tiResult.IsValueClass())
4499 case CORINFO_TYPE_VAR:
4500 return verMakeTypeInfo(clsHnd);
4502 case CORINFO_TYPE_PTR: // for now, pointers are treated as an error
4503 case CORINFO_TYPE_VOID:
4507 case CORINFO_TYPE_BYREF:
4509 CORINFO_CLASS_HANDLE childClassHandle;
4510 CorInfoType childType = info.compCompHnd->getChildType(clsHnd, &childClassHandle);
4511 return ByRef(verMakeTypeInfo(childType, childClassHandle));
4517 { // If we have more precise information, use it
4518 return typeInfo(TI_STRUCT, clsHnd);
4522 return typeInfo(JITtype2tiType(ciType));
4528 /******************************************************************************/
4530 typeInfo Compiler::verMakeTypeInfo(CORINFO_CLASS_HANDLE clsHnd, bool bashStructToRef /* = false */)
4532 if (clsHnd == nullptr)
4537 // Byrefs should only occur in method and local signatures, which are accessed
4538 // using ICorClassInfo and ICorClassInfo.getChildType.
4539 // So findClass() and getClassAttribs() should not be called for byrefs
4541 if (JITtype2varType(info.compCompHnd->asCorInfoType(clsHnd)) == TYP_BYREF)
4543 assert(!"Did findClass() return a Byref?");
4547 unsigned attribs = info.compCompHnd->getClassAttribs(clsHnd);
4549 if (attribs & CORINFO_FLG_VALUECLASS)
4551 CorInfoType t = info.compCompHnd->getTypeForPrimitiveValueClass(clsHnd);
4553 // Meta-data validation should ensure that CORINF_TYPE_BYREF should
4554 // not occur here, so we may want to change this to an assert instead.
4555 if (t == CORINFO_TYPE_VOID || t == CORINFO_TYPE_BYREF || t == CORINFO_TYPE_PTR)
4560 #ifdef _TARGET_64BIT_
4561 if (t == CORINFO_TYPE_NATIVEINT || t == CORINFO_TYPE_NATIVEUINT)
4563 return typeInfo::nativeInt();
4565 #endif // _TARGET_64BIT_
4567 if (t != CORINFO_TYPE_UNDEF)
4569 return (typeInfo(JITtype2tiType(t)));
4571 else if (bashStructToRef)
4573 return (typeInfo(TI_REF, clsHnd));
4577 return (typeInfo(TI_STRUCT, clsHnd));
4580 else if (attribs & CORINFO_FLG_GENERIC_TYPE_VARIABLE)
4582 // See comment in _typeInfo.h for why we do it this way.
4583 return (typeInfo(TI_REF, clsHnd, true));
4587 return (typeInfo(TI_REF, clsHnd));
4591 /******************************************************************************/
4592 BOOL Compiler::verIsSDArray(typeInfo ti)
4594 if (ti.IsNullObjRef())
4595 { // nulls are SD arrays
4599 if (!ti.IsType(TI_REF))
4604 if (!info.compCompHnd->isSDArray(ti.GetClassHandleForObjRef()))
4611 /******************************************************************************/
4612 /* Given 'arrayObjectType' which is an array type, fetch the element type. */
4613 /* Returns an error type if anything goes wrong */
4615 typeInfo Compiler::verGetArrayElemType(typeInfo arrayObjectType)
4617 assert(!arrayObjectType.IsNullObjRef()); // you need to check for null explictly since that is a success case
4619 if (!verIsSDArray(arrayObjectType))
4624 CORINFO_CLASS_HANDLE childClassHandle = nullptr;
4625 CorInfoType ciType = info.compCompHnd->getChildType(arrayObjectType.GetClassHandleForObjRef(), &childClassHandle);
4627 return verMakeTypeInfo(ciType, childClassHandle);
4630 /*****************************************************************************
4632 typeInfo Compiler::verParseArgSigToTypeInfo(CORINFO_SIG_INFO* sig, CORINFO_ARG_LIST_HANDLE args)
4634 CORINFO_CLASS_HANDLE classHandle;
4635 CorInfoType ciType = strip(info.compCompHnd->getArgType(sig, args, &classHandle));
4637 var_types type = JITtype2varType(ciType);
4638 if (varTypeIsGC(type))
4640 // For efficiency, getArgType only returns something in classHandle for
4641 // value types. For other types that have addition type info, you
4642 // have to call back explicitly
4643 classHandle = info.compCompHnd->getArgClass(sig, args);
4646 NO_WAY("Could not figure out Class specified in argument or local signature");
4650 return verMakeTypeInfo(ciType, classHandle);
4653 /*****************************************************************************/
4655 // This does the expensive check to figure out whether the method
4656 // needs to be verified. It is called only when we fail verification,
4657 // just before throwing the verification exception.
4659 BOOL Compiler::verNeedsVerification()
4661 // If we have previously determined that verification is NOT needed
4662 // (for example in Compiler::compCompile), that means verification is really not needed.
4663 // Return the same decision we made before.
4664 // (Note: This literally means that tiVerificationNeeded can never go from 0 to 1.)
4666 if (!tiVerificationNeeded)
4668 return tiVerificationNeeded;
4671 assert(tiVerificationNeeded);
4673 // Ok, we haven't concluded that verification is NOT needed. Consult the EE now to
4674 // obtain the answer.
4675 CorInfoCanSkipVerificationResult canSkipVerificationResult =
4676 info.compCompHnd->canSkipMethodVerification(info.compMethodHnd);
4678 // canSkipVerification will return one of the following three values:
4679 // CORINFO_VERIFICATION_CANNOT_SKIP = 0, // Cannot skip verification during jit time.
4680 // CORINFO_VERIFICATION_CAN_SKIP = 1, // Can skip verification during jit time.
4681 // CORINFO_VERIFICATION_RUNTIME_CHECK = 2, // Skip verification during jit time,
4682 // but need to insert a callout to the VM to ask during runtime
4683 // whether to skip verification or not.
4685 // Set tiRuntimeCalloutNeeded if canSkipVerification() instructs us to insert a callout for runtime check
4686 if (canSkipVerificationResult == CORINFO_VERIFICATION_RUNTIME_CHECK)
4688 tiRuntimeCalloutNeeded = true;
4691 if (canSkipVerificationResult == CORINFO_VERIFICATION_DONT_JIT)
4693 // Dev10 706080 - Testers don't like the assert, so just silence it
4694 // by not using the macros that invoke debugAssert.
4698 // When tiVerificationNeeded is true, JIT will do the verification during JIT time.
4699 // The following line means we will NOT do jit time verification if canSkipVerification
4700 // returns CORINFO_VERIFICATION_CAN_SKIP or CORINFO_VERIFICATION_RUNTIME_CHECK.
4701 tiVerificationNeeded = (canSkipVerificationResult == CORINFO_VERIFICATION_CANNOT_SKIP);
4702 return tiVerificationNeeded;
4705 BOOL Compiler::verIsByRefLike(const typeInfo& ti)
4711 if (!ti.IsType(TI_STRUCT))
4715 return info.compCompHnd->getClassAttribs(ti.GetClassHandleForValueClass()) & CORINFO_FLG_CONTAINS_STACK_PTR;
4718 BOOL Compiler::verIsSafeToReturnByRef(const typeInfo& ti)
4720 if (ti.IsPermanentHomeByRef())
4730 BOOL Compiler::verIsBoxable(const typeInfo& ti)
4732 return (ti.IsPrimitiveType() || ti.IsObjRef() // includes boxed generic type variables
4733 || ti.IsUnboxedGenericTypeVar() ||
4734 (ti.IsType(TI_STRUCT) &&
4735 // exclude byreflike structs
4736 !(info.compCompHnd->getClassAttribs(ti.GetClassHandleForValueClass()) & CORINFO_FLG_CONTAINS_STACK_PTR)));
4739 // Is it a boxed value type?
4740 bool Compiler::verIsBoxedValueType(typeInfo ti)
4742 if (ti.GetType() == TI_REF)
4744 CORINFO_CLASS_HANDLE clsHnd = ti.GetClassHandleForObjRef();
4745 return !!eeIsValueClass(clsHnd);
4753 /*****************************************************************************
4755 * Check if a TailCall is legal.
4758 bool Compiler::verCheckTailCallConstraint(
4760 CORINFO_RESOLVED_TOKEN* pResolvedToken,
4761 CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken, // Is this a "constrained." call on a type parameter?
4762 bool speculative // If true, won't throw if verificatoin fails. Instead it will
4763 // return false to the caller.
4764 // If false, it will throw.
4768 CORINFO_SIG_INFO sig;
4769 unsigned int popCount = 0; // we can't pop the stack since impImportCall needs it, so
4770 // this counter is used to keep track of how many items have been
4773 CORINFO_METHOD_HANDLE methodHnd = nullptr;
4774 CORINFO_CLASS_HANDLE methodClassHnd = nullptr;
4775 unsigned methodClassFlgs = 0;
4777 assert(impOpcodeIsCallOpcode(opcode));
4779 if (compIsForInlining())
4784 // for calli, VerifyOrReturn that this is not a virtual method
4785 if (opcode == CEE_CALLI)
4787 /* Get the call sig */
4788 eeGetSig(pResolvedToken->token, info.compScopeHnd, impTokenLookupContextHandle, &sig);
4790 // We don't know the target method, so we have to infer the flags, or
4791 // assume the worst-case.
4792 mflags = (sig.callConv & CORINFO_CALLCONV_HASTHIS) ? 0 : CORINFO_FLG_STATIC;
4796 methodHnd = pResolvedToken->hMethod;
4798 mflags = info.compCompHnd->getMethodAttribs(methodHnd);
4800 // When verifying generic code we pair the method handle with its
4801 // owning class to get the exact method signature.
4802 methodClassHnd = pResolvedToken->hClass;
4803 assert(methodClassHnd);
4805 eeGetMethodSig(methodHnd, &sig, methodClassHnd);
4807 // opcode specific check
4808 methodClassFlgs = info.compCompHnd->getClassAttribs(methodClassHnd);
4811 // We must have got the methodClassHnd if opcode is not CEE_CALLI
4812 assert((methodHnd != nullptr && methodClassHnd != nullptr) || opcode == CEE_CALLI);
4814 if ((sig.callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG)
4816 eeGetCallSiteSig(pResolvedToken->token, info.compScopeHnd, impTokenLookupContextHandle, &sig);
4819 // check compatibility of the arguments
4820 unsigned int argCount;
4821 argCount = sig.numArgs;
4822 CORINFO_ARG_LIST_HANDLE args;
4826 typeInfo tiDeclared = verParseArgSigToTypeInfo(&sig, args).NormaliseForStack();
4828 // check that the argument is not a byref for tailcalls
4829 VerifyOrReturnSpeculative(!verIsByRefLike(tiDeclared), "tailcall on byrefs", speculative);
4831 // For unsafe code, we might have parameters containing pointer to the stack location.
4832 // Disallow the tailcall for this kind.
4833 CORINFO_CLASS_HANDLE classHandle;
4834 CorInfoType ciType = strip(info.compCompHnd->getArgType(&sig, args, &classHandle));
4835 VerifyOrReturnSpeculative(ciType != CORINFO_TYPE_PTR, "tailcall on CORINFO_TYPE_PTR", speculative);
4837 args = info.compCompHnd->getArgNext(args);
4841 popCount += sig.numArgs;
4843 // check for 'this' which is on non-static methods, not called via NEWOBJ
4844 if (!(mflags & CORINFO_FLG_STATIC))
4846 // Always update the popCount.
4847 // This is crucial for the stack calculation to be correct.
4848 typeInfo tiThis = impStackTop(popCount).seTypeInfo;
4851 if (opcode == CEE_CALLI)
4853 // For CALLI, we don't know the methodClassHnd. Therefore, let's check the "this" object
4855 if (tiThis.IsValueClass())
4859 VerifyOrReturnSpeculative(!verIsByRefLike(tiThis), "byref in tailcall", speculative);
4863 // Check type compatibility of the this argument
4864 typeInfo tiDeclaredThis = verMakeTypeInfo(methodClassHnd);
4865 if (tiDeclaredThis.IsValueClass())
4867 tiDeclaredThis.MakeByRef();
4870 VerifyOrReturnSpeculative(!verIsByRefLike(tiDeclaredThis), "byref in tailcall", speculative);
4874 // Tail calls on constrained calls should be illegal too:
4875 // when instantiated at a value type, a constrained call may pass the address of a stack allocated value
4876 VerifyOrReturnSpeculative(!pConstrainedResolvedToken, "byref in constrained tailcall", speculative);
4878 // Get the exact view of the signature for an array method
4879 if (sig.retType != CORINFO_TYPE_VOID)
4881 if (methodClassFlgs & CORINFO_FLG_ARRAY)
4883 assert(opcode != CEE_CALLI);
4884 eeGetCallSiteSig(pResolvedToken->token, info.compScopeHnd, impTokenLookupContextHandle, &sig);
4888 typeInfo tiCalleeRetType = verMakeTypeInfo(sig.retType, sig.retTypeClass);
4889 typeInfo tiCallerRetType =
4890 verMakeTypeInfo(info.compMethodInfo->args.retType, info.compMethodInfo->args.retTypeClass);
4892 // void return type gets morphed into the error type, so we have to treat them specially here
4893 if (sig.retType == CORINFO_TYPE_VOID)
4895 VerifyOrReturnSpeculative(info.compMethodInfo->args.retType == CORINFO_TYPE_VOID, "tailcall return mismatch",
4900 VerifyOrReturnSpeculative(tiCompatibleWith(NormaliseForStack(tiCalleeRetType),
4901 NormaliseForStack(tiCallerRetType), true),
4902 "tailcall return mismatch", speculative);
4905 // for tailcall, stack must be empty
4906 VerifyOrReturnSpeculative(verCurrentState.esStackDepth == popCount, "stack non-empty on tailcall", speculative);
4908 return true; // Yes, tailcall is legal
4911 /*****************************************************************************
4913 * Checks the IL verification rules for the call
4916 void Compiler::verVerifyCall(OPCODE opcode,
4917 CORINFO_RESOLVED_TOKEN* pResolvedToken,
4918 CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
4921 const BYTE* delegateCreateStart,
4922 const BYTE* codeAddr,
4923 CORINFO_CALL_INFO* callInfo DEBUGARG(const char* methodName))
4926 CORINFO_SIG_INFO* sig = nullptr;
4927 unsigned int popCount = 0; // we can't pop the stack since impImportCall needs it, so
4928 // this counter is used to keep track of how many items have been
4931 // for calli, VerifyOrReturn that this is not a virtual method
4932 if (opcode == CEE_CALLI)
4934 Verify(false, "Calli not verifiable");
4938 //<NICE> It would be nice to cache the rest of it, but eeFindMethod is the big ticket item.
4939 mflags = callInfo->verMethodFlags;
4941 sig = &callInfo->verSig;
4943 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG)
4945 eeGetCallSiteSig(pResolvedToken->token, pResolvedToken->tokenScope, pResolvedToken->tokenContext, sig);
4948 // opcode specific check
4949 unsigned methodClassFlgs = callInfo->classFlags;
4953 // cannot do callvirt on valuetypes
4954 VerifyOrReturn(!(methodClassFlgs & CORINFO_FLG_VALUECLASS), "callVirt on value class");
4955 VerifyOrReturn(sig->hasThis(), "CallVirt on static method");
4960 assert(!tailCall); // Importer should not allow this
4961 VerifyOrReturn((mflags & CORINFO_FLG_CONSTRUCTOR) && !(mflags & CORINFO_FLG_STATIC),
4962 "newobj must be on instance");
4964 if (methodClassFlgs & CORINFO_FLG_DELEGATE)
4966 VerifyOrReturn(sig->numArgs == 2, "wrong number args to delegate ctor");
4967 typeInfo tiDeclaredObj = verParseArgSigToTypeInfo(sig, sig->args).NormaliseForStack();
4968 typeInfo tiDeclaredFtn =
4969 verParseArgSigToTypeInfo(sig, info.compCompHnd->getArgNext(sig->args)).NormaliseForStack();
4970 VerifyOrReturn(tiDeclaredFtn.IsNativeIntType(), "ftn arg needs to be a native int type");
4972 assert(popCount == 0);
4973 typeInfo tiActualObj = impStackTop(1).seTypeInfo;
4974 typeInfo tiActualFtn = impStackTop(0).seTypeInfo;
4976 VerifyOrReturn(tiActualFtn.IsMethod(), "delegate needs method as first arg");
4977 VerifyOrReturn(tiCompatibleWith(tiActualObj, tiDeclaredObj, true), "delegate object type mismatch");
4978 VerifyOrReturn(tiActualObj.IsNullObjRef() || tiActualObj.IsType(TI_REF),
4979 "delegate object type mismatch");
4981 CORINFO_CLASS_HANDLE objTypeHandle =
4982 tiActualObj.IsNullObjRef() ? nullptr : tiActualObj.GetClassHandleForObjRef();
4984 // the method signature must be compatible with the delegate's invoke method
4986 // check that for virtual functions, the type of the object used to get the
4987 // ftn ptr is the same as the type of the object passed to the delegate ctor.
4988 // since this is a bit of work to determine in general, we pattern match stylized
4991 // the delegate creation code check, which used to be done later, is now done here
4992 // so we can read delegateMethodRef directly from
4993 // from the preceding LDFTN or CEE_LDVIRTFN instruction sequence;
4994 // we then use it in our call to isCompatibleDelegate().
4996 mdMemberRef delegateMethodRef = mdMemberRefNil;
4997 VerifyOrReturn(verCheckDelegateCreation(delegateCreateStart, codeAddr, delegateMethodRef),
4998 "must create delegates with certain IL");
5000 CORINFO_RESOLVED_TOKEN delegateResolvedToken;
5001 delegateResolvedToken.tokenContext = impTokenLookupContextHandle;
5002 delegateResolvedToken.tokenScope = info.compScopeHnd;
5003 delegateResolvedToken.token = delegateMethodRef;
5004 delegateResolvedToken.tokenType = CORINFO_TOKENKIND_Method;
5005 info.compCompHnd->resolveToken(&delegateResolvedToken);
5007 CORINFO_CALL_INFO delegateCallInfo;
5008 eeGetCallInfo(&delegateResolvedToken, nullptr /* constraint typeRef */,
5009 addVerifyFlag(CORINFO_CALLINFO_SECURITYCHECKS), &delegateCallInfo);
5011 BOOL isOpenDelegate = FALSE;
5012 VerifyOrReturn(info.compCompHnd->isCompatibleDelegate(objTypeHandle, delegateResolvedToken.hClass,
5013 tiActualFtn.GetMethod(), pResolvedToken->hClass,
5015 "function incompatible with delegate");
5017 // check the constraints on the target method
5018 VerifyOrReturn(info.compCompHnd->satisfiesClassConstraints(delegateResolvedToken.hClass),
5019 "delegate target has unsatisfied class constraints");
5020 VerifyOrReturn(info.compCompHnd->satisfiesMethodConstraints(delegateResolvedToken.hClass,
5021 tiActualFtn.GetMethod()),
5022 "delegate target has unsatisfied method constraints");
5024 // See ECMA spec section 1.8.1.5.2 (Delegating via instance dispatch)
5025 // for additional verification rules for delegates
5026 CORINFO_METHOD_HANDLE actualMethodHandle = tiActualFtn.GetMethod();
5027 DWORD actualMethodAttribs = info.compCompHnd->getMethodAttribs(actualMethodHandle);
5028 if (impIsLDFTN_TOKEN(delegateCreateStart, codeAddr))
5031 if ((actualMethodAttribs & CORINFO_FLG_VIRTUAL) && ((actualMethodAttribs & CORINFO_FLG_FINAL) == 0)
5033 && StrictCheckForNonVirtualCallToVirtualMethod()
5037 if (info.compCompHnd->shouldEnforceCallvirtRestriction(info.compScopeHnd))
5039 VerifyOrReturn(tiActualObj.IsThisPtr() && lvaIsOriginalThisReadOnly() ||
5040 verIsBoxedValueType(tiActualObj),
5041 "The 'this' parameter to the call must be either the calling method's "
5042 "'this' parameter or "
5043 "a boxed value type.");
5048 if (actualMethodAttribs & CORINFO_FLG_PROTECTED)
5050 BOOL targetIsStatic = actualMethodAttribs & CORINFO_FLG_STATIC;
5052 Verify(targetIsStatic || !isOpenDelegate,
5053 "Unverifiable creation of an open instance delegate for a protected member.");
5055 CORINFO_CLASS_HANDLE instanceClassHnd = (tiActualObj.IsNullObjRef() || targetIsStatic)
5057 : tiActualObj.GetClassHandleForObjRef();
5059 // In the case of protected methods, it is a requirement that the 'this'
5060 // pointer be a subclass of the current context. Perform this check.
5061 Verify(info.compCompHnd->canAccessFamily(info.compMethodHnd, instanceClassHnd),
5062 "Accessing protected method through wrong type.");
5067 // fall thru to default checks
5069 VerifyOrReturn(!(mflags & CORINFO_FLG_ABSTRACT), "method abstract");
5071 VerifyOrReturn(!((mflags & CORINFO_FLG_CONSTRUCTOR) && (methodClassFlgs & CORINFO_FLG_DELEGATE)),
5072 "can only newobj a delegate constructor");
5074 // check compatibility of the arguments
5075 unsigned int argCount;
5076 argCount = sig->numArgs;
5077 CORINFO_ARG_LIST_HANDLE args;
5081 typeInfo tiActual = impStackTop(popCount + argCount).seTypeInfo;
5083 typeInfo tiDeclared = verParseArgSigToTypeInfo(sig, args).NormaliseForStack();
5084 VerifyOrReturn(tiCompatibleWith(tiActual, tiDeclared, true), "type mismatch");
5086 args = info.compCompHnd->getArgNext(args);
5092 popCount += sig->numArgs;
5094 // check for 'this' which are is non-static methods, not called via NEWOBJ
5095 CORINFO_CLASS_HANDLE instanceClassHnd = info.compClassHnd;
5096 if (!(mflags & CORINFO_FLG_STATIC) && (opcode != CEE_NEWOBJ))
5098 typeInfo tiThis = impStackTop(popCount).seTypeInfo;
5101 // If it is null, we assume we can access it (since it will AV shortly)
5102 // If it is anything but a reference class, there is no hierarchy, so
5103 // again, we don't need the precise instance class to compute 'protected' access
5104 if (tiThis.IsType(TI_REF))
5106 instanceClassHnd = tiThis.GetClassHandleForObjRef();
5109 // Check type compatibility of the this argument
5110 typeInfo tiDeclaredThis = verMakeTypeInfo(pResolvedToken->hClass);
5111 if (tiDeclaredThis.IsValueClass())
5113 tiDeclaredThis.MakeByRef();
5116 // If this is a call to the base class .ctor, set thisPtr Init for
5118 if (mflags & CORINFO_FLG_CONSTRUCTOR)
5120 if (verTrackObjCtorInitState && tiThis.IsThisPtr() &&
5121 verIsCallToInitThisPtr(info.compClassHnd, pResolvedToken->hClass))
5123 assert(verCurrentState.thisInitialized !=
5124 TIS_Bottom); // This should never be the case just from the logic of the verifier.
5125 VerifyOrReturn(verCurrentState.thisInitialized == TIS_Uninit,
5126 "Call to base class constructor when 'this' is possibly initialized");
5127 // Otherwise, 'this' is now initialized.
5128 verCurrentState.thisInitialized = TIS_Init;
5129 tiThis.SetInitialisedObjRef();
5133 // We allow direct calls to value type constructors
5134 // NB: we have to check that the contents of tiThis is a value type, otherwise we could use a
5135 // constrained callvirt to illegally re-enter a .ctor on a value of reference type.
5136 VerifyOrReturn(tiThis.IsByRef() && DereferenceByRef(tiThis).IsValueClass(),
5137 "Bad call to a constructor");
5141 if (pConstrainedResolvedToken != nullptr)
5143 VerifyOrReturn(tiThis.IsByRef(), "non-byref this type in constrained call");
5145 typeInfo tiConstraint = verMakeTypeInfo(pConstrainedResolvedToken->hClass);
5147 // We just dereference this and test for equality
5148 tiThis.DereferenceByRef();
5149 VerifyOrReturn(typeInfo::AreEquivalent(tiThis, tiConstraint),
5150 "this type mismatch with constrained type operand");
5152 // Now pretend the this type is the boxed constrained type, for the sake of subsequent checks
5153 tiThis = typeInfo(TI_REF, pConstrainedResolvedToken->hClass);
5156 // To support direct calls on readonly byrefs, just pretend tiDeclaredThis is readonly too
5157 if (tiDeclaredThis.IsByRef() && tiThis.IsReadonlyByRef())
5159 tiDeclaredThis.SetIsReadonlyByRef();
5162 VerifyOrReturn(tiCompatibleWith(tiThis, tiDeclaredThis, true), "this type mismatch");
5164 if (tiThis.IsByRef())
5166 // Find the actual type where the method exists (as opposed to what is declared
5167 // in the metadata). This is to prevent passing a byref as the "this" argument
5168 // while calling methods like System.ValueType.GetHashCode() which expect boxed objects.
5170 CORINFO_CLASS_HANDLE actualClassHnd = info.compCompHnd->getMethodClass(pResolvedToken->hMethod);
5171 VerifyOrReturn(eeIsValueClass(actualClassHnd),
5172 "Call to base type of valuetype (which is never a valuetype)");
5175 // Rules for non-virtual call to a non-final virtual method:
5178 // The "this" pointer is considered to be "possibly written" if
5179 // 1. Its address have been taken (LDARGA 0) anywhere in the method.
5181 // 2. It has been stored to (STARG.0) anywhere in the method.
5183 // A non-virtual call to a non-final virtual method is only allowed if
5184 // 1. The this pointer passed to the callee is an instance of a boxed value type.
5186 // 2. The this pointer passed to the callee is the current method's this pointer.
5187 // (and) The current method's this pointer is not "possibly written".
5189 // Thus the rule is that if you assign to this ANYWHERE you can't make "base" calls to
5190 // virtual methods. (Luckily this does affect .ctors, since they are not virtual).
5191 // This is stronger that is strictly needed, but implementing a laxer rule is significantly
5192 // hard and more error prone.
5194 if (opcode == CEE_CALL && (mflags & CORINFO_FLG_VIRTUAL) && ((mflags & CORINFO_FLG_FINAL) == 0)
5196 && StrictCheckForNonVirtualCallToVirtualMethod()
5200 if (info.compCompHnd->shouldEnforceCallvirtRestriction(info.compScopeHnd))
5203 tiThis.IsThisPtr() && lvaIsOriginalThisReadOnly() || verIsBoxedValueType(tiThis),
5204 "The 'this' parameter to the call must be either the calling method's 'this' parameter or "
5205 "a boxed value type.");
5210 // check any constraints on the callee's class and type parameters
5211 VerifyOrReturn(info.compCompHnd->satisfiesClassConstraints(pResolvedToken->hClass),
5212 "method has unsatisfied class constraints");
5213 VerifyOrReturn(info.compCompHnd->satisfiesMethodConstraints(pResolvedToken->hClass, pResolvedToken->hMethod),
5214 "method has unsatisfied method constraints");
5216 if (mflags & CORINFO_FLG_PROTECTED)
5218 VerifyOrReturn(info.compCompHnd->canAccessFamily(info.compMethodHnd, instanceClassHnd),
5219 "Can't access protected method");
5222 // Get the exact view of the signature for an array method
5223 if (sig->retType != CORINFO_TYPE_VOID)
5225 eeGetMethodSig(pResolvedToken->hMethod, sig, pResolvedToken->hClass);
5228 // "readonly." prefixed calls only allowed for the Address operation on arrays.
5229 // The methods supported by array types are under the control of the EE
5230 // so we can trust that only the Address operation returns a byref.
5233 typeInfo tiCalleeRetType = verMakeTypeInfo(sig->retType, sig->retTypeClass);
5234 VerifyOrReturn((methodClassFlgs & CORINFO_FLG_ARRAY) && tiCalleeRetType.IsByRef(),
5235 "unexpected use of readonly prefix");
5238 // Verify the tailcall
5241 verCheckTailCallConstraint(opcode, pResolvedToken, pConstrainedResolvedToken, false);
5245 /*****************************************************************************
5246 * Checks that a delegate creation is done using the following pattern:
5248 * ldvirtftn targetMemberRef
5250 * ldftn targetMemberRef
5252 * 'delegateCreateStart' points at the last dup or ldftn in this basic block (null if
5253 * not in this basic block)
5255 * targetMemberRef is read from the code sequence.
5256 * targetMemberRef is validated iff verificationNeeded.
5259 BOOL Compiler::verCheckDelegateCreation(const BYTE* delegateCreateStart,
5260 const BYTE* codeAddr,
5261 mdMemberRef& targetMemberRef)
5263 if (impIsLDFTN_TOKEN(delegateCreateStart, codeAddr))
5265 targetMemberRef = getU4LittleEndian(&delegateCreateStart[2]);
5268 else if (impIsDUP_LDVIRTFTN_TOKEN(delegateCreateStart, codeAddr))
5270 targetMemberRef = getU4LittleEndian(&delegateCreateStart[3]);
5277 typeInfo Compiler::verVerifySTIND(const typeInfo& tiTo, const typeInfo& value, const typeInfo& instrType)
5279 Verify(!tiTo.IsReadonlyByRef(), "write to readonly byref");
5280 typeInfo ptrVal = verVerifyLDIND(tiTo, instrType);
5281 typeInfo normPtrVal = typeInfo(ptrVal).NormaliseForStack();
5282 if (!tiCompatibleWith(value, normPtrVal, true))
5284 Verify(tiCompatibleWith(value, normPtrVal, true), "type mismatch");
5285 compUnsafeCastUsed = true;
5290 typeInfo Compiler::verVerifyLDIND(const typeInfo& ptr, const typeInfo& instrType)
5292 assert(!instrType.IsStruct());
5297 ptrVal = DereferenceByRef(ptr);
5298 if (instrType.IsObjRef() && !ptrVal.IsObjRef())
5300 Verify(false, "bad pointer");
5301 compUnsafeCastUsed = true;
5303 else if (!instrType.IsObjRef() && !typeInfo::AreEquivalent(instrType, ptrVal))
5305 Verify(false, "pointer not consistent with instr");
5306 compUnsafeCastUsed = true;
5311 Verify(false, "pointer not byref");
5312 compUnsafeCastUsed = true;
5318 // Verify that the field is used properly. 'tiThis' is NULL for statics,
5319 // 'fieldFlags' is the fields attributes, and mutator is TRUE if it is a
5320 // ld*flda or a st*fld.
5321 // 'enclosingClass' is given if we are accessing a field in some specific type.
5323 void Compiler::verVerifyField(CORINFO_RESOLVED_TOKEN* pResolvedToken,
5324 const CORINFO_FIELD_INFO& fieldInfo,
5325 const typeInfo* tiThis,
5327 BOOL allowPlainStructAsThis)
5329 CORINFO_CLASS_HANDLE enclosingClass = pResolvedToken->hClass;
5330 unsigned fieldFlags = fieldInfo.fieldFlags;
5331 CORINFO_CLASS_HANDLE instanceClass =
5332 info.compClassHnd; // for statics, we imagine the instance is the current class.
5334 bool isStaticField = ((fieldFlags & CORINFO_FLG_FIELD_STATIC) != 0);
5337 Verify(!(fieldFlags & CORINFO_FLG_FIELD_UNMANAGED), "mutating an RVA bases static");
5338 if ((fieldFlags & CORINFO_FLG_FIELD_FINAL))
5340 Verify((info.compFlags & CORINFO_FLG_CONSTRUCTOR) && enclosingClass == info.compClassHnd &&
5341 info.compIsStatic == isStaticField,
5342 "bad use of initonly field (set or address taken)");
5346 if (tiThis == nullptr)
5348 Verify(isStaticField, "used static opcode with non-static field");
5352 typeInfo tThis = *tiThis;
5354 if (allowPlainStructAsThis && tThis.IsValueClass())
5359 // If it is null, we assume we can access it (since it will AV shortly)
5360 // If it is anything but a refernce class, there is no hierarchy, so
5361 // again, we don't need the precise instance class to compute 'protected' access
5362 if (tiThis->IsType(TI_REF))
5364 instanceClass = tiThis->GetClassHandleForObjRef();
5367 // Note that even if the field is static, we require that the this pointer
5368 // satisfy the same constraints as a non-static field This happens to
5369 // be simpler and seems reasonable
5370 typeInfo tiDeclaredThis = verMakeTypeInfo(enclosingClass);
5371 if (tiDeclaredThis.IsValueClass())
5373 tiDeclaredThis.MakeByRef();
5375 // we allow read-only tThis, on any field access (even stores!), because if the
5376 // class implementor wants to prohibit stores he should make the field private.
5377 // we do this by setting the read-only bit on the type we compare tThis to.
5378 tiDeclaredThis.SetIsReadonlyByRef();
5380 else if (verTrackObjCtorInitState && tThis.IsThisPtr())
5382 // Any field access is legal on "uninitialized" this pointers.
5383 // The easiest way to implement this is to simply set the
5384 // initialized bit for the duration of the type check on the
5385 // field access only. It does not change the state of the "this"
5386 // for the function as a whole. Note that the "tThis" is a copy
5387 // of the original "this" type (*tiThis) passed in.
5388 tThis.SetInitialisedObjRef();
5391 Verify(tiCompatibleWith(tThis, tiDeclaredThis, true), "this type mismatch");
5394 // Presently the JIT does not check that we don't store or take the address of init-only fields
5395 // since we cannot guarantee their immutability and it is not a security issue.
5397 // check any constraints on the fields's class --- accessing the field might cause a class constructor to run.
5398 VerifyOrReturn(info.compCompHnd->satisfiesClassConstraints(enclosingClass),
5399 "field has unsatisfied class constraints");
5400 if (fieldFlags & CORINFO_FLG_FIELD_PROTECTED)
5402 Verify(info.compCompHnd->canAccessFamily(info.compMethodHnd, instanceClass),
5403 "Accessing protected method through wrong type.");
5407 void Compiler::verVerifyCond(const typeInfo& tiOp1, const typeInfo& tiOp2, unsigned opcode)
5409 if (tiOp1.IsNumberType())
5411 #ifdef _TARGET_64BIT_
5412 Verify(tiCompatibleWith(tiOp1, tiOp2, true), "Cond type mismatch");
5413 #else // _TARGET_64BIT
5414 // [10/17/2013] Consider changing this: to put on my verification lawyer hat,
5415 // this is non-conforming to the ECMA Spec: types don't have to be equivalent,
5416 // but compatible, since we can coalesce native int with int32 (see section III.1.5).
5417 Verify(typeInfo::AreEquivalent(tiOp1, tiOp2), "Cond type mismatch");
5418 #endif // !_TARGET_64BIT_
5420 else if (tiOp1.IsObjRef())
5432 Verify(FALSE, "Cond not allowed on object types");
5434 Verify(tiOp2.IsObjRef(), "Cond type mismatch");
5436 else if (tiOp1.IsByRef())
5438 Verify(tiOp2.IsByRef(), "Cond type mismatch");
5442 Verify(tiOp1.IsMethod() && tiOp2.IsMethod(), "Cond type mismatch");
5446 void Compiler::verVerifyThisPtrInitialised()
5448 if (verTrackObjCtorInitState)
5450 Verify(verCurrentState.thisInitialized == TIS_Init, "this ptr is not initialized");
5454 BOOL Compiler::verIsCallToInitThisPtr(CORINFO_CLASS_HANDLE context, CORINFO_CLASS_HANDLE target)
5456 // Either target == context, in this case calling an alternate .ctor
5457 // Or target is the immediate parent of context
5459 return ((target == context) || (target == info.compCompHnd->getParentType(context)));
5462 GenTreePtr Compiler::impImportLdvirtftn(GenTreePtr thisPtr,
5463 CORINFO_RESOLVED_TOKEN* pResolvedToken,
5464 CORINFO_CALL_INFO* pCallInfo)
5466 if ((pCallInfo->methodFlags & CORINFO_FLG_EnC) && !(pCallInfo->classFlags & CORINFO_FLG_INTERFACE))
5468 NO_WAY("Virtual call to a function added via EnC is not supported");
5471 // CoreRT generic virtual method
5472 if ((pCallInfo->sig.sigInst.methInstCount != 0) && IsTargetAbi(CORINFO_CORERT_ABI))
5474 GenTreePtr runtimeMethodHandle = nullptr;
5475 if (pCallInfo->exactContextNeedsRuntimeLookup)
5477 runtimeMethodHandle =
5478 impRuntimeLookupToTree(pResolvedToken, &pCallInfo->codePointerLookup, pCallInfo->hMethod);
5482 runtimeMethodHandle = gtNewIconEmbMethHndNode(pResolvedToken->hMethod);
5484 return gtNewHelperCallNode(CORINFO_HELP_GVMLOOKUP_FOR_SLOT, TYP_I_IMPL,
5485 gtNewArgList(thisPtr, runtimeMethodHandle));
5488 #ifdef FEATURE_READYTORUN_COMPILER
5489 if (opts.IsReadyToRun())
5491 if (!pCallInfo->exactContextNeedsRuntimeLookup)
5494 gtNewHelperCallNode(CORINFO_HELP_READYTORUN_VIRTUAL_FUNC_PTR, TYP_I_IMPL, gtNewArgList(thisPtr));
5496 call->setEntryPoint(pCallInfo->codePointerLookup.constLookup);
5501 // We need a runtime lookup. CoreRT has a ReadyToRun helper for that too.
5502 if (IsTargetAbi(CORINFO_CORERT_ABI))
5504 GenTreePtr ctxTree = getRuntimeContextTree(pCallInfo->codePointerLookup.lookupKind.runtimeLookupKind);
5506 return impReadyToRunHelperToTree(pResolvedToken, CORINFO_HELP_READYTORUN_GENERIC_HANDLE, TYP_I_IMPL,
5507 gtNewArgList(ctxTree), &pCallInfo->codePointerLookup.lookupKind);
5512 // Get the exact descriptor for the static callsite
5513 GenTreePtr exactTypeDesc = impParentClassTokenToHandle(pResolvedToken);
5514 if (exactTypeDesc == nullptr)
5515 { // compDonotInline()
5519 GenTreePtr exactMethodDesc = impTokenToHandle(pResolvedToken);
5520 if (exactMethodDesc == nullptr)
5521 { // compDonotInline()
5525 GenTreeArgList* helpArgs = gtNewArgList(exactMethodDesc);
5527 helpArgs = gtNewListNode(exactTypeDesc, helpArgs);
5529 helpArgs = gtNewListNode(thisPtr, helpArgs);
5531 // Call helper function. This gets the target address of the final destination callsite.
5533 return gtNewHelperCallNode(CORINFO_HELP_VIRTUAL_FUNC_PTR, TYP_I_IMPL, helpArgs);
5536 //------------------------------------------------------------------------
5537 // impImportAndPushBox: build and import a value-type box
5540 // pResolvedToken - resolved token from the box operation
5546 // The value to be boxed is popped from the stack, and a tree for
5547 // the boxed value is pushed. This method may create upstream
5548 // statements, spill side effecting trees, and create new temps.
5550 // If importing an inlinee, we may also discover the inline must
5551 // fail. If so there is no new value pushed on the stack. Callers
5552 // should use CompDoNotInline after calling this method to see if
5553 // ongoing importation should be aborted.
5556 // Boxing of ref classes results in the same value as the value on
5557 // the top of the stack, so is handled inline in impImportBlockCode
5558 // for the CEE_BOX case. Only value or primitive type boxes make it
5561 // Boxing for nullable types is done via a helper call; boxing
5562 // of other value types is expanded inline or handled via helper
5563 // call, depending on the jit's codegen mode.
5565 // When the jit is operating in size and time constrained modes,
5566 // using a helper call here can save jit time and code size. But it
5567 // also may inhibit cleanup optimizations that could have also had a
5568 // even greater benefit effect on code size and jit time. An optimal
5569 // strategy may need to peek ahead and see if it is easy to tell how
5570 // the box is being used. For now, we defer.
5572 void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
5574 // Spill any special side effects
5575 impSpillSpecialSideEff();
5577 // Get get the expression to box from the stack.
5578 GenTreePtr op1 = nullptr;
5579 GenTreePtr op2 = nullptr;
5580 StackEntry se = impPopStack();
5581 CORINFO_CLASS_HANDLE operCls = se.seTypeInfo.GetClassHandle();
5582 GenTreePtr exprToBox = se.val;
5584 // Look at what helper we should use.
5585 CorInfoHelpFunc boxHelper = info.compCompHnd->getBoxHelper(pResolvedToken->hClass);
5587 // Determine what expansion to prefer.
5589 // In size/time/debuggable constrained modes, the helper call
5590 // expansion for box is generally smaller and is preferred, unless
5591 // the value to box is a struct that comes from a call. In that
5592 // case the call can construct its return value directly into the
5593 // box payload, saving possibly some up-front zeroing.
5595 // Currently primitive type boxes always get inline expanded. We may
5596 // want to do the same for small structs if they don't come from
5597 // calls and don't have GC pointers, since explicitly copying such
5598 // structs is cheap.
5599 JITDUMP("\nCompiler::impImportAndPushBox -- handling BOX(value class) via");
5600 bool canExpandInline = (boxHelper == CORINFO_HELP_BOX);
5601 bool optForSize = !exprToBox->IsCall() && (operCls != nullptr) && (opts.compDbgCode || opts.MinOpts());
5602 bool expandInline = canExpandInline && !optForSize;
5606 JITDUMP(" inline allocate/copy sequence\n");
5608 // we are doing 'normal' boxing. This means that we can inline the box operation
5609 // Box(expr) gets morphed into
5610 // temp = new(clsHnd)
5611 // cpobj(temp+4, expr, clsHnd)
5613 // The code paths differ slightly below for structs and primitives because
5614 // "cpobj" differs in these cases. In one case you get
5615 // impAssignStructPtr(temp+4, expr, clsHnd)
5616 // and the other you get
5619 if (opts.MinOpts() || opts.compDbgCode)
5621 // For minopts/debug code, try and minimize the total number
5622 // of box temps by reusing an existing temp when possible.
5623 if (impBoxTempInUse || impBoxTemp == BAD_VAR_NUM)
5625 impBoxTemp = lvaGrabTemp(true DEBUGARG("Reusable Box Helper"));
5630 // When optimizing, use a new temp for each box operation
5631 // since we then know the exact class of the box temp.
5632 impBoxTemp = lvaGrabTemp(true DEBUGARG("Single-def Box Helper"));
5633 lvaTable[impBoxTemp].lvType = TYP_REF;
5634 const bool isExact = true;
5635 lvaSetClass(impBoxTemp, pResolvedToken->hClass, isExact);
5638 // needs to stay in use until this box expression is appended
5639 // some other node. We approximate this by keeping it alive until
5640 // the opcode stack becomes empty
5641 impBoxTempInUse = true;
5643 #ifdef FEATURE_READYTORUN_COMPILER
5644 bool usingReadyToRunHelper = false;
5646 if (opts.IsReadyToRun())
5648 op1 = impReadyToRunHelperToTree(pResolvedToken, CORINFO_HELP_READYTORUN_NEW, TYP_REF);
5649 usingReadyToRunHelper = (op1 != nullptr);
5652 if (!usingReadyToRunHelper)
5655 // TODO: ReadyToRun: When generic dictionary lookups are necessary, replace the lookup call
5656 // and the newfast call with a single call to a dynamic R2R cell that will:
5657 // 1) Load the context
5658 // 2) Perform the generic dictionary lookup and caching, and generate the appropriate stub
5659 // 3) Allocate and return the new object for boxing
5660 // Reason: performance (today, we'll always use the slow helper for the R2R generics case)
5662 // Ensure that the value class is restored
5663 op2 = impTokenToHandle(pResolvedToken, nullptr, TRUE /* mustRestoreHandle */);
5666 // We must be backing out of an inline.
5667 assert(compDonotInline());
5671 op1 = gtNewAllocObjNode(info.compCompHnd->getNewHelper(pResolvedToken, info.compMethodHnd),
5672 pResolvedToken->hClass, TYP_REF, op2);
5675 /* Remember that this basic block contains 'new' of an object, and so does this method */
5676 compCurBB->bbFlags |= BBF_HAS_NEWOBJ;
5677 optMethodFlags |= OMF_HAS_NEWOBJ;
5679 GenTreePtr asg = gtNewTempAssign(impBoxTemp, op1);
5681 GenTreePtr asgStmt = impAppendTree(asg, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
5683 op1 = gtNewLclvNode(impBoxTemp, TYP_REF);
5684 op2 = gtNewIconNode(TARGET_POINTER_SIZE, TYP_I_IMPL);
5685 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1, op2);
5687 if (varTypeIsStruct(exprToBox))
5689 assert(info.compCompHnd->getClassSize(pResolvedToken->hClass) == info.compCompHnd->getClassSize(operCls));
5690 op1 = impAssignStructPtr(op1, exprToBox, operCls, (unsigned)CHECK_SPILL_ALL);
5694 var_types lclTyp = exprToBox->TypeGet();
5695 if (lclTyp == TYP_BYREF)
5697 lclTyp = TYP_I_IMPL;
5699 CorInfoType jitType = info.compCompHnd->asCorInfoType(pResolvedToken->hClass);
5700 if (impIsPrimitive(jitType))
5702 lclTyp = JITtype2varType(jitType);
5704 assert(genActualType(exprToBox->TypeGet()) == genActualType(lclTyp) ||
5705 varTypeIsFloating(lclTyp) == varTypeIsFloating(exprToBox->TypeGet()));
5706 var_types srcTyp = exprToBox->TypeGet();
5707 var_types dstTyp = lclTyp;
5709 if (srcTyp != dstTyp)
5711 assert((varTypeIsFloating(srcTyp) && varTypeIsFloating(dstTyp)) ||
5712 (varTypeIsIntegral(srcTyp) && varTypeIsIntegral(dstTyp)));
5713 exprToBox = gtNewCastNode(dstTyp, exprToBox, dstTyp);
5715 op1 = gtNewAssignNode(gtNewOperNode(GT_IND, lclTyp, op1), exprToBox);
5718 // Spill eval stack to flush out any pending side effects.
5719 impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("impImportAndPushBox"));
5721 // Set up this copy as a second assignment.
5722 GenTreePtr copyStmt = impAppendTree(op1, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
5724 op1 = gtNewLclvNode(impBoxTemp, TYP_REF);
5726 // Record that this is a "box" node and keep track of the matching parts.
5727 op1 = new (this, GT_BOX) GenTreeBox(TYP_REF, op1, asgStmt, copyStmt);
5729 // If it is a value class, mark the "box" node. We can use this information
5730 // to optimise several cases:
5731 // "box(x) == null" --> false
5732 // "(box(x)).CallAnInterfaceMethod(...)" --> "(&x).CallAValueTypeMethod"
5733 // "(box(x)).CallAnObjectMethod(...)" --> "(&x).CallAValueTypeMethod"
5735 op1->gtFlags |= GTF_BOX_VALUE;
5736 assert(op1->IsBoxedValue());
5737 assert(asg->gtOper == GT_ASG);
5741 // Don't optimize, just call the helper and be done with it.
5742 JITDUMP(" helper call because: %s\n", canExpandInline ? "optimizing for size" : "nullable");
5743 assert(operCls != nullptr);
5745 // Ensure that the value class is restored
5746 op2 = impTokenToHandle(pResolvedToken, nullptr, TRUE /* mustRestoreHandle */);
5749 // We must be backing out of an inline.
5750 assert(compDonotInline());
5754 GenTreeArgList* args = gtNewArgList(op2, impGetStructAddr(exprToBox, operCls, (unsigned)CHECK_SPILL_ALL, true));
5755 op1 = gtNewHelperCallNode(boxHelper, TYP_REF, args);
5758 /* Push the result back on the stack, */
5759 /* even if clsHnd is a value class we want the TI_REF */
5760 typeInfo tiRetVal = typeInfo(TI_REF, info.compCompHnd->getTypeForBox(pResolvedToken->hClass));
5761 impPushOnStack(op1, tiRetVal);
5764 //------------------------------------------------------------------------
5765 // impImportNewObjArray: Build and import `new` of multi-dimmensional array
5768 // pResolvedToken - The CORINFO_RESOLVED_TOKEN that has been initialized
5769 // by a call to CEEInfo::resolveToken().
5770 // pCallInfo - The CORINFO_CALL_INFO that has been initialized
5771 // by a call to CEEInfo::getCallInfo().
5774 // The multi-dimensional array constructor arguments (array dimensions) are
5775 // pushed on the IL stack on entry to this method.
5778 // Multi-dimensional array constructors are imported as calls to a JIT
5779 // helper, not as regular calls.
5781 void Compiler::impImportNewObjArray(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_CALL_INFO* pCallInfo)
5783 GenTreePtr classHandle = impParentClassTokenToHandle(pResolvedToken);
5784 if (classHandle == nullptr)
5785 { // compDonotInline()
5789 assert(pCallInfo->sig.numArgs);
5792 GenTreeArgList* args;
5795 // There are two different JIT helpers that can be used to allocate
5796 // multi-dimensional arrays:
5798 // - CORINFO_HELP_NEW_MDARR - takes the array dimensions as varargs.
5799 // This variant is deprecated. It should be eventually removed.
5801 // - CORINFO_HELP_NEW_MDARR_NONVARARG - takes the array dimensions as
5802 // pointer to block of int32s. This variant is more portable.
5804 // The non-varargs helper is enabled for CoreRT only for now. Enabling this
5805 // unconditionally would require ReadyToRun version bump.
5807 CLANG_FORMAT_COMMENT_ANCHOR;
5809 if (!opts.IsReadyToRun() || IsTargetAbi(CORINFO_CORERT_ABI))
5812 // Reuse the temp used to pass the array dimensions to avoid bloating
5813 // the stack frame in case there are multiple calls to multi-dim array
5814 // constructors within a single method.
5815 if (lvaNewObjArrayArgs == BAD_VAR_NUM)
5817 lvaNewObjArrayArgs = lvaGrabTemp(false DEBUGARG("NewObjArrayArgs"));
5818 lvaTable[lvaNewObjArrayArgs].lvType = TYP_BLK;
5819 lvaTable[lvaNewObjArrayArgs].lvExactSize = 0;
5822 // Increase size of lvaNewObjArrayArgs to be the largest size needed to hold 'numArgs' integers
5823 // for our call to CORINFO_HELP_NEW_MDARR_NONVARARG.
5824 lvaTable[lvaNewObjArrayArgs].lvExactSize =
5825 max(lvaTable[lvaNewObjArrayArgs].lvExactSize, pCallInfo->sig.numArgs * sizeof(INT32));
5827 // The side-effects may include allocation of more multi-dimensional arrays. Spill all side-effects
5828 // to ensure that the shared lvaNewObjArrayArgs local variable is only ever used to pass arguments
5829 // to one allocation at a time.
5830 impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("impImportNewObjArray"));
5833 // The arguments of the CORINFO_HELP_NEW_MDARR_NONVARARG helper are:
5834 // - Array class handle
5835 // - Number of dimension arguments
5836 // - Pointer to block of int32 dimensions - address of lvaNewObjArrayArgs temp.
5839 node = gtNewLclvNode(lvaNewObjArrayArgs, TYP_BLK);
5840 node = gtNewOperNode(GT_ADDR, TYP_I_IMPL, node);
5842 // Pop dimension arguments from the stack one at a time and store it
5843 // into lvaNewObjArrayArgs temp.
5844 for (int i = pCallInfo->sig.numArgs - 1; i >= 0; i--)
5846 GenTreePtr arg = impImplicitIorI4Cast(impPopStack().val, TYP_INT);
5848 GenTreePtr dest = gtNewLclvNode(lvaNewObjArrayArgs, TYP_BLK);
5849 dest = gtNewOperNode(GT_ADDR, TYP_I_IMPL, dest);
5850 dest = gtNewOperNode(GT_ADD, TYP_I_IMPL, dest,
5851 new (this, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, sizeof(INT32) * i));
5852 dest = gtNewOperNode(GT_IND, TYP_INT, dest);
5854 node = gtNewOperNode(GT_COMMA, node->TypeGet(), gtNewAssignNode(dest, arg), node);
5857 args = gtNewArgList(node);
5859 // pass number of arguments to the helper
5860 args = gtNewListNode(gtNewIconNode(pCallInfo->sig.numArgs), args);
5862 args = gtNewListNode(classHandle, args);
5864 node = gtNewHelperCallNode(CORINFO_HELP_NEW_MDARR_NONVARARG, TYP_REF, args);
5869 // The varargs helper needs the type and method handles as last
5870 // and last-1 param (this is a cdecl call, so args will be
5871 // pushed in reverse order on the CPU stack)
5874 args = gtNewArgList(classHandle);
5876 // pass number of arguments to the helper
5877 args = gtNewListNode(gtNewIconNode(pCallInfo->sig.numArgs), args);
5879 unsigned argFlags = 0;
5880 args = impPopList(pCallInfo->sig.numArgs, &pCallInfo->sig, args);
5882 node = gtNewHelperCallNode(CORINFO_HELP_NEW_MDARR, TYP_REF, args);
5884 // varargs, so we pop the arguments
5885 node->gtFlags |= GTF_CALL_POP_ARGS;
5888 // At the present time we don't track Caller pop arguments
5889 // that have GC references in them
5890 for (GenTreeArgList* temp = args; temp; temp = temp->Rest())
5892 assert(temp->Current()->gtType != TYP_REF);
5897 node->gtFlags |= args->gtFlags & GTF_GLOB_EFFECT;
5898 node->gtCall.compileTimeHelperArgumentHandle = (CORINFO_GENERIC_HANDLE)pResolvedToken->hClass;
5900 // Remember that this basic block contains 'new' of a md array
5901 compCurBB->bbFlags |= BBF_HAS_NEWARRAY;
5903 impPushOnStack(node, typeInfo(TI_REF, pResolvedToken->hClass));
5906 GenTreePtr Compiler::impTransformThis(GenTreePtr thisPtr,
5907 CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
5908 CORINFO_THIS_TRANSFORM transform)
5912 case CORINFO_DEREF_THIS:
5914 GenTreePtr obj = thisPtr;
5916 // This does a LDIND on the obj, which should be a byref. pointing to a ref
5917 impBashVarAddrsToI(obj);
5918 assert(genActualType(obj->gtType) == TYP_I_IMPL || obj->gtType == TYP_BYREF);
5919 CorInfoType constraintTyp = info.compCompHnd->asCorInfoType(pConstrainedResolvedToken->hClass);
5921 obj = gtNewOperNode(GT_IND, JITtype2varType(constraintTyp), obj);
5922 // ldind could point anywhere, example a boxed class static int
5923 obj->gtFlags |= (GTF_EXCEPT | GTF_GLOB_REF | GTF_IND_TGTANYWHERE);
5928 case CORINFO_BOX_THIS:
5930 // Constraint calls where there might be no
5931 // unboxed entry point require us to implement the call via helper.
5932 // These only occur when a possible target of the call
5933 // may have inherited an implementation of an interface
5934 // method from System.Object or System.ValueType. The EE does not provide us with
5935 // "unboxed" versions of these methods.
5937 GenTreePtr obj = thisPtr;
5939 assert(obj->TypeGet() == TYP_BYREF || obj->TypeGet() == TYP_I_IMPL);
5940 obj = gtNewObjNode(pConstrainedResolvedToken->hClass, obj);
5941 obj->gtFlags |= GTF_EXCEPT;
5943 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(pConstrainedResolvedToken->hClass);
5944 var_types objType = JITtype2varType(jitTyp);
5945 if (impIsPrimitive(jitTyp))
5947 if (obj->OperIsBlk())
5949 obj->ChangeOperUnchecked(GT_IND);
5951 // Obj could point anywhere, example a boxed class static int
5952 obj->gtFlags |= GTF_IND_TGTANYWHERE;
5953 obj->gtOp.gtOp2 = nullptr; // must be zero for tree walkers
5956 obj->gtType = JITtype2varType(jitTyp);
5957 assert(varTypeIsArithmetic(obj->gtType));
5960 // This pushes on the dereferenced byref
5961 // This is then used immediately to box.
5962 impPushOnStack(obj, verMakeTypeInfo(pConstrainedResolvedToken->hClass).NormaliseForStack());
5964 // This pops off the byref-to-a-value-type remaining on the stack and
5965 // replaces it with a boxed object.
5966 // This is then used as the object to the virtual call immediately below.
5967 impImportAndPushBox(pConstrainedResolvedToken);
5968 if (compDonotInline())
5973 obj = impPopStack().val;
5976 case CORINFO_NO_THIS_TRANSFORM:
5982 //------------------------------------------------------------------------
5983 // impCanPInvokeInline: check whether PInvoke inlining should enabled in current method.
5986 // true if PInvoke inlining should be enabled in current method, false otherwise
5989 // Checks a number of ambient conditions where we could pinvoke but choose not to
5991 bool Compiler::impCanPInvokeInline()
5993 return getInlinePInvokeEnabled() && (!opts.compDbgCode) && (compCodeOpt() != SMALL_CODE) &&
5994 (!opts.compNoPInvokeInlineCB) // profiler is preventing inline pinvoke
5998 //------------------------------------------------------------------------
5999 // impCanPInvokeInlineCallSite: basic legality checks using information
6000 // from a call to see if the call qualifies as an inline pinvoke.
6003 // block - block contaning the call, or for inlinees, block
6004 // containing the call being inlined
6007 // true if this call can legally qualify as an inline pinvoke, false otherwise
6010 // For runtimes that support exception handling interop there are
6011 // restrictions on using inline pinvoke in handler regions.
6013 // * We have to disable pinvoke inlining inside of filters because
6014 // in case the main execution (i.e. in the try block) is inside
6015 // unmanaged code, we cannot reuse the inlined stub (we still need
6016 // the original state until we are in the catch handler)
6018 // * We disable pinvoke inlining inside handlers since the GSCookie
6019 // is in the inlined Frame (see
6020 // CORINFO_EE_INFO::InlinedCallFrameInfo::offsetOfGSCookie), but
6021 // this would not protect framelets/return-address of handlers.
6023 // These restrictions are currently also in place for CoreCLR but
6024 // can be relaxed when coreclr/#8459 is addressed.
6026 bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)
6028 if (block->hasHndIndex())
6033 // The remaining limitations do not apply to CoreRT
6034 if (IsTargetAbi(CORINFO_CORERT_ABI))
6039 #ifdef _TARGET_AMD64_
6040 // On x64, we disable pinvoke inlining inside of try regions.
6041 // Here is the comment from JIT64 explaining why:
6043 // [VSWhidbey: 611015] - because the jitted code links in the
6044 // Frame (instead of the stub) we rely on the Frame not being
6045 // 'active' until inside the stub. This normally happens by the
6046 // stub setting the return address pointer in the Frame object
6047 // inside the stub. On a normal return, the return address
6048 // pointer is zeroed out so the Frame can be safely re-used, but
6049 // if an exception occurs, nobody zeros out the return address
6050 // pointer. Thus if we re-used the Frame object, it would go
6051 // 'active' as soon as we link it into the Frame chain.
6053 // Technically we only need to disable PInvoke inlining if we're
6054 // in a handler or if we're in a try body with a catch or
6055 // filter/except where other non-handler code in this method
6056 // might run and try to re-use the dirty Frame object.
6058 // A desktop test case where this seems to matter is
6059 // jit\jit64\ebvts\mcpp\sources2\ijw\__clrcall\vector_ctor_dtor.02\deldtor_clr.exe
6060 if (block->hasTryIndex())
6064 #endif // _TARGET_AMD64_
6069 //------------------------------------------------------------------------
6070 // impCheckForPInvokeCall examine call to see if it is a pinvoke and if so
6071 // if it can be expressed as an inline pinvoke.
6074 // call - tree for the call
6075 // methHnd - handle for the method being called (may be null)
6076 // sig - signature of the method being called
6077 // mflags - method flags for the method being called
6078 // block - block contaning the call, or for inlinees, block
6079 // containing the call being inlined
6082 // Sets GTF_CALL_M_PINVOKE on the call for pinvokes.
6084 // Also sets GTF_CALL_UNMANAGED on call for inline pinvokes if the
6085 // call passes a combination of legality and profitabilty checks.
6087 // If GTF_CALL_UNMANAGED is set, increments info.compCallUnmanaged
6089 void Compiler::impCheckForPInvokeCall(
6090 GenTreeCall* call, CORINFO_METHOD_HANDLE methHnd, CORINFO_SIG_INFO* sig, unsigned mflags, BasicBlock* block)
6092 CorInfoUnmanagedCallConv unmanagedCallConv;
6094 // If VM flagged it as Pinvoke, flag the call node accordingly
6095 if ((mflags & CORINFO_FLG_PINVOKE) != 0)
6097 call->gtCallMoreFlags |= GTF_CALL_M_PINVOKE;
6102 if ((mflags & CORINFO_FLG_PINVOKE) == 0 || (mflags & CORINFO_FLG_NOSECURITYWRAP) == 0)
6107 unmanagedCallConv = info.compCompHnd->getUnmanagedCallConv(methHnd);
6111 CorInfoCallConv callConv = CorInfoCallConv(sig->callConv & CORINFO_CALLCONV_MASK);
6112 if (callConv == CORINFO_CALLCONV_NATIVEVARARG)
6114 // Used by the IL Stubs.
6115 callConv = CORINFO_CALLCONV_C;
6117 static_assert_no_msg((unsigned)CORINFO_CALLCONV_C == (unsigned)CORINFO_UNMANAGED_CALLCONV_C);
6118 static_assert_no_msg((unsigned)CORINFO_CALLCONV_STDCALL == (unsigned)CORINFO_UNMANAGED_CALLCONV_STDCALL);
6119 static_assert_no_msg((unsigned)CORINFO_CALLCONV_THISCALL == (unsigned)CORINFO_UNMANAGED_CALLCONV_THISCALL);
6120 unmanagedCallConv = CorInfoUnmanagedCallConv(callConv);
6122 assert(!call->gtCallCookie);
6125 if (unmanagedCallConv != CORINFO_UNMANAGED_CALLCONV_C && unmanagedCallConv != CORINFO_UNMANAGED_CALLCONV_STDCALL &&
6126 unmanagedCallConv != CORINFO_UNMANAGED_CALLCONV_THISCALL)
6130 optNativeCallCount++;
6132 if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && methHnd == nullptr)
6134 // PInvoke CALLI in IL stubs must be inlined
6139 if (!impCanPInvokeInlineCallSite(block))
6144 // PInvoke CALL in IL stubs must be inlined on CoreRT. Skip the ambient conditions checks and
6145 // profitability checks
6146 if (!(opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && IsTargetAbi(CORINFO_CORERT_ABI)))
6148 if (!impCanPInvokeInline())
6153 // Size-speed tradeoff: don't use inline pinvoke at rarely
6154 // executed call sites. The non-inline version is more
6156 if (block->isRunRarely())
6162 // The expensive check should be last
6163 if (info.compCompHnd->pInvokeMarshalingRequired(methHnd, sig))
6169 JITLOG((LL_INFO1000000, "\nInline a CALLI PINVOKE call from method %s", info.compFullName));
6171 call->gtFlags |= GTF_CALL_UNMANAGED;
6172 info.compCallUnmanaged++;
6174 // AMD64 convention is same for native and managed
6175 if (unmanagedCallConv == CORINFO_UNMANAGED_CALLCONV_C)
6177 call->gtFlags |= GTF_CALL_POP_ARGS;
6180 if (unmanagedCallConv == CORINFO_UNMANAGED_CALLCONV_THISCALL)
6182 call->gtCallMoreFlags |= GTF_CALL_M_UNMGD_THISCALL;
6186 GenTreeCall* Compiler::impImportIndirectCall(CORINFO_SIG_INFO* sig, IL_OFFSETX ilOffset)
6188 var_types callRetTyp = JITtype2varType(sig->retType);
6190 /* The function pointer is on top of the stack - It may be a
6191 * complex expression. As it is evaluated after the args,
6192 * it may cause registered args to be spilled. Simply spill it.
6195 // Ignore this trivial case.
6196 if (impStackTop().val->gtOper != GT_LCL_VAR)
6198 impSpillStackEntry(verCurrentState.esStackDepth - 1,
6199 BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impImportIndirectCall"));
6202 /* Get the function pointer */
6204 GenTreePtr fptr = impPopStack().val;
6206 // The function pointer is typically a sized to match the target pointer size
6207 // However, stubgen IL optimization can change LDC.I8 to LDC.I4
6208 // See ILCodeStream::LowerOpcode
6209 assert(genActualType(fptr->gtType) == TYP_I_IMPL || genActualType(fptr->gtType) == TYP_INT);
6212 // This temporary must never be converted to a double in stress mode,
6213 // because that can introduce a call to the cast helper after the
6214 // arguments have already been evaluated.
6216 if (fptr->OperGet() == GT_LCL_VAR)
6218 lvaTable[fptr->gtLclVarCommon.gtLclNum].lvKeepType = 1;
6222 /* Create the call node */
6224 GenTreeCall* call = gtNewIndCallNode(fptr, callRetTyp, nullptr, ilOffset);
6226 call->gtFlags |= GTF_EXCEPT | (fptr->gtFlags & GTF_GLOB_EFFECT);
6231 /*****************************************************************************/
6233 void Compiler::impPopArgsForUnmanagedCall(GenTreePtr call, CORINFO_SIG_INFO* sig)
6235 assert(call->gtFlags & GTF_CALL_UNMANAGED);
6237 /* Since we push the arguments in reverse order (i.e. right -> left)
6238 * spill any side effects from the stack
6240 * OBS: If there is only one side effect we do not need to spill it
6241 * thus we have to spill all side-effects except last one
6244 unsigned lastLevelWithSideEffects = UINT_MAX;
6246 unsigned argsToReverse = sig->numArgs;
6248 // For "thiscall", the first argument goes in a register. Since its
6249 // order does not need to be changed, we do not need to spill it
6251 if (call->gtCall.gtCallMoreFlags & GTF_CALL_M_UNMGD_THISCALL)
6253 assert(argsToReverse);
6257 #ifndef _TARGET_X86_
6258 // Don't reverse args on ARM or x64 - first four args always placed in regs in order
6262 for (unsigned level = verCurrentState.esStackDepth - argsToReverse; level < verCurrentState.esStackDepth; level++)
6264 if (verCurrentState.esStack[level].val->gtFlags & GTF_ORDER_SIDEEFF)
6266 assert(lastLevelWithSideEffects == UINT_MAX);
6268 impSpillStackEntry(level,
6269 BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impPopArgsForUnmanagedCall - other side effect"));
6271 else if (verCurrentState.esStack[level].val->gtFlags & GTF_SIDE_EFFECT)
6273 if (lastLevelWithSideEffects != UINT_MAX)
6275 /* We had a previous side effect - must spill it */
6276 impSpillStackEntry(lastLevelWithSideEffects,
6277 BAD_VAR_NUM DEBUGARG(false) DEBUGARG("impPopArgsForUnmanagedCall - side effect"));
6279 /* Record the level for the current side effect in case we will spill it */
6280 lastLevelWithSideEffects = level;
6284 /* This is the first side effect encountered - record its level */
6286 lastLevelWithSideEffects = level;
6291 /* The argument list is now "clean" - no out-of-order side effects
6292 * Pop the argument list in reverse order */
6294 GenTreePtr args = call->gtCall.gtCallArgs = impPopRevList(sig->numArgs, sig, sig->numArgs - argsToReverse);
6296 if (call->gtCall.gtCallMoreFlags & GTF_CALL_M_UNMGD_THISCALL)
6298 GenTreePtr thisPtr = args->Current();
6299 impBashVarAddrsToI(thisPtr);
6300 assert(thisPtr->TypeGet() == TYP_I_IMPL || thisPtr->TypeGet() == TYP_BYREF);
6305 call->gtFlags |= args->gtFlags & GTF_GLOB_EFFECT;
6309 //------------------------------------------------------------------------
6310 // impInitClass: Build a node to initialize the class before accessing the
6311 // field if necessary
6314 // pResolvedToken - The CORINFO_RESOLVED_TOKEN that has been initialized
6315 // by a call to CEEInfo::resolveToken().
6317 // Return Value: If needed, a pointer to the node that will perform the class
6318 // initializtion. Otherwise, nullptr.
6321 GenTreePtr Compiler::impInitClass(CORINFO_RESOLVED_TOKEN* pResolvedToken)
6323 CorInfoInitClassResult initClassResult =
6324 info.compCompHnd->initClass(pResolvedToken->hField, info.compMethodHnd, impTokenLookupContextHandle);
6326 if ((initClassResult & CORINFO_INITCLASS_USE_HELPER) == 0)
6332 GenTreePtr node = impParentClassTokenToHandle(pResolvedToken, &runtimeLookup);
6334 if (node == nullptr)
6336 assert(compDonotInline());
6342 node = gtNewHelperCallNode(CORINFO_HELP_INITCLASS, TYP_VOID, gtNewArgList(node));
6346 // Call the shared non gc static helper, as its the fastest
6347 node = fgGetSharedCCtor(pResolvedToken->hClass);
6353 GenTreePtr Compiler::impImportStaticReadOnlyField(void* fldAddr, var_types lclTyp)
6355 GenTreePtr op1 = nullptr;
6364 ival = *((bool*)fldAddr);
6368 ival = *((signed char*)fldAddr);
6372 ival = *((unsigned char*)fldAddr);
6376 ival = *((short*)fldAddr);
6380 ival = *((unsigned short*)fldAddr);
6385 ival = *((int*)fldAddr);
6387 op1 = gtNewIconNode(ival);
6392 lval = *((__int64*)fldAddr);
6393 op1 = gtNewLconNode(lval);
6397 dval = *((float*)fldAddr);
6398 op1 = gtNewDconNode(dval);
6399 #if !FEATURE_X87_DOUBLES
6400 // X87 stack doesn't differentiate between float/double
6401 // so R4 is treated as R8, but everybody else does
6402 op1->gtType = TYP_FLOAT;
6403 #endif // FEATURE_X87_DOUBLES
6407 dval = *((double*)fldAddr);
6408 op1 = gtNewDconNode(dval);
6412 assert(!"Unexpected lclTyp");
6419 GenTreePtr Compiler::impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedToken,
6420 CORINFO_ACCESS_FLAGS access,
6421 CORINFO_FIELD_INFO* pFieldInfo,
6426 switch (pFieldInfo->fieldAccessor)
6428 case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
6430 assert(!compIsForInlining());
6432 // We first call a special helper to get the statics base pointer
6433 op1 = impParentClassTokenToHandle(pResolvedToken);
6435 // compIsForInlining() is false so we should not neve get NULL here
6436 assert(op1 != nullptr);
6438 var_types type = TYP_BYREF;
6440 switch (pFieldInfo->helper)
6442 case CORINFO_HELP_GETGENERICS_NONGCTHREADSTATIC_BASE:
6445 case CORINFO_HELP_GETGENERICS_GCSTATIC_BASE:
6446 case CORINFO_HELP_GETGENERICS_NONGCSTATIC_BASE:
6447 case CORINFO_HELP_GETGENERICS_GCTHREADSTATIC_BASE:
6450 assert(!"unknown generic statics helper");
6454 op1 = gtNewHelperCallNode(pFieldInfo->helper, type, gtNewArgList(op1));
6456 FieldSeqNode* fs = GetFieldSeqStore()->CreateSingleton(pResolvedToken->hField);
6457 op1 = gtNewOperNode(GT_ADD, type, op1,
6458 new (this, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, pFieldInfo->offset, fs));
6462 case CORINFO_FIELD_STATIC_SHARED_STATIC_HELPER:
6464 #ifdef FEATURE_READYTORUN_COMPILER
6465 if (opts.IsReadyToRun())
6467 unsigned callFlags = 0;
6469 if (info.compCompHnd->getClassAttribs(pResolvedToken->hClass) & CORINFO_FLG_BEFOREFIELDINIT)
6471 callFlags |= GTF_CALL_HOISTABLE;
6474 op1 = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_STATIC_BASE, TYP_BYREF);
6475 op1->gtFlags |= callFlags;
6477 op1->gtCall.setEntryPoint(pFieldInfo->fieldLookup);
6482 op1 = fgGetStaticsCCtorHelper(pResolvedToken->hClass, pFieldInfo->helper);
6486 FieldSeqNode* fs = GetFieldSeqStore()->CreateSingleton(pResolvedToken->hField);
6487 op1 = gtNewOperNode(GT_ADD, op1->TypeGet(), op1,
6488 new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, pFieldInfo->offset, fs));
6493 case CORINFO_FIELD_STATIC_READYTORUN_HELPER:
6495 #ifdef FEATURE_READYTORUN_COMPILER
6496 noway_assert(opts.IsReadyToRun());
6497 CORINFO_LOOKUP_KIND kind = info.compCompHnd->getLocationOfThisType(info.compMethodHnd);
6498 assert(kind.needsRuntimeLookup);
6500 GenTreePtr ctxTree = getRuntimeContextTree(kind.runtimeLookupKind);
6501 GenTreeArgList* args = gtNewArgList(ctxTree);
6503 unsigned callFlags = 0;
6505 if (info.compCompHnd->getClassAttribs(pResolvedToken->hClass) & CORINFO_FLG_BEFOREFIELDINIT)
6507 callFlags |= GTF_CALL_HOISTABLE;
6509 var_types type = TYP_BYREF;
6510 op1 = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_GENERIC_STATIC_BASE, type, args);
6511 op1->gtFlags |= callFlags;
6513 op1->gtCall.setEntryPoint(pFieldInfo->fieldLookup);
6514 FieldSeqNode* fs = GetFieldSeqStore()->CreateSingleton(pResolvedToken->hField);
6515 op1 = gtNewOperNode(GT_ADD, type, op1,
6516 new (this, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, pFieldInfo->offset, fs));
6519 #endif // FEATURE_READYTORUN_COMPILER
6525 if (!(access & CORINFO_ACCESS_ADDRESS))
6527 // In future, it may be better to just create the right tree here instead of folding it later.
6528 op1 = gtNewFieldRef(lclTyp, pResolvedToken->hField);
6530 if (pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_INITCLASS)
6532 op1->gtFlags |= GTF_FLD_INITCLASS;
6535 if (pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_STATIC_IN_HEAP)
6537 op1->gtType = TYP_REF; // points at boxed object
6538 FieldSeqNode* firstElemFldSeq =
6539 GetFieldSeqStore()->CreateSingleton(FieldSeqStore::FirstElemPseudoField);
6540 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1,
6541 new (this, GT_CNS_INT)
6542 GenTreeIntCon(TYP_I_IMPL, TARGET_POINTER_SIZE, firstElemFldSeq));
6544 if (varTypeIsStruct(lclTyp))
6546 // Constructor adds GTF_GLOB_REF. Note that this is *not* GTF_EXCEPT.
6547 op1 = gtNewObjNode(pFieldInfo->structType, op1);
6551 op1 = gtNewOperNode(GT_IND, lclTyp, op1);
6552 op1->gtFlags |= GTF_GLOB_REF | GTF_IND_NONFAULTING;
6560 void** pFldAddr = nullptr;
6561 void* fldAddr = info.compCompHnd->getFieldAddress(pResolvedToken->hField, (void**)&pFldAddr);
6563 FieldSeqNode* fldSeq = GetFieldSeqStore()->CreateSingleton(pResolvedToken->hField);
6565 /* Create the data member node */
6566 op1 = gtNewIconHandleNode(pFldAddr == nullptr ? (size_t)fldAddr : (size_t)pFldAddr, GTF_ICON_STATIC_HDL,
6569 if (pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_INITCLASS)
6571 op1->gtFlags |= GTF_ICON_INITCLASS;
6574 if (pFldAddr != nullptr)
6576 // There are two cases here, either the static is RVA based,
6577 // in which case the type of the FIELD node is not a GC type
6578 // and the handle to the RVA is a TYP_I_IMPL. Or the FIELD node is
6579 // a GC type and the handle to it is a TYP_BYREF in the GC heap
6580 // because handles to statics now go into the large object heap
6582 var_types handleTyp = (var_types)(varTypeIsGC(lclTyp) ? TYP_BYREF : TYP_I_IMPL);
6583 op1 = gtNewOperNode(GT_IND, handleTyp, op1);
6584 op1->gtFlags |= GTF_IND_INVARIANT | GTF_IND_NONFAULTING;
6591 if (pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_STATIC_IN_HEAP)
6593 op1 = gtNewOperNode(GT_IND, TYP_REF, op1);
6595 FieldSeqNode* fldSeq = GetFieldSeqStore()->CreateSingleton(FieldSeqStore::FirstElemPseudoField);
6597 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1,
6598 new (this, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, TARGET_POINTER_SIZE, fldSeq));
6601 if (!(access & CORINFO_ACCESS_ADDRESS))
6603 op1 = gtNewOperNode(GT_IND, lclTyp, op1);
6604 op1->gtFlags |= GTF_GLOB_REF;
6610 // In general try to call this before most of the verification work. Most people expect the access
6611 // exceptions before the verification exceptions. If you do this after, that usually doesn't happen. Turns
6612 // out if you can't access something we also think that you're unverifiable for other reasons.
6613 void Compiler::impHandleAccessAllowed(CorInfoIsAccessAllowedResult result, CORINFO_HELPER_DESC* helperCall)
6615 if (result != CORINFO_ACCESS_ALLOWED)
6617 impHandleAccessAllowedInternal(result, helperCall);
6621 void Compiler::impHandleAccessAllowedInternal(CorInfoIsAccessAllowedResult result, CORINFO_HELPER_DESC* helperCall)
6625 case CORINFO_ACCESS_ALLOWED:
6627 case CORINFO_ACCESS_ILLEGAL:
6628 // if we're verifying, then we need to reject the illegal access to ensure that we don't think the
6629 // method is verifiable. Otherwise, delay the exception to runtime.
6630 if (compIsForImportOnly())
6632 info.compCompHnd->ThrowExceptionForHelper(helperCall);
6636 impInsertHelperCall(helperCall);
6639 case CORINFO_ACCESS_RUNTIME_CHECK:
6640 impInsertHelperCall(helperCall);
6645 void Compiler::impInsertHelperCall(CORINFO_HELPER_DESC* helperInfo)
6647 // Construct the argument list
6648 GenTreeArgList* args = nullptr;
6649 assert(helperInfo->helperNum != CORINFO_HELP_UNDEF);
6650 for (unsigned i = helperInfo->numArgs; i > 0; --i)
6652 const CORINFO_HELPER_ARG& helperArg = helperInfo->args[i - 1];
6653 GenTreePtr currentArg = nullptr;
6654 switch (helperArg.argType)
6656 case CORINFO_HELPER_ARG_TYPE_Field:
6657 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(
6658 info.compCompHnd->getFieldClass(helperArg.fieldHandle));
6659 currentArg = gtNewIconEmbFldHndNode(helperArg.fieldHandle);
6661 case CORINFO_HELPER_ARG_TYPE_Method:
6662 info.compCompHnd->methodMustBeLoadedBeforeCodeIsRun(helperArg.methodHandle);
6663 currentArg = gtNewIconEmbMethHndNode(helperArg.methodHandle);
6665 case CORINFO_HELPER_ARG_TYPE_Class:
6666 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(helperArg.classHandle);
6667 currentArg = gtNewIconEmbClsHndNode(helperArg.classHandle);
6669 case CORINFO_HELPER_ARG_TYPE_Module:
6670 currentArg = gtNewIconEmbScpHndNode(helperArg.moduleHandle);
6672 case CORINFO_HELPER_ARG_TYPE_Const:
6673 currentArg = gtNewIconNode(helperArg.constant);
6676 NO_WAY("Illegal helper arg type");
6678 args = (currentArg == nullptr) ? gtNewArgList(currentArg) : gtNewListNode(currentArg, args);
6682 * Mark as CSE'able, and hoistable. Consider marking hoistable unless you're in the inlinee.
6683 * Also, consider sticking this in the first basic block.
6685 GenTreePtr callout = gtNewHelperCallNode(helperInfo->helperNum, TYP_VOID, args);
6686 impAppendTree(callout, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
6689 // Checks whether the return types of caller and callee are compatible
6690 // so that callee can be tail called. Note that here we don't check
6691 // compatibility in IL Verifier sense, but on the lines of return type
6692 // sizes are equal and get returned in the same return register.
6693 bool Compiler::impTailCallRetTypeCompatible(var_types callerRetType,
6694 CORINFO_CLASS_HANDLE callerRetTypeClass,
6695 var_types calleeRetType,
6696 CORINFO_CLASS_HANDLE calleeRetTypeClass)
6698 // Note that we can not relax this condition with genActualType() as the
6699 // calling convention dictates that the caller of a function with a small
6700 // typed return value is responsible for normalizing the return val.
6701 if (callerRetType == calleeRetType)
6706 // If the class handles are the same and not null, the return types are compatible.
6707 if ((callerRetTypeClass != nullptr) && (callerRetTypeClass == calleeRetTypeClass))
6712 #if defined(_TARGET_AMD64_) || defined(_TARGET_ARM64_)
6714 if (callerRetType == TYP_VOID)
6716 // This needs to be allowed to support the following IL pattern that Jit64 allows:
6721 // Note that the above IL pattern is not valid as per IL verification rules.
6722 // Therefore, only full trust code can take advantage of this pattern.
6726 // These checks return true if the return value type sizes are the same and
6727 // get returned in the same return register i.e. caller doesn't need to normalize
6728 // return value. Some of the tail calls permitted by below checks would have
6729 // been rejected by IL Verifier before we reached here. Therefore, only full
6730 // trust code can make those tail calls.
6731 unsigned callerRetTypeSize = 0;
6732 unsigned calleeRetTypeSize = 0;
6733 bool isCallerRetTypMBEnreg =
6734 VarTypeIsMultiByteAndCanEnreg(callerRetType, callerRetTypeClass, &callerRetTypeSize, true);
6735 bool isCalleeRetTypMBEnreg =
6736 VarTypeIsMultiByteAndCanEnreg(calleeRetType, calleeRetTypeClass, &calleeRetTypeSize, true);
6738 if (varTypeIsIntegral(callerRetType) || isCallerRetTypMBEnreg)
6740 return (varTypeIsIntegral(calleeRetType) || isCalleeRetTypMBEnreg) && (callerRetTypeSize == calleeRetTypeSize);
6742 #endif // _TARGET_AMD64_ || _TARGET_ARM64_
6750 PREFIX_TAILCALL_EXPLICIT = 0x00000001, // call has "tail" IL prefix
6751 PREFIX_TAILCALL_IMPLICIT =
6752 0x00000010, // call is treated as having "tail" prefix even though there is no "tail" IL prefix
6753 PREFIX_TAILCALL = (PREFIX_TAILCALL_EXPLICIT | PREFIX_TAILCALL_IMPLICIT),
6754 PREFIX_VOLATILE = 0x00000100,
6755 PREFIX_UNALIGNED = 0x00001000,
6756 PREFIX_CONSTRAINED = 0x00010000,
6757 PREFIX_READONLY = 0x00100000
6760 /********************************************************************************
6762 * Returns true if the current opcode and and the opcodes following it correspond
6763 * to a supported tail call IL pattern.
6766 bool Compiler::impIsTailCallILPattern(bool tailPrefixed,
6768 const BYTE* codeAddrOfNextOpcode,
6769 const BYTE* codeEnd,
6771 bool* isCallPopAndRet /* = nullptr */)
6773 // Bail out if the current opcode is not a call.
6774 if (!impOpcodeIsCallOpcode(curOpcode))
6779 #if !FEATURE_TAILCALL_OPT_SHARED_RETURN
6780 // If shared ret tail opt is not enabled, we will enable
6781 // it for recursive methods.
6785 // we can actually handle if the ret is in a fallthrough block, as long as that is the only part of the
6786 // sequence. Make sure we don't go past the end of the IL however.
6787 codeEnd = min(codeEnd + 1, info.compCode + info.compILCodeSize);
6790 // Bail out if there is no next opcode after call
6791 if (codeAddrOfNextOpcode >= codeEnd)
6796 // Scan the opcodes to look for the following IL patterns if either
6797 // i) the call is not tail prefixed (i.e. implicit tail call) or
6798 // ii) if tail prefixed, IL verification is not needed for the method.
6800 // Only in the above two cases we can allow the below tail call patterns
6801 // violating ECMA spec.
6817 #if !defined(FEATURE_CORECLR) && defined(_TARGET_AMD64_)
6820 nextOpcode = (OPCODE)getU1LittleEndian(codeAddrOfNextOpcode);
6821 codeAddrOfNextOpcode += sizeof(__int8);
6822 } while ((codeAddrOfNextOpcode < codeEnd) && // Haven't reached end of method
6823 (!tailPrefixed || !tiVerificationNeeded) && // Not ".tail" prefixed or method requires no IL verification
6824 ((nextOpcode == CEE_NOP) || ((nextOpcode == CEE_POP) && (++cntPop == 1)))); // Next opcode = nop or exactly
6825 // one pop seen so far.
6827 nextOpcode = (OPCODE)getU1LittleEndian(codeAddrOfNextOpcode);
6828 #endif // !FEATURE_CORECLR && _TARGET_AMD64_
6830 if (isCallPopAndRet)
6832 // Allow call+pop+ret to be tail call optimized if caller ret type is void
6833 *isCallPopAndRet = (nextOpcode == CEE_RET) && (cntPop == 1);
6836 #if !defined(FEATURE_CORECLR) && defined(_TARGET_AMD64_)
6838 // Tail call IL pattern could be either of the following
6839 // 1) call/callvirt/calli + ret
6840 // 2) call/callvirt/calli + pop + ret in a method returning void.
6841 return (nextOpcode == CEE_RET) && ((cntPop == 0) || ((cntPop == 1) && (info.compRetType == TYP_VOID)));
6843 return (nextOpcode == CEE_RET) && (cntPop == 0);
6844 #endif // !FEATURE_CORECLR && _TARGET_AMD64_
6847 /*****************************************************************************
6849 * Determine whether the call could be converted to an implicit tail call
6852 bool Compiler::impIsImplicitTailCallCandidate(
6853 OPCODE opcode, const BYTE* codeAddrOfNextOpcode, const BYTE* codeEnd, int prefixFlags, bool isRecursive)
6856 #if FEATURE_TAILCALL_OPT
6857 if (!opts.compTailCallOpt)
6862 if (opts.compDbgCode || opts.MinOpts())
6867 // must not be tail prefixed
6868 if (prefixFlags & PREFIX_TAILCALL_EXPLICIT)
6873 #if !FEATURE_TAILCALL_OPT_SHARED_RETURN
6874 // the block containing call is marked as BBJ_RETURN
6875 // We allow shared ret tail call optimization on recursive calls even under
6876 // !FEATURE_TAILCALL_OPT_SHARED_RETURN.
6877 if (!isRecursive && (compCurBB->bbJumpKind != BBJ_RETURN))
6879 #endif // !FEATURE_TAILCALL_OPT_SHARED_RETURN
6881 // must be call+ret or call+pop+ret
6882 if (!impIsTailCallILPattern(false, opcode, codeAddrOfNextOpcode, codeEnd, isRecursive))
6890 #endif // FEATURE_TAILCALL_OPT
6893 //------------------------------------------------------------------------
6894 // impImportCall: import a call-inspiring opcode
6897 // opcode - opcode that inspires the call
6898 // pResolvedToken - resolved token for the call target
6899 // pConstrainedResolvedToken - resolved constraint token (or nullptr)
6900 // newObjThis - tree for this pointer or uninitalized newobj temp (or nullptr)
6901 // prefixFlags - IL prefix flags for the call
6902 // callInfo - EE supplied info for the call
6903 // rawILOffset - IL offset of the opcode
6906 // Type of the call's return value.
6907 // If we're importing an inlinee and have realized the inline must fail, the call return type should be TYP_UNDEF.
6908 // However we can't assert for this here yet because there are cases we miss. See issue #13272.
6912 // opcode can be CEE_CALL, CEE_CALLI, CEE_CALLVIRT, or CEE_NEWOBJ.
6914 // For CEE_NEWOBJ, newobjThis should be the temp grabbed for the allocated
6915 // uninitalized object.
6918 #pragma warning(push)
6919 #pragma warning(disable : 21000) // Suppress PREFast warning about overly large function
6922 var_types Compiler::impImportCall(OPCODE opcode,
6923 CORINFO_RESOLVED_TOKEN* pResolvedToken,
6924 CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
6925 GenTreePtr newobjThis,
6927 CORINFO_CALL_INFO* callInfo,
6928 IL_OFFSET rawILOffset)
6930 assert(opcode == CEE_CALL || opcode == CEE_CALLVIRT || opcode == CEE_NEWOBJ || opcode == CEE_CALLI);
6932 IL_OFFSETX ilOffset = impCurILOffset(rawILOffset, true);
6933 var_types callRetTyp = TYP_COUNT;
6934 CORINFO_SIG_INFO* sig = nullptr;
6935 CORINFO_METHOD_HANDLE methHnd = nullptr;
6936 CORINFO_CLASS_HANDLE clsHnd = nullptr;
6937 unsigned clsFlags = 0;
6938 unsigned mflags = 0;
6939 unsigned argFlags = 0;
6940 GenTreePtr call = nullptr;
6941 GenTreeArgList* args = nullptr;
6942 CORINFO_THIS_TRANSFORM constraintCallThisTransform = CORINFO_NO_THIS_TRANSFORM;
6943 CORINFO_CONTEXT_HANDLE exactContextHnd = nullptr;
6944 bool exactContextNeedsRuntimeLookup = false;
6945 bool canTailCall = true;
6946 const char* szCanTailCallFailReason = nullptr;
6947 int tailCall = prefixFlags & PREFIX_TAILCALL;
6948 bool readonlyCall = (prefixFlags & PREFIX_READONLY) != 0;
6950 CORINFO_RESOLVED_TOKEN* ldftnToken = nullptr;
6952 // Synchronized methods need to call CORINFO_HELP_MON_EXIT at the end. We could
6953 // do that before tailcalls, but that is probably not the intended
6954 // semantic. So just disallow tailcalls from synchronized methods.
6955 // Also, popping arguments in a varargs function is more work and NYI
6956 // If we have a security object, we have to keep our frame around for callers
6957 // to see any imperative security.
6958 if (info.compFlags & CORINFO_FLG_SYNCH)
6960 canTailCall = false;
6961 szCanTailCallFailReason = "Caller is synchronized";
6963 #if !FEATURE_FIXED_OUT_ARGS
6964 else if (info.compIsVarArgs)
6966 canTailCall = false;
6967 szCanTailCallFailReason = "Caller is varargs";
6969 #endif // FEATURE_FIXED_OUT_ARGS
6970 else if (opts.compNeedSecurityCheck)
6972 canTailCall = false;
6973 szCanTailCallFailReason = "Caller requires a security check.";
6976 // We only need to cast the return value of pinvoke inlined calls that return small types
6978 // TODO-AMD64-Cleanup: Remove this when we stop interoperating with JIT64, or if we decide to stop
6979 // widening everything! CoreCLR does not support JIT64 interoperation so no need to widen there.
6980 // The existing x64 JIT doesn't bother widening all types to int, so we have to assume for
6981 // the time being that the callee might be compiled by the other JIT and thus the return
6982 // value will need to be widened by us (or not widened at all...)
6984 // ReadyToRun code sticks with default calling convention that does not widen small return types.
6986 bool checkForSmallType = opts.IsJit64Compat() || opts.IsReadyToRun();
6987 bool bIntrinsicImported = false;
6989 CORINFO_SIG_INFO calliSig;
6990 GenTreeArgList* extraArg = nullptr;
6992 /*-------------------------------------------------------------------------
6993 * First create the call node
6996 if (opcode == CEE_CALLI)
6998 /* Get the call site sig */
6999 eeGetSig(pResolvedToken->token, info.compScopeHnd, impTokenLookupContextHandle, &calliSig);
7001 callRetTyp = JITtype2varType(calliSig.retType);
7003 call = impImportIndirectCall(&calliSig, ilOffset);
7005 // We don't know the target method, so we have to infer the flags, or
7006 // assume the worst-case.
7007 mflags = (calliSig.callConv & CORINFO_CALLCONV_HASTHIS) ? 0 : CORINFO_FLG_STATIC;
7012 unsigned structSize =
7013 (callRetTyp == TYP_STRUCT) ? info.compCompHnd->getClassSize(calliSig.retTypeSigClass) : 0;
7014 printf("\nIn Compiler::impImportCall: opcode is %s, kind=%d, callRetType is %s, structSize is %d\n",
7015 opcodeNames[opcode], callInfo->kind, varTypeName(callRetTyp), structSize);
7018 // This should be checked in impImportBlockCode.
7019 assert(!compIsForInlining() || !(impInlineInfo->inlineCandidateInfo->dwRestrictions & INLINE_RESPECT_BOUNDARY));
7024 // We cannot lazily obtain the signature of a CALLI call because it has no method
7025 // handle that we can use, so we need to save its full call signature here.
7026 assert(call->gtCall.callSig == nullptr);
7027 call->gtCall.callSig = new (this, CMK_CorSig) CORINFO_SIG_INFO;
7028 *call->gtCall.callSig = calliSig;
7031 if (IsTargetAbi(CORINFO_CORERT_ABI))
7033 bool managedCall = (((calliSig.callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_STDCALL) &&
7034 ((calliSig.callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_C) &&
7035 ((calliSig.callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_THISCALL) &&
7036 ((calliSig.callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_FASTCALL));
7039 addFatPointerCandidate(call->AsCall());
7043 else // (opcode != CEE_CALLI)
7045 CorInfoIntrinsics intrinsicID = CORINFO_INTRINSIC_Count;
7047 // Passing CORINFO_CALLINFO_ALLOWINSTPARAM indicates that this JIT is prepared to
7048 // supply the instantiation parameters necessary to make direct calls to underlying
7049 // shared generic code, rather than calling through instantiating stubs. If the
7050 // returned signature has CORINFO_CALLCONV_PARAMTYPE then this indicates that the JIT
7051 // must indeed pass an instantiation parameter.
7053 methHnd = callInfo->hMethod;
7055 sig = &(callInfo->sig);
7056 callRetTyp = JITtype2varType(sig->retType);
7058 mflags = callInfo->methodFlags;
7063 unsigned structSize = (callRetTyp == TYP_STRUCT) ? info.compCompHnd->getClassSize(sig->retTypeSigClass) : 0;
7064 printf("\nIn Compiler::impImportCall: opcode is %s, kind=%d, callRetType is %s, structSize is %d\n",
7065 opcodeNames[opcode], callInfo->kind, varTypeName(callRetTyp), structSize);
7068 if (compIsForInlining())
7070 /* Does this call site have security boundary restrictions? */
7072 if (impInlineInfo->inlineCandidateInfo->dwRestrictions & INLINE_RESPECT_BOUNDARY)
7074 compInlineResult->NoteFatal(InlineObservation::CALLSITE_CROSS_BOUNDARY_SECURITY);
7078 /* Does the inlinee need a security check token on the frame */
7080 if (mflags & CORINFO_FLG_SECURITYCHECK)
7082 compInlineResult->NoteFatal(InlineObservation::CALLEE_NEEDS_SECURITY_CHECK);
7086 /* Does the inlinee use StackCrawlMark */
7088 if (mflags & CORINFO_FLG_DONT_INLINE_CALLER)
7090 compInlineResult->NoteFatal(InlineObservation::CALLEE_STACK_CRAWL_MARK);
7094 /* For now ignore delegate invoke */
7096 if (mflags & CORINFO_FLG_DELEGATE_INVOKE)
7098 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_DELEGATE_INVOKE);
7102 /* For now ignore varargs */
7103 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_NATIVEVARARG)
7105 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_NATIVE_VARARGS);
7109 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG)
7111 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_MANAGED_VARARGS);
7115 if ((mflags & CORINFO_FLG_VIRTUAL) && (sig->sigInst.methInstCount != 0) && (opcode == CEE_CALLVIRT))
7117 compInlineResult->NoteFatal(InlineObservation::CALLEE_IS_GENERIC_VIRTUAL);
7122 clsHnd = pResolvedToken->hClass;
7124 clsFlags = callInfo->classFlags;
7127 // If this is a call to JitTestLabel.Mark, do "early inlining", and record the test attribute.
7129 // This recognition should really be done by knowing the methHnd of the relevant Mark method(s).
7130 // These should be in mscorlib.h, and available through a JIT/EE interface call.
7131 const char* modName;
7132 const char* className;
7133 const char* methodName;
7134 if ((className = eeGetClassName(clsHnd)) != nullptr &&
7135 strcmp(className, "System.Runtime.CompilerServices.JitTestLabel") == 0 &&
7136 (methodName = eeGetMethodName(methHnd, &modName)) != nullptr && strcmp(methodName, "Mark") == 0)
7138 return impImportJitTestLabelMark(sig->numArgs);
7142 // <NICE> Factor this into getCallInfo </NICE>
7143 bool isSpecialIntrinsic = false;
7144 if ((mflags & (CORINFO_FLG_INTRINSIC | CORINFO_FLG_JIT_INTRINSIC)) != 0)
7146 const bool isTail = canTailCall && (tailCall != 0);
7148 call = impIntrinsic(newobjThis, clsHnd, methHnd, sig, mflags, pResolvedToken->token, readonlyCall, isTail,
7149 pConstrainedResolvedToken, callInfo->thisTransform, &intrinsicID, &isSpecialIntrinsic);
7151 if (compDonotInline())
7156 if (call != nullptr)
7158 assert(!(mflags & CORINFO_FLG_VIRTUAL) || (mflags & CORINFO_FLG_FINAL) ||
7159 (clsFlags & CORINFO_FLG_FINAL));
7161 #ifdef FEATURE_READYTORUN_COMPILER
7162 if (call->OperGet() == GT_INTRINSIC)
7164 if (opts.IsReadyToRun())
7166 noway_assert(callInfo->kind == CORINFO_CALL);
7167 call->gtIntrinsic.gtEntryPoint = callInfo->codePointerLookup.constLookup;
7171 call->gtIntrinsic.gtEntryPoint.addr = nullptr;
7176 bIntrinsicImported = true;
7184 call = impSIMDIntrinsic(opcode, newobjThis, clsHnd, methHnd, sig, pResolvedToken->token);
7185 if (call != nullptr)
7187 bIntrinsicImported = true;
7191 #endif // FEATURE_SIMD
7193 if ((mflags & CORINFO_FLG_VIRTUAL) && (mflags & CORINFO_FLG_EnC) && (opcode == CEE_CALLVIRT))
7195 NO_WAY("Virtual call to a function added via EnC is not supported");
7198 if ((sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_DEFAULT &&
7199 (sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_VARARG &&
7200 (sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_NATIVEVARARG)
7202 BADCODE("Bad calling convention");
7205 //-------------------------------------------------------------------------
7206 // Construct the call node
7208 // Work out what sort of call we're making.
7209 // Dispense with virtual calls implemented via LDVIRTFTN immediately.
7211 constraintCallThisTransform = callInfo->thisTransform;
7212 exactContextHnd = callInfo->contextHandle;
7213 exactContextNeedsRuntimeLookup = callInfo->exactContextNeedsRuntimeLookup == TRUE;
7215 // Recursive call is treated as a loop to the begining of the method.
7216 if (gtIsRecursiveCall(methHnd))
7221 JITDUMP("\nFound recursive call in the method. Mark BB%02u to BB%02u as having a backward branch.\n",
7222 fgFirstBB->bbNum, compCurBB->bbNum);
7225 fgMarkBackwardJump(fgFirstBB, compCurBB);
7228 switch (callInfo->kind)
7231 case CORINFO_VIRTUALCALL_STUB:
7233 assert(!(mflags & CORINFO_FLG_STATIC)); // can't call a static method
7234 assert(!(clsFlags & CORINFO_FLG_VALUECLASS));
7235 if (callInfo->stubLookup.lookupKind.needsRuntimeLookup)
7238 if (compIsForInlining())
7240 // Don't import runtime lookups when inlining
7241 // Inlining has to be aborted in such a case
7242 /* XXX Fri 3/20/2009
7243 * By the way, this would never succeed. If the handle lookup is into the generic
7244 * dictionary for a candidate, you'll generate different dictionary offsets and the
7245 * inlined code will crash.
7247 * To anyone code reviewing this, when could this ever succeed in the future? It'll
7248 * always have a handle lookup. These lookups are safe intra-module, but we're just
7251 compInlineResult->NoteFatal(InlineObservation::CALLSITE_HAS_COMPLEX_HANDLE);
7255 GenTreePtr stubAddr = impRuntimeLookupToTree(pResolvedToken, &callInfo->stubLookup, methHnd);
7256 assert(!compDonotInline());
7258 // This is the rough code to set up an indirect stub call
7259 assert(stubAddr != nullptr);
7261 // The stubAddr may be a
7262 // complex expression. As it is evaluated after the args,
7263 // it may cause registered args to be spilled. Simply spill it.
7265 unsigned lclNum = lvaGrabTemp(true DEBUGARG("VirtualCall with runtime lookup"));
7266 impAssignTempGen(lclNum, stubAddr, (unsigned)CHECK_SPILL_ALL);
7267 stubAddr = gtNewLclvNode(lclNum, TYP_I_IMPL);
7269 // Create the actual call node
7271 assert((sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_VARARG &&
7272 (sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_NATIVEVARARG);
7274 call = gtNewIndCallNode(stubAddr, callRetTyp, nullptr);
7276 call->gtFlags |= GTF_EXCEPT | (stubAddr->gtFlags & GTF_GLOB_EFFECT);
7277 call->gtFlags |= GTF_CALL_VIRT_STUB;
7280 // No tailcalls allowed for these yet...
7281 canTailCall = false;
7282 szCanTailCallFailReason = "VirtualCall with runtime lookup";
7287 // ok, the stub is available at compile type.
7289 call = gtNewCallNode(CT_USER_FUNC, callInfo->hMethod, callRetTyp, nullptr, ilOffset);
7290 call->gtCall.gtStubCallStubAddr = callInfo->stubLookup.constLookup.addr;
7291 call->gtFlags |= GTF_CALL_VIRT_STUB;
7292 assert(callInfo->stubLookup.constLookup.accessType != IAT_PPVALUE);
7293 if (callInfo->stubLookup.constLookup.accessType == IAT_PVALUE)
7295 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_VIRTSTUB_REL_INDIRECT;
7299 #ifdef FEATURE_READYTORUN_COMPILER
7300 if (opts.IsReadyToRun())
7302 // Null check is sometimes needed for ready to run to handle
7303 // non-virtual <-> virtual changes between versions
7304 if (callInfo->nullInstanceCheck)
7306 call->gtFlags |= GTF_CALL_NULLCHECK;
7314 case CORINFO_VIRTUALCALL_VTABLE:
7316 assert(!(mflags & CORINFO_FLG_STATIC)); // can't call a static method
7317 assert(!(clsFlags & CORINFO_FLG_VALUECLASS));
7318 call = gtNewCallNode(CT_USER_FUNC, callInfo->hMethod, callRetTyp, nullptr, ilOffset);
7319 call->gtFlags |= GTF_CALL_VIRT_VTABLE;
7323 case CORINFO_VIRTUALCALL_LDVIRTFTN:
7325 if (compIsForInlining())
7327 compInlineResult->NoteFatal(InlineObservation::CALLSITE_HAS_CALL_VIA_LDVIRTFTN);
7331 assert(!(mflags & CORINFO_FLG_STATIC)); // can't call a static method
7332 assert(!(clsFlags & CORINFO_FLG_VALUECLASS));
7333 // OK, We've been told to call via LDVIRTFTN, so just
7334 // take the call now....
7336 args = impPopList(sig->numArgs, sig);
7338 GenTreePtr thisPtr = impPopStack().val;
7339 thisPtr = impTransformThis(thisPtr, pConstrainedResolvedToken, callInfo->thisTransform);
7340 assert(thisPtr != nullptr);
7342 // Clone the (possibly transformed) "this" pointer
7343 GenTreePtr thisPtrCopy;
7344 thisPtr = impCloneExpr(thisPtr, &thisPtrCopy, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
7345 nullptr DEBUGARG("LDVIRTFTN this pointer"));
7347 GenTreePtr fptr = impImportLdvirtftn(thisPtr, pResolvedToken, callInfo);
7348 assert(fptr != nullptr);
7350 thisPtr = nullptr; // can't reuse it
7352 // Now make an indirect call through the function pointer
7354 unsigned lclNum = lvaGrabTemp(true DEBUGARG("VirtualCall through function pointer"));
7355 impAssignTempGen(lclNum, fptr, (unsigned)CHECK_SPILL_ALL);
7356 fptr = gtNewLclvNode(lclNum, TYP_I_IMPL);
7358 // Create the actual call node
7360 call = gtNewIndCallNode(fptr, callRetTyp, args, ilOffset);
7361 call->gtCall.gtCallObjp = thisPtrCopy;
7362 call->gtFlags |= GTF_EXCEPT | (fptr->gtFlags & GTF_GLOB_EFFECT);
7364 if ((sig->sigInst.methInstCount != 0) && IsTargetAbi(CORINFO_CORERT_ABI))
7366 // CoreRT generic virtual method: need to handle potential fat function pointers
7367 addFatPointerCandidate(call->AsCall());
7369 #ifdef FEATURE_READYTORUN_COMPILER
7370 if (opts.IsReadyToRun())
7372 // Null check is needed for ready to run to handle
7373 // non-virtual <-> virtual changes between versions
7374 call->gtFlags |= GTF_CALL_NULLCHECK;
7378 // Sine we are jumping over some code, check that its OK to skip that code
7379 assert((sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_VARARG &&
7380 (sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_NATIVEVARARG);
7386 // This is for a non-virtual, non-interface etc. call
7387 call = gtNewCallNode(CT_USER_FUNC, callInfo->hMethod, callRetTyp, nullptr, ilOffset);
7389 // We remove the nullcheck for the GetType call instrinsic.
7390 // TODO-CQ: JIT64 does not introduce the null check for many more helper calls
7392 if (callInfo->nullInstanceCheck &&
7393 !((mflags & CORINFO_FLG_INTRINSIC) != 0 && (intrinsicID == CORINFO_INTRINSIC_Object_GetType)))
7395 call->gtFlags |= GTF_CALL_NULLCHECK;
7398 #ifdef FEATURE_READYTORUN_COMPILER
7399 if (opts.IsReadyToRun())
7401 call->gtCall.setEntryPoint(callInfo->codePointerLookup.constLookup);
7407 case CORINFO_CALL_CODE_POINTER:
7409 // The EE has asked us to call by computing a code pointer and then doing an
7410 // indirect call. This is because a runtime lookup is required to get the code entry point.
7412 // These calls always follow a uniform calling convention, i.e. no extra hidden params
7413 assert((sig->callConv & CORINFO_CALLCONV_PARAMTYPE) == 0);
7415 assert((sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_VARARG);
7416 assert((sig->callConv & CORINFO_CALLCONV_MASK) != CORINFO_CALLCONV_NATIVEVARARG);
7419 impLookupToTree(pResolvedToken, &callInfo->codePointerLookup, GTF_ICON_FTN_ADDR, callInfo->hMethod);
7421 if (compDonotInline())
7426 // Now make an indirect call through the function pointer
7428 unsigned lclNum = lvaGrabTemp(true DEBUGARG("Indirect call through function pointer"));
7429 impAssignTempGen(lclNum, fptr, (unsigned)CHECK_SPILL_ALL);
7430 fptr = gtNewLclvNode(lclNum, TYP_I_IMPL);
7432 call = gtNewIndCallNode(fptr, callRetTyp, nullptr, ilOffset);
7433 call->gtFlags |= GTF_EXCEPT | (fptr->gtFlags & GTF_GLOB_EFFECT);
7434 if (callInfo->nullInstanceCheck)
7436 call->gtFlags |= GTF_CALL_NULLCHECK;
7443 assert(!"unknown call kind");
7447 //-------------------------------------------------------------------------
7450 PREFIX_ASSUME(call != nullptr);
7452 if (mflags & CORINFO_FLG_NOGCCHECK)
7454 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_NOGCCHECK;
7457 // Mark call if it's one of the ones we will maybe treat as an intrinsic
7458 if (isSpecialIntrinsic)
7460 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_SPECIAL_INTRINSIC;
7464 assert(clsHnd || (opcode == CEE_CALLI)); // We're never verifying for CALLI, so this is not set.
7466 /* Some sanity checks */
7468 // CALL_VIRT and NEWOBJ must have a THIS pointer
7469 assert((opcode != CEE_CALLVIRT && opcode != CEE_NEWOBJ) || (sig->callConv & CORINFO_CALLCONV_HASTHIS));
7470 // static bit and hasThis are negations of one another
7471 assert(((mflags & CORINFO_FLG_STATIC) != 0) == ((sig->callConv & CORINFO_CALLCONV_HASTHIS) == 0));
7472 assert(call != nullptr);
7474 /*-------------------------------------------------------------------------
7475 * Check special-cases etc
7478 /* Special case - Check if it is a call to Delegate.Invoke(). */
7480 if (mflags & CORINFO_FLG_DELEGATE_INVOKE)
7482 assert(!compIsForInlining());
7483 assert(!(mflags & CORINFO_FLG_STATIC)); // can't call a static method
7484 assert(mflags & CORINFO_FLG_FINAL);
7486 /* Set the delegate flag */
7487 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_DELEGATE_INV;
7489 if (callInfo->secureDelegateInvoke)
7491 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_SECURE_DELEGATE_INV;
7494 if (opcode == CEE_CALLVIRT)
7496 assert(mflags & CORINFO_FLG_FINAL);
7498 /* It should have the GTF_CALL_NULLCHECK flag set. Reset it */
7499 assert(call->gtFlags & GTF_CALL_NULLCHECK);
7500 call->gtFlags &= ~GTF_CALL_NULLCHECK;
7504 CORINFO_CLASS_HANDLE actualMethodRetTypeSigClass;
7505 actualMethodRetTypeSigClass = sig->retTypeSigClass;
7506 if (varTypeIsStruct(callRetTyp))
7508 callRetTyp = impNormStructType(actualMethodRetTypeSigClass);
7509 call->gtType = callRetTyp;
7513 /* Check for varargs */
7514 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG ||
7515 (sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_NATIVEVARARG)
7517 BADCODE("Varargs not supported.");
7519 #endif // !FEATURE_VARARG
7522 if (call->gtCall.callSig == nullptr)
7524 call->gtCall.callSig = new (this, CMK_CorSig) CORINFO_SIG_INFO;
7525 *call->gtCall.callSig = *sig;
7527 #endif // UNIX_X86_ABI
7529 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG ||
7530 (sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_NATIVEVARARG)
7532 assert(!compIsForInlining());
7534 /* Set the right flags */
7536 call->gtFlags |= GTF_CALL_POP_ARGS;
7537 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_VARARGS;
7539 /* Can't allow tailcall for varargs as it is caller-pop. The caller
7540 will be expecting to pop a certain number of arguments, but if we
7541 tailcall to a function with a different number of arguments, we
7542 are hosed. There are ways around this (caller remembers esp value,
7543 varargs is not caller-pop, etc), but not worth it. */
7544 CLANG_FORMAT_COMMENT_ANCHOR;
7549 canTailCall = false;
7550 szCanTailCallFailReason = "Callee is varargs";
7554 /* Get the total number of arguments - this is already correct
7555 * for CALLI - for methods we have to get it from the call site */
7557 if (opcode != CEE_CALLI)
7560 unsigned numArgsDef = sig->numArgs;
7562 eeGetCallSiteSig(pResolvedToken->token, info.compScopeHnd, impTokenLookupContextHandle, sig);
7565 // We cannot lazily obtain the signature of a vararg call because using its method
7566 // handle will give us only the declared argument list, not the full argument list.
7567 assert(call->gtCall.callSig == nullptr);
7568 call->gtCall.callSig = new (this, CMK_CorSig) CORINFO_SIG_INFO;
7569 *call->gtCall.callSig = *sig;
7572 // For vararg calls we must be sure to load the return type of the
7573 // method actually being called, as well as the return types of the
7574 // specified in the vararg signature. With type equivalency, these types
7575 // may not be the same.
7576 if (sig->retTypeSigClass != actualMethodRetTypeSigClass)
7578 if (actualMethodRetTypeSigClass != nullptr && sig->retType != CORINFO_TYPE_CLASS &&
7579 sig->retType != CORINFO_TYPE_BYREF && sig->retType != CORINFO_TYPE_PTR &&
7580 sig->retType != CORINFO_TYPE_VAR)
7582 // Make sure that all valuetypes (including enums) that we push are loaded.
7583 // This is to guarantee that if a GC is triggerred from the prestub of this methods,
7584 // all valuetypes in the method signature are already loaded.
7585 // We need to be able to find the size of the valuetypes, but we cannot
7586 // do a class-load from within GC.
7587 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(actualMethodRetTypeSigClass);
7591 assert(numArgsDef <= sig->numArgs);
7594 /* We will have "cookie" as the last argument but we cannot push
7595 * it on the operand stack because we may overflow, so we append it
7596 * to the arg list next after we pop them */
7599 if (mflags & CORINFO_FLG_SECURITYCHECK)
7601 assert(!compIsForInlining());
7603 // Need security prolog/epilog callouts when there is
7604 // imperative security in the method. This is to give security a
7605 // chance to do any setup in the prolog and cleanup in the epilog if needed.
7607 if (compIsForInlining())
7609 // Cannot handle this if the method being imported is an inlinee by itself.
7610 // Because inlinee method does not have its own frame.
7612 compInlineResult->NoteFatal(InlineObservation::CALLEE_NEEDS_SECURITY_CHECK);
7617 tiSecurityCalloutNeeded = true;
7619 // If the current method calls a method which needs a security check,
7620 // (i.e. the method being compiled has imperative security)
7621 // we need to reserve a slot for the security object in
7622 // the current method's stack frame
7623 opts.compNeedSecurityCheck = true;
7627 //--------------------------- Inline NDirect ------------------------------
7629 // For inline cases we technically should look at both the current
7630 // block and the call site block (or just the latter if we've
7631 // fused the EH trees). However the block-related checks pertain to
7632 // EH and we currently won't inline a method with EH. So for
7633 // inlinees, just checking the call site block is sufficient.
7635 // New lexical block here to avoid compilation errors because of GOTOs.
7636 BasicBlock* block = compIsForInlining() ? impInlineInfo->iciBlock : compCurBB;
7637 impCheckForPInvokeCall(call->AsCall(), methHnd, sig, mflags, block);
7640 if (call->gtFlags & GTF_CALL_UNMANAGED)
7642 // We set up the unmanaged call by linking the frame, disabling GC, etc
7643 // This needs to be cleaned up on return
7646 canTailCall = false;
7647 szCanTailCallFailReason = "Callee is native";
7650 checkForSmallType = true;
7652 impPopArgsForUnmanagedCall(call, sig);
7656 else if ((opcode == CEE_CALLI) && (((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_STDCALL) ||
7657 ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_C) ||
7658 ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_THISCALL) ||
7659 ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_FASTCALL)))
7661 if (!info.compCompHnd->canGetCookieForPInvokeCalliSig(sig))
7663 // Normally this only happens with inlining.
7664 // However, a generic method (or type) being NGENd into another module
7665 // can run into this issue as well. There's not an easy fall-back for NGEN
7666 // so instead we fallback to JIT.
7667 if (compIsForInlining())
7669 compInlineResult->NoteFatal(InlineObservation::CALLSITE_CANT_EMBED_PINVOKE_COOKIE);
7673 IMPL_LIMITATION("Can't get PInvoke cookie (cross module generics)");
7679 GenTreePtr cookie = eeGetPInvokeCookie(sig);
7681 // This cookie is required to be either a simple GT_CNS_INT or
7682 // an indirection of a GT_CNS_INT
7684 GenTreePtr cookieConst = cookie;
7685 if (cookie->gtOper == GT_IND)
7687 cookieConst = cookie->gtOp.gtOp1;
7689 assert(cookieConst->gtOper == GT_CNS_INT);
7691 // Setting GTF_DONT_CSE on the GT_CNS_INT as well as on the GT_IND (if it exists) will ensure that
7692 // we won't allow this tree to participate in any CSE logic
7694 cookie->gtFlags |= GTF_DONT_CSE;
7695 cookieConst->gtFlags |= GTF_DONT_CSE;
7697 call->gtCall.gtCallCookie = cookie;
7701 canTailCall = false;
7702 szCanTailCallFailReason = "PInvoke calli";
7706 /*-------------------------------------------------------------------------
7707 * Create the argument list
7710 //-------------------------------------------------------------------------
7711 // Special case - for varargs we have an implicit last argument
7713 if ((sig->callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG)
7715 assert(!compIsForInlining());
7717 void *varCookie, *pVarCookie;
7718 if (!info.compCompHnd->canGetVarArgsHandle(sig))
7720 compInlineResult->NoteFatal(InlineObservation::CALLSITE_CANT_EMBED_VARARGS_COOKIE);
7724 varCookie = info.compCompHnd->getVarArgsHandle(sig, &pVarCookie);
7725 assert((!varCookie) != (!pVarCookie));
7726 GenTreePtr cookie = gtNewIconEmbHndNode(varCookie, pVarCookie, GTF_ICON_VARG_HDL, sig);
7728 assert(extraArg == nullptr);
7729 extraArg = gtNewArgList(cookie);
7732 //-------------------------------------------------------------------------
7733 // Extra arg for shared generic code and array methods
7735 // Extra argument containing instantiation information is passed in the
7736 // following circumstances:
7737 // (a) To the "Address" method on array classes; the extra parameter is
7738 // the array's type handle (a TypeDesc)
7739 // (b) To shared-code instance methods in generic structs; the extra parameter
7740 // is the struct's type handle (a vtable ptr)
7741 // (c) To shared-code per-instantiation non-generic static methods in generic
7742 // classes and structs; the extra parameter is the type handle
7743 // (d) To shared-code generic methods; the extra parameter is an
7744 // exact-instantiation MethodDesc
7746 // We also set the exact type context associated with the call so we can
7747 // inline the call correctly later on.
7749 if (sig->callConv & CORINFO_CALLCONV_PARAMTYPE)
7751 assert(call->gtCall.gtCallType == CT_USER_FUNC);
7752 if (clsHnd == nullptr)
7754 NO_WAY("CALLI on parameterized type");
7757 assert(opcode != CEE_CALLI);
7759 GenTreePtr instParam;
7762 // Instantiated generic method
7763 if (((SIZE_T)exactContextHnd & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_METHOD)
7765 CORINFO_METHOD_HANDLE exactMethodHandle =
7766 (CORINFO_METHOD_HANDLE)((SIZE_T)exactContextHnd & ~CORINFO_CONTEXTFLAGS_MASK);
7768 if (!exactContextNeedsRuntimeLookup)
7770 #ifdef FEATURE_READYTORUN_COMPILER
7771 if (opts.IsReadyToRun())
7774 impReadyToRunLookupToTree(&callInfo->instParamLookup, GTF_ICON_METHOD_HDL, exactMethodHandle);
7775 if (instParam == nullptr)
7777 assert(compDonotInline());
7784 instParam = gtNewIconEmbMethHndNode(exactMethodHandle);
7785 info.compCompHnd->methodMustBeLoadedBeforeCodeIsRun(exactMethodHandle);
7790 instParam = impTokenToHandle(pResolvedToken, &runtimeLookup, TRUE /*mustRestoreHandle*/);
7791 if (instParam == nullptr)
7793 assert(compDonotInline());
7799 // otherwise must be an instance method in a generic struct,
7800 // a static method in a generic type, or a runtime-generated array method
7803 assert(((SIZE_T)exactContextHnd & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_CLASS);
7804 CORINFO_CLASS_HANDLE exactClassHandle =
7805 (CORINFO_CLASS_HANDLE)((SIZE_T)exactContextHnd & ~CORINFO_CONTEXTFLAGS_MASK);
7807 if (compIsForInlining() && (clsFlags & CORINFO_FLG_ARRAY) != 0)
7809 compInlineResult->NoteFatal(InlineObservation::CALLEE_IS_ARRAY_METHOD);
7813 if ((clsFlags & CORINFO_FLG_ARRAY) && readonlyCall)
7815 // We indicate "readonly" to the Address operation by using a null
7817 instParam = gtNewIconNode(0, TYP_REF);
7819 else if (!exactContextNeedsRuntimeLookup)
7821 #ifdef FEATURE_READYTORUN_COMPILER
7822 if (opts.IsReadyToRun())
7825 impReadyToRunLookupToTree(&callInfo->instParamLookup, GTF_ICON_CLASS_HDL, exactClassHandle);
7826 if (instParam == nullptr)
7828 assert(compDonotInline());
7835 instParam = gtNewIconEmbClsHndNode(exactClassHandle);
7836 info.compCompHnd->classMustBeLoadedBeforeCodeIsRun(exactClassHandle);
7841 // If the EE was able to resolve a constrained call, the instantiating parameter to use is the type
7842 // by which the call was constrained with. We embed pConstrainedResolvedToken as the extra argument
7843 // because pResolvedToken is an interface method and interface types make a poor generic context.
7844 if (pConstrainedResolvedToken)
7846 instParam = impTokenToHandle(pConstrainedResolvedToken, &runtimeLookup, TRUE /*mustRestoreHandle*/,
7847 FALSE /* importParent */);
7851 instParam = impParentClassTokenToHandle(pResolvedToken, &runtimeLookup, TRUE /*mustRestoreHandle*/);
7854 if (instParam == nullptr)
7856 assert(compDonotInline());
7862 assert(extraArg == nullptr);
7863 extraArg = gtNewArgList(instParam);
7866 // Inlining may need the exact type context (exactContextHnd) if we're inlining shared generic code, in particular
7867 // to inline 'polytypic' operations such as static field accesses, type tests and method calls which
7868 // rely on the exact context. The exactContextHnd is passed back to the JitInterface at appropriate points.
7869 // exactContextHnd is not currently required when inlining shared generic code into shared
7870 // generic code, since the inliner aborts whenever shared code polytypic operations are encountered
7871 // (e.g. anything marked needsRuntimeLookup)
7872 if (exactContextNeedsRuntimeLookup)
7874 exactContextHnd = nullptr;
7877 if ((opcode == CEE_NEWOBJ) && ((clsFlags & CORINFO_FLG_DELEGATE) != 0))
7879 // Only verifiable cases are supported.
7880 // dup; ldvirtftn; newobj; or ldftn; newobj.
7881 // IL test could contain unverifiable sequence, in this case optimization should not be done.
7882 if (impStackHeight() > 0)
7884 typeInfo delegateTypeInfo = impStackTop().seTypeInfo;
7885 if (delegateTypeInfo.IsToken())
7887 ldftnToken = delegateTypeInfo.GetToken();
7892 //-------------------------------------------------------------------------
7893 // The main group of arguments
7895 args = call->gtCall.gtCallArgs = impPopList(sig->numArgs, sig, extraArg);
7899 call->gtFlags |= args->gtFlags & GTF_GLOB_EFFECT;
7902 //-------------------------------------------------------------------------
7903 // The "this" pointer
7905 if (!(mflags & CORINFO_FLG_STATIC) && !((opcode == CEE_NEWOBJ) && (newobjThis == nullptr)))
7909 if (opcode == CEE_NEWOBJ)
7915 obj = impPopStack().val;
7916 obj = impTransformThis(obj, pConstrainedResolvedToken, constraintCallThisTransform);
7917 if (compDonotInline())
7923 // Store the "this" value in the call
7924 call->gtFlags |= obj->gtFlags & GTF_GLOB_EFFECT;
7925 call->gtCall.gtCallObjp = obj;
7927 // Is this a virtual or interface call?
7928 if (call->gtCall.IsVirtual())
7930 // only true object pointers can be virtual
7931 assert(obj->gtType == TYP_REF);
7933 // See if we can devirtualize.
7934 impDevirtualizeCall(call->AsCall(), &callInfo->hMethod, &callInfo->methodFlags, &callInfo->contextHandle,
7940 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_NONVIRT_SAME_THIS;
7944 //-------------------------------------------------------------------------
7945 // The "this" pointer for "newobj"
7947 if (opcode == CEE_NEWOBJ)
7949 if (clsFlags & CORINFO_FLG_VAROBJSIZE)
7951 assert(!(clsFlags & CORINFO_FLG_ARRAY)); // arrays handled separately
7952 // This is a 'new' of a variable sized object, wher
7953 // the constructor is to return the object. In this case
7954 // the constructor claims to return VOID but we know it
7955 // actually returns the new object
7956 assert(callRetTyp == TYP_VOID);
7957 callRetTyp = TYP_REF;
7958 call->gtType = TYP_REF;
7959 impSpillSpecialSideEff();
7961 impPushOnStack(call, typeInfo(TI_REF, clsHnd));
7965 if (clsFlags & CORINFO_FLG_DELEGATE)
7967 // New inliner morph it in impImportCall.
7968 // This will allow us to inline the call to the delegate constructor.
7969 call = fgOptimizeDelegateConstructor(call->AsCall(), &exactContextHnd, ldftnToken);
7972 if (!bIntrinsicImported)
7975 #if defined(DEBUG) || defined(INLINE_DATA)
7977 // Keep track of the raw IL offset of the call
7978 call->gtCall.gtRawILOffset = rawILOffset;
7980 #endif // defined(DEBUG) || defined(INLINE_DATA)
7982 // Is it an inline candidate?
7983 impMarkInlineCandidate(call, exactContextHnd, exactContextNeedsRuntimeLookup, callInfo);
7986 // append the call node.
7987 impAppendTree(call, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
7989 // Now push the value of the 'new onto the stack
7991 // This is a 'new' of a non-variable sized object.
7992 // Append the new node (op1) to the statement list,
7993 // and then push the local holding the value of this
7994 // new instruction on the stack.
7996 if (clsFlags & CORINFO_FLG_VALUECLASS)
7998 assert(newobjThis->gtOper == GT_ADDR && newobjThis->gtOp.gtOp1->gtOper == GT_LCL_VAR);
8000 unsigned tmp = newobjThis->gtOp.gtOp1->gtLclVarCommon.gtLclNum;
8001 impPushOnStack(gtNewLclvNode(tmp, lvaGetRealType(tmp)), verMakeTypeInfo(clsHnd).NormaliseForStack());
8005 if (newobjThis->gtOper == GT_COMMA)
8007 // In coreclr the callout can be inserted even if verification is disabled
8008 // so we cannot rely on tiVerificationNeeded alone
8010 // We must have inserted the callout. Get the real newobj.
8011 newobjThis = newobjThis->gtOp.gtOp2;
8014 assert(newobjThis->gtOper == GT_LCL_VAR);
8015 impPushOnStack(gtNewLclvNode(newobjThis->gtLclVarCommon.gtLclNum, TYP_REF), typeInfo(TI_REF, clsHnd));
8025 // This check cannot be performed for implicit tail calls for the reason
8026 // that impIsImplicitTailCallCandidate() is not checking whether return
8027 // types are compatible before marking a call node with PREFIX_TAILCALL_IMPLICIT.
8028 // As a result it is possible that in the following case, we find that
8029 // the type stack is non-empty if Callee() is considered for implicit
8031 // int Caller(..) { .... void Callee(); ret val; ... }
8033 // Note that we cannot check return type compatibility before ImpImportCall()
8034 // as we don't have required info or need to duplicate some of the logic of
8037 // For implicit tail calls, we perform this check after return types are
8038 // known to be compatible.
8039 if ((tailCall & PREFIX_TAILCALL_EXPLICIT) && (verCurrentState.esStackDepth != 0))
8041 BADCODE("Stack should be empty after tailcall");
8044 // Note that we can not relax this condition with genActualType() as
8045 // the calling convention dictates that the caller of a function with
8046 // a small-typed return value is responsible for normalizing the return val
8049 !impTailCallRetTypeCompatible(info.compRetType, info.compMethodInfo->args.retTypeClass, callRetTyp,
8050 callInfo->sig.retTypeClass))
8052 canTailCall = false;
8053 szCanTailCallFailReason = "Return types are not tail call compatible";
8056 // Stack empty check for implicit tail calls.
8057 if (canTailCall && (tailCall & PREFIX_TAILCALL_IMPLICIT) && (verCurrentState.esStackDepth != 0))
8059 #ifdef _TARGET_AMD64_
8060 // JIT64 Compatibility: Opportunistic tail call stack mismatch throws a VerificationException
8061 // in JIT64, not an InvalidProgramException.
8062 Verify(false, "Stack should be empty after tailcall");
8063 #else // _TARGET_64BIT_
8064 BADCODE("Stack should be empty after tailcall");
8065 #endif //!_TARGET_64BIT_
8068 // assert(compCurBB is not a catch, finally or filter block);
8069 // assert(compCurBB is not a try block protected by a finally block);
8071 // Check for permission to tailcall
8072 bool explicitTailCall = (tailCall & PREFIX_TAILCALL_EXPLICIT) != 0;
8074 assert(!explicitTailCall || compCurBB->bbJumpKind == BBJ_RETURN);
8078 // True virtual or indirect calls, shouldn't pass in a callee handle.
8079 CORINFO_METHOD_HANDLE exactCalleeHnd =
8080 ((call->gtCall.gtCallType != CT_USER_FUNC) || call->gtCall.IsVirtual()) ? nullptr : methHnd;
8081 GenTreePtr thisArg = call->gtCall.gtCallObjp;
8083 if (info.compCompHnd->canTailCall(info.compMethodHnd, methHnd, exactCalleeHnd, explicitTailCall))
8086 if (explicitTailCall)
8088 // In case of explicit tail calls, mark it so that it is not considered
8090 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_EXPLICIT_TAILCALL;
8094 printf("\nGTF_CALL_M_EXPLICIT_TAILCALL bit set for call ");
8102 #if FEATURE_TAILCALL_OPT
8103 // Must be an implicit tail call.
8104 assert((tailCall & PREFIX_TAILCALL_IMPLICIT) != 0);
8106 // It is possible that a call node is both an inline candidate and marked
8107 // for opportunistic tail calling. In-lining happens before morhphing of
8108 // trees. If in-lining of an in-line candidate gets aborted for whatever
8109 // reason, it will survive to the morphing stage at which point it will be
8110 // transformed into a tail call after performing additional checks.
8112 call->gtCall.gtCallMoreFlags |= GTF_CALL_M_IMPLICIT_TAILCALL;
8116 printf("\nGTF_CALL_M_IMPLICIT_TAILCALL bit set for call ");
8122 #else //! FEATURE_TAILCALL_OPT
8123 NYI("Implicit tail call prefix on a target which doesn't support opportunistic tail calls");
8125 #endif // FEATURE_TAILCALL_OPT
8128 // we can't report success just yet...
8132 canTailCall = false;
8133 // canTailCall reported its reasons already
8137 printf("\ninfo.compCompHnd->canTailCall returned false for call ");
8146 // If this assert fires it means that canTailCall was set to false without setting a reason!
8147 assert(szCanTailCallFailReason != nullptr);
8152 printf("\nRejecting %splicit tail call for call ", explicitTailCall ? "ex" : "im");
8154 printf(": %s\n", szCanTailCallFailReason);
8157 info.compCompHnd->reportTailCallDecision(info.compMethodHnd, methHnd, explicitTailCall, TAILCALL_FAIL,
8158 szCanTailCallFailReason);
8162 // Note: we assume that small return types are already normalized by the managed callee
8163 // or by the pinvoke stub for calls to unmanaged code.
8165 if (!bIntrinsicImported)
8168 // Things needed to be checked when bIntrinsicImported is false.
8171 assert(call->gtOper == GT_CALL);
8172 assert(sig != nullptr);
8174 // Tail calls require us to save the call site's sig info so we can obtain an argument
8175 // copying thunk from the EE later on.
8176 if (call->gtCall.callSig == nullptr)
8178 call->gtCall.callSig = new (this, CMK_CorSig) CORINFO_SIG_INFO;
8179 *call->gtCall.callSig = *sig;
8182 if (compIsForInlining() && opcode == CEE_CALLVIRT)
8184 GenTreePtr callObj = call->gtCall.gtCallObjp;
8185 assert(callObj != nullptr);
8187 if ((call->gtCall.IsVirtual() || (call->gtFlags & GTF_CALL_NULLCHECK)) &&
8188 impInlineIsGuaranteedThisDerefBeforeAnySideEffects(call->gtCall.gtCallArgs, callObj,
8189 impInlineInfo->inlArgInfo))
8191 impInlineInfo->thisDereferencedFirst = true;
8195 #if defined(DEBUG) || defined(INLINE_DATA)
8197 // Keep track of the raw IL offset of the call
8198 call->gtCall.gtRawILOffset = rawILOffset;
8200 #endif // defined(DEBUG) || defined(INLINE_DATA)
8202 // Is it an inline candidate?
8203 impMarkInlineCandidate(call, exactContextHnd, exactContextNeedsRuntimeLookup, callInfo);
8207 // Push or append the result of the call
8208 if (callRetTyp == TYP_VOID)
8210 if (opcode == CEE_NEWOBJ)
8212 // we actually did push something, so don't spill the thing we just pushed.
8213 assert(verCurrentState.esStackDepth > 0);
8214 impAppendTree(call, verCurrentState.esStackDepth - 1, impCurStmtOffs);
8218 impAppendTree(call, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
8223 impSpillSpecialSideEff();
8225 if (clsFlags & CORINFO_FLG_ARRAY)
8227 eeGetCallSiteSig(pResolvedToken->token, pResolvedToken->tokenScope, pResolvedToken->tokenContext, sig);
8230 // Find the return type used for verification by interpreting the method signature.
8231 // NB: we are clobbering the already established sig.
8232 if (tiVerificationNeeded)
8234 // Actually, we never get the sig for the original method.
8235 sig = &(callInfo->verSig);
8238 typeInfo tiRetVal = verMakeTypeInfo(sig->retType, sig->retTypeClass);
8239 tiRetVal.NormaliseForStack();
8241 // The CEE_READONLY prefix modifies the verification semantics of an Address
8242 // operation on an array type.
8243 if ((clsFlags & CORINFO_FLG_ARRAY) && readonlyCall && tiRetVal.IsByRef())
8245 tiRetVal.SetIsReadonlyByRef();
8248 if (tiVerificationNeeded)
8250 // We assume all calls return permanent home byrefs. If they
8251 // didn't they wouldn't be verifiable. This is also covering
8252 // the Address() helper for multidimensional arrays.
8253 if (tiRetVal.IsByRef())
8255 tiRetVal.SetIsPermanentHomeByRef();
8261 // Sometimes "call" is not a GT_CALL (if we imported an intrinsic that didn't turn into a call)
8263 bool fatPointerCandidate = call->AsCall()->IsFatPointerCandidate();
8264 if (varTypeIsStruct(callRetTyp))
8266 call = impFixupCallStructReturn(call->AsCall(), sig->retTypeClass);
8269 if ((call->gtFlags & GTF_CALL_INLINE_CANDIDATE) != 0)
8271 assert(opts.OptEnabled(CLFLG_INLINING));
8272 assert(!fatPointerCandidate); // We should not try to inline calli.
8274 // Make the call its own tree (spill the stack if needed).
8275 impAppendTree(call, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
8277 // TODO: Still using the widened type.
8278 call = gtNewInlineCandidateReturnExpr(call, genActualType(callRetTyp));
8282 if (fatPointerCandidate)
8284 // fatPointer candidates should be in statements of the form call() or var = call().
8285 // Such form allows to find statements with fat calls without walking through whole trees
8286 // and removes problems with cutting trees.
8287 assert(!bIntrinsicImported);
8288 assert(IsTargetAbi(CORINFO_CORERT_ABI));
8289 if (call->OperGet() != GT_LCL_VAR) // can be already converted by impFixupCallStructReturn.
8291 unsigned calliSlot = lvaGrabTemp(true DEBUGARG("calli"));
8292 LclVarDsc* varDsc = &lvaTable[calliSlot];
8293 varDsc->lvVerTypeInfo = tiRetVal;
8294 impAssignTempGen(calliSlot, call, tiRetVal.GetClassHandle(), (unsigned)CHECK_SPILL_NONE);
8295 // impAssignTempGen can change src arg list and return type for call that returns struct.
8296 var_types type = genActualType(lvaTable[calliSlot].TypeGet());
8297 call = gtNewLclvNode(calliSlot, type);
8301 // For non-candidates we must also spill, since we
8302 // might have locals live on the eval stack that this
8305 // Suppress this for certain well-known call targets
8306 // that we know won't modify locals, eg calls that are
8307 // recognized in gtCanOptimizeTypeEquality. Otherwise
8308 // we may break key fragile pattern matches later on.
8309 bool spillStack = true;
8312 GenTreeCall* callNode = call->AsCall();
8313 if ((callNode->gtCallType == CT_HELPER) && gtIsTypeHandleToRuntimeTypeHelper(callNode))
8317 else if ((callNode->gtCallMoreFlags & GTF_CALL_M_SPECIAL_INTRINSIC) != 0)
8325 impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("non-inline candidate call"));
8330 if (!bIntrinsicImported)
8332 //-------------------------------------------------------------------------
8334 /* If the call is of a small type and the callee is managed, the callee will normalize the result
8336 However, we need to normalize small type values returned by unmanaged
8337 functions (pinvoke). The pinvoke stub does the normalization, but we need to do it here
8338 if we use the shorter inlined pinvoke stub. */
8340 if (checkForSmallType && varTypeIsIntegral(callRetTyp) && genTypeSize(callRetTyp) < genTypeSize(TYP_INT))
8342 call = gtNewCastNode(genActualType(callRetTyp), call, callRetTyp);
8346 impPushOnStack(call, tiRetVal);
8349 // VSD functions get a new call target each time we getCallInfo, so clear the cache.
8350 // Also, the call info cache for CALLI instructions is largely incomplete, so clear it out.
8351 // if ( (opcode == CEE_CALLI) || (callInfoCache.fetchCallInfo().kind == CORINFO_VIRTUALCALL_STUB))
8352 // callInfoCache.uncacheCallInfo();
8357 #pragma warning(pop)
8360 bool Compiler::impMethodInfo_hasRetBuffArg(CORINFO_METHOD_INFO* methInfo)
8362 CorInfoType corType = methInfo->args.retType;
8364 if ((corType == CORINFO_TYPE_VALUECLASS) || (corType == CORINFO_TYPE_REFANY))
8366 // We have some kind of STRUCT being returned
8368 structPassingKind howToReturnStruct = SPK_Unknown;
8370 var_types returnType = getReturnTypeForStruct(methInfo->args.retTypeClass, &howToReturnStruct);
8372 if (howToReturnStruct == SPK_ByReference)
8383 var_types Compiler::impImportJitTestLabelMark(int numArgs)
8385 TestLabelAndNum tlAndN;
8389 StackEntry se = impPopStack();
8390 assert(se.seTypeInfo.GetType() == TI_INT);
8391 GenTreePtr val = se.val;
8392 assert(val->IsCnsIntOrI());
8393 tlAndN.m_tl = (TestLabel)val->AsIntConCommon()->IconValue();
8395 else if (numArgs == 3)
8397 StackEntry se = impPopStack();
8398 assert(se.seTypeInfo.GetType() == TI_INT);
8399 GenTreePtr val = se.val;
8400 assert(val->IsCnsIntOrI());
8401 tlAndN.m_num = val->AsIntConCommon()->IconValue();
8403 assert(se.seTypeInfo.GetType() == TI_INT);
8405 assert(val->IsCnsIntOrI());
8406 tlAndN.m_tl = (TestLabel)val->AsIntConCommon()->IconValue();
8413 StackEntry expSe = impPopStack();
8414 GenTreePtr node = expSe.val;
8416 // There are a small number of special cases, where we actually put the annotation on a subnode.
8417 if (tlAndN.m_tl == TL_LoopHoist && tlAndN.m_num >= 100)
8419 // A loop hoist annotation with value >= 100 means that the expression should be a static field access,
8420 // a GT_IND of a static field address, which should be the sum of a (hoistable) helper call and possibly some
8421 // offset within the the static field block whose address is returned by the helper call.
8422 // The annotation is saying that this address calculation, but not the entire access, should be hoisted.
8423 GenTreePtr helperCall = nullptr;
8424 assert(node->OperGet() == GT_IND);
8425 tlAndN.m_num -= 100;
8426 GetNodeTestData()->Set(node->gtOp.gtOp1, tlAndN);
8427 GetNodeTestData()->Remove(node);
8431 GetNodeTestData()->Set(node, tlAndN);
8434 impPushOnStack(node, expSe.seTypeInfo);
8435 return node->TypeGet();
8439 //-----------------------------------------------------------------------------------
8440 // impFixupCallStructReturn: For a call node that returns a struct type either
8441 // adjust the return type to an enregisterable type, or set the flag to indicate
8442 // struct return via retbuf arg.
8445 // call - GT_CALL GenTree node
8446 // retClsHnd - Class handle of return type of the call
8449 // Returns new GenTree node after fixing struct return of call node
8451 GenTreePtr Compiler::impFixupCallStructReturn(GenTreeCall* call, CORINFO_CLASS_HANDLE retClsHnd)
8453 if (!varTypeIsStruct(call))
8458 call->gtRetClsHnd = retClsHnd;
8460 #if FEATURE_MULTIREG_RET
8461 // Initialize Return type descriptor of call node
8462 ReturnTypeDesc* retTypeDesc = call->GetReturnTypeDesc();
8463 retTypeDesc->InitializeStructReturnType(this, retClsHnd);
8464 #endif // FEATURE_MULTIREG_RET
8466 #ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
8468 // Not allowed for FEATURE_CORCLR which is the only SKU available for System V OSs.
8469 assert(!call->IsVarargs() && "varargs not allowed for System V OSs.");
8471 // The return type will remain as the incoming struct type unless normalized to a
8472 // single eightbyte return type below.
8473 call->gtReturnType = call->gtType;
8475 unsigned retRegCount = retTypeDesc->GetReturnRegCount();
8476 if (retRegCount != 0)
8478 if (retRegCount == 1)
8480 // struct returned in a single register
8481 call->gtReturnType = retTypeDesc->GetReturnRegType(0);
8485 // must be a struct returned in two registers
8486 assert(retRegCount == 2);
8488 if ((!call->CanTailCall()) && (!call->IsInlineCandidate()))
8490 // Force a call returning multi-reg struct to be always of the IR form
8493 // No need to assign a multi-reg struct to a local var if:
8494 // - It is a tail call or
8495 // - The call is marked for in-lining later
8496 return impAssignMultiRegTypeToVar(call, retClsHnd);
8502 // struct not returned in registers i.e returned via hiddden retbuf arg.
8503 call->gtCallMoreFlags |= GTF_CALL_M_RETBUFFARG;
8506 #else // not FEATURE_UNIX_AMD64_STRUCT_PASSING
8508 // Check for TYP_STRUCT type that wraps a primitive type
8509 // Such structs are returned using a single register
8510 // and we change the return type on those calls here.
8512 structPassingKind howToReturnStruct;
8513 var_types returnType = getReturnTypeForStruct(retClsHnd, &howToReturnStruct);
8515 if (howToReturnStruct == SPK_ByReference)
8517 assert(returnType == TYP_UNKNOWN);
8518 call->gtCallMoreFlags |= GTF_CALL_M_RETBUFFARG;
8522 assert(returnType != TYP_UNKNOWN);
8523 call->gtReturnType = returnType;
8525 // ToDo: Refactor this common code sequence into its own method as it is used 4+ times
8526 if ((returnType == TYP_LONG) && (compLongUsed == false))
8528 compLongUsed = true;
8530 else if (((returnType == TYP_FLOAT) || (returnType == TYP_DOUBLE)) && (compFloatingPointUsed == false))
8532 compFloatingPointUsed = true;
8535 #if FEATURE_MULTIREG_RET
8536 unsigned retRegCount = retTypeDesc->GetReturnRegCount();
8537 assert(retRegCount != 0);
8539 if (retRegCount >= 2)
8541 if ((!call->CanTailCall()) && (!call->IsInlineCandidate()))
8543 // Force a call returning multi-reg struct to be always of the IR form
8546 // No need to assign a multi-reg struct to a local var if:
8547 // - It is a tail call or
8548 // - The call is marked for in-lining later
8549 return impAssignMultiRegTypeToVar(call, retClsHnd);
8552 #endif // FEATURE_MULTIREG_RET
8555 #endif // not FEATURE_UNIX_AMD64_STRUCT_PASSING
8560 /*****************************************************************************
8561 For struct return values, re-type the operand in the case where the ABI
8562 does not use a struct return buffer
8563 Note that this method is only call for !_TARGET_X86_
8566 GenTreePtr Compiler::impFixupStructReturnType(GenTreePtr op, CORINFO_CLASS_HANDLE retClsHnd)
8568 assert(varTypeIsStruct(info.compRetType));
8569 assert(info.compRetBuffArg == BAD_VAR_NUM);
8571 #if defined(_TARGET_XARCH_)
8573 #ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
8574 // No VarArgs for CoreCLR on x64 Unix
8575 assert(!info.compIsVarArgs);
8577 // Is method returning a multi-reg struct?
8578 if (varTypeIsStruct(info.compRetNativeType) && IsMultiRegReturnedType(retClsHnd))
8580 // In case of multi-reg struct return, we force IR to be one of the following:
8581 // GT_RETURN(lclvar) or GT_RETURN(call). If op is anything other than a
8582 // lclvar or call, it is assigned to a temp to create: temp = op and GT_RETURN(tmp).
8584 if (op->gtOper == GT_LCL_VAR)
8586 // Make sure that this struct stays in memory and doesn't get promoted.
8587 unsigned lclNum = op->gtLclVarCommon.gtLclNum;
8588 lvaTable[lclNum].lvIsMultiRegRet = true;
8590 // TODO-1stClassStructs: Handle constant propagation and CSE-ing of multireg returns.
8591 op->gtFlags |= GTF_DONT_CSE;
8596 if (op->gtOper == GT_CALL)
8601 return impAssignMultiRegTypeToVar(op, retClsHnd);
8603 #else // !FEATURE_UNIX_AMD64_STRUCT_PASSING
8604 assert(info.compRetNativeType != TYP_STRUCT);
8605 #endif // !FEATURE_UNIX_AMD64_STRUCT_PASSING
8607 #elif FEATURE_MULTIREG_RET && defined(_TARGET_ARM_)
8609 if (varTypeIsStruct(info.compRetNativeType) && !info.compIsVarArgs && IsHfa(retClsHnd))
8611 if (op->gtOper == GT_LCL_VAR)
8613 // This LCL_VAR is an HFA return value, it stays as a TYP_STRUCT
8614 unsigned lclNum = op->gtLclVarCommon.gtLclNum;
8615 // Make sure this struct type stays as struct so that we can return it as an HFA
8616 lvaTable[lclNum].lvIsMultiRegRet = true;
8618 // TODO-1stClassStructs: Handle constant propagation and CSE-ing of multireg returns.
8619 op->gtFlags |= GTF_DONT_CSE;
8624 if (op->gtOper == GT_CALL)
8626 if (op->gtCall.IsVarargs())
8628 // We cannot tail call because control needs to return to fixup the calling
8629 // convention for result return.
8630 op->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_TAILCALL;
8631 op->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_EXPLICIT_TAILCALL;
8638 return impAssignMultiRegTypeToVar(op, retClsHnd);
8641 #elif FEATURE_MULTIREG_RET && defined(_TARGET_ARM64_)
8643 // Is method returning a multi-reg struct?
8644 if (IsMultiRegReturnedType(retClsHnd))
8646 if (op->gtOper == GT_LCL_VAR)
8648 // This LCL_VAR stays as a TYP_STRUCT
8649 unsigned lclNum = op->gtLclVarCommon.gtLclNum;
8651 // Make sure this struct type is not struct promoted
8652 lvaTable[lclNum].lvIsMultiRegRet = true;
8654 // TODO-1stClassStructs: Handle constant propagation and CSE-ing of multireg returns.
8655 op->gtFlags |= GTF_DONT_CSE;
8660 if (op->gtOper == GT_CALL)
8662 if (op->gtCall.IsVarargs())
8664 // We cannot tail call because control needs to return to fixup the calling
8665 // convention for result return.
8666 op->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_TAILCALL;
8667 op->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_EXPLICIT_TAILCALL;
8674 return impAssignMultiRegTypeToVar(op, retClsHnd);
8677 #endif // FEATURE_MULTIREG_RET && FEATURE_HFA
8680 // adjust the type away from struct to integral
8681 // and no normalizing
8682 if (op->gtOper == GT_LCL_VAR)
8684 op->ChangeOper(GT_LCL_FLD);
8686 else if (op->gtOper == GT_OBJ)
8688 GenTreePtr op1 = op->AsObj()->Addr();
8690 // We will fold away OBJ/ADDR
8691 // except for OBJ/ADDR/INDEX
8692 // as the array type influences the array element's offset
8693 // Later in this method we change op->gtType to info.compRetNativeType
8694 // This is not correct when op is a GT_INDEX as the starting offset
8695 // for the array elements 'elemOffs' is different for an array of
8696 // TYP_REF than an array of TYP_STRUCT (which simply wraps a TYP_REF)
8697 // Also refer to the GTF_INX_REFARR_LAYOUT flag
8699 if ((op1->gtOper == GT_ADDR) && (op1->gtOp.gtOp1->gtOper != GT_INDEX))
8701 // Change '*(&X)' to 'X' and see if we can do better
8702 op = op1->gtOp.gtOp1;
8703 goto REDO_RETURN_NODE;
8705 op->gtObj.gtClass = NO_CLASS_HANDLE;
8706 op->ChangeOperUnchecked(GT_IND);
8707 op->gtFlags |= GTF_IND_TGTANYWHERE;
8709 else if (op->gtOper == GT_CALL)
8711 if (op->AsCall()->TreatAsHasRetBufArg(this))
8713 // This must be one of those 'special' helpers that don't
8714 // really have a return buffer, but instead use it as a way
8715 // to keep the trees cleaner with fewer address-taken temps.
8717 // Well now we have to materialize the the return buffer as
8718 // an address-taken temp. Then we can return the temp.
8720 // NOTE: this code assumes that since the call directly
8721 // feeds the return, then the call must be returning the
8722 // same structure/class/type.
8724 unsigned tmpNum = lvaGrabTemp(true DEBUGARG("pseudo return buffer"));
8726 // No need to spill anything as we're about to return.
8727 impAssignTempGen(tmpNum, op, info.compMethodInfo->args.retTypeClass, (unsigned)CHECK_SPILL_NONE);
8729 // Don't create both a GT_ADDR & GT_OBJ just to undo all of that; instead,
8730 // jump directly to a GT_LCL_FLD.
8731 op = gtNewLclvNode(tmpNum, info.compRetNativeType);
8732 op->ChangeOper(GT_LCL_FLD);
8736 assert(info.compRetNativeType == op->gtCall.gtReturnType);
8738 // Don't change the gtType of the node just yet, it will get changed later.
8742 else if (op->gtOper == GT_COMMA)
8744 op->gtOp.gtOp2 = impFixupStructReturnType(op->gtOp.gtOp2, retClsHnd);
8747 op->gtType = info.compRetNativeType;
8752 /*****************************************************************************
8753 CEE_LEAVE may be jumping out of a protected block, viz, a catch or a
8754 finally-protected try. We find the finally blocks protecting the current
8755 offset (in order) by walking over the complete exception table and
8756 finding enclosing clauses. This assumes that the table is sorted.
8757 This will create a series of BBJ_CALLFINALLY -> BBJ_CALLFINALLY ... -> BBJ_ALWAYS.
8759 If we are leaving a catch handler, we need to attach the
8760 CPX_ENDCATCHes to the correct BBJ_CALLFINALLY blocks.
8762 After this function, the BBJ_LEAVE block has been converted to a different type.
8765 #if !FEATURE_EH_FUNCLETS
8767 void Compiler::impImportLeave(BasicBlock* block)
8772 printf("\nBefore import CEE_LEAVE:\n");
8773 fgDispBasicBlocks();
8778 bool invalidatePreds = false; // If we create new blocks, invalidate the predecessor lists (if created)
8779 unsigned blkAddr = block->bbCodeOffs;
8780 BasicBlock* leaveTarget = block->bbJumpDest;
8781 unsigned jmpAddr = leaveTarget->bbCodeOffs;
8783 // LEAVE clears the stack, spill side effects, and set stack to 0
8785 impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("impImportLeave"));
8786 verCurrentState.esStackDepth = 0;
8788 assert(block->bbJumpKind == BBJ_LEAVE);
8789 assert(fgBBs == (BasicBlock**)0xCDCD || fgLookupBB(jmpAddr) != NULL); // should be a BB boundary
8791 BasicBlock* step = DUMMY_INIT(NULL);
8792 unsigned encFinallies = 0; // Number of enclosing finallies.
8793 GenTreePtr endCatches = NULL;
8794 GenTreePtr endLFin = NULL; // The statement tree to indicate the end of locally-invoked finally.
8799 for (XTnum = 0, HBtab = compHndBBtab; XTnum < compHndBBtabCount; XTnum++, HBtab++)
8801 // Grab the handler offsets
8803 IL_OFFSET tryBeg = HBtab->ebdTryBegOffs();
8804 IL_OFFSET tryEnd = HBtab->ebdTryEndOffs();
8805 IL_OFFSET hndBeg = HBtab->ebdHndBegOffs();
8806 IL_OFFSET hndEnd = HBtab->ebdHndEndOffs();
8808 /* Is this a catch-handler we are CEE_LEAVEing out of?
8809 * If so, we need to call CORINFO_HELP_ENDCATCH.
8812 if (jitIsBetween(blkAddr, hndBeg, hndEnd) && !jitIsBetween(jmpAddr, hndBeg, hndEnd))
8814 // Can't CEE_LEAVE out of a finally/fault handler
8815 if (HBtab->HasFinallyOrFaultHandler())
8816 BADCODE("leave out of fault/finally block");
8818 // Create the call to CORINFO_HELP_ENDCATCH
8819 GenTreePtr endCatch = gtNewHelperCallNode(CORINFO_HELP_ENDCATCH, TYP_VOID);
8821 // Make a list of all the currently pending endCatches
8823 endCatches = gtNewOperNode(GT_COMMA, TYP_VOID, endCatches, endCatch);
8825 endCatches = endCatch;
8830 printf("impImportLeave - BB%02u jumping out of catch handler EH#%u, adding call to "
8831 "CORINFO_HELP_ENDCATCH\n",
8832 block->bbNum, XTnum);
8836 else if (HBtab->HasFinallyHandler() && jitIsBetween(blkAddr, tryBeg, tryEnd) &&
8837 !jitIsBetween(jmpAddr, tryBeg, tryEnd))
8839 /* This is a finally-protected try we are jumping out of */
8841 /* If there are any pending endCatches, and we have already
8842 jumped out of a finally-protected try, then the endCatches
8843 have to be put in a block in an outer try for async
8844 exceptions to work correctly.
8845 Else, just use append to the original block */
8847 BasicBlock* callBlock;
8849 assert(!encFinallies == !endLFin); // if we have finallies, we better have an endLFin tree, and vice-versa
8851 if (encFinallies == 0)
8853 assert(step == DUMMY_INIT(NULL));
8855 callBlock->bbJumpKind = BBJ_CALLFINALLY; // convert the BBJ_LEAVE to BBJ_CALLFINALLY
8858 impAppendTree(endCatches, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
8863 printf("impImportLeave - jumping out of a finally-protected try, convert block to BBJ_CALLFINALLY "
8865 callBlock->dspToString());
8871 assert(step != DUMMY_INIT(NULL));
8873 /* Calling the finally block */
8874 callBlock = fgNewBBinRegion(BBJ_CALLFINALLY, XTnum + 1, 0, step);
8875 assert(step->bbJumpKind == BBJ_ALWAYS);
8876 step->bbJumpDest = callBlock; // the previous call to a finally returns to this call (to the next
8877 // finally in the chain)
8878 step->bbJumpDest->bbRefs++;
8880 /* The new block will inherit this block's weight */
8881 callBlock->setBBWeight(block->bbWeight);
8882 callBlock->bbFlags |= block->bbFlags & BBF_RUN_RARELY;
8887 printf("impImportLeave - jumping out of a finally-protected try, new BBJ_CALLFINALLY block %s\n",
8888 callBlock->dspToString());
8892 GenTreePtr lastStmt;
8896 lastStmt = gtNewStmt(endCatches);
8897 endLFin->gtNext = lastStmt;
8898 lastStmt->gtPrev = endLFin;
8905 // note that this sets BBF_IMPORTED on the block
8906 impEndTreeList(callBlock, endLFin, lastStmt);
8909 step = fgNewBBafter(BBJ_ALWAYS, callBlock, true);
8910 /* The new block will inherit this block's weight */
8911 step->setBBWeight(block->bbWeight);
8912 step->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED | BBF_KEEP_BBJ_ALWAYS;
8917 printf("impImportLeave - jumping out of a finally-protected try, created step (BBJ_ALWAYS) block %s\n",
8918 step->dspToString());
8922 unsigned finallyNesting = compHndBBtab[XTnum].ebdHandlerNestingLevel;
8923 assert(finallyNesting <= compHndBBtabCount);
8925 callBlock->bbJumpDest = HBtab->ebdHndBeg; // This callBlock will call the "finally" handler.
8926 endLFin = new (this, GT_END_LFIN) GenTreeVal(GT_END_LFIN, TYP_VOID, finallyNesting);
8927 endLFin = gtNewStmt(endLFin);
8932 invalidatePreds = true;
8936 /* Append any remaining endCatches, if any */
8938 assert(!encFinallies == !endLFin);
8940 if (encFinallies == 0)
8942 assert(step == DUMMY_INIT(NULL));
8943 block->bbJumpKind = BBJ_ALWAYS; // convert the BBJ_LEAVE to a BBJ_ALWAYS
8946 impAppendTree(endCatches, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
8951 printf("impImportLeave - no enclosing finally-protected try blocks; convert CEE_LEAVE block to BBJ_ALWAYS "
8953 block->dspToString());
8959 // If leaveTarget is the start of another try block, we want to make sure that
8960 // we do not insert finalStep into that try block. Hence, we find the enclosing
8962 unsigned tryIndex = bbFindInnermostCommonTryRegion(step, leaveTarget);
8964 // Insert a new BB either in the try region indicated by tryIndex or
8965 // the handler region indicated by leaveTarget->bbHndIndex,
8966 // depending on which is the inner region.
8967 BasicBlock* finalStep = fgNewBBinRegion(BBJ_ALWAYS, tryIndex, leaveTarget->bbHndIndex, step);
8968 finalStep->bbFlags |= BBF_KEEP_BBJ_ALWAYS;
8969 step->bbJumpDest = finalStep;
8971 /* The new block will inherit this block's weight */
8972 finalStep->setBBWeight(block->bbWeight);
8973 finalStep->bbFlags |= block->bbFlags & BBF_RUN_RARELY;
8978 printf("impImportLeave - finalStep block required (encFinallies(%d) > 0), new block %s\n", encFinallies,
8979 finalStep->dspToString());
8983 GenTreePtr lastStmt;
8987 lastStmt = gtNewStmt(endCatches);
8988 endLFin->gtNext = lastStmt;
8989 lastStmt->gtPrev = endLFin;
8996 impEndTreeList(finalStep, endLFin, lastStmt);
8998 finalStep->bbJumpDest = leaveTarget; // this is the ultimate destination of the LEAVE
9000 // Queue up the jump target for importing
9002 impImportBlockPending(leaveTarget);
9004 invalidatePreds = true;
9007 if (invalidatePreds && fgComputePredsDone)
9009 JITDUMP("\n**** impImportLeave - Removing preds after creating new blocks\n");
9014 fgVerifyHandlerTab();
9018 printf("\nAfter import CEE_LEAVE:\n");
9019 fgDispBasicBlocks();
9025 #else // FEATURE_EH_FUNCLETS
9027 void Compiler::impImportLeave(BasicBlock* block)
9032 printf("\nBefore import CEE_LEAVE in BB%02u (targetting BB%02u):\n", block->bbNum, block->bbJumpDest->bbNum);
9033 fgDispBasicBlocks();
9038 bool invalidatePreds = false; // If we create new blocks, invalidate the predecessor lists (if created)
9039 unsigned blkAddr = block->bbCodeOffs;
9040 BasicBlock* leaveTarget = block->bbJumpDest;
9041 unsigned jmpAddr = leaveTarget->bbCodeOffs;
9043 // LEAVE clears the stack, spill side effects, and set stack to 0
9045 impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("impImportLeave"));
9046 verCurrentState.esStackDepth = 0;
9048 assert(block->bbJumpKind == BBJ_LEAVE);
9049 assert(fgBBs == (BasicBlock**)0xCDCD || fgLookupBB(jmpAddr) != nullptr); // should be a BB boundary
9051 BasicBlock* step = nullptr;
9055 // No step type; step == NULL.
9058 // Is the step block the BBJ_ALWAYS block of a BBJ_CALLFINALLY/BBJ_ALWAYS pair?
9059 // That is, is step->bbJumpDest where a finally will return to?
9062 // The step block is a catch return.
9065 // The step block is in a "try", created as the target for a finally return or the target for a catch return.
9068 StepType stepType = ST_None;
9073 for (XTnum = 0, HBtab = compHndBBtab; XTnum < compHndBBtabCount; XTnum++, HBtab++)
9075 // Grab the handler offsets
9077 IL_OFFSET tryBeg = HBtab->ebdTryBegOffs();
9078 IL_OFFSET tryEnd = HBtab->ebdTryEndOffs();
9079 IL_OFFSET hndBeg = HBtab->ebdHndBegOffs();
9080 IL_OFFSET hndEnd = HBtab->ebdHndEndOffs();
9082 /* Is this a catch-handler we are CEE_LEAVEing out of?
9085 if (jitIsBetween(blkAddr, hndBeg, hndEnd) && !jitIsBetween(jmpAddr, hndBeg, hndEnd))
9087 // Can't CEE_LEAVE out of a finally/fault handler
9088 if (HBtab->HasFinallyOrFaultHandler())
9090 BADCODE("leave out of fault/finally block");
9093 /* We are jumping out of a catch */
9095 if (step == nullptr)
9098 step->bbJumpKind = BBJ_EHCATCHRET; // convert the BBJ_LEAVE to BBJ_EHCATCHRET
9099 stepType = ST_Catch;
9104 printf("impImportLeave - jumping out of a catch (EH#%u), convert block BB%02u to BBJ_EHCATCHRET "
9106 XTnum, step->bbNum);
9112 BasicBlock* exitBlock;
9114 /* Create a new catch exit block in the catch region for the existing step block to jump to in this
9116 exitBlock = fgNewBBinRegion(BBJ_EHCATCHRET, 0, XTnum + 1, step);
9118 assert(step->bbJumpKind == BBJ_ALWAYS || step->bbJumpKind == BBJ_EHCATCHRET);
9119 step->bbJumpDest = exitBlock; // the previous step (maybe a call to a nested finally, or a nested catch
9120 // exit) returns to this block
9121 step->bbJumpDest->bbRefs++;
9123 #if defined(_TARGET_ARM_)
9124 if (stepType == ST_FinallyReturn)
9126 assert(step->bbJumpKind == BBJ_ALWAYS);
9127 // Mark the target of a finally return
9128 step->bbJumpDest->bbFlags |= BBF_FINALLY_TARGET;
9130 #endif // defined(_TARGET_ARM_)
9132 /* The new block will inherit this block's weight */
9133 exitBlock->setBBWeight(block->bbWeight);
9134 exitBlock->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED;
9136 /* This exit block is the new step */
9138 stepType = ST_Catch;
9140 invalidatePreds = true;
9145 printf("impImportLeave - jumping out of a catch (EH#%u), new BBJ_EHCATCHRET block BB%02u\n", XTnum,
9151 else if (HBtab->HasFinallyHandler() && jitIsBetween(blkAddr, tryBeg, tryEnd) &&
9152 !jitIsBetween(jmpAddr, tryBeg, tryEnd))
9154 /* We are jumping out of a finally-protected try */
9156 BasicBlock* callBlock;
9158 if (step == nullptr)
9160 #if FEATURE_EH_CALLFINALLY_THUNKS
9162 // Put the call to the finally in the enclosing region.
9163 unsigned callFinallyTryIndex =
9164 (HBtab->ebdEnclosingTryIndex == EHblkDsc::NO_ENCLOSING_INDEX) ? 0 : HBtab->ebdEnclosingTryIndex + 1;
9165 unsigned callFinallyHndIndex =
9166 (HBtab->ebdEnclosingHndIndex == EHblkDsc::NO_ENCLOSING_INDEX) ? 0 : HBtab->ebdEnclosingHndIndex + 1;
9167 callBlock = fgNewBBinRegion(BBJ_CALLFINALLY, callFinallyTryIndex, callFinallyHndIndex, block);
9169 // Convert the BBJ_LEAVE to BBJ_ALWAYS, jumping to the new BBJ_CALLFINALLY. This is because
9170 // the new BBJ_CALLFINALLY is in a different EH region, thus it can't just replace the BBJ_LEAVE,
9171 // which might be in the middle of the "try". In most cases, the BBJ_ALWAYS will jump to the
9172 // next block, and flow optimizations will remove it.
9173 block->bbJumpKind = BBJ_ALWAYS;
9174 block->bbJumpDest = callBlock;
9175 block->bbJumpDest->bbRefs++;
9177 /* The new block will inherit this block's weight */
9178 callBlock->setBBWeight(block->bbWeight);
9179 callBlock->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED;
9184 printf("impImportLeave - jumping out of a finally-protected try (EH#%u), convert block BB%02u to "
9185 "BBJ_ALWAYS, add BBJ_CALLFINALLY block BB%02u\n",
9186 XTnum, block->bbNum, callBlock->bbNum);
9190 #else // !FEATURE_EH_CALLFINALLY_THUNKS
9193 callBlock->bbJumpKind = BBJ_CALLFINALLY; // convert the BBJ_LEAVE to BBJ_CALLFINALLY
9198 printf("impImportLeave - jumping out of a finally-protected try (EH#%u), convert block BB%02u to "
9199 "BBJ_CALLFINALLY block\n",
9200 XTnum, callBlock->bbNum);
9204 #endif // !FEATURE_EH_CALLFINALLY_THUNKS
9208 // Calling the finally block. We already have a step block that is either the call-to-finally from a
9209 // more nested try/finally (thus we are jumping out of multiple nested 'try' blocks, each protected by
9210 // a 'finally'), or the step block is the return from a catch.
9212 // Due to ThreadAbortException, we can't have the catch return target the call-to-finally block
9213 // directly. Note that if a 'catch' ends without resetting the ThreadAbortException, the VM will
9214 // automatically re-raise the exception, using the return address of the catch (that is, the target
9215 // block of the BBJ_EHCATCHRET) as the re-raise address. If this address is in a finally, the VM will
9216 // refuse to do the re-raise, and the ThreadAbortException will get eaten (and lost). On AMD64/ARM64,
9217 // we put the call-to-finally thunk in a special "cloned finally" EH region that does look like a
9218 // finally clause to the VM. Thus, on these platforms, we can't have BBJ_EHCATCHRET target a
9219 // BBJ_CALLFINALLY directly. (Note that on ARM32, we don't mark the thunk specially -- it lives directly
9220 // within the 'try' region protected by the finally, since we generate code in such a way that execution
9221 // never returns to the call-to-finally call, and the finally-protected 'try' region doesn't appear on
9224 assert(step->bbJumpKind == BBJ_ALWAYS || step->bbJumpKind == BBJ_EHCATCHRET);
9226 #if FEATURE_EH_CALLFINALLY_THUNKS
9227 if (step->bbJumpKind == BBJ_EHCATCHRET)
9229 // Need to create another step block in the 'try' region that will actually branch to the
9230 // call-to-finally thunk.
9231 BasicBlock* step2 = fgNewBBinRegion(BBJ_ALWAYS, XTnum + 1, 0, step);
9232 step->bbJumpDest = step2;
9233 step->bbJumpDest->bbRefs++;
9234 step2->setBBWeight(block->bbWeight);
9235 step2->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED;
9240 printf("impImportLeave - jumping out of a finally-protected try (EH#%u), step block is "
9241 "BBJ_EHCATCHRET (BB%02u), new BBJ_ALWAYS step-step block BB%02u\n",
9242 XTnum, step->bbNum, step2->bbNum);
9247 assert(stepType == ST_Catch); // Leave it as catch type for now.
9249 #endif // FEATURE_EH_CALLFINALLY_THUNKS
9251 #if FEATURE_EH_CALLFINALLY_THUNKS
9252 unsigned callFinallyTryIndex =
9253 (HBtab->ebdEnclosingTryIndex == EHblkDsc::NO_ENCLOSING_INDEX) ? 0 : HBtab->ebdEnclosingTryIndex + 1;
9254 unsigned callFinallyHndIndex =
9255 (HBtab->ebdEnclosingHndIndex == EHblkDsc::NO_ENCLOSING_INDEX) ? 0 : HBtab->ebdEnclosingHndIndex + 1;
9256 #else // !FEATURE_EH_CALLFINALLY_THUNKS
9257 unsigned callFinallyTryIndex = XTnum + 1;
9258 unsigned callFinallyHndIndex = 0; // don't care
9259 #endif // !FEATURE_EH_CALLFINALLY_THUNKS
9261 callBlock = fgNewBBinRegion(BBJ_CALLFINALLY, callFinallyTryIndex, callFinallyHndIndex, step);
9262 step->bbJumpDest = callBlock; // the previous call to a finally returns to this call (to the next
9263 // finally in the chain)
9264 step->bbJumpDest->bbRefs++;
9266 #if defined(_TARGET_ARM_)
9267 if (stepType == ST_FinallyReturn)
9269 assert(step->bbJumpKind == BBJ_ALWAYS);
9270 // Mark the target of a finally return
9271 step->bbJumpDest->bbFlags |= BBF_FINALLY_TARGET;
9273 #endif // defined(_TARGET_ARM_)
9275 /* The new block will inherit this block's weight */
9276 callBlock->setBBWeight(block->bbWeight);
9277 callBlock->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED;
9282 printf("impImportLeave - jumping out of a finally-protected try (EH#%u), new BBJ_CALLFINALLY block "
9284 XTnum, callBlock->bbNum);
9289 step = fgNewBBafter(BBJ_ALWAYS, callBlock, true);
9290 stepType = ST_FinallyReturn;
9292 /* The new block will inherit this block's weight */
9293 step->setBBWeight(block->bbWeight);
9294 step->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED | BBF_KEEP_BBJ_ALWAYS;
9299 printf("impImportLeave - jumping out of a finally-protected try (EH#%u), created step (BBJ_ALWAYS) "
9301 XTnum, step->bbNum);
9305 callBlock->bbJumpDest = HBtab->ebdHndBeg; // This callBlock will call the "finally" handler.
9307 invalidatePreds = true;
9309 else if (HBtab->HasCatchHandler() && jitIsBetween(blkAddr, tryBeg, tryEnd) &&
9310 !jitIsBetween(jmpAddr, tryBeg, tryEnd))
9312 // We are jumping out of a catch-protected try.
9314 // If we are returning from a call to a finally, then we must have a step block within a try
9315 // that is protected by a catch. This is so when unwinding from that finally (e.g., if code within the
9316 // finally raises an exception), the VM will find this step block, notice that it is in a protected region,
9317 // and invoke the appropriate catch.
9319 // We also need to handle a special case with the handling of ThreadAbortException. If a try/catch
9320 // catches a ThreadAbortException (which might be because it catches a parent, e.g. System.Exception),
9321 // and the catch doesn't call System.Threading.Thread::ResetAbort(), then when the catch returns to the VM,
9322 // the VM will automatically re-raise the ThreadAbortException. When it does this, it uses the target
9323 // address of the catch return as the new exception address. That is, the re-raised exception appears to
9324 // occur at the catch return address. If this exception return address skips an enclosing try/catch that
9325 // catches ThreadAbortException, then the enclosing try/catch will not catch the exception, as it should.
9330 // // something here raises ThreadAbortException
9331 // LEAVE LABEL_1; // no need to stop at LABEL_2
9332 // } catch (Exception) {
9333 // // This catches ThreadAbortException, but doesn't call System.Threading.Thread::ResetAbort(), so
9334 // // ThreadAbortException is re-raised by the VM at the address specified by the LEAVE opcode.
9335 // // This is bad, since it means the outer try/catch won't get a chance to catch the re-raised
9336 // // ThreadAbortException. So, instead, create step block LABEL_2 and LEAVE to that. We only
9337 // // need to do this transformation if the current EH block is a try/catch that catches
9338 // // ThreadAbortException (or one of its parents), however we might not be able to find that
9339 // // information, so currently we do it for all catch types.
9340 // LEAVE LABEL_1; // Convert this to LEAVE LABEL2;
9342 // LABEL_2: LEAVE LABEL_1; // inserted by this step creation code
9343 // } catch (ThreadAbortException) {
9347 // Note that this pattern isn't theoretical: it occurs in ASP.NET, in IL code generated by the Roslyn C#
9350 if ((stepType == ST_FinallyReturn) || (stepType == ST_Catch))
9352 BasicBlock* catchStep;
9356 if (stepType == ST_FinallyReturn)
9358 assert(step->bbJumpKind == BBJ_ALWAYS);
9362 assert(stepType == ST_Catch);
9363 assert(step->bbJumpKind == BBJ_EHCATCHRET);
9366 /* Create a new exit block in the try region for the existing step block to jump to in this scope */
9367 catchStep = fgNewBBinRegion(BBJ_ALWAYS, XTnum + 1, 0, step);
9368 step->bbJumpDest = catchStep;
9369 step->bbJumpDest->bbRefs++;
9371 #if defined(_TARGET_ARM_)
9372 if (stepType == ST_FinallyReturn)
9374 // Mark the target of a finally return
9375 step->bbJumpDest->bbFlags |= BBF_FINALLY_TARGET;
9377 #endif // defined(_TARGET_ARM_)
9379 /* The new block will inherit this block's weight */
9380 catchStep->setBBWeight(block->bbWeight);
9381 catchStep->bbFlags |= (block->bbFlags & BBF_RUN_RARELY) | BBF_IMPORTED;
9386 if (stepType == ST_FinallyReturn)
9388 printf("impImportLeave - return from finally jumping out of a catch-protected try (EH#%u), new "
9389 "BBJ_ALWAYS block BB%02u\n",
9390 XTnum, catchStep->bbNum);
9394 assert(stepType == ST_Catch);
9395 printf("impImportLeave - return from catch jumping out of a catch-protected try (EH#%u), new "
9396 "BBJ_ALWAYS block BB%02u\n",
9397 XTnum, catchStep->bbNum);
9402 /* This block is the new step */
9406 invalidatePreds = true;
9411 if (step == nullptr)
9413 block->bbJumpKind = BBJ_ALWAYS; // convert the BBJ_LEAVE to a BBJ_ALWAYS
9418 printf("impImportLeave - no enclosing finally-protected try blocks or catch handlers; convert CEE_LEAVE "
9419 "block BB%02u to BBJ_ALWAYS\n",
9426 step->bbJumpDest = leaveTarget; // this is the ultimate destination of the LEAVE
9428 #if defined(_TARGET_ARM_)
9429 if (stepType == ST_FinallyReturn)
9431 assert(step->bbJumpKind == BBJ_ALWAYS);
9432 // Mark the target of a finally return
9433 step->bbJumpDest->bbFlags |= BBF_FINALLY_TARGET;
9435 #endif // defined(_TARGET_ARM_)
9440 printf("impImportLeave - final destination of step blocks set to BB%02u\n", leaveTarget->bbNum);
9444 // Queue up the jump target for importing
9446 impImportBlockPending(leaveTarget);
9449 if (invalidatePreds && fgComputePredsDone)
9451 JITDUMP("\n**** impImportLeave - Removing preds after creating new blocks\n");
9456 fgVerifyHandlerTab();
9460 printf("\nAfter import CEE_LEAVE:\n");
9461 fgDispBasicBlocks();
9467 #endif // FEATURE_EH_FUNCLETS
9469 /*****************************************************************************/
9470 // This is called when reimporting a leave block. It resets the JumpKind,
9471 // JumpDest, and bbNext to the original values
9473 void Compiler::impResetLeaveBlock(BasicBlock* block, unsigned jmpAddr)
9475 #if FEATURE_EH_FUNCLETS
9476 // With EH Funclets, while importing leave opcode we create another block ending with BBJ_ALWAYS (call it B1)
9477 // and the block containing leave (say B0) is marked as BBJ_CALLFINALLY. Say for some reason we reimport B0,
9478 // it is reset (in this routine) by marking as ending with BBJ_LEAVE and further down when B0 is reimported, we
9479 // create another BBJ_ALWAYS (call it B2). In this process B1 gets orphaned and any blocks to which B1 is the
9480 // only predecessor are also considered orphans and attempted to be deleted.
9487 // leave OUTSIDE; // B0 is the block containing this leave, following this would be B1
9492 // In the above nested try-finally example, we create a step block (call it Bstep) which in branches to a block
9493 // where a finally would branch to (and such block is marked as finally target). Block B1 branches to step block.
9494 // Because of re-import of B0, Bstep is also orphaned. Since Bstep is a finally target it cannot be removed. To
9495 // work around this we will duplicate B0 (call it B0Dup) before reseting. B0Dup is marked as BBJ_CALLFINALLY and
9496 // only serves to pair up with B1 (BBJ_ALWAYS) that got orphaned. Now during orphan block deletion B0Dup and B1
9497 // will be treated as pair and handled correctly.
9498 if (block->bbJumpKind == BBJ_CALLFINALLY)
9500 BasicBlock* dupBlock = bbNewBasicBlock(block->bbJumpKind);
9501 dupBlock->bbFlags = block->bbFlags;
9502 dupBlock->bbJumpDest = block->bbJumpDest;
9503 dupBlock->copyEHRegion(block);
9504 dupBlock->bbCatchTyp = block->bbCatchTyp;
9506 // Mark this block as
9507 // a) not referenced by any other block to make sure that it gets deleted
9509 // c) prevent from being imported
9512 dupBlock->bbRefs = 0;
9513 dupBlock->bbWeight = 0;
9514 dupBlock->bbFlags |= BBF_IMPORTED | BBF_INTERNAL | BBF_RUN_RARELY;
9516 // Insert the block right after the block which is getting reset so that BBJ_CALLFINALLY and BBJ_ALWAYS
9517 // will be next to each other.
9518 fgInsertBBafter(block, dupBlock);
9523 printf("New Basic Block BB%02u duplicate of BB%02u created.\n", dupBlock->bbNum, block->bbNum);
9527 #endif // FEATURE_EH_FUNCLETS
9529 block->bbJumpKind = BBJ_LEAVE;
9531 block->bbJumpDest = fgLookupBB(jmpAddr);
9533 // We will leave the BBJ_ALWAYS block we introduced. When it's reimported
9534 // the BBJ_ALWAYS block will be unreachable, and will be removed after. The
9535 // reason we don't want to remove the block at this point is that if we call
9536 // fgInitBBLookup() again we will do it wrong as the BBJ_ALWAYS block won't be
9537 // added and the linked list length will be different than fgBBcount.
9540 /*****************************************************************************/
9541 // Get the first non-prefix opcode. Used for verification of valid combinations
9542 // of prefixes and actual opcodes.
9544 static OPCODE impGetNonPrefixOpcode(const BYTE* codeAddr, const BYTE* codeEndp)
9546 while (codeAddr < codeEndp)
9548 OPCODE opcode = (OPCODE)getU1LittleEndian(codeAddr);
9549 codeAddr += sizeof(__int8);
9551 if (opcode == CEE_PREFIX1)
9553 if (codeAddr >= codeEndp)
9557 opcode = (OPCODE)(getU1LittleEndian(codeAddr) + 256);
9558 codeAddr += sizeof(__int8);
9566 case CEE_CONSTRAINED:
9573 codeAddr += opcodeSizes[opcode];
9579 /*****************************************************************************/
9580 // Checks whether the opcode is a valid opcode for volatile. and unaligned. prefixes
9582 static void impValidateMemoryAccessOpcode(const BYTE* codeAddr, const BYTE* codeEndp, bool volatilePrefix)
9584 OPCODE opcode = impGetNonPrefixOpcode(codeAddr, codeEndp);
9587 // Opcode of all ldind and stdind happen to be in continuous, except stind.i.
9588 ((CEE_LDIND_I1 <= opcode) && (opcode <= CEE_STIND_R8)) || (opcode == CEE_STIND_I) ||
9589 (opcode == CEE_LDFLD) || (opcode == CEE_STFLD) || (opcode == CEE_LDOBJ) || (opcode == CEE_STOBJ) ||
9590 (opcode == CEE_INITBLK) || (opcode == CEE_CPBLK) ||
9591 // volatile. prefix is allowed with the ldsfld and stsfld
9592 (volatilePrefix && ((opcode == CEE_LDSFLD) || (opcode == CEE_STSFLD)))))
9594 BADCODE("Invalid opcode for unaligned. or volatile. prefix");
9598 /*****************************************************************************/
9602 #undef RETURN // undef contracts RETURN macro
9617 const static controlFlow_t controlFlow[] = {
9618 #define OPDEF(c, s, pop, push, args, type, l, s1, s2, flow) flow,
9619 #include "opcode.def"
9625 /*****************************************************************************
9626 * Determine the result type of an arithemetic operation
9627 * On 64-bit inserts upcasts when native int is mixed with int32
9629 var_types Compiler::impGetByRefResultType(genTreeOps oper, bool fUnsigned, GenTreePtr* pOp1, GenTreePtr* pOp2)
9631 var_types type = TYP_UNDEF;
9632 GenTreePtr op1 = *pOp1, op2 = *pOp2;
9634 // Arithemetic operations are generally only allowed with
9635 // primitive types, but certain operations are allowed
9638 if ((oper == GT_SUB) && (genActualType(op1->TypeGet()) == TYP_BYREF || genActualType(op2->TypeGet()) == TYP_BYREF))
9640 if ((genActualType(op1->TypeGet()) == TYP_BYREF) && (genActualType(op2->TypeGet()) == TYP_BYREF))
9642 // byref1-byref2 => gives a native int
9645 else if (genActualTypeIsIntOrI(op1->TypeGet()) && (genActualType(op2->TypeGet()) == TYP_BYREF))
9647 // [native] int - byref => gives a native int
9650 // The reason is that it is possible, in managed C++,
9651 // to have a tree like this:
9658 // const(h) int addr byref
9660 // <BUGNUM> VSW 318822 </BUGNUM>
9662 // So here we decide to make the resulting type to be a native int.
9663 CLANG_FORMAT_COMMENT_ANCHOR;
9665 #ifdef _TARGET_64BIT_
9666 if (genActualType(op1->TypeGet()) != TYP_I_IMPL)
9668 // insert an explicit upcast
9669 op1 = *pOp1 = gtNewCastNode(TYP_I_IMPL, op1, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9671 #endif // _TARGET_64BIT_
9677 // byref - [native] int => gives a byref
9678 assert(genActualType(op1->TypeGet()) == TYP_BYREF && genActualTypeIsIntOrI(op2->TypeGet()));
9680 #ifdef _TARGET_64BIT_
9681 if ((genActualType(op2->TypeGet()) != TYP_I_IMPL))
9683 // insert an explicit upcast
9684 op2 = *pOp2 = gtNewCastNode(TYP_I_IMPL, op2, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9686 #endif // _TARGET_64BIT_
9691 else if ((oper == GT_ADD) &&
9692 (genActualType(op1->TypeGet()) == TYP_BYREF || genActualType(op2->TypeGet()) == TYP_BYREF))
9694 // byref + [native] int => gives a byref
9696 // [native] int + byref => gives a byref
9698 // only one can be a byref : byref op byref not allowed
9699 assert(genActualType(op1->TypeGet()) != TYP_BYREF || genActualType(op2->TypeGet()) != TYP_BYREF);
9700 assert(genActualTypeIsIntOrI(op1->TypeGet()) || genActualTypeIsIntOrI(op2->TypeGet()));
9702 #ifdef _TARGET_64BIT_
9703 if (genActualType(op2->TypeGet()) == TYP_BYREF)
9705 if (genActualType(op1->TypeGet()) != TYP_I_IMPL)
9707 // insert an explicit upcast
9708 op1 = *pOp1 = gtNewCastNode(TYP_I_IMPL, op1, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9711 else if (genActualType(op2->TypeGet()) != TYP_I_IMPL)
9713 // insert an explicit upcast
9714 op2 = *pOp2 = gtNewCastNode(TYP_I_IMPL, op2, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9716 #endif // _TARGET_64BIT_
9720 #ifdef _TARGET_64BIT_
9721 else if (genActualType(op1->TypeGet()) == TYP_I_IMPL || genActualType(op2->TypeGet()) == TYP_I_IMPL)
9723 assert(!varTypeIsFloating(op1->gtType) && !varTypeIsFloating(op2->gtType));
9725 // int + long => gives long
9726 // long + int => gives long
9727 // we get this because in the IL the long isn't Int64, it's just IntPtr
9729 if (genActualType(op1->TypeGet()) != TYP_I_IMPL)
9731 // insert an explicit upcast
9732 op1 = *pOp1 = gtNewCastNode(TYP_I_IMPL, op1, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9734 else if (genActualType(op2->TypeGet()) != TYP_I_IMPL)
9736 // insert an explicit upcast
9737 op2 = *pOp2 = gtNewCastNode(TYP_I_IMPL, op2, (var_types)(fUnsigned ? TYP_U_IMPL : TYP_I_IMPL));
9742 #else // 32-bit TARGET
9743 else if (genActualType(op1->TypeGet()) == TYP_LONG || genActualType(op2->TypeGet()) == TYP_LONG)
9745 assert(!varTypeIsFloating(op1->gtType) && !varTypeIsFloating(op2->gtType));
9747 // int + long => gives long
9748 // long + int => gives long
9752 #endif // _TARGET_64BIT_
9755 // int + int => gives an int
9756 assert(genActualType(op1->TypeGet()) != TYP_BYREF && genActualType(op2->TypeGet()) != TYP_BYREF);
9758 assert(genActualType(op1->TypeGet()) == genActualType(op2->TypeGet()) ||
9759 varTypeIsFloating(op1->gtType) && varTypeIsFloating(op2->gtType));
9761 type = genActualType(op1->gtType);
9763 #if FEATURE_X87_DOUBLES
9765 // For x87, since we only have 1 size of registers, prefer double
9766 // For everybody else, be more precise
9767 if (type == TYP_FLOAT)
9770 #else // !FEATURE_X87_DOUBLES
9772 // If both operands are TYP_FLOAT, then leave it as TYP_FLOAT.
9773 // Otherwise, turn floats into doubles
9774 if ((type == TYP_FLOAT) && (genActualType(op2->gtType) != TYP_FLOAT))
9776 assert(genActualType(op2->gtType) == TYP_DOUBLE);
9780 #endif // FEATURE_X87_DOUBLES
9783 #if FEATURE_X87_DOUBLES
9784 assert(type == TYP_BYREF || type == TYP_DOUBLE || type == TYP_LONG || type == TYP_INT);
9785 #else // FEATURE_X87_DOUBLES
9786 assert(type == TYP_BYREF || type == TYP_DOUBLE || type == TYP_FLOAT || type == TYP_LONG || type == TYP_INT);
9787 #endif // FEATURE_X87_DOUBLES
9792 //------------------------------------------------------------------------
9793 // impOptimizeCastClassOrIsInst: attempt to resolve a cast when jitting
9796 // op1 - value to cast
9797 // pResolvedToken - resolved token for type to cast to
9798 // isCastClass - true if this is a castclass, false if isinst
9801 // tree representing optimized cast, or null if no optimization possible
9803 GenTree* Compiler::impOptimizeCastClassOrIsInst(GenTree* op1, CORINFO_RESOLVED_TOKEN* pResolvedToken, bool isCastClass)
9805 assert(op1->TypeGet() == TYP_REF);
9807 // Don't optimize for minopts or debug codegen.
9808 if (opts.compDbgCode || opts.MinOpts())
9813 // See what we know about the type of the object being cast.
9814 bool isExact = false;
9815 bool isNonNull = false;
9816 CORINFO_CLASS_HANDLE fromClass = gtGetClassHandle(op1, &isExact, &isNonNull);
9817 GenTree* optResult = nullptr;
9819 if (fromClass != nullptr)
9821 CORINFO_CLASS_HANDLE toClass = pResolvedToken->hClass;
9822 JITDUMP("\nConsidering optimization of %s from %s%p (%s) to %p (%s)\n", isCastClass ? "castclass" : "isinst",
9823 isExact ? "exact " : "", dspPtr(fromClass), info.compCompHnd->getClassName(fromClass), dspPtr(toClass),
9824 info.compCompHnd->getClassName(toClass));
9826 // Perhaps we know if the cast will succeed or fail.
9827 TypeCompareState castResult = info.compCompHnd->compareTypesForCast(fromClass, toClass);
9829 if (castResult == TypeCompareState::Must)
9831 // Cast will succeed, result is simply op1.
9832 JITDUMP("Cast will succeed, optimizing to simply return input\n");
9835 else if (castResult == TypeCompareState::MustNot)
9837 // See if we can sharpen exactness by looking for final classes
9840 DWORD flags = info.compCompHnd->getClassAttribs(fromClass);
9841 DWORD flagsMask = CORINFO_FLG_FINAL | CORINFO_FLG_MARSHAL_BYREF | CORINFO_FLG_CONTEXTFUL |
9842 CORINFO_FLG_VARIANCE | CORINFO_FLG_ARRAY;
9843 isExact = ((flags & flagsMask) == CORINFO_FLG_FINAL);
9846 // Cast to exact type will fail. Handle case where we have
9847 // an exact type (that is, fromClass is not a subtype)
9848 // and we're not going to throw on failure.
9849 if (isExact && !isCastClass)
9851 JITDUMP("Cast will fail, optimizing to return null\n");
9852 GenTree* result = gtNewIconNode(0, TYP_REF);
9854 // If the cast was fed by a box, we can remove that too.
9855 if (op1->IsBoxedValue())
9857 JITDUMP("Also removing upstream box\n");
9858 gtTryRemoveBoxUpstreamEffects(op1);
9865 JITDUMP("Not optimizing failing castclass (yet)\n");
9869 JITDUMP("Can't optimize since fromClass is inexact\n");
9874 JITDUMP("Result of cast unknown, must generate runtime test\n");
9879 JITDUMP("\nCan't optimize since fromClass is unknown\n");
9885 //------------------------------------------------------------------------
9886 // impCastClassOrIsInstToTree: build and import castclass/isinst
9889 // op1 - value to cast
9890 // op2 - type handle for type to cast to
9891 // pResolvedToken - resolved token from the cast operation
9892 // isCastClass - true if this is castclass, false means isinst
9895 // Tree representing the cast
9898 // May expand into a series of runtime checks or a helper call.
9900 GenTreePtr Compiler::impCastClassOrIsInstToTree(GenTreePtr op1,
9902 CORINFO_RESOLVED_TOKEN* pResolvedToken,
9905 assert(op1->TypeGet() == TYP_REF);
9907 // Optimistically assume the jit should expand this as an inline test
9908 bool shouldExpandInline = true;
9910 // Profitability check.
9912 // Don't bother with inline expansion when jit is trying to
9913 // generate code quickly, or the cast is in code that won't run very
9914 // often, or the method already is pretty big.
9915 if (compCurBB->isRunRarely() || opts.compDbgCode || opts.MinOpts())
9917 // not worth the code expansion if jitting fast or in a rarely run block
9918 shouldExpandInline = false;
9920 else if ((op1->gtFlags & GTF_GLOB_EFFECT) && lvaHaveManyLocals())
9922 // not worth creating an untracked local variable
9923 shouldExpandInline = false;
9926 // Pessimistically assume the jit cannot expand this as an inline test
9927 bool canExpandInline = false;
9928 const CorInfoHelpFunc helper = info.compCompHnd->getCastingHelper(pResolvedToken, isCastClass);
9932 // Not all classclass/isinst operations can be inline expanded.
9933 // Check legality only if an inline expansion is desirable.
9934 if (shouldExpandInline)
9938 // Jit can only inline expand the normal CHKCASTCLASS helper.
9939 canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS);
9943 if (helper == CORINFO_HELP_ISINSTANCEOFCLASS)
9945 // Check the class attributes.
9946 DWORD flags = info.compCompHnd->getClassAttribs(pResolvedToken->hClass);
9948 // If the class is final and is not marshal byref or
9949 // contextful, the jit can expand the IsInst check inline.
9950 DWORD flagsMask = CORINFO_FLG_FINAL | CORINFO_FLG_MARSHAL_BYREF | CORINFO_FLG_CONTEXTFUL;
9951 canExpandInline = ((flags & flagsMask) == CORINFO_FLG_FINAL);
9956 const bool expandInline = canExpandInline && shouldExpandInline;
9960 JITDUMP("\nExpanding %s as call because %s\n", isCastClass ? "castclass" : "isinst",
9961 canExpandInline ? "want smaller code or faster jitting" : "inline expansion not legal");
9963 // If we CSE this class handle we prevent assertionProp from making SubType assertions
9964 // so instead we force the CSE logic to not consider CSE-ing this class handle.
9966 op2->gtFlags |= GTF_DONT_CSE;
9968 return gtNewHelperCallNode(helper, TYP_REF, gtNewArgList(op2, op1));
9971 JITDUMP("\nExpanding %s inline\n", isCastClass ? "castclass" : "isinst");
9973 impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("bubbling QMark2"));
9978 // expand the methodtable match:
9982 // GT_IND op2 (typically CNS_INT)
9987 // This can replace op1 with a GT_COMMA that evaluates op1 into a local
9989 op1 = impCloneExpr(op1, &temp, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL, nullptr DEBUGARG("CASTCLASS eval op1"));
9991 // op1 is now known to be a non-complex tree
9992 // thus we can use gtClone(op1) from now on
9995 GenTreePtr op2Var = op2;
9998 op2Var = fgInsertCommaFormTemp(&op2);
9999 lvaTable[op2Var->AsLclVarCommon()->GetLclNum()].lvIsCSE = true;
10001 temp = gtNewOperNode(GT_IND, TYP_I_IMPL, temp);
10002 temp->gtFlags |= GTF_EXCEPT;
10003 condMT = gtNewOperNode(GT_NE, TYP_INT, temp, op2);
10005 GenTreePtr condNull;
10007 // expand the null check:
10009 // condNull ==> GT_EQ
10014 condNull = gtNewOperNode(GT_EQ, TYP_INT, gtClone(op1), gtNewIconNode(0, TYP_REF));
10017 // expand the true and false trees for the condMT
10019 GenTreePtr condFalse = gtClone(op1);
10020 GenTreePtr condTrue;
10024 // use the special helper that skips the cases checked by our inlined cast
10026 const CorInfoHelpFunc specialHelper = CORINFO_HELP_CHKCASTCLASS_SPECIAL;
10028 condTrue = gtNewHelperCallNode(specialHelper, TYP_REF, gtNewArgList(op2Var, gtClone(op1)));
10032 condTrue = gtNewIconNode(0, TYP_REF);
10035 #define USE_QMARK_TREES
10037 #ifdef USE_QMARK_TREES
10038 GenTreePtr qmarkMT;
10040 // Generate first QMARK - COLON tree
10042 // qmarkMT ==> GT_QMARK
10046 // condFalse condTrue
10048 temp = new (this, GT_COLON) GenTreeColon(TYP_REF, condTrue, condFalse);
10049 qmarkMT = gtNewQmarkNode(TYP_REF, condMT, temp);
10050 condMT->gtFlags |= GTF_RELOP_QMARK;
10052 GenTreePtr qmarkNull;
10054 // Generate second QMARK - COLON tree
10056 // qmarkNull ==> GT_QMARK
10058 // condNull GT_COLON
10062 temp = new (this, GT_COLON) GenTreeColon(TYP_REF, gtClone(op1), qmarkMT);
10063 qmarkNull = gtNewQmarkNode(TYP_REF, condNull, temp);
10064 qmarkNull->gtFlags |= GTF_QMARK_CAST_INSTOF;
10065 condNull->gtFlags |= GTF_RELOP_QMARK;
10067 // Make QMark node a top level node by spilling it.
10068 unsigned tmp = lvaGrabTemp(true DEBUGARG("spilling QMark2"));
10069 impAssignTempGen(tmp, qmarkNull, (unsigned)CHECK_SPILL_NONE);
10071 // TODO: Is it possible op1 has a better type?
10072 lvaSetClass(tmp, pResolvedToken->hClass);
10073 return gtNewLclvNode(tmp, TYP_REF);
10078 #define assertImp(cond) ((void)0)
10080 #define assertImp(cond) \
10085 const int cchAssertImpBuf = 600; \
10086 char* assertImpBuf = (char*)alloca(cchAssertImpBuf); \
10087 _snprintf_s(assertImpBuf, cchAssertImpBuf, cchAssertImpBuf - 1, \
10088 "%s : Possibly bad IL with CEE_%s at offset %04Xh (op1=%s op2=%s stkDepth=%d)", #cond, \
10089 impCurOpcName, impCurOpcOffs, op1 ? varTypeName(op1->TypeGet()) : "NULL", \
10090 op2 ? varTypeName(op2->TypeGet()) : "NULL", verCurrentState.esStackDepth); \
10091 assertAbort(assertImpBuf, __FILE__, __LINE__); \
10097 #pragma warning(push)
10098 #pragma warning(disable : 21000) // Suppress PREFast warning about overly large function
10100 /*****************************************************************************
10101 * Import the instr for the given basic block
10103 void Compiler::impImportBlockCode(BasicBlock* block)
10105 #define _impResolveToken(kind) impResolveToken(codeAddr, &resolvedToken, kind)
10111 printf("\nImporting BB%02u (PC=%03u) of '%s'", block->bbNum, block->bbCodeOffs, info.compFullName);
10115 unsigned nxtStmtIndex = impInitBlockLineInfo();
10116 IL_OFFSET nxtStmtOffs;
10118 GenTreePtr arrayNodeFrom, arrayNodeTo, arrayNodeToIndex;
10119 CorInfoHelpFunc helper;
10120 CorInfoIsAccessAllowedResult accessAllowedResult;
10121 CORINFO_HELPER_DESC calloutHelper;
10122 const BYTE* lastLoadToken = nullptr;
10124 // reject cyclic constraints
10125 if (tiVerificationNeeded)
10127 Verify(!info.hasCircularClassConstraints, "Method parent has circular class type parameter constraints.");
10128 Verify(!info.hasCircularMethodConstraints, "Method has circular method type parameter constraints.");
10131 /* Get the tree list started */
10133 impBeginTreeList();
10135 /* Walk the opcodes that comprise the basic block */
10137 const BYTE* codeAddr = info.compCode + block->bbCodeOffs;
10138 const BYTE* codeEndp = info.compCode + block->bbCodeOffsEnd;
10140 IL_OFFSET opcodeOffs = block->bbCodeOffs;
10141 IL_OFFSET lastSpillOffs = opcodeOffs;
10145 /* remember the start of the delegate creation sequence (used for verification) */
10146 const BYTE* delegateCreateStart = nullptr;
10148 int prefixFlags = 0;
10149 bool explicitTailCall, constraintCall, readonlyCall;
10153 unsigned numArgs = info.compArgsCount;
10155 /* Now process all the opcodes in the block */
10157 var_types callTyp = TYP_COUNT;
10158 OPCODE prevOpcode = CEE_ILLEGAL;
10160 if (block->bbCatchTyp)
10162 if (info.compStmtOffsetsImplicit & ICorDebugInfo::CALL_SITE_BOUNDARIES)
10164 impCurStmtOffsSet(block->bbCodeOffs);
10167 // We will spill the GT_CATCH_ARG and the input of the BB_QMARK block
10168 // to a temp. This is a trade off for code simplicity
10169 impSpillSpecialSideEff();
10172 while (codeAddr < codeEndp)
10174 bool usingReadyToRunHelper = false;
10175 CORINFO_RESOLVED_TOKEN resolvedToken;
10176 CORINFO_RESOLVED_TOKEN constrainedResolvedToken;
10177 CORINFO_CALL_INFO callInfo;
10178 CORINFO_FIELD_INFO fieldInfo;
10180 tiRetVal = typeInfo(); // Default type info
10182 //---------------------------------------------------------------------
10184 /* We need to restrict the max tree depth as many of the Compiler
10185 functions are recursive. We do this by spilling the stack */
10187 if (verCurrentState.esStackDepth)
10189 /* Has it been a while since we last saw a non-empty stack (which
10190 guarantees that the tree depth isnt accumulating. */
10192 if ((opcodeOffs - lastSpillOffs) > MAX_TREE_SIZE && impCanSpillNow(prevOpcode))
10194 impSpillStackEnsure();
10195 lastSpillOffs = opcodeOffs;
10200 lastSpillOffs = opcodeOffs;
10201 impBoxTempInUse = false; // nothing on the stack, box temp OK to use again
10204 /* Compute the current instr offset */
10206 opcodeOffs = (IL_OFFSET)(codeAddr - info.compCode);
10209 if (opts.compDbgInfo)
10212 if (!compIsForInlining())
10215 (nxtStmtIndex < info.compStmtOffsetsCount) ? info.compStmtOffsets[nxtStmtIndex] : BAD_IL_OFFSET;
10217 /* Have we reached the next stmt boundary ? */
10219 if (nxtStmtOffs != BAD_IL_OFFSET && opcodeOffs >= nxtStmtOffs)
10221 assert(nxtStmtOffs == info.compStmtOffsets[nxtStmtIndex]);
10223 if (verCurrentState.esStackDepth != 0 && opts.compDbgCode)
10225 /* We need to provide accurate IP-mapping at this point.
10226 So spill anything on the stack so that it will form
10227 gtStmts with the correct stmt offset noted */
10229 impSpillStackEnsure(true);
10232 // Has impCurStmtOffs been reported in any tree?
10234 if (impCurStmtOffs != BAD_IL_OFFSET && opts.compDbgCode)
10236 GenTreePtr placeHolder = new (this, GT_NO_OP) GenTree(GT_NO_OP, TYP_VOID);
10237 impAppendTree(placeHolder, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
10239 assert(impCurStmtOffs == BAD_IL_OFFSET);
10242 if (impCurStmtOffs == BAD_IL_OFFSET)
10244 /* Make sure that nxtStmtIndex is in sync with opcodeOffs.
10245 If opcodeOffs has gone past nxtStmtIndex, catch up */
10247 while ((nxtStmtIndex + 1) < info.compStmtOffsetsCount &&
10248 info.compStmtOffsets[nxtStmtIndex + 1] <= opcodeOffs)
10253 /* Go to the new stmt */
10255 impCurStmtOffsSet(info.compStmtOffsets[nxtStmtIndex]);
10257 /* Update the stmt boundary index */
10260 assert(nxtStmtIndex <= info.compStmtOffsetsCount);
10262 /* Are there any more line# entries after this one? */
10264 if (nxtStmtIndex < info.compStmtOffsetsCount)
10266 /* Remember where the next line# starts */
10268 nxtStmtOffs = info.compStmtOffsets[nxtStmtIndex];
10272 /* No more line# entries */
10274 nxtStmtOffs = BAD_IL_OFFSET;
10278 else if ((info.compStmtOffsetsImplicit & ICorDebugInfo::STACK_EMPTY_BOUNDARIES) &&
10279 (verCurrentState.esStackDepth == 0))
10281 /* At stack-empty locations, we have already added the tree to
10282 the stmt list with the last offset. We just need to update
10286 impCurStmtOffsSet(opcodeOffs);
10288 else if ((info.compStmtOffsetsImplicit & ICorDebugInfo::CALL_SITE_BOUNDARIES) &&
10289 impOpcodeIsCallSiteBoundary(prevOpcode))
10291 /* Make sure we have a type cached */
10292 assert(callTyp != TYP_COUNT);
10294 if (callTyp == TYP_VOID)
10296 impCurStmtOffsSet(opcodeOffs);
10298 else if (opts.compDbgCode)
10300 impSpillStackEnsure(true);
10301 impCurStmtOffsSet(opcodeOffs);
10304 else if ((info.compStmtOffsetsImplicit & ICorDebugInfo::NOP_BOUNDARIES) && (prevOpcode == CEE_NOP))
10306 if (opts.compDbgCode)
10308 impSpillStackEnsure(true);
10311 impCurStmtOffsSet(opcodeOffs);
10314 assert(impCurStmtOffs == BAD_IL_OFFSET || nxtStmtOffs == BAD_IL_OFFSET ||
10315 jitGetILoffs(impCurStmtOffs) <= nxtStmtOffs);
10319 CORINFO_CLASS_HANDLE clsHnd = DUMMY_INIT(NULL);
10320 CORINFO_CLASS_HANDLE ldelemClsHnd = DUMMY_INIT(NULL);
10321 CORINFO_CLASS_HANDLE stelemClsHnd = DUMMY_INIT(NULL);
10323 var_types lclTyp, ovflType = TYP_UNKNOWN;
10324 GenTreePtr op1 = DUMMY_INIT(NULL);
10325 GenTreePtr op2 = DUMMY_INIT(NULL);
10326 GenTreeArgList* args = nullptr; // What good do these "DUMMY_INIT"s do?
10327 GenTreePtr newObjThisPtr = DUMMY_INIT(NULL);
10328 bool uns = DUMMY_INIT(false);
10329 bool isLocal = false;
10331 /* Get the next opcode and the size of its parameters */
10333 OPCODE opcode = (OPCODE)getU1LittleEndian(codeAddr);
10334 codeAddr += sizeof(__int8);
10337 impCurOpcOffs = (IL_OFFSET)(codeAddr - info.compCode - 1);
10338 JITDUMP("\n [%2u] %3u (0x%03x) ", verCurrentState.esStackDepth, impCurOpcOffs, impCurOpcOffs);
10343 // Return if any previous code has caused inline to fail.
10344 if (compDonotInline())
10349 /* Get the size of additional parameters */
10351 signed int sz = opcodeSizes[opcode];
10354 clsHnd = NO_CLASS_HANDLE;
10355 lclTyp = TYP_COUNT;
10356 callTyp = TYP_COUNT;
10358 impCurOpcOffs = (IL_OFFSET)(codeAddr - info.compCode - 1);
10359 impCurOpcName = opcodeNames[opcode];
10361 if (verbose && (opcode != CEE_PREFIX1))
10363 printf("%s", impCurOpcName);
10366 /* Use assertImp() to display the opcode */
10368 op1 = op2 = nullptr;
10371 /* See what kind of an opcode we have, then */
10373 unsigned mflags = 0;
10374 unsigned clsFlags = 0;
10387 CORINFO_SIG_INFO sig;
10389 bool ovfl, unordered, callNode;
10391 CORINFO_CLASS_HANDLE tokenType;
10401 opcode = (OPCODE)(getU1LittleEndian(codeAddr) + 256);
10402 opcodeOffs = (IL_OFFSET)(codeAddr - info.compCode);
10403 codeAddr += sizeof(__int8);
10404 goto DECODE_OPCODE;
10408 // We need to call impSpillLclRefs() for a struct type lclVar.
10409 // This is done for non-block assignments in the handling of stloc.
10410 if ((op1->OperGet() == GT_ASG) && varTypeIsStruct(op1->gtOp.gtOp1) &&
10411 (op1->gtOp.gtOp1->gtOper == GT_LCL_VAR))
10413 impSpillLclRefs(op1->gtOp.gtOp1->AsLclVarCommon()->gtLclNum);
10416 /* Append 'op1' to the list of statements */
10417 impAppendTree(op1, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
10422 /* Append 'op1' to the list of statements */
10424 impAppendTree(op1, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
10430 // Remember at which BC offset the tree was finished
10431 impNoteLastILoffs();
10436 impPushNullObjRefOnStack();
10439 case CEE_LDC_I4_M1:
10449 cval.intVal = (opcode - CEE_LDC_I4_0);
10450 assert(-1 <= cval.intVal && cval.intVal <= 8);
10454 cval.intVal = getI1LittleEndian(codeAddr);
10457 cval.intVal = getI4LittleEndian(codeAddr);
10460 JITDUMP(" %d", cval.intVal);
10461 impPushOnStack(gtNewIconNode(cval.intVal), typeInfo(TI_INT));
10465 cval.lngVal = getI8LittleEndian(codeAddr);
10466 JITDUMP(" 0x%016llx", cval.lngVal);
10467 impPushOnStack(gtNewLconNode(cval.lngVal), typeInfo(TI_LONG));
10471 cval.dblVal = getR8LittleEndian(codeAddr);
10472 JITDUMP(" %#.17g", cval.dblVal);
10473 impPushOnStack(gtNewDconNode(cval.dblVal), typeInfo(TI_DOUBLE));
10477 cval.dblVal = getR4LittleEndian(codeAddr);
10478 JITDUMP(" %#.17g", cval.dblVal);
10480 GenTreePtr cnsOp = gtNewDconNode(cval.dblVal);
10481 #if !FEATURE_X87_DOUBLES
10482 // X87 stack doesn't differentiate between float/double
10483 // so R4 is treated as R8, but everybody else does
10484 cnsOp->gtType = TYP_FLOAT;
10485 #endif // FEATURE_X87_DOUBLES
10486 impPushOnStack(cnsOp, typeInfo(TI_DOUBLE));
10492 if (compIsForInlining())
10494 if (impInlineInfo->inlineCandidateInfo->dwRestrictions & INLINE_NO_CALLEE_LDSTR)
10496 compInlineResult->NoteFatal(InlineObservation::CALLSITE_HAS_LDSTR_RESTRICTION);
10501 val = getU4LittleEndian(codeAddr);
10502 JITDUMP(" %08X", val);
10503 if (tiVerificationNeeded)
10505 Verify(info.compCompHnd->isValidStringRef(info.compScopeHnd, val), "bad string");
10506 tiRetVal = typeInfo(TI_REF, impGetStringClass());
10508 impPushOnStack(gtNewSconNode(val, info.compScopeHnd), tiRetVal);
10513 lclNum = getU2LittleEndian(codeAddr);
10514 JITDUMP(" %u", lclNum);
10515 impLoadArg(lclNum, opcodeOffs + sz + 1);
10519 lclNum = getU1LittleEndian(codeAddr);
10520 JITDUMP(" %u", lclNum);
10521 impLoadArg(lclNum, opcodeOffs + sz + 1);
10528 lclNum = (opcode - CEE_LDARG_0);
10529 assert(lclNum >= 0 && lclNum < 4);
10530 impLoadArg(lclNum, opcodeOffs + sz + 1);
10534 lclNum = getU2LittleEndian(codeAddr);
10535 JITDUMP(" %u", lclNum);
10536 impLoadLoc(lclNum, opcodeOffs + sz + 1);
10540 lclNum = getU1LittleEndian(codeAddr);
10541 JITDUMP(" %u", lclNum);
10542 impLoadLoc(lclNum, opcodeOffs + sz + 1);
10549 lclNum = (opcode - CEE_LDLOC_0);
10550 assert(lclNum >= 0 && lclNum < 4);
10551 impLoadLoc(lclNum, opcodeOffs + sz + 1);
10555 lclNum = getU2LittleEndian(codeAddr);
10559 lclNum = getU1LittleEndian(codeAddr);
10561 JITDUMP(" %u", lclNum);
10563 if (tiVerificationNeeded)
10565 Verify(lclNum < info.compILargsCount, "bad arg num");
10568 if (compIsForInlining())
10570 op1 = impInlineFetchArg(lclNum, impInlineInfo->inlArgInfo, impInlineInfo->lclVarInfo);
10571 noway_assert(op1->gtOper == GT_LCL_VAR);
10572 lclNum = op1->AsLclVar()->gtLclNum;
10577 lclNum = compMapILargNum(lclNum); // account for possible hidden param
10578 assertImp(lclNum < numArgs);
10580 if (lclNum == info.compThisArg)
10582 lclNum = lvaArg0Var;
10585 // We should have seen this arg write in the prescan
10586 assert(lvaTable[lclNum].lvHasILStoreOp);
10588 if (tiVerificationNeeded)
10590 typeInfo& tiLclVar = lvaTable[lclNum].lvVerTypeInfo;
10591 Verify(tiCompatibleWith(impStackTop().seTypeInfo, NormaliseForStack(tiLclVar), true),
10594 if (verTrackObjCtorInitState && (verCurrentState.thisInitialized != TIS_Init))
10596 Verify(!tiLclVar.IsThisPtr(), "storing to uninit this ptr");
10603 lclNum = getU2LittleEndian(codeAddr);
10605 JITDUMP(" %u", lclNum);
10609 lclNum = getU1LittleEndian(codeAddr);
10611 JITDUMP(" %u", lclNum);
10619 lclNum = (opcode - CEE_STLOC_0);
10620 assert(lclNum >= 0 && lclNum < 4);
10623 if (tiVerificationNeeded)
10625 Verify(lclNum < info.compMethodInfo->locals.numArgs, "bad local num");
10626 Verify(tiCompatibleWith(impStackTop().seTypeInfo,
10627 NormaliseForStack(lvaTable[lclNum + numArgs].lvVerTypeInfo), true),
10631 if (compIsForInlining())
10633 lclTyp = impInlineInfo->lclVarInfo[lclNum + impInlineInfo->argCnt].lclTypeInfo;
10635 /* Have we allocated a temp for this local? */
10637 lclNum = impInlineFetchLocal(lclNum DEBUGARG("Inline stloc first use temp"));
10646 if (lclNum >= info.compLocalsCount && lclNum != lvaArg0Var)
10648 assert(!tiVerificationNeeded); // We should have thrown the VerificationException before.
10654 /* if it is a struct assignment, make certain we don't overflow the buffer */
10655 assert(lclTyp != TYP_STRUCT || lvaLclSize(lclNum) >= info.compCompHnd->getClassSize(clsHnd));
10657 if (lvaTable[lclNum].lvNormalizeOnLoad())
10659 lclTyp = lvaGetRealType(lclNum);
10663 lclTyp = lvaGetActualType(lclNum);
10667 /* Pop the value being assigned */
10670 StackEntry se = impPopStack();
10671 clsHnd = se.seTypeInfo.GetClassHandle();
10673 tiRetVal = se.seTypeInfo;
10676 #ifdef FEATURE_SIMD
10677 if (varTypeIsSIMD(lclTyp) && (lclTyp != op1->TypeGet()))
10679 assert(op1->TypeGet() == TYP_STRUCT);
10680 op1->gtType = lclTyp;
10682 #endif // FEATURE_SIMD
10684 op1 = impImplicitIorI4Cast(op1, lclTyp);
10686 #ifdef _TARGET_64BIT_
10687 // Downcast the TYP_I_IMPL into a 32-bit Int for x86 JIT compatiblity
10688 if (varTypeIsI(op1->TypeGet()) && (genActualType(lclTyp) == TYP_INT))
10690 assert(!tiVerificationNeeded); // We should have thrown the VerificationException before.
10691 op1 = gtNewCastNode(TYP_INT, op1, TYP_INT);
10693 #endif // _TARGET_64BIT_
10695 // We had better assign it a value of the correct type
10697 genActualType(lclTyp) == genActualType(op1->gtType) ||
10698 genActualType(lclTyp) == TYP_I_IMPL && op1->IsVarAddr() ||
10699 (genActualType(lclTyp) == TYP_I_IMPL && (op1->gtType == TYP_BYREF || op1->gtType == TYP_REF)) ||
10700 (genActualType(op1->gtType) == TYP_I_IMPL && lclTyp == TYP_BYREF) ||
10701 (varTypeIsFloating(lclTyp) && varTypeIsFloating(op1->TypeGet())) ||
10702 ((genActualType(lclTyp) == TYP_BYREF) && genActualType(op1->TypeGet()) == TYP_REF));
10704 /* If op1 is "&var" then its type is the transient "*" and it can
10705 be used either as TYP_BYREF or TYP_I_IMPL */
10707 if (op1->IsVarAddr())
10709 assertImp(genActualType(lclTyp) == TYP_I_IMPL || lclTyp == TYP_BYREF);
10711 /* When "&var" is created, we assume it is a byref. If it is
10712 being assigned to a TYP_I_IMPL var, change the type to
10713 prevent unnecessary GC info */
10715 if (genActualType(lclTyp) == TYP_I_IMPL)
10717 op1->gtType = TYP_I_IMPL;
10721 // If this is a local and the local is a ref type, see
10722 // if we can improve type information based on the
10723 // value being assigned.
10724 if (isLocal && (lclTyp == TYP_REF))
10726 // We should have seen a stloc in our IL prescan.
10727 assert(lvaTable[lclNum].lvHasILStoreOp);
10729 const bool isSingleILStoreLocal =
10730 !lvaTable[lclNum].lvHasMultipleILStoreOp && !lvaTable[lclNum].lvHasLdAddrOp;
10732 // Conservative check that there is just one
10733 // definition that reaches this store.
10734 const bool hasSingleReachingDef = (block->bbStackDepthOnEntry() == 0);
10736 if (isSingleILStoreLocal && hasSingleReachingDef)
10738 lvaUpdateClass(lclNum, op1, clsHnd);
10742 /* Filter out simple assignments to itself */
10744 if (op1->gtOper == GT_LCL_VAR && lclNum == op1->gtLclVarCommon.gtLclNum)
10746 if (opts.compDbgCode)
10748 op1 = gtNewNothingNode();
10757 /* Create the assignment node */
10759 op2 = gtNewLclvNode(lclNum, lclTyp, opcodeOffs + sz + 1);
10761 /* If the local is aliased or pinned, we need to spill calls and
10762 indirections from the stack. */
10764 if ((lvaTable[lclNum].lvAddrExposed || lvaTable[lclNum].lvHasLdAddrOp || lvaTable[lclNum].lvPinned) &&
10765 (verCurrentState.esStackDepth > 0))
10767 impSpillSideEffects(false,
10768 (unsigned)CHECK_SPILL_ALL DEBUGARG("Local could be aliased or is pinned"));
10771 /* Spill any refs to the local from the stack */
10773 impSpillLclRefs(lclNum);
10775 #if !FEATURE_X87_DOUBLES
10776 // We can generate an assignment to a TYP_FLOAT from a TYP_DOUBLE
10777 // We insert a cast to the dest 'op2' type
10779 if ((op1->TypeGet() != op2->TypeGet()) && varTypeIsFloating(op1->gtType) &&
10780 varTypeIsFloating(op2->gtType))
10782 op1 = gtNewCastNode(op2->TypeGet(), op1, op2->TypeGet());
10784 #endif // !FEATURE_X87_DOUBLES
10786 if (varTypeIsStruct(lclTyp))
10788 op1 = impAssignStruct(op2, op1, clsHnd, (unsigned)CHECK_SPILL_ALL);
10792 // The code generator generates GC tracking information
10793 // based on the RHS of the assignment. Later the LHS (which is
10794 // is a BYREF) gets used and the emitter checks that that variable
10795 // is being tracked. It is not (since the RHS was an int and did
10796 // not need tracking). To keep this assert happy, we change the RHS
10797 if (lclTyp == TYP_BYREF && !varTypeIsGC(op1->gtType))
10799 op1->gtType = TYP_BYREF;
10801 op1 = gtNewAssignNode(op2, op1);
10807 lclNum = getU2LittleEndian(codeAddr);
10811 lclNum = getU1LittleEndian(codeAddr);
10813 JITDUMP(" %u", lclNum);
10814 if (tiVerificationNeeded)
10816 Verify(lclNum < info.compMethodInfo->locals.numArgs, "bad local num");
10817 Verify(info.compInitMem, "initLocals not set");
10820 if (compIsForInlining())
10822 // Get the local type
10823 lclTyp = impInlineInfo->lclVarInfo[lclNum + impInlineInfo->argCnt].lclTypeInfo;
10825 /* Have we allocated a temp for this local? */
10827 lclNum = impInlineFetchLocal(lclNum DEBUGARG("Inline ldloca(s) first use temp"));
10829 op1 = gtNewLclvNode(lclNum, lvaGetActualType(lclNum));
10835 assertImp(lclNum < info.compLocalsCount);
10839 lclNum = getU2LittleEndian(codeAddr);
10843 lclNum = getU1LittleEndian(codeAddr);
10845 JITDUMP(" %u", lclNum);
10846 Verify(lclNum < info.compILargsCount, "bad arg num");
10848 if (compIsForInlining())
10850 // In IL, LDARGA(_S) is used to load the byref managed pointer of struct argument,
10851 // followed by a ldfld to load the field.
10853 op1 = impInlineFetchArg(lclNum, impInlineInfo->inlArgInfo, impInlineInfo->lclVarInfo);
10854 if (op1->gtOper != GT_LCL_VAR)
10856 compInlineResult->NoteFatal(InlineObservation::CALLSITE_LDARGA_NOT_LOCAL_VAR);
10860 assert(op1->gtOper == GT_LCL_VAR);
10865 lclNum = compMapILargNum(lclNum); // account for possible hidden param
10866 assertImp(lclNum < numArgs);
10868 if (lclNum == info.compThisArg)
10870 lclNum = lvaArg0Var;
10877 op1 = gtNewLclvNode(lclNum, lvaGetActualType(lclNum), opcodeOffs + sz + 1);
10880 assert(op1->gtOper == GT_LCL_VAR);
10882 /* Note that this is supposed to create the transient type "*"
10883 which may be used as a TYP_I_IMPL. However we catch places
10884 where it is used as a TYP_I_IMPL and change the node if needed.
10885 Thus we are pessimistic and may report byrefs in the GC info
10886 where it was not absolutely needed, but it is safer this way.
10888 op1 = gtNewOperNode(GT_ADDR, TYP_BYREF, op1);
10890 // &aliasedVar doesnt need GTF_GLOB_REF, though alisasedVar does
10891 assert((op1->gtFlags & GTF_GLOB_REF) == 0);
10893 tiRetVal = lvaTable[lclNum].lvVerTypeInfo;
10894 if (tiVerificationNeeded)
10896 // Don't allow taking address of uninit this ptr.
10897 if (verTrackObjCtorInitState && (verCurrentState.thisInitialized != TIS_Init))
10899 Verify(!tiRetVal.IsThisPtr(), "address of uninit this ptr");
10902 if (!tiRetVal.IsByRef())
10904 tiRetVal.MakeByRef();
10908 Verify(false, "byref to byref");
10912 impPushOnStack(op1, tiRetVal);
10917 if (!info.compIsVarArgs)
10919 BADCODE("arglist in non-vararg method");
10922 if (tiVerificationNeeded)
10924 tiRetVal = typeInfo(TI_STRUCT, impGetRuntimeArgumentHandle());
10926 assertImp((info.compMethodInfo->args.callConv & CORINFO_CALLCONV_MASK) == CORINFO_CALLCONV_VARARG);
10928 /* The ARGLIST cookie is a hidden 'last' parameter, we have already
10929 adjusted the arg count cos this is like fetching the last param */
10930 assertImp(0 < numArgs);
10931 assert(lvaTable[lvaVarargsHandleArg].lvAddrExposed);
10932 lclNum = lvaVarargsHandleArg;
10933 op1 = gtNewLclvNode(lclNum, TYP_I_IMPL, opcodeOffs + sz + 1);
10934 op1 = gtNewOperNode(GT_ADDR, TYP_BYREF, op1);
10935 impPushOnStack(op1, tiRetVal);
10938 case CEE_ENDFINALLY:
10940 if (compIsForInlining())
10942 assert(!"Shouldn't have exception handlers in the inliner!");
10943 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_ENDFINALLY);
10947 if (verCurrentState.esStackDepth > 0)
10949 impEvalSideEffects();
10952 if (info.compXcptnsCount == 0)
10954 BADCODE("endfinally outside finally");
10957 assert(verCurrentState.esStackDepth == 0);
10959 op1 = gtNewOperNode(GT_RETFILT, TYP_VOID, nullptr);
10962 case CEE_ENDFILTER:
10964 if (compIsForInlining())
10966 assert(!"Shouldn't have exception handlers in the inliner!");
10967 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_ENDFILTER);
10971 block->bbSetRunRarely(); // filters are rare
10973 if (info.compXcptnsCount == 0)
10975 BADCODE("endfilter outside filter");
10978 if (tiVerificationNeeded)
10980 Verify(impStackTop().seTypeInfo.IsType(TI_INT), "bad endfilt arg");
10983 op1 = impPopStack().val;
10984 assertImp(op1->gtType == TYP_INT);
10985 if (!bbInFilterILRange(block))
10987 BADCODE("EndFilter outside a filter handler");
10990 /* Mark current bb as end of filter */
10992 assert(compCurBB->bbFlags & BBF_DONT_REMOVE);
10993 assert(compCurBB->bbJumpKind == BBJ_EHFILTERRET);
10995 /* Mark catch handler as successor */
10997 op1 = gtNewOperNode(GT_RETFILT, op1->TypeGet(), op1);
10998 if (verCurrentState.esStackDepth != 0)
11000 verRaiseVerifyException(INDEBUG("stack must be 1 on end of filter") DEBUGARG(__FILE__)
11001 DEBUGARG(__LINE__));
11006 prefixFlags &= ~PREFIX_TAILCALL; // ret without call before it
11008 if (!impReturnInstruction(block, prefixFlags, opcode))
11019 assert(!compIsForInlining());
11021 if (tiVerificationNeeded)
11023 Verify(false, "Invalid opcode: CEE_JMP");
11026 if ((info.compFlags & CORINFO_FLG_SYNCH) || block->hasTryIndex() || block->hasHndIndex())
11028 /* CEE_JMP does not make sense in some "protected" regions. */
11030 BADCODE("Jmp not allowed in protected region");
11033 if (verCurrentState.esStackDepth != 0)
11035 BADCODE("Stack must be empty after CEE_JMPs");
11038 _impResolveToken(CORINFO_TOKENKIND_Method);
11040 JITDUMP(" %08X", resolvedToken.token);
11042 /* The signature of the target has to be identical to ours.
11043 At least check that argCnt and returnType match */
11045 eeGetMethodSig(resolvedToken.hMethod, &sig);
11046 if (sig.numArgs != info.compMethodInfo->args.numArgs ||
11047 sig.retType != info.compMethodInfo->args.retType ||
11048 sig.callConv != info.compMethodInfo->args.callConv)
11050 BADCODE("Incompatible target for CEE_JMPs");
11053 op1 = new (this, GT_JMP) GenTreeVal(GT_JMP, TYP_VOID, (size_t)resolvedToken.hMethod);
11055 /* Mark the basic block as being a JUMP instead of RETURN */
11057 block->bbFlags |= BBF_HAS_JMP;
11059 /* Set this flag to make sure register arguments have a location assigned
11060 * even if we don't use them inside the method */
11062 compJmpOpUsed = true;
11064 fgNoStructPromotion = true;
11069 assertImp(sz == sizeof(unsigned));
11071 _impResolveToken(CORINFO_TOKENKIND_Class);
11073 JITDUMP(" %08X", resolvedToken.token);
11075 ldelemClsHnd = resolvedToken.hClass;
11077 if (tiVerificationNeeded)
11079 typeInfo tiArray = impStackTop(1).seTypeInfo;
11080 typeInfo tiIndex = impStackTop().seTypeInfo;
11082 // As per ECMA 'index' specified can be either int32 or native int.
11083 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11085 typeInfo arrayElemType = verMakeTypeInfo(ldelemClsHnd);
11086 Verify(tiArray.IsNullObjRef() ||
11087 typeInfo::AreEquivalent(verGetArrayElemType(tiArray), arrayElemType),
11090 tiRetVal = arrayElemType;
11091 tiRetVal.MakeByRef();
11092 if (prefixFlags & PREFIX_READONLY)
11094 tiRetVal.SetIsReadonlyByRef();
11097 // an array interior pointer is always in the heap
11098 tiRetVal.SetIsPermanentHomeByRef();
11101 // If it's a value class array we just do a simple address-of
11102 if (eeIsValueClass(ldelemClsHnd))
11104 CorInfoType cit = info.compCompHnd->getTypeForPrimitiveValueClass(ldelemClsHnd);
11105 if (cit == CORINFO_TYPE_UNDEF)
11107 lclTyp = TYP_STRUCT;
11111 lclTyp = JITtype2varType(cit);
11113 goto ARR_LD_POST_VERIFY;
11116 // Similarly, if its a readonly access, we can do a simple address-of
11117 // without doing a runtime type-check
11118 if (prefixFlags & PREFIX_READONLY)
11121 goto ARR_LD_POST_VERIFY;
11124 // Otherwise we need the full helper function with run-time type check
11125 op1 = impTokenToHandle(&resolvedToken);
11126 if (op1 == nullptr)
11127 { // compDonotInline()
11131 args = gtNewArgList(op1); // Type
11132 args = gtNewListNode(impPopStack().val, args); // index
11133 args = gtNewListNode(impPopStack().val, args); // array
11134 op1 = gtNewHelperCallNode(CORINFO_HELP_LDELEMA_REF, TYP_BYREF, args);
11136 impPushOnStack(op1, tiRetVal);
11139 // ldelem for reference and value types
11141 assertImp(sz == sizeof(unsigned));
11143 _impResolveToken(CORINFO_TOKENKIND_Class);
11145 JITDUMP(" %08X", resolvedToken.token);
11147 ldelemClsHnd = resolvedToken.hClass;
11149 if (tiVerificationNeeded)
11151 typeInfo tiArray = impStackTop(1).seTypeInfo;
11152 typeInfo tiIndex = impStackTop().seTypeInfo;
11154 // As per ECMA 'index' specified can be either int32 or native int.
11155 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11156 tiRetVal = verMakeTypeInfo(ldelemClsHnd);
11158 Verify(tiArray.IsNullObjRef() || tiCompatibleWith(verGetArrayElemType(tiArray), tiRetVal, false),
11159 "type of array incompatible with type operand");
11160 tiRetVal.NormaliseForStack();
11163 // If it's a reference type or generic variable type
11164 // then just generate code as though it's a ldelem.ref instruction
11165 if (!eeIsValueClass(ldelemClsHnd))
11168 opcode = CEE_LDELEM_REF;
11172 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(ldelemClsHnd);
11173 lclTyp = JITtype2varType(jitTyp);
11174 tiRetVal = verMakeTypeInfo(ldelemClsHnd); // precise type always needed for struct
11175 tiRetVal.NormaliseForStack();
11177 goto ARR_LD_POST_VERIFY;
11179 case CEE_LDELEM_I1:
11182 case CEE_LDELEM_I2:
11183 lclTyp = TYP_SHORT;
11186 lclTyp = TYP_I_IMPL;
11189 // Should be UINT, but since no platform widens 4->8 bytes it doesn't matter
11190 // and treating it as TYP_INT avoids other asserts.
11191 case CEE_LDELEM_U4:
11195 case CEE_LDELEM_I4:
11198 case CEE_LDELEM_I8:
11201 case CEE_LDELEM_REF:
11204 case CEE_LDELEM_R4:
11205 lclTyp = TYP_FLOAT;
11207 case CEE_LDELEM_R8:
11208 lclTyp = TYP_DOUBLE;
11210 case CEE_LDELEM_U1:
11211 lclTyp = TYP_UBYTE;
11213 case CEE_LDELEM_U2:
11214 lclTyp = TYP_USHORT;
11219 if (tiVerificationNeeded)
11221 typeInfo tiArray = impStackTop(1).seTypeInfo;
11222 typeInfo tiIndex = impStackTop().seTypeInfo;
11224 // As per ECMA 'index' specified can be either int32 or native int.
11225 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11226 if (tiArray.IsNullObjRef())
11228 if (lclTyp == TYP_REF)
11229 { // we will say a deref of a null array yields a null ref
11230 tiRetVal = typeInfo(TI_NULL);
11234 tiRetVal = typeInfo(lclTyp);
11239 tiRetVal = verGetArrayElemType(tiArray);
11240 typeInfo arrayElemTi = typeInfo(lclTyp);
11241 #ifdef _TARGET_64BIT_
11242 if (opcode == CEE_LDELEM_I)
11244 arrayElemTi = typeInfo::nativeInt();
11247 if (lclTyp != TYP_REF && lclTyp != TYP_STRUCT)
11249 Verify(typeInfo::AreEquivalent(tiRetVal, arrayElemTi), "bad array");
11252 #endif // _TARGET_64BIT_
11254 Verify(tiRetVal.IsType(arrayElemTi.GetType()), "bad array");
11257 tiRetVal.NormaliseForStack();
11259 ARR_LD_POST_VERIFY:
11261 /* Pull the index value and array address */
11262 op2 = impPopStack().val;
11263 op1 = impPopStack().val;
11264 assertImp(op1->gtType == TYP_REF);
11266 /* Check for null pointer - in the inliner case we simply abort */
11268 if (compIsForInlining())
11270 if (op1->gtOper == GT_CNS_INT)
11272 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_NULL_FOR_LDELEM);
11277 op1 = impCheckForNullPointer(op1);
11279 /* Mark the block as containing an index expression */
11281 if (op1->gtOper == GT_LCL_VAR)
11283 if (op2->gtOper == GT_LCL_VAR || op2->gtOper == GT_CNS_INT || op2->gtOper == GT_ADD)
11285 block->bbFlags |= BBF_HAS_IDX_LEN;
11286 optMethodFlags |= OMF_HAS_ARRAYREF;
11290 /* Create the index node and push it on the stack */
11292 op1 = gtNewIndexRef(lclTyp, op1, op2);
11294 ldstruct = (opcode == CEE_LDELEM && lclTyp == TYP_STRUCT);
11296 if ((opcode == CEE_LDELEMA) || ldstruct ||
11297 (ldelemClsHnd != DUMMY_INIT(NULL) && eeIsValueClass(ldelemClsHnd)))
11299 assert(ldelemClsHnd != DUMMY_INIT(NULL));
11301 // remember the element size
11302 if (lclTyp == TYP_REF)
11304 op1->gtIndex.gtIndElemSize = TARGET_POINTER_SIZE;
11308 // If ldElemClass is precisely a primitive type, use that, otherwise, preserve the struct type.
11309 if (info.compCompHnd->getTypeForPrimitiveValueClass(ldelemClsHnd) == CORINFO_TYPE_UNDEF)
11311 op1->gtIndex.gtStructElemClass = ldelemClsHnd;
11313 assert(lclTyp != TYP_STRUCT || op1->gtIndex.gtStructElemClass != nullptr);
11314 if (lclTyp == TYP_STRUCT)
11316 size = info.compCompHnd->getClassSize(ldelemClsHnd);
11317 op1->gtIndex.gtIndElemSize = size;
11318 op1->gtType = lclTyp;
11322 if ((opcode == CEE_LDELEMA) || ldstruct)
11325 lclTyp = TYP_BYREF;
11327 op1 = gtNewOperNode(GT_ADDR, lclTyp, op1);
11331 assert(lclTyp != TYP_STRUCT);
11337 // Create an OBJ for the result
11338 op1 = gtNewObjNode(ldelemClsHnd, op1);
11339 op1->gtFlags |= GTF_EXCEPT;
11341 impPushOnStack(op1, tiRetVal);
11344 // stelem for reference and value types
11347 assertImp(sz == sizeof(unsigned));
11349 _impResolveToken(CORINFO_TOKENKIND_Class);
11351 JITDUMP(" %08X", resolvedToken.token);
11353 stelemClsHnd = resolvedToken.hClass;
11355 if (tiVerificationNeeded)
11357 typeInfo tiArray = impStackTop(2).seTypeInfo;
11358 typeInfo tiIndex = impStackTop(1).seTypeInfo;
11359 typeInfo tiValue = impStackTop().seTypeInfo;
11361 // As per ECMA 'index' specified can be either int32 or native int.
11362 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11363 typeInfo arrayElem = verMakeTypeInfo(stelemClsHnd);
11365 Verify(tiArray.IsNullObjRef() || tiCompatibleWith(arrayElem, verGetArrayElemType(tiArray), false),
11366 "type operand incompatible with array element type");
11367 arrayElem.NormaliseForStack();
11368 Verify(tiCompatibleWith(tiValue, arrayElem, true), "value incompatible with type operand");
11371 // If it's a reference type just behave as though it's a stelem.ref instruction
11372 if (!eeIsValueClass(stelemClsHnd))
11374 goto STELEM_REF_POST_VERIFY;
11377 // Otherwise extract the type
11379 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(stelemClsHnd);
11380 lclTyp = JITtype2varType(jitTyp);
11381 goto ARR_ST_POST_VERIFY;
11384 case CEE_STELEM_REF:
11386 if (tiVerificationNeeded)
11388 typeInfo tiArray = impStackTop(2).seTypeInfo;
11389 typeInfo tiIndex = impStackTop(1).seTypeInfo;
11390 typeInfo tiValue = impStackTop().seTypeInfo;
11392 // As per ECMA 'index' specified can be either int32 or native int.
11393 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11394 Verify(tiValue.IsObjRef(), "bad value");
11396 // we only check that it is an object referece, The helper does additional checks
11397 Verify(tiArray.IsNullObjRef() || verGetArrayElemType(tiArray).IsType(TI_REF), "bad array");
11400 STELEM_REF_POST_VERIFY:
11402 arrayNodeTo = impStackTop(2).val;
11403 arrayNodeToIndex = impStackTop(1).val;
11404 arrayNodeFrom = impStackTop().val;
11407 // Note that it is not legal to optimize away CORINFO_HELP_ARRADDR_ST in a
11408 // lot of cases because of covariance. ie. foo[] can be cast to object[].
11411 // Check for assignment to same array, ie. arrLcl[i] = arrLcl[j]
11412 // This does not need CORINFO_HELP_ARRADDR_ST
11413 if (arrayNodeFrom->OperGet() == GT_INDEX && arrayNodeFrom->gtOp.gtOp1->gtOper == GT_LCL_VAR &&
11414 arrayNodeTo->gtOper == GT_LCL_VAR &&
11415 arrayNodeTo->gtLclVarCommon.gtLclNum == arrayNodeFrom->gtOp.gtOp1->gtLclVarCommon.gtLclNum &&
11416 !lvaTable[arrayNodeTo->gtLclVarCommon.gtLclNum].lvAddrExposed)
11418 JITDUMP("\nstelem of ref from same array: skipping covariant store check\n");
11420 goto ARR_ST_POST_VERIFY;
11423 // Check for assignment of NULL. This does not need CORINFO_HELP_ARRADDR_ST
11424 if (arrayNodeFrom->OperGet() == GT_CNS_INT)
11426 JITDUMP("\nstelem of null: skipping covariant store check\n");
11427 assert(arrayNodeFrom->gtType == TYP_REF && arrayNodeFrom->gtIntCon.gtIconVal == 0);
11429 goto ARR_ST_POST_VERIFY;
11432 /* Call a helper function to do the assignment */
11433 op1 = gtNewHelperCallNode(CORINFO_HELP_ARRADDR_ST, TYP_VOID, impPopList(3, nullptr));
11437 case CEE_STELEM_I1:
11440 case CEE_STELEM_I2:
11441 lclTyp = TYP_SHORT;
11444 lclTyp = TYP_I_IMPL;
11446 case CEE_STELEM_I4:
11449 case CEE_STELEM_I8:
11452 case CEE_STELEM_R4:
11453 lclTyp = TYP_FLOAT;
11455 case CEE_STELEM_R8:
11456 lclTyp = TYP_DOUBLE;
11461 if (tiVerificationNeeded)
11463 typeInfo tiArray = impStackTop(2).seTypeInfo;
11464 typeInfo tiIndex = impStackTop(1).seTypeInfo;
11465 typeInfo tiValue = impStackTop().seTypeInfo;
11467 // As per ECMA 'index' specified can be either int32 or native int.
11468 Verify(tiIndex.IsIntOrNativeIntType(), "bad index");
11469 typeInfo arrayElem = typeInfo(lclTyp);
11470 #ifdef _TARGET_64BIT_
11471 if (opcode == CEE_STELEM_I)
11473 arrayElem = typeInfo::nativeInt();
11475 #endif // _TARGET_64BIT_
11476 Verify(tiArray.IsNullObjRef() || typeInfo::AreEquivalent(verGetArrayElemType(tiArray), arrayElem),
11479 Verify(tiCompatibleWith(NormaliseForStack(tiValue), arrayElem.NormaliseForStack(), true),
11483 ARR_ST_POST_VERIFY:
11484 /* The strict order of evaluation is LHS-operands, RHS-operands,
11485 range-check, and then assignment. However, codegen currently
11486 does the range-check before evaluation the RHS-operands. So to
11487 maintain strict ordering, we spill the stack. */
11489 if (impStackTop().val->gtFlags & GTF_SIDE_EFFECT)
11491 impSpillSideEffects(false, (unsigned)CHECK_SPILL_ALL DEBUGARG(
11492 "Strict ordering of exceptions for Array store"));
11495 /* Pull the new value from the stack */
11496 op2 = impPopStack().val;
11498 /* Pull the index value */
11499 op1 = impPopStack().val;
11501 /* Pull the array address */
11502 op3 = impPopStack().val;
11504 assertImp(op3->gtType == TYP_REF);
11505 if (op2->IsVarAddr())
11507 op2->gtType = TYP_I_IMPL;
11510 op3 = impCheckForNullPointer(op3);
11512 // Mark the block as containing an index expression
11514 if (op3->gtOper == GT_LCL_VAR)
11516 if (op1->gtOper == GT_LCL_VAR || op1->gtOper == GT_CNS_INT || op1->gtOper == GT_ADD)
11518 block->bbFlags |= BBF_HAS_IDX_LEN;
11519 optMethodFlags |= OMF_HAS_ARRAYREF;
11523 /* Create the index node */
11525 op1 = gtNewIndexRef(lclTyp, op3, op1);
11527 /* Create the assignment node and append it */
11529 if (lclTyp == TYP_STRUCT)
11531 assert(stelemClsHnd != DUMMY_INIT(NULL));
11533 op1->gtIndex.gtStructElemClass = stelemClsHnd;
11534 op1->gtIndex.gtIndElemSize = info.compCompHnd->getClassSize(stelemClsHnd);
11536 if (varTypeIsStruct(op1))
11538 op1 = impAssignStruct(op1, op2, stelemClsHnd, (unsigned)CHECK_SPILL_ALL);
11542 op2 = impImplicitR4orR8Cast(op2, op1->TypeGet());
11543 op1 = gtNewAssignNode(op1, op2);
11546 /* Mark the expression as containing an assignment */
11548 op1->gtFlags |= GTF_ASG;
11559 case CEE_ADD_OVF_UN:
11567 goto MATH_OP2_FLAGS;
11576 case CEE_SUB_OVF_UN:
11584 goto MATH_OP2_FLAGS;
11588 goto MATH_MAYBE_CALL_NO_OVF;
11593 case CEE_MUL_OVF_UN:
11600 goto MATH_MAYBE_CALL_OVF;
11602 // Other binary math operations
11606 goto MATH_MAYBE_CALL_NO_OVF;
11610 goto MATH_MAYBE_CALL_NO_OVF;
11614 goto MATH_MAYBE_CALL_NO_OVF;
11618 goto MATH_MAYBE_CALL_NO_OVF;
11620 MATH_MAYBE_CALL_NO_OVF:
11622 MATH_MAYBE_CALL_OVF:
11623 // Morpher has some complex logic about when to turn different
11624 // typed nodes on different platforms into helper calls. We
11625 // need to either duplicate that logic here, or just
11626 // pessimistically make all the nodes large enough to become
11627 // call nodes. Since call nodes aren't that much larger and
11628 // these opcodes are infrequent enough I chose the latter.
11630 goto MATH_OP2_FLAGS;
11642 MATH_OP2: // For default values of 'ovfl' and 'callNode'
11647 MATH_OP2_FLAGS: // If 'ovfl' and 'callNode' have already been set
11649 /* Pull two values and push back the result */
11651 if (tiVerificationNeeded)
11653 const typeInfo& tiOp1 = impStackTop(1).seTypeInfo;
11654 const typeInfo& tiOp2 = impStackTop().seTypeInfo;
11656 Verify(tiCompatibleWith(tiOp1, tiOp2, true), "different arg type");
11657 if (oper == GT_ADD || oper == GT_DIV || oper == GT_SUB || oper == GT_MUL || oper == GT_MOD)
11659 Verify(tiOp1.IsNumberType(), "not number");
11663 Verify(tiOp1.IsIntegerType(), "not integer");
11666 Verify(!ovfl || tiOp1.IsIntegerType(), "not integer");
11670 #ifdef _TARGET_64BIT_
11671 if (tiOp2.IsNativeIntType())
11675 #endif // _TARGET_64BIT_
11678 op2 = impPopStack().val;
11679 op1 = impPopStack().val;
11681 #if !CPU_HAS_FP_SUPPORT
11682 if (varTypeIsFloating(op1->gtType))
11687 /* Can't do arithmetic with references */
11688 assertImp(genActualType(op1->TypeGet()) != TYP_REF && genActualType(op2->TypeGet()) != TYP_REF);
11690 // Change both to TYP_I_IMPL (impBashVarAddrsToI won't change if its a true byref, only
11691 // if it is in the stack)
11692 impBashVarAddrsToI(op1, op2);
11694 type = impGetByRefResultType(oper, uns, &op1, &op2);
11696 assert(!ovfl || !varTypeIsFloating(op1->gtType));
11698 /* Special case: "int+0", "int-0", "int*1", "int/1" */
11700 if (op2->gtOper == GT_CNS_INT)
11702 if ((op2->IsIntegralConst(0) && (oper == GT_ADD || oper == GT_SUB)) ||
11703 (op2->IsIntegralConst(1) && (oper == GT_MUL || oper == GT_DIV)))
11706 impPushOnStack(op1, tiRetVal);
11711 #if !FEATURE_X87_DOUBLES
11712 // We can generate a TYP_FLOAT operation that has a TYP_DOUBLE operand
11714 if (varTypeIsFloating(type) && varTypeIsFloating(op1->gtType) && varTypeIsFloating(op2->gtType))
11716 if (op1->TypeGet() != type)
11718 // We insert a cast of op1 to 'type'
11719 op1 = gtNewCastNode(type, op1, type);
11721 if (op2->TypeGet() != type)
11723 // We insert a cast of op2 to 'type'
11724 op2 = gtNewCastNode(type, op2, type);
11727 #endif // !FEATURE_X87_DOUBLES
11729 #if SMALL_TREE_NODES
11732 /* These operators can later be transformed into 'GT_CALL' */
11734 assert(GenTree::s_gtNodeSizes[GT_CALL] > GenTree::s_gtNodeSizes[GT_MUL]);
11735 #ifndef _TARGET_ARM_
11736 assert(GenTree::s_gtNodeSizes[GT_CALL] > GenTree::s_gtNodeSizes[GT_DIV]);
11737 assert(GenTree::s_gtNodeSizes[GT_CALL] > GenTree::s_gtNodeSizes[GT_UDIV]);
11738 assert(GenTree::s_gtNodeSizes[GT_CALL] > GenTree::s_gtNodeSizes[GT_MOD]);
11739 assert(GenTree::s_gtNodeSizes[GT_CALL] > GenTree::s_gtNodeSizes[GT_UMOD]);
11741 // It's tempting to use LargeOpOpcode() here, but this logic is *not* saying
11742 // that we'll need to transform into a general large node, but rather specifically
11743 // to a call: by doing it this way, things keep working if there are multiple sizes,
11744 // and a CALL is no longer the largest.
11745 // That said, as of now it *is* a large node, so we'll do this with an assert rather
11747 assert(GenTree::s_gtNodeSizes[GT_CALL] == TREE_NODE_SZ_LARGE);
11748 op1 = new (this, GT_CALL) GenTreeOp(oper, type, op1, op2 DEBUGARG(/*largeNode*/ true));
11751 #endif // SMALL_TREE_NODES
11753 op1 = gtNewOperNode(oper, type, op1, op2);
11756 /* Special case: integer/long division may throw an exception */
11758 if (varTypeIsIntegral(op1->TypeGet()) && op1->OperMayThrow(this))
11760 op1->gtFlags |= GTF_EXCEPT;
11765 assert(oper == GT_ADD || oper == GT_SUB || oper == GT_MUL);
11766 if (ovflType != TYP_UNKNOWN)
11768 op1->gtType = ovflType;
11770 op1->gtFlags |= (GTF_EXCEPT | GTF_OVERFLOW);
11773 op1->gtFlags |= GTF_UNSIGNED;
11777 impPushOnStack(op1, tiRetVal);
11792 if (tiVerificationNeeded)
11794 const typeInfo& tiVal = impStackTop(1).seTypeInfo;
11795 const typeInfo& tiShift = impStackTop(0).seTypeInfo;
11796 Verify(tiVal.IsIntegerType() && tiShift.IsType(TI_INT), "Bad shift args");
11799 op2 = impPopStack().val;
11800 op1 = impPopStack().val; // operand to be shifted
11801 impBashVarAddrsToI(op1, op2);
11803 type = genActualType(op1->TypeGet());
11804 op1 = gtNewOperNode(oper, type, op1, op2);
11806 impPushOnStack(op1, tiRetVal);
11810 if (tiVerificationNeeded)
11812 tiRetVal = impStackTop().seTypeInfo;
11813 Verify(tiRetVal.IsIntegerType(), "bad int value");
11816 op1 = impPopStack().val;
11817 impBashVarAddrsToI(op1, nullptr);
11818 type = genActualType(op1->TypeGet());
11819 impPushOnStack(gtNewOperNode(GT_NOT, type, op1), tiRetVal);
11823 if (tiVerificationNeeded)
11825 tiRetVal = impStackTop().seTypeInfo;
11826 Verify(tiRetVal.IsType(TI_DOUBLE), "bad R value");
11828 op1 = impPopStack().val;
11829 type = op1->TypeGet();
11830 op1 = gtNewOperNode(GT_CKFINITE, type, op1);
11831 op1->gtFlags |= GTF_EXCEPT;
11833 impPushOnStack(op1, tiRetVal);
11838 val = getI4LittleEndian(codeAddr); // jump distance
11839 jmpAddr = (IL_OFFSET)((codeAddr - info.compCode + sizeof(__int32)) + val);
11843 val = getI1LittleEndian(codeAddr); // jump distance
11844 jmpAddr = (IL_OFFSET)((codeAddr - info.compCode + sizeof(__int8)) + val);
11848 if (compIsForInlining())
11850 compInlineResult->NoteFatal(InlineObservation::CALLEE_HAS_LEAVE);
11854 JITDUMP(" %04X", jmpAddr);
11855 if (block->bbJumpKind != BBJ_LEAVE)
11857 impResetLeaveBlock(block, jmpAddr);
11860 assert(jmpAddr == block->bbJumpDest->bbCodeOffs);
11861 impImportLeave(block);
11862 impNoteBranchOffs();
11868 jmpDist = (sz == 1) ? getI1LittleEndian(codeAddr) : getI4LittleEndian(codeAddr);
11870 if (compIsForInlining() && jmpDist == 0)
11875 impNoteBranchOffs();
11881 case CEE_BRFALSE_S:
11883 /* Pop the comparand (now there's a neat term) from the stack */
11884 if (tiVerificationNeeded)
11886 typeInfo& tiVal = impStackTop().seTypeInfo;
11887 Verify(tiVal.IsObjRef() || tiVal.IsByRef() || tiVal.IsIntegerType() || tiVal.IsMethod(),
11891 op1 = impPopStack().val;
11892 type = op1->TypeGet();
11894 // brfalse and brtrue is only allowed on I4, refs, and byrefs.
11895 if (!opts.MinOpts() && !opts.compDbgCode && block->bbJumpDest == block->bbNext)
11897 block->bbJumpKind = BBJ_NONE;
11899 if (op1->gtFlags & GTF_GLOB_EFFECT)
11901 op1 = gtUnusedValNode(op1);
11910 if (op1->OperIsCompare())
11912 if (opcode == CEE_BRFALSE || opcode == CEE_BRFALSE_S)
11914 // Flip the sense of the compare
11916 op1 = gtReverseCond(op1);
11921 /* We'll compare against an equally-sized integer 0 */
11922 /* For small types, we always compare against int */
11923 op2 = gtNewZeroConNode(genActualType(op1->gtType));
11925 /* Create the comparison operator and try to fold it */
11927 oper = (opcode == CEE_BRTRUE || opcode == CEE_BRTRUE_S) ? GT_NE : GT_EQ;
11928 op1 = gtNewOperNode(oper, TYP_INT, op1, op2);
11935 /* Fold comparison if we can */
11937 op1 = gtFoldExpr(op1);
11939 /* Try to fold the really simple cases like 'iconst *, ifne/ifeq'*/
11940 /* Don't make any blocks unreachable in import only mode */
11942 if ((op1->gtOper == GT_CNS_INT) && !compIsForImportOnly())
11944 /* gtFoldExpr() should prevent this as we don't want to make any blocks
11945 unreachable under compDbgCode */
11946 assert(!opts.compDbgCode);
11948 BBjumpKinds foldedJumpKind = (BBjumpKinds)(op1->gtIntCon.gtIconVal ? BBJ_ALWAYS : BBJ_NONE);
11949 assertImp((block->bbJumpKind == BBJ_COND) // normal case
11950 || (block->bbJumpKind == foldedJumpKind)); // this can happen if we are reimporting the
11951 // block for the second time
11953 block->bbJumpKind = foldedJumpKind;
11957 if (op1->gtIntCon.gtIconVal)
11959 printf("\nThe conditional jump becomes an unconditional jump to BB%02u\n",
11960 block->bbJumpDest->bbNum);
11964 printf("\nThe block falls through into the next BB%02u\n", block->bbNext->bbNum);
11971 op1 = gtNewOperNode(GT_JTRUE, TYP_VOID, op1);
11973 /* GT_JTRUE is handled specially for non-empty stacks. See 'addStmt'
11974 in impImportBlock(block). For correct line numbers, spill stack. */
11976 if (opts.compDbgCode && impCurStmtOffs != BAD_IL_OFFSET)
11978 impSpillStackEnsure(true);
12005 if (tiVerificationNeeded)
12007 verVerifyCond(impStackTop(1).seTypeInfo, impStackTop().seTypeInfo, opcode);
12008 tiRetVal = typeInfo(TI_INT);
12011 op2 = impPopStack().val;
12012 op1 = impPopStack().val;
12014 #ifdef _TARGET_64BIT_
12015 if (varTypeIsI(op1->TypeGet()) && (genActualType(op2->TypeGet()) == TYP_INT))
12017 op2 = gtNewCastNode(TYP_I_IMPL, op2, (var_types)(uns ? TYP_U_IMPL : TYP_I_IMPL));
12019 else if (varTypeIsI(op2->TypeGet()) && (genActualType(op1->TypeGet()) == TYP_INT))
12021 op1 = gtNewCastNode(TYP_I_IMPL, op1, (var_types)(uns ? TYP_U_IMPL : TYP_I_IMPL));
12023 #endif // _TARGET_64BIT_
12025 assertImp(genActualType(op1->TypeGet()) == genActualType(op2->TypeGet()) ||
12026 varTypeIsI(op1->TypeGet()) && varTypeIsI(op2->TypeGet()) ||
12027 varTypeIsFloating(op1->gtType) && varTypeIsFloating(op2->gtType));
12029 /* Create the comparison node */
12031 op1 = gtNewOperNode(oper, TYP_INT, op1, op2);
12033 /* TODO: setting both flags when only one is appropriate */
12034 if (opcode == CEE_CGT_UN || opcode == CEE_CLT_UN)
12036 op1->gtFlags |= GTF_RELOP_NAN_UN | GTF_UNSIGNED;
12039 impPushOnStack(op1, tiRetVal);
12045 goto CMP_2_OPs_AND_BR;
12050 goto CMP_2_OPs_AND_BR;
12055 goto CMP_2_OPs_AND_BR_UN;
12060 goto CMP_2_OPs_AND_BR;
12065 goto CMP_2_OPs_AND_BR_UN;
12070 goto CMP_2_OPs_AND_BR;
12075 goto CMP_2_OPs_AND_BR_UN;
12080 goto CMP_2_OPs_AND_BR;
12085 goto CMP_2_OPs_AND_BR_UN;
12090 goto CMP_2_OPs_AND_BR_UN;
12092 CMP_2_OPs_AND_BR_UN:
12095 goto CMP_2_OPs_AND_BR_ALL;
12099 goto CMP_2_OPs_AND_BR_ALL;
12100 CMP_2_OPs_AND_BR_ALL:
12102 if (tiVerificationNeeded)
12104 verVerifyCond(impStackTop(1).seTypeInfo, impStackTop().seTypeInfo, opcode);
12107 /* Pull two values */
12108 op2 = impPopStack().val;
12109 op1 = impPopStack().val;
12111 #ifdef _TARGET_64BIT_
12112 if ((op1->TypeGet() == TYP_I_IMPL) && (genActualType(op2->TypeGet()) == TYP_INT))
12114 op2 = gtNewCastNode(TYP_I_IMPL, op2, (var_types)(uns ? TYP_U_IMPL : TYP_I_IMPL));
12116 else if ((op2->TypeGet() == TYP_I_IMPL) && (genActualType(op1->TypeGet()) == TYP_INT))
12118 op1 = gtNewCastNode(TYP_I_IMPL, op1, (var_types)(uns ? TYP_U_IMPL : TYP_I_IMPL));
12120 #endif // _TARGET_64BIT_
12122 assertImp(genActualType(op1->TypeGet()) == genActualType(op2->TypeGet()) ||
12123 varTypeIsI(op1->TypeGet()) && varTypeIsI(op2->TypeGet()) ||
12124 varTypeIsFloating(op1->gtType) && varTypeIsFloating(op2->gtType));
12126 if (!opts.MinOpts() && !opts.compDbgCode && block->bbJumpDest == block->bbNext)
12128 block->bbJumpKind = BBJ_NONE;
12130 if (op1->gtFlags & GTF_GLOB_EFFECT)
12132 impSpillSideEffects(false, (unsigned)CHECK_SPILL_ALL DEBUGARG(
12133 "Branch to next Optimization, op1 side effect"));
12134 impAppendTree(gtUnusedValNode(op1), (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
12136 if (op2->gtFlags & GTF_GLOB_EFFECT)
12138 impSpillSideEffects(false, (unsigned)CHECK_SPILL_ALL DEBUGARG(
12139 "Branch to next Optimization, op2 side effect"));
12140 impAppendTree(gtUnusedValNode(op2), (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
12144 if ((op1->gtFlags | op2->gtFlags) & GTF_GLOB_EFFECT)
12146 impNoteLastILoffs();
12151 #if !FEATURE_X87_DOUBLES
12152 // We can generate an compare of different sized floating point op1 and op2
12153 // We insert a cast
12155 if (varTypeIsFloating(op1->TypeGet()))
12157 if (op1->TypeGet() != op2->TypeGet())
12159 assert(varTypeIsFloating(op2->TypeGet()));
12161 // say op1=double, op2=float. To avoid loss of precision
12162 // while comparing, op2 is converted to double and double
12163 // comparison is done.
12164 if (op1->TypeGet() == TYP_DOUBLE)
12166 // We insert a cast of op2 to TYP_DOUBLE
12167 op2 = gtNewCastNode(TYP_DOUBLE, op2, TYP_DOUBLE);
12169 else if (op2->TypeGet() == TYP_DOUBLE)
12171 // We insert a cast of op1 to TYP_DOUBLE
12172 op1 = gtNewCastNode(TYP_DOUBLE, op1, TYP_DOUBLE);
12176 #endif // !FEATURE_X87_DOUBLES
12178 /* Create and append the operator */
12180 op1 = gtNewOperNode(oper, TYP_INT, op1, op2);
12184 op1->gtFlags |= GTF_UNSIGNED;
12189 op1->gtFlags |= GTF_RELOP_NAN_UN;
12195 assert(!compIsForInlining());
12197 if (tiVerificationNeeded)
12199 Verify(impStackTop().seTypeInfo.IsType(TI_INT), "Bad switch val");
12201 /* Pop the switch value off the stack */
12202 op1 = impPopStack().val;
12203 assertImp(genActualTypeIsIntOrI(op1->TypeGet()));
12205 /* We can create a switch node */
12207 op1 = gtNewOperNode(GT_SWITCH, TYP_VOID, op1);
12209 val = (int)getU4LittleEndian(codeAddr);
12210 codeAddr += 4 + val * 4; // skip over the switch-table
12214 /************************** Casting OPCODES ***************************/
12216 case CEE_CONV_OVF_I1:
12219 case CEE_CONV_OVF_I2:
12220 lclTyp = TYP_SHORT;
12222 case CEE_CONV_OVF_I:
12223 lclTyp = TYP_I_IMPL;
12225 case CEE_CONV_OVF_I4:
12228 case CEE_CONV_OVF_I8:
12232 case CEE_CONV_OVF_U1:
12233 lclTyp = TYP_UBYTE;
12235 case CEE_CONV_OVF_U2:
12236 lclTyp = TYP_USHORT;
12238 case CEE_CONV_OVF_U:
12239 lclTyp = TYP_U_IMPL;
12241 case CEE_CONV_OVF_U4:
12244 case CEE_CONV_OVF_U8:
12245 lclTyp = TYP_ULONG;
12248 case CEE_CONV_OVF_I1_UN:
12251 case CEE_CONV_OVF_I2_UN:
12252 lclTyp = TYP_SHORT;
12254 case CEE_CONV_OVF_I_UN:
12255 lclTyp = TYP_I_IMPL;
12257 case CEE_CONV_OVF_I4_UN:
12260 case CEE_CONV_OVF_I8_UN:
12264 case CEE_CONV_OVF_U1_UN:
12265 lclTyp = TYP_UBYTE;
12267 case CEE_CONV_OVF_U2_UN:
12268 lclTyp = TYP_USHORT;
12270 case CEE_CONV_OVF_U_UN:
12271 lclTyp = TYP_U_IMPL;
12273 case CEE_CONV_OVF_U4_UN:
12276 case CEE_CONV_OVF_U8_UN:
12277 lclTyp = TYP_ULONG;
12282 goto CONV_OVF_COMMON;
12285 goto CONV_OVF_COMMON;
12295 lclTyp = TYP_SHORT;
12298 lclTyp = TYP_I_IMPL;
12308 lclTyp = TYP_UBYTE;
12311 lclTyp = TYP_USHORT;
12313 #if (REGSIZE_BYTES == 8)
12315 lclTyp = TYP_U_IMPL;
12319 lclTyp = TYP_U_IMPL;
12326 lclTyp = TYP_ULONG;
12330 lclTyp = TYP_FLOAT;
12333 lclTyp = TYP_DOUBLE;
12336 case CEE_CONV_R_UN:
12337 lclTyp = TYP_DOUBLE;
12351 // just check that we have a number on the stack
12352 if (tiVerificationNeeded)
12354 const typeInfo& tiVal = impStackTop().seTypeInfo;
12355 Verify(tiVal.IsNumberType(), "bad arg");
12357 #ifdef _TARGET_64BIT_
12358 bool isNative = false;
12362 case CEE_CONV_OVF_I:
12363 case CEE_CONV_OVF_I_UN:
12365 case CEE_CONV_OVF_U:
12366 case CEE_CONV_OVF_U_UN:
12370 // leave 'isNative' = false;
12375 tiRetVal = typeInfo::nativeInt();
12378 #endif // _TARGET_64BIT_
12380 tiRetVal = typeInfo(lclTyp).NormaliseForStack();
12384 // only converts from FLOAT or DOUBLE to an integer type
12385 // and converts from ULONG (or LONG on ARM) to DOUBLE are morphed to calls
12387 if (varTypeIsFloating(lclTyp))
12389 callNode = varTypeIsLong(impStackTop().val) || uns // uint->dbl gets turned into uint->long->dbl
12390 #ifdef _TARGET_64BIT_
12391 // TODO-ARM64-Bug?: This was AMD64; I enabled it for ARM64 also. OK?
12392 // TYP_BYREF could be used as TYP_I_IMPL which is long.
12393 // TODO-CQ: remove this when we lower casts long/ulong --> float/double
12394 // and generate SSE2 code instead of going through helper calls.
12395 || (impStackTop().val->TypeGet() == TYP_BYREF)
12401 callNode = varTypeIsFloating(impStackTop().val->TypeGet());
12404 // At this point uns, ovf, callNode all set
12406 op1 = impPopStack().val;
12407 impBashVarAddrsToI(op1);
12409 if (varTypeIsSmall(lclTyp) && !ovfl && op1->gtType == TYP_INT && op1->gtOper == GT_AND)
12411 op2 = op1->gtOp.gtOp2;
12413 if (op2->gtOper == GT_CNS_INT)
12415 ssize_t ival = op2->gtIntCon.gtIconVal;
12416 ssize_t mask, umask;
12432 assert(!"unexpected type");
12436 if (((ival & umask) == ival) || ((ival & mask) == ival && uns))
12438 /* Toss the cast, it's a waste of time */
12440 impPushOnStack(op1, tiRetVal);
12443 else if (ival == mask)
12445 /* Toss the masking, it's a waste of time, since
12446 we sign-extend from the small value anyways */
12448 op1 = op1->gtOp.gtOp1;
12453 /* The 'op2' sub-operand of a cast is the 'real' type number,
12454 since the result of a cast to one of the 'small' integer
12455 types is an integer.
12458 type = genActualType(lclTyp);
12460 #if SMALL_TREE_NODES
12463 op1 = gtNewCastNodeL(type, op1, lclTyp);
12466 #endif // SMALL_TREE_NODES
12468 op1 = gtNewCastNode(type, op1, lclTyp);
12473 op1->gtFlags |= (GTF_OVERFLOW | GTF_EXCEPT);
12477 op1->gtFlags |= GTF_UNSIGNED;
12479 impPushOnStack(op1, tiRetVal);
12483 if (tiVerificationNeeded)
12485 tiRetVal = impStackTop().seTypeInfo;
12486 Verify(tiRetVal.IsNumberType(), "Bad arg");
12489 op1 = impPopStack().val;
12490 impBashVarAddrsToI(op1, nullptr);
12491 impPushOnStack(gtNewOperNode(GT_NEG, genActualType(op1->gtType), op1), tiRetVal);
12496 /* Pull the top value from the stack */
12498 StackEntry se = impPopStack();
12499 clsHnd = se.seTypeInfo.GetClassHandle();
12502 /* Get hold of the type of the value being duplicated */
12504 lclTyp = genActualType(op1->gtType);
12506 /* Does the value have any side effects? */
12508 if ((op1->gtFlags & GTF_SIDE_EFFECT) || opts.compDbgCode)
12510 // Since we are throwing away the value, just normalize
12511 // it to its address. This is more efficient.
12513 if (varTypeIsStruct(op1))
12515 #ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
12516 // Non-calls, such as obj or ret_expr, have to go through this.
12517 // Calls with large struct return value have to go through this.
12518 // Helper calls with small struct return value also have to go
12519 // through this since they do not follow Unix calling convention.
12520 if (op1->gtOper != GT_CALL || !IsMultiRegReturnedType(clsHnd) ||
12521 op1->AsCall()->gtCallType == CT_HELPER)
12522 #endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
12524 op1 = impGetStructAddr(op1, clsHnd, (unsigned)CHECK_SPILL_ALL, false);
12528 // If op1 is non-overflow cast, throw it away since it is useless.
12529 // Another reason for throwing away the useless cast is in the context of
12530 // implicit tail calls when the operand of pop is GT_CAST(GT_CALL(..)).
12531 // The cast gets added as part of importing GT_CALL, which gets in the way
12532 // of fgMorphCall() on the forms of tail call nodes that we assert.
12533 if ((op1->gtOper == GT_CAST) && !op1->gtOverflow())
12535 op1 = op1->gtOp.gtOp1;
12538 // If 'op1' is an expression, create an assignment node.
12539 // Helps analyses (like CSE) to work fine.
12541 if (op1->gtOper != GT_CALL)
12543 op1 = gtUnusedValNode(op1);
12546 /* Append the value to the tree list */
12550 /* No side effects - just throw the <BEEP> thing away */
12556 if (tiVerificationNeeded)
12558 // Dup could start the begining of delegate creation sequence, remember that
12559 delegateCreateStart = codeAddr - 1;
12563 // If the expression to dup is simple, just clone it.
12564 // Otherwise spill it to a temp, and reload the temp
12566 StackEntry se = impPopStack();
12567 GenTree* tree = se.val;
12568 tiRetVal = se.seTypeInfo;
12571 if (!opts.compDbgCode && !op1->IsIntegralConst(0) && !op1->IsFPZero() && !op1->IsLocal())
12573 const unsigned tmpNum = lvaGrabTemp(true DEBUGARG("dup spill"));
12574 impAssignTempGen(tmpNum, op1, tiRetVal.GetClassHandle(), (unsigned)CHECK_SPILL_ALL);
12575 var_types type = genActualType(lvaTable[tmpNum].TypeGet());
12576 op1 = gtNewLclvNode(tmpNum, type);
12578 // Propagate type info to the temp from the stack and the original tree
12579 if (type == TYP_REF)
12581 lvaSetClass(tmpNum, tree, tiRetVal.GetClassHandle());
12585 op1 = impCloneExpr(op1, &op2, tiRetVal.GetClassHandle(), (unsigned)CHECK_SPILL_ALL,
12586 nullptr DEBUGARG("DUP instruction"));
12588 assert(!(op1->gtFlags & GTF_GLOB_EFFECT) && !(op2->gtFlags & GTF_GLOB_EFFECT));
12589 impPushOnStack(op1, tiRetVal);
12590 impPushOnStack(op2, tiRetVal);
12598 lclTyp = TYP_SHORT;
12607 lclTyp = TYP_I_IMPL;
12609 case CEE_STIND_REF:
12613 lclTyp = TYP_FLOAT;
12616 lclTyp = TYP_DOUBLE;
12620 if (tiVerificationNeeded)
12622 typeInfo instrType(lclTyp);
12623 #ifdef _TARGET_64BIT_
12624 if (opcode == CEE_STIND_I)
12626 instrType = typeInfo::nativeInt();
12628 #endif // _TARGET_64BIT_
12629 verVerifySTIND(impStackTop(1).seTypeInfo, impStackTop(0).seTypeInfo, instrType);
12633 compUnsafeCastUsed = true; // Have to go conservative
12638 op2 = impPopStack().val; // value to store
12639 op1 = impPopStack().val; // address to store to
12641 // you can indirect off of a TYP_I_IMPL (if we are in C) or a BYREF
12642 assertImp(genActualType(op1->gtType) == TYP_I_IMPL || op1->gtType == TYP_BYREF);
12644 impBashVarAddrsToI(op1, op2);
12646 op2 = impImplicitR4orR8Cast(op2, lclTyp);
12648 #ifdef _TARGET_64BIT_
12649 // Automatic upcast for a GT_CNS_INT into TYP_I_IMPL
12650 if ((op2->OperGet() == GT_CNS_INT) && varTypeIsI(lclTyp) && !varTypeIsI(op2->gtType))
12652 op2->gtType = TYP_I_IMPL;
12656 // Allow a downcast of op2 from TYP_I_IMPL into a 32-bit Int for x86 JIT compatiblity
12658 if (varTypeIsI(op2->gtType) && (genActualType(lclTyp) == TYP_INT))
12660 assert(!tiVerificationNeeded); // We should have thrown the VerificationException before.
12661 op2 = gtNewCastNode(TYP_INT, op2, TYP_INT);
12663 // Allow an upcast of op2 from a 32-bit Int into TYP_I_IMPL for x86 JIT compatiblity
12665 if (varTypeIsI(lclTyp) && (genActualType(op2->gtType) == TYP_INT))
12667 assert(!tiVerificationNeeded); // We should have thrown the VerificationException before.
12668 op2 = gtNewCastNode(TYP_I_IMPL, op2, TYP_I_IMPL);
12671 #endif // _TARGET_64BIT_
12673 if (opcode == CEE_STIND_REF)
12675 // STIND_REF can be used to store TYP_INT, TYP_I_IMPL, TYP_REF, or TYP_BYREF
12676 assertImp(varTypeIsIntOrI(op2->gtType) || varTypeIsGC(op2->gtType));
12677 lclTyp = genActualType(op2->TypeGet());
12680 // Check target type.
12682 if (op2->gtType == TYP_BYREF || lclTyp == TYP_BYREF)
12684 if (op2->gtType == TYP_BYREF)
12686 assertImp(lclTyp == TYP_BYREF || lclTyp == TYP_I_IMPL);
12688 else if (lclTyp == TYP_BYREF)
12690 assertImp(op2->gtType == TYP_BYREF || varTypeIsIntOrI(op2->gtType));
12695 assertImp(genActualType(op2->gtType) == genActualType(lclTyp) ||
12696 ((lclTyp == TYP_I_IMPL) && (genActualType(op2->gtType) == TYP_INT)) ||
12697 (varTypeIsFloating(op2->gtType) && varTypeIsFloating(lclTyp)));
12701 op1 = gtNewOperNode(GT_IND, lclTyp, op1);
12703 // stind could point anywhere, example a boxed class static int
12704 op1->gtFlags |= GTF_IND_TGTANYWHERE;
12706 if (prefixFlags & PREFIX_VOLATILE)
12708 assert(op1->OperGet() == GT_IND);
12709 op1->gtFlags |= GTF_DONT_CSE; // Can't CSE a volatile
12710 op1->gtFlags |= GTF_ORDER_SIDEEFF; // Prevent this from being reordered
12711 op1->gtFlags |= GTF_IND_VOLATILE;
12714 if ((prefixFlags & PREFIX_UNALIGNED) && !varTypeIsByte(lclTyp))
12716 assert(op1->OperGet() == GT_IND);
12717 op1->gtFlags |= GTF_IND_UNALIGNED;
12720 op1 = gtNewAssignNode(op1, op2);
12721 op1->gtFlags |= GTF_EXCEPT | GTF_GLOB_REF;
12723 // Spill side-effects AND global-data-accesses
12724 if (verCurrentState.esStackDepth > 0)
12726 impSpillSideEffects(true, (unsigned)CHECK_SPILL_ALL DEBUGARG("spill side effects before STIND"));
12735 lclTyp = TYP_SHORT;
12744 case CEE_LDIND_REF:
12748 lclTyp = TYP_I_IMPL;
12751 lclTyp = TYP_FLOAT;
12754 lclTyp = TYP_DOUBLE;
12757 lclTyp = TYP_UBYTE;
12760 lclTyp = TYP_USHORT;
12764 if (tiVerificationNeeded)
12766 typeInfo lclTiType(lclTyp);
12767 #ifdef _TARGET_64BIT_
12768 if (opcode == CEE_LDIND_I)
12770 lclTiType = typeInfo::nativeInt();
12772 #endif // _TARGET_64BIT_
12773 tiRetVal = verVerifyLDIND(impStackTop().seTypeInfo, lclTiType);
12774 tiRetVal.NormaliseForStack();
12778 compUnsafeCastUsed = true; // Have to go conservative
12783 op1 = impPopStack().val; // address to load from
12784 impBashVarAddrsToI(op1);
12786 #ifdef _TARGET_64BIT_
12787 // Allow an upcast of op1 from a 32-bit Int into TYP_I_IMPL for x86 JIT compatiblity
12789 if (genActualType(op1->gtType) == TYP_INT)
12791 assert(!tiVerificationNeeded); // We should have thrown the VerificationException before.
12792 op1 = gtNewCastNode(TYP_I_IMPL, op1, TYP_I_IMPL);
12796 assertImp(genActualType(op1->gtType) == TYP_I_IMPL || op1->gtType == TYP_BYREF);
12798 op1 = gtNewOperNode(GT_IND, lclTyp, op1);
12800 // ldind could point anywhere, example a boxed class static int
12801 op1->gtFlags |= (GTF_EXCEPT | GTF_GLOB_REF | GTF_IND_TGTANYWHERE);
12803 if (prefixFlags & PREFIX_VOLATILE)
12805 assert(op1->OperGet() == GT_IND);
12806 op1->gtFlags |= GTF_DONT_CSE; // Can't CSE a volatile
12807 op1->gtFlags |= GTF_ORDER_SIDEEFF; // Prevent this from being reordered
12808 op1->gtFlags |= GTF_IND_VOLATILE;
12811 if ((prefixFlags & PREFIX_UNALIGNED) && !varTypeIsByte(lclTyp))
12813 assert(op1->OperGet() == GT_IND);
12814 op1->gtFlags |= GTF_IND_UNALIGNED;
12817 impPushOnStack(op1, tiRetVal);
12821 case CEE_UNALIGNED:
12824 val = getU1LittleEndian(codeAddr);
12826 JITDUMP(" %u", val);
12827 if ((val != 1) && (val != 2) && (val != 4))
12829 BADCODE("Alignment unaligned. must be 1, 2, or 4");
12832 Verify(!(prefixFlags & PREFIX_UNALIGNED), "Multiple unaligned. prefixes");
12833 prefixFlags |= PREFIX_UNALIGNED;
12835 impValidateMemoryAccessOpcode(codeAddr, codeEndp, false);
12838 opcode = (OPCODE)getU1LittleEndian(codeAddr);
12839 opcodeOffs = (IL_OFFSET)(codeAddr - info.compCode);
12840 codeAddr += sizeof(__int8);
12841 goto DECODE_OPCODE;
12845 Verify(!(prefixFlags & PREFIX_VOLATILE), "Multiple volatile. prefixes");
12846 prefixFlags |= PREFIX_VOLATILE;
12848 impValidateMemoryAccessOpcode(codeAddr, codeEndp, true);
12855 // Need to do a lookup here so that we perform an access check
12856 // and do a NOWAY if protections are violated
12857 _impResolveToken(CORINFO_TOKENKIND_Method);
12859 JITDUMP(" %08X", resolvedToken.token);
12861 eeGetCallInfo(&resolvedToken, nullptr /* constraint typeRef*/,
12862 addVerifyFlag(combine(CORINFO_CALLINFO_SECURITYCHECKS, CORINFO_CALLINFO_LDFTN)),
12865 // This check really only applies to intrinsic Array.Address methods
12866 if (callInfo.sig.callConv & CORINFO_CALLCONV_PARAMTYPE)
12868 NO_WAY("Currently do not support LDFTN of Parameterized functions");
12871 // Do this before DO_LDFTN since CEE_LDVIRTFN does it on its own.
12872 impHandleAccessAllowed(callInfo.accessAllowed, &callInfo.callsiteCalloutHelper);
12874 if (tiVerificationNeeded)
12876 // LDFTN could start the begining of delegate creation sequence, remember that
12877 delegateCreateStart = codeAddr - 2;
12879 // check any constraints on the callee's class and type parameters
12880 VerifyOrReturn(info.compCompHnd->satisfiesClassConstraints(resolvedToken.hClass),
12881 "method has unsatisfied class constraints");
12882 VerifyOrReturn(info.compCompHnd->satisfiesMethodConstraints(resolvedToken.hClass,
12883 resolvedToken.hMethod),
12884 "method has unsatisfied method constraints");
12886 mflags = callInfo.verMethodFlags;
12887 Verify(!(mflags & CORINFO_FLG_CONSTRUCTOR), "LDFTN on a constructor");
12891 op1 = impMethodPointer(&resolvedToken, &callInfo);
12892 if (compDonotInline())
12897 CORINFO_RESOLVED_TOKEN* heapToken = impAllocateToken(resolvedToken);
12898 impPushOnStack(op1, typeInfo(heapToken));
12903 case CEE_LDVIRTFTN:
12905 /* Get the method token */
12907 _impResolveToken(CORINFO_TOKENKIND_Method);
12909 JITDUMP(" %08X", resolvedToken.token);
12911 eeGetCallInfo(&resolvedToken, nullptr /* constraint typeRef */,
12912 addVerifyFlag(combine(combine(CORINFO_CALLINFO_SECURITYCHECKS, CORINFO_CALLINFO_LDFTN),
12913 CORINFO_CALLINFO_CALLVIRT)),
12916 // This check really only applies to intrinsic Array.Address methods
12917 if (callInfo.sig.callConv & CORINFO_CALLCONV_PARAMTYPE)
12919 NO_WAY("Currently do not support LDFTN of Parameterized functions");
12922 mflags = callInfo.methodFlags;
12924 impHandleAccessAllowed(callInfo.accessAllowed, &callInfo.callsiteCalloutHelper);
12926 if (compIsForInlining())
12928 if (mflags & (CORINFO_FLG_FINAL | CORINFO_FLG_STATIC) || !(mflags & CORINFO_FLG_VIRTUAL))
12930 compInlineResult->NoteFatal(InlineObservation::CALLSITE_LDVIRTFN_ON_NON_VIRTUAL);
12935 CORINFO_SIG_INFO& ftnSig = callInfo.sig;
12937 if (tiVerificationNeeded)
12940 Verify(ftnSig.hasThis(), "ldvirtftn on a static method");
12941 Verify(!(mflags & CORINFO_FLG_CONSTRUCTOR), "LDVIRTFTN on a constructor");
12943 // JIT32 verifier rejects verifiable ldvirtftn pattern
12944 typeInfo declType =
12945 verMakeTypeInfo(resolvedToken.hClass, true); // Change TI_STRUCT to TI_REF when necessary
12947 typeInfo arg = impStackTop().seTypeInfo;
12948 Verify((arg.IsType(TI_REF) || arg.IsType(TI_NULL)) && tiCompatibleWith(arg, declType, true),
12951 CORINFO_CLASS_HANDLE instanceClassHnd = info.compClassHnd;
12952 if (!(arg.IsType(TI_NULL) || (mflags & CORINFO_FLG_STATIC)))
12954 instanceClassHnd = arg.GetClassHandleForObjRef();
12957 // check any constraints on the method's class and type parameters
12958 VerifyOrReturn(info.compCompHnd->satisfiesClassConstraints(resolvedToken.hClass),
12959 "method has unsatisfied class constraints");
12960 VerifyOrReturn(info.compCompHnd->satisfiesMethodConstraints(resolvedToken.hClass,
12961 resolvedToken.hMethod),
12962 "method has unsatisfied method constraints");
12964 if (mflags & CORINFO_FLG_PROTECTED)
12966 Verify(info.compCompHnd->canAccessFamily(info.compMethodHnd, instanceClassHnd),
12967 "Accessing protected method through wrong type.");
12971 /* Get the object-ref */
12972 op1 = impPopStack().val;
12973 assertImp(op1->gtType == TYP_REF);
12975 if (opts.IsReadyToRun())
12977 if (callInfo.kind != CORINFO_VIRTUALCALL_LDVIRTFTN)
12979 if (op1->gtFlags & GTF_SIDE_EFFECT)
12981 op1 = gtUnusedValNode(op1);
12982 impAppendTree(op1, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
12987 else if (mflags & (CORINFO_FLG_FINAL | CORINFO_FLG_STATIC) || !(mflags & CORINFO_FLG_VIRTUAL))
12989 if (op1->gtFlags & GTF_SIDE_EFFECT)
12991 op1 = gtUnusedValNode(op1);
12992 impAppendTree(op1, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
12997 GenTreePtr fptr = impImportLdvirtftn(op1, &resolvedToken, &callInfo);
12998 if (compDonotInline())
13003 CORINFO_RESOLVED_TOKEN* heapToken = impAllocateToken(resolvedToken);
13004 assert(heapToken->tokenType == CORINFO_TOKENKIND_Method);
13005 heapToken->tokenType = CORINFO_TOKENKIND_Ldvirtftn;
13006 impPushOnStack(fptr, typeInfo(heapToken));
13011 case CEE_CONSTRAINED:
13013 assertImp(sz == sizeof(unsigned));
13014 impResolveToken(codeAddr, &constrainedResolvedToken, CORINFO_TOKENKIND_Constrained);
13015 codeAddr += sizeof(unsigned); // prefix instructions must increment codeAddr manually
13016 JITDUMP(" (%08X) ", constrainedResolvedToken.token);
13018 Verify(!(prefixFlags & PREFIX_CONSTRAINED), "Multiple constrained. prefixes");
13019 prefixFlags |= PREFIX_CONSTRAINED;
13022 OPCODE actualOpcode = impGetNonPrefixOpcode(codeAddr, codeEndp);
13023 if (actualOpcode != CEE_CALLVIRT)
13025 BADCODE("constrained. has to be followed by callvirt");
13032 JITDUMP(" readonly.");
13034 Verify(!(prefixFlags & PREFIX_READONLY), "Multiple readonly. prefixes");
13035 prefixFlags |= PREFIX_READONLY;
13038 OPCODE actualOpcode = impGetNonPrefixOpcode(codeAddr, codeEndp);
13039 if (actualOpcode != CEE_LDELEMA && !impOpcodeIsCallOpcode(actualOpcode))
13041 BADCODE("readonly. has to be followed by ldelema or call");
13051 Verify(!(prefixFlags & PREFIX_TAILCALL_EXPLICIT), "Multiple tailcall. prefixes");
13052 prefixFlags |= PREFIX_TAILCALL_EXPLICIT;
13055 OPCODE actualOpcode = impGetNonPrefixOpcode(codeAddr, codeEndp);
13056 if (!impOpcodeIsCallOpcode(actualOpcode))
13058 BADCODE("tailcall. has to be followed by call, callvirt or calli");
13066 /* Since we will implicitly insert newObjThisPtr at the start of the
13067 argument list, spill any GTF_ORDER_SIDEEFF */
13068 impSpillSpecialSideEff();
13070 /* NEWOBJ does not respond to TAIL */
13071 prefixFlags &= ~PREFIX_TAILCALL_EXPLICIT;
13073 /* NEWOBJ does not respond to CONSTRAINED */
13074 prefixFlags &= ~PREFIX_CONSTRAINED;
13076 _impResolveToken(CORINFO_TOKENKIND_NewObj);
13078 eeGetCallInfo(&resolvedToken, nullptr /* constraint typeRef*/,
13079 addVerifyFlag(combine(CORINFO_CALLINFO_SECURITYCHECKS, CORINFO_CALLINFO_ALLOWINSTPARAM)),
13082 if (compIsForInlining())
13084 if (impInlineInfo->inlineCandidateInfo->dwRestrictions & INLINE_RESPECT_BOUNDARY)
13086 // Check to see if this call violates the boundary.
13087 compInlineResult->NoteFatal(InlineObservation::CALLSITE_CROSS_BOUNDARY_SECURITY);
13092 mflags = callInfo.methodFlags;
13094 if ((mflags & (CORINFO_FLG_STATIC | CORINFO_FLG_ABSTRACT)) != 0)
13096 BADCODE("newobj on static or abstract method");
13099 // Insert the security callout before any actual code is generated
13100 impHandleAccessAllowed(callInfo.accessAllowed, &callInfo.callsiteCalloutHelper);
13102 // There are three different cases for new
13103 // Object size is variable (depends on arguments)
13104 // 1) Object is an array (arrays treated specially by the EE)
13105 // 2) Object is some other variable sized object (e.g. String)
13106 // 3) Class Size can be determined beforehand (normal case)
13107 // In the first case, we need to call a NEWOBJ helper (multinewarray)
13108 // in the second case we call the constructor with a '0' this pointer
13109 // In the third case we alloc the memory, then call the constuctor
13111 clsFlags = callInfo.classFlags;
13112 if (clsFlags & CORINFO_FLG_ARRAY)
13114 if (tiVerificationNeeded)
13116 CORINFO_CLASS_HANDLE elemTypeHnd;
13117 INDEBUG(CorInfoType corType =)
13118 info.compCompHnd->getChildType(resolvedToken.hClass, &elemTypeHnd);
13119 assert(!(elemTypeHnd == nullptr && corType == CORINFO_TYPE_VALUECLASS));
13120 Verify(elemTypeHnd == nullptr ||
13121 !(info.compCompHnd->getClassAttribs(elemTypeHnd) & CORINFO_FLG_CONTAINS_STACK_PTR),
13122 "newarr of byref-like objects");
13123 verVerifyCall(opcode, &resolvedToken, nullptr, ((prefixFlags & PREFIX_TAILCALL_EXPLICIT) != 0),
13124 ((prefixFlags & PREFIX_READONLY) != 0), delegateCreateStart, codeAddr - 1,
13125 &callInfo DEBUGARG(info.compFullName));
13127 // Arrays need to call the NEWOBJ helper.
13128 assertImp(clsFlags & CORINFO_FLG_VAROBJSIZE);
13130 impImportNewObjArray(&resolvedToken, &callInfo);
13131 if (compDonotInline())
13139 // At present this can only be String
13140 else if (clsFlags & CORINFO_FLG_VAROBJSIZE)
13142 if (IsTargetAbi(CORINFO_CORERT_ABI))
13144 // The dummy argument does not exist in CoreRT
13145 newObjThisPtr = nullptr;
13149 // This is the case for variable-sized objects that are not
13150 // arrays. In this case, call the constructor with a null 'this'
13152 newObjThisPtr = gtNewIconNode(0, TYP_REF);
13155 /* Remember that this basic block contains 'new' of an object */
13156 block->bbFlags |= BBF_HAS_NEWOBJ;
13157 optMethodFlags |= OMF_HAS_NEWOBJ;
13161 // This is the normal case where the size of the object is
13162 // fixed. Allocate the memory and call the constructor.
13164 // Note: We cannot add a peep to avoid use of temp here
13165 // becase we don't have enough interference info to detect when
13166 // sources and destination interfere, example: s = new S(ref);
13168 // TODO: We find the correct place to introduce a general
13169 // reverse copy prop for struct return values from newobj or
13170 // any function returning structs.
13172 /* get a temporary for the new object */
13173 lclNum = lvaGrabTemp(true DEBUGARG("NewObj constructor temp"));
13174 if (compDonotInline())
13176 // Fail fast if lvaGrabTemp fails with CALLSITE_TOO_MANY_LOCALS.
13177 assert(compInlineResult->GetObservation() == InlineObservation::CALLSITE_TOO_MANY_LOCALS);
13181 // In the value class case we only need clsHnd for size calcs.
13183 // The lookup of the code pointer will be handled by CALL in this case
13184 if (clsFlags & CORINFO_FLG_VALUECLASS)
13186 if (compIsForInlining())
13188 // If value class has GC fields, inform the inliner. It may choose to
13189 // bail out on the inline.
13190 DWORD typeFlags = info.compCompHnd->getClassAttribs(resolvedToken.hClass);
13191 if ((typeFlags & CORINFO_FLG_CONTAINS_GC_PTR) != 0)
13193 compInlineResult->Note(InlineObservation::CALLEE_HAS_GC_STRUCT);
13194 if (compInlineResult->IsFailure())
13199 // Do further notification in the case where the call site is rare;
13200 // some policies do not track the relative hotness of call sites for
13201 // "always" inline cases.
13202 if (impInlineInfo->iciBlock->isRunRarely())
13204 compInlineResult->Note(InlineObservation::CALLSITE_RARE_GC_STRUCT);
13205 if (compInlineResult->IsFailure())
13213 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(resolvedToken.hClass);
13214 unsigned size = info.compCompHnd->getClassSize(resolvedToken.hClass);
13216 if (impIsPrimitive(jitTyp))
13218 lvaTable[lclNum].lvType = JITtype2varType(jitTyp);
13222 // The local variable itself is the allocated space.
13223 // Here we need unsafe value cls check, since the address of struct is taken for further use
13224 // and potentially exploitable.
13225 lvaSetStruct(lclNum, resolvedToken.hClass, true /* unsafe value cls check */);
13227 if (compIsForInlining() || fgStructTempNeedsExplicitZeroInit(lvaTable + lclNum, block))
13229 // Append a tree to zero-out the temp
13230 newObjThisPtr = gtNewLclvNode(lclNum, lvaTable[lclNum].TypeGet());
13232 newObjThisPtr = gtNewBlkOpNode(newObjThisPtr, // Dest
13233 gtNewIconNode(0), // Value
13235 false, // isVolatile
13236 false); // not copyBlock
13237 impAppendTree(newObjThisPtr, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
13240 // Obtain the address of the temp
13242 gtNewOperNode(GT_ADDR, TYP_BYREF, gtNewLclvNode(lclNum, lvaTable[lclNum].TypeGet()));
13246 #ifdef FEATURE_READYTORUN_COMPILER
13247 if (opts.IsReadyToRun())
13249 op1 = impReadyToRunHelperToTree(&resolvedToken, CORINFO_HELP_READYTORUN_NEW, TYP_REF);
13250 usingReadyToRunHelper = (op1 != nullptr);
13253 if (!usingReadyToRunHelper)
13256 op1 = impParentClassTokenToHandle(&resolvedToken, nullptr, TRUE);
13257 if (op1 == nullptr)
13258 { // compDonotInline()
13262 // TODO: ReadyToRun: When generic dictionary lookups are necessary, replace the lookup call
13263 // and the newfast call with a single call to a dynamic R2R cell that will:
13264 // 1) Load the context
13265 // 2) Perform the generic dictionary lookup and caching, and generate the appropriate
13267 // 3) Allocate and return the new object
13268 // Reason: performance (today, we'll always use the slow helper for the R2R generics case)
13270 op1 = gtNewAllocObjNode(info.compCompHnd->getNewHelper(&resolvedToken, info.compMethodHnd),
13271 resolvedToken.hClass, TYP_REF, op1);
13274 // Remember that this basic block contains 'new' of an object
13275 block->bbFlags |= BBF_HAS_NEWOBJ;
13276 optMethodFlags |= OMF_HAS_NEWOBJ;
13278 // Append the assignment to the temp/local. Dont need to spill
13279 // at all as we are just calling an EE-Jit helper which can only
13280 // cause an (async) OutOfMemoryException.
13282 // We assign the newly allocated object (by a GT_ALLOCOBJ node)
13283 // to a temp. Note that the pattern "temp = allocObj" is required
13284 // by ObjectAllocator phase to be able to determine GT_ALLOCOBJ nodes
13285 // without exhaustive walk over all expressions.
13287 impAssignTempGen(lclNum, op1, (unsigned)CHECK_SPILL_NONE);
13288 lvaSetClass(lclNum, resolvedToken.hClass, true /* is Exact */);
13290 newObjThisPtr = gtNewLclvNode(lclNum, TYP_REF);
13297 /* CALLI does not respond to CONSTRAINED */
13298 prefixFlags &= ~PREFIX_CONSTRAINED;
13300 if (compIsForInlining())
13302 // CALLI doesn't have a method handle, so assume the worst.
13303 if (impInlineInfo->inlineCandidateInfo->dwRestrictions & INLINE_RESPECT_BOUNDARY)
13305 compInlineResult->NoteFatal(InlineObservation::CALLSITE_CROSS_BOUNDARY_CALLI);
13315 // We can't call getCallInfo on the token from a CALLI, but we need it in
13316 // many other places. We unfortunately embed that knowledge here.
13317 if (opcode != CEE_CALLI)
13319 _impResolveToken(CORINFO_TOKENKIND_Method);
13321 eeGetCallInfo(&resolvedToken,
13322 (prefixFlags & PREFIX_CONSTRAINED) ? &constrainedResolvedToken : nullptr,
13323 // this is how impImportCall invokes getCallInfo
13325 combine(combine(CORINFO_CALLINFO_ALLOWINSTPARAM, CORINFO_CALLINFO_SECURITYCHECKS),
13326 (opcode == CEE_CALLVIRT) ? CORINFO_CALLINFO_CALLVIRT
13327 : CORINFO_CALLINFO_NONE)),
13332 // Suppress uninitialized use warning.
13333 memset(&resolvedToken, 0, sizeof(resolvedToken));
13334 memset(&callInfo, 0, sizeof(callInfo));
13336 resolvedToken.token = getU4LittleEndian(codeAddr);
13339 CALL: // memberRef should be set.
13340 // newObjThisPtr should be set for CEE_NEWOBJ
13342 JITDUMP(" %08X", resolvedToken.token);
13343 constraintCall = (prefixFlags & PREFIX_CONSTRAINED) != 0;
13345 bool newBBcreatedForTailcallStress;
13347 newBBcreatedForTailcallStress = false;
13349 if (compIsForInlining())
13351 if (compDonotInline())
13355 // We rule out inlinees with explicit tail calls in fgMakeBasicBlocks.
13356 assert((prefixFlags & PREFIX_TAILCALL_EXPLICIT) == 0);
13360 if (compTailCallStress())
13362 // Have we created a new BB after the "call" instruction in fgMakeBasicBlocks()?
13363 // Tail call stress only recognizes call+ret patterns and forces them to be
13364 // explicit tail prefixed calls. Also fgMakeBasicBlocks() under tail call stress
13365 // doesn't import 'ret' opcode following the call into the basic block containing
13366 // the call instead imports it to a new basic block. Note that fgMakeBasicBlocks()
13367 // is already checking that there is an opcode following call and hence it is
13368 // safe here to read next opcode without bounds check.
13369 newBBcreatedForTailcallStress =
13370 impOpcodeIsCallOpcode(opcode) && // Current opcode is a CALL, (not a CEE_NEWOBJ). So, don't
13371 // make it jump to RET.
13372 (OPCODE)getU1LittleEndian(codeAddr + sz) == CEE_RET; // Next opcode is a CEE_RET
13374 if (newBBcreatedForTailcallStress &&
13375 !(prefixFlags & PREFIX_TAILCALL_EXPLICIT) && // User hasn't set "tail." prefix yet.
13376 verCheckTailCallConstraint(opcode, &resolvedToken,
13377 constraintCall ? &constrainedResolvedToken : nullptr,
13378 true) // Is it legal to do tailcall?
13381 // Stress the tailcall.
13382 JITDUMP(" (Tailcall stress: prefixFlags |= PREFIX_TAILCALL_EXPLICIT)");
13383 prefixFlags |= PREFIX_TAILCALL_EXPLICIT;
13388 // This is split up to avoid goto flow warnings.
13390 isRecursive = !compIsForInlining() && (callInfo.hMethod == info.compMethodHnd);
13392 // Note that when running under tail call stress, a call will be marked as explicit tail prefixed
13393 // hence will not be considered for implicit tail calling.
13394 if (impIsImplicitTailCallCandidate(opcode, codeAddr + sz, codeEndp, prefixFlags, isRecursive))
13396 if (compIsForInlining())
13398 #if FEATURE_TAILCALL_OPT_SHARED_RETURN
13399 // Are we inlining at an implicit tail call site? If so the we can flag
13400 // implicit tail call sites in the inline body. These call sites
13401 // often end up in non BBJ_RETURN blocks, so only flag them when
13402 // we're able to handle shared returns.
13403 if (impInlineInfo->iciCall->IsImplicitTailCall())
13405 JITDUMP(" (Inline Implicit Tail call: prefixFlags |= PREFIX_TAILCALL_IMPLICIT)");
13406 prefixFlags |= PREFIX_TAILCALL_IMPLICIT;
13408 #endif // FEATURE_TAILCALL_OPT_SHARED_RETURN
13412 JITDUMP(" (Implicit Tail call: prefixFlags |= PREFIX_TAILCALL_IMPLICIT)");
13413 prefixFlags |= PREFIX_TAILCALL_IMPLICIT;
13417 // Treat this call as tail call for verification only if "tail" prefixed (i.e. explicit tail call).
13418 explicitTailCall = (prefixFlags & PREFIX_TAILCALL_EXPLICIT) != 0;
13419 readonlyCall = (prefixFlags & PREFIX_READONLY) != 0;
13421 if (opcode != CEE_CALLI && opcode != CEE_NEWOBJ)
13423 // All calls and delegates need a security callout.
13424 // For delegates, this is the call to the delegate constructor, not the access check on the
13426 impHandleAccessAllowed(callInfo.accessAllowed, &callInfo.callsiteCalloutHelper);
13428 #if 0 // DevDiv 410397 - This breaks too many obfuscated apps to do this in an in-place release
13430 // DevDiv 291703 - we need to check for accessibility between the caller of InitializeArray
13431 // and the field it is reading, thus it is now unverifiable to not immediately precede with
13432 // ldtoken <filed token>, and we now check accessibility
13433 if ((callInfo.methodFlags & CORINFO_FLG_INTRINSIC) &&
13434 (info.compCompHnd->getIntrinsicID(callInfo.hMethod) == CORINFO_INTRINSIC_InitializeArray))
13436 if (prevOpcode != CEE_LDTOKEN)
13438 Verify(prevOpcode == CEE_LDTOKEN, "Need ldtoken for InitializeArray");
13442 assert(lastLoadToken != NULL);
13443 // Now that we know we have a token, verify that it is accessible for loading
13444 CORINFO_RESOLVED_TOKEN resolvedLoadField;
13445 impResolveToken(lastLoadToken, &resolvedLoadField, CORINFO_TOKENKIND_Field);
13446 eeGetFieldInfo(&resolvedLoadField, CORINFO_ACCESS_INIT_ARRAY, &fieldInfo);
13447 impHandleAccessAllowed(fieldInfo.accessAllowed, &fieldInfo.accessCalloutHelper);
13451 #endif // DevDiv 410397
13454 if (tiVerificationNeeded)
13456 verVerifyCall(opcode, &resolvedToken, constraintCall ? &constrainedResolvedToken : nullptr,
13457 explicitTailCall, readonlyCall, delegateCreateStart, codeAddr - 1,
13458 &callInfo DEBUGARG(info.compFullName));
13461 // Insert delegate callout here.
13462 if (opcode == CEE_NEWOBJ && (mflags & CORINFO_FLG_CONSTRUCTOR) && (clsFlags & CORINFO_FLG_DELEGATE))
13465 // We should do this only if verification is enabled
13466 // If verification is disabled, delegateCreateStart will not be initialized correctly
13467 if (tiVerificationNeeded)
13469 mdMemberRef delegateMethodRef = mdMemberRefNil;
13470 // We should get here only for well formed delegate creation.
13471 assert(verCheckDelegateCreation(delegateCreateStart, codeAddr - 1, delegateMethodRef));
13476 callTyp = impImportCall(opcode, &resolvedToken, constraintCall ? &constrainedResolvedToken : nullptr,
13477 newObjThisPtr, prefixFlags, &callInfo, opcodeOffs);
13478 if (compDonotInline())
13480 // We do not check fails after lvaGrabTemp. It is covered with CoreCLR_13272 issue.
13481 assert((callTyp == TYP_UNDEF) ||
13482 (compInlineResult->GetObservation() == InlineObservation::CALLSITE_TOO_MANY_LOCALS));
13486 if (explicitTailCall || newBBcreatedForTailcallStress) // If newBBcreatedForTailcallStress is true, we
13487 // have created a new BB after the "call"
13488 // instruction in fgMakeBasicBlocks(). So we need to jump to RET regardless.
13490 assert(!compIsForInlining());
13502 BOOL isLoadAddress = (opcode == CEE_LDFLDA || opcode == CEE_LDSFLDA);
13503 BOOL isLoadStatic = (opcode == CEE_LDSFLD || opcode == CEE_LDSFLDA);
13505 /* Get the CP_Fieldref index */
13506 assertImp(sz == sizeof(unsigned));
13508 _impResolveToken(CORINFO_TOKENKIND_Field);
13510 JITDUMP(" %08X", resolvedToken.token);
13512 int aflags = isLoadAddress ? CORINFO_ACCESS_ADDRESS : CORINFO_ACCESS_GET;
13514 GenTreePtr obj = nullptr;
13515 typeInfo* tiObj = nullptr;
13516 CORINFO_CLASS_HANDLE objType = nullptr; // used for fields
13518 if (opcode == CEE_LDFLD || opcode == CEE_LDFLDA)
13520 tiObj = &impStackTop().seTypeInfo;
13521 StackEntry se = impPopStack();
13522 objType = se.seTypeInfo.GetClassHandle();
13525 if (impIsThis(obj))
13527 aflags |= CORINFO_ACCESS_THIS;
13529 // An optimization for Contextful classes:
13530 // we unwrap the proxy when we have a 'this reference'
13532 if (info.compUnwrapContextful)
13534 aflags |= CORINFO_ACCESS_UNWRAP;
13539 eeGetFieldInfo(&resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo);
13541 // Figure out the type of the member. We always call canAccessField, so you always need this
13543 CorInfoType ciType = fieldInfo.fieldType;
13544 clsHnd = fieldInfo.structType;
13546 lclTyp = JITtype2varType(ciType);
13548 #ifdef _TARGET_AMD64
13549 noway_assert(varTypeIsIntegralOrI(lclTyp) || varTypeIsFloating(lclTyp) || lclTyp == TYP_STRUCT);
13550 #endif // _TARGET_AMD64
13552 if (compIsForInlining())
13554 switch (fieldInfo.fieldAccessor)
13556 case CORINFO_FIELD_INSTANCE_HELPER:
13557 case CORINFO_FIELD_INSTANCE_ADDR_HELPER:
13558 case CORINFO_FIELD_STATIC_ADDR_HELPER:
13559 case CORINFO_FIELD_STATIC_TLS:
13561 compInlineResult->NoteFatal(InlineObservation::CALLEE_LDFLD_NEEDS_HELPER);
13564 case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
13565 case CORINFO_FIELD_STATIC_READYTORUN_HELPER:
13566 /* We may be able to inline the field accessors in specific instantiations of generic
13568 compInlineResult->NoteFatal(InlineObservation::CALLSITE_LDFLD_NEEDS_HELPER);
13575 if (!isLoadAddress && (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC) && lclTyp == TYP_STRUCT &&
13578 if ((info.compCompHnd->getTypeForPrimitiveValueClass(clsHnd) == CORINFO_TYPE_UNDEF) &&
13579 !(info.compFlags & CORINFO_FLG_FORCEINLINE))
13581 // Loading a static valuetype field usually will cause a JitHelper to be called
13582 // for the static base. This will bloat the code.
13583 compInlineResult->Note(InlineObservation::CALLEE_LDFLD_STATIC_VALUECLASS);
13585 if (compInlineResult->IsFailure())
13593 tiRetVal = verMakeTypeInfo(ciType, clsHnd);
13596 tiRetVal.MakeByRef();
13600 tiRetVal.NormaliseForStack();
13603 // Perform this check always to ensure that we get field access exceptions even with
13604 // SkipVerification.
13605 impHandleAccessAllowed(fieldInfo.accessAllowed, &fieldInfo.accessCalloutHelper);
13607 if (tiVerificationNeeded)
13609 // You can also pass the unboxed struct to LDFLD
13610 BOOL bAllowPlainValueTypeAsThis = FALSE;
13611 if (opcode == CEE_LDFLD && impIsValueType(tiObj))
13613 bAllowPlainValueTypeAsThis = TRUE;
13616 verVerifyField(&resolvedToken, fieldInfo, tiObj, isLoadAddress, bAllowPlainValueTypeAsThis);
13618 // If we're doing this on a heap object or from a 'safe' byref
13619 // then the result is a safe byref too
13620 if (isLoadAddress) // load address
13622 if (fieldInfo.fieldFlags &
13623 CORINFO_FLG_FIELD_STATIC) // statics marked as safe will have permanent home
13625 if (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_SAFESTATIC_BYREF_RETURN)
13627 tiRetVal.SetIsPermanentHomeByRef();
13630 else if (tiObj->IsObjRef() || tiObj->IsPermanentHomeByRef())
13632 // ldflda of byref is safe if done on a gc object or on a
13634 tiRetVal.SetIsPermanentHomeByRef();
13640 // tiVerificationNeeded is false.
13641 // Raise InvalidProgramException if static load accesses non-static field
13642 if (isLoadStatic && ((fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC) == 0))
13644 BADCODE("static access on an instance field");
13648 // We are using ldfld/a on a static field. We allow it, but need to get side-effect from obj.
13649 if ((fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC) && obj != nullptr)
13651 if (obj->gtFlags & GTF_SIDE_EFFECT)
13653 obj = gtUnusedValNode(obj);
13654 impAppendTree(obj, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
13659 /* Preserve 'small' int types */
13660 if (!varTypeIsSmall(lclTyp))
13662 lclTyp = genActualType(lclTyp);
13665 bool usesHelper = false;
13667 switch (fieldInfo.fieldAccessor)
13669 case CORINFO_FIELD_INSTANCE:
13670 #ifdef FEATURE_READYTORUN_COMPILER
13671 case CORINFO_FIELD_INSTANCE_WITH_BASE:
13674 bool nullcheckNeeded = false;
13676 obj = impCheckForNullPointer(obj);
13678 if (isLoadAddress && (obj->gtType == TYP_BYREF) && fgAddrCouldBeNull(obj))
13680 nullcheckNeeded = true;
13683 // If the object is a struct, what we really want is
13684 // for the field to operate on the address of the struct.
13685 if (!varTypeGCtype(obj->TypeGet()) && impIsValueType(tiObj))
13687 assert(opcode == CEE_LDFLD && objType != nullptr);
13689 obj = impGetStructAddr(obj, objType, (unsigned)CHECK_SPILL_ALL, true);
13692 /* Create the data member node */
13693 op1 = gtNewFieldRef(lclTyp, resolvedToken.hField, obj, fieldInfo.offset, nullcheckNeeded);
13695 #ifdef FEATURE_READYTORUN_COMPILER
13696 if (fieldInfo.fieldAccessor == CORINFO_FIELD_INSTANCE_WITH_BASE)
13698 op1->gtField.gtFieldLookup = fieldInfo.fieldLookup;
13702 op1->gtFlags |= (obj->gtFlags & GTF_GLOB_EFFECT);
13704 if (fgAddrCouldBeNull(obj))
13706 op1->gtFlags |= GTF_EXCEPT;
13709 // If gtFldObj is a BYREF then our target is a value class and
13710 // it could point anywhere, example a boxed class static int
13711 if (obj->gtType == TYP_BYREF)
13713 op1->gtFlags |= GTF_IND_TGTANYWHERE;
13716 DWORD typeFlags = info.compCompHnd->getClassAttribs(resolvedToken.hClass);
13717 if (StructHasOverlappingFields(typeFlags))
13719 op1->gtField.gtFldMayOverlap = true;
13722 // wrap it in a address of operator if necessary
13725 op1 = gtNewOperNode(GT_ADDR,
13726 (var_types)(varTypeIsGC(obj->TypeGet()) ? TYP_BYREF : TYP_I_IMPL), op1);
13730 if (compIsForInlining() &&
13731 impInlineIsGuaranteedThisDerefBeforeAnySideEffects(nullptr, obj,
13732 impInlineInfo->inlArgInfo))
13734 impInlineInfo->thisDereferencedFirst = true;
13740 case CORINFO_FIELD_STATIC_TLS:
13741 #ifdef _TARGET_X86_
13742 // Legacy TLS access is implemented as intrinsic on x86 only
13744 /* Create the data member node */
13745 op1 = gtNewFieldRef(lclTyp, resolvedToken.hField, NULL, fieldInfo.offset);
13746 op1->gtFlags |= GTF_IND_TLS_REF; // fgMorphField will handle the transformation
13750 op1 = gtNewOperNode(GT_ADDR, (var_types)TYP_I_IMPL, op1);
13754 fieldInfo.fieldAccessor = CORINFO_FIELD_STATIC_ADDR_HELPER;
13759 case CORINFO_FIELD_STATIC_ADDR_HELPER:
13760 case CORINFO_FIELD_INSTANCE_HELPER:
13761 case CORINFO_FIELD_INSTANCE_ADDR_HELPER:
13762 op1 = gtNewRefCOMfield(obj, &resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo, lclTyp,
13767 case CORINFO_FIELD_STATIC_ADDRESS:
13768 // Replace static read-only fields with constant if possible
13769 if ((aflags & CORINFO_ACCESS_GET) && (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_FINAL) &&
13770 !(fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC_IN_HEAP) &&
13771 (varTypeIsIntegral(lclTyp) || varTypeIsFloating(lclTyp)))
13773 CorInfoInitClassResult initClassResult =
13774 info.compCompHnd->initClass(resolvedToken.hField, info.compMethodHnd,
13775 impTokenLookupContextHandle);
13777 if (initClassResult & CORINFO_INITCLASS_INITIALIZED)
13779 void** pFldAddr = nullptr;
13781 info.compCompHnd->getFieldAddress(resolvedToken.hField, (void**)&pFldAddr);
13783 // We should always be able to access this static's address directly
13784 assert(pFldAddr == nullptr);
13786 op1 = impImportStaticReadOnlyField(fldAddr, lclTyp);
13793 case CORINFO_FIELD_STATIC_RVA_ADDRESS:
13794 case CORINFO_FIELD_STATIC_SHARED_STATIC_HELPER:
13795 case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
13796 case CORINFO_FIELD_STATIC_READYTORUN_HELPER:
13797 op1 = impImportStaticFieldAccess(&resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo,
13801 case CORINFO_FIELD_INTRINSIC_ZERO:
13803 assert(aflags & CORINFO_ACCESS_GET);
13804 op1 = gtNewIconNode(0, lclTyp);
13809 case CORINFO_FIELD_INTRINSIC_EMPTY_STRING:
13811 assert(aflags & CORINFO_ACCESS_GET);
13814 InfoAccessType iat = info.compCompHnd->emptyStringLiteral(&pValue);
13815 op1 = gtNewStringLiteralNode(iat, pValue);
13820 case CORINFO_FIELD_INTRINSIC_ISLITTLEENDIAN:
13822 assert(aflags & CORINFO_ACCESS_GET);
13824 op1 = gtNewIconNode(0, lclTyp);
13826 op1 = gtNewIconNode(1, lclTyp);
13833 assert(!"Unexpected fieldAccessor");
13836 if (!isLoadAddress)
13839 if (prefixFlags & PREFIX_VOLATILE)
13841 op1->gtFlags |= GTF_DONT_CSE; // Can't CSE a volatile
13842 op1->gtFlags |= GTF_ORDER_SIDEEFF; // Prevent this from being reordered
13846 assert((op1->OperGet() == GT_FIELD) || (op1->OperGet() == GT_IND) ||
13847 (op1->OperGet() == GT_OBJ));
13848 op1->gtFlags |= GTF_IND_VOLATILE;
13852 if ((prefixFlags & PREFIX_UNALIGNED) && !varTypeIsByte(lclTyp))
13856 assert((op1->OperGet() == GT_FIELD) || (op1->OperGet() == GT_IND) ||
13857 (op1->OperGet() == GT_OBJ));
13858 op1->gtFlags |= GTF_IND_UNALIGNED;
13863 /* Check if the class needs explicit initialization */
13865 if (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_INITCLASS)
13867 GenTreePtr helperNode = impInitClass(&resolvedToken);
13868 if (compDonotInline())
13872 if (helperNode != nullptr)
13874 op1 = gtNewOperNode(GT_COMMA, op1->TypeGet(), helperNode, op1);
13879 impPushOnStack(op1, tiRetVal);
13887 BOOL isStoreStatic = (opcode == CEE_STSFLD);
13889 CORINFO_CLASS_HANDLE fieldClsHnd; // class of the field (if it's a ref type)
13891 /* Get the CP_Fieldref index */
13893 assertImp(sz == sizeof(unsigned));
13895 _impResolveToken(CORINFO_TOKENKIND_Field);
13897 JITDUMP(" %08X", resolvedToken.token);
13899 int aflags = CORINFO_ACCESS_SET;
13900 GenTreePtr obj = nullptr;
13901 typeInfo* tiObj = nullptr;
13904 /* Pull the value from the stack */
13905 StackEntry se = impPopStack();
13907 tiVal = se.seTypeInfo;
13908 clsHnd = tiVal.GetClassHandle();
13910 if (opcode == CEE_STFLD)
13912 tiObj = &impStackTop().seTypeInfo;
13913 obj = impPopStack().val;
13915 if (impIsThis(obj))
13917 aflags |= CORINFO_ACCESS_THIS;
13919 // An optimization for Contextful classes:
13920 // we unwrap the proxy when we have a 'this reference'
13922 if (info.compUnwrapContextful)
13924 aflags |= CORINFO_ACCESS_UNWRAP;
13929 eeGetFieldInfo(&resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo);
13931 // Figure out the type of the member. We always call canAccessField, so you always need this
13933 CorInfoType ciType = fieldInfo.fieldType;
13934 fieldClsHnd = fieldInfo.structType;
13936 lclTyp = JITtype2varType(ciType);
13938 if (compIsForInlining())
13940 /* Is this a 'special' (COM) field? or a TLS ref static field?, field stored int GC heap? or
13941 * per-inst static? */
13943 switch (fieldInfo.fieldAccessor)
13945 case CORINFO_FIELD_INSTANCE_HELPER:
13946 case CORINFO_FIELD_INSTANCE_ADDR_HELPER:
13947 case CORINFO_FIELD_STATIC_ADDR_HELPER:
13948 case CORINFO_FIELD_STATIC_TLS:
13950 compInlineResult->NoteFatal(InlineObservation::CALLEE_STFLD_NEEDS_HELPER);
13953 case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
13954 case CORINFO_FIELD_STATIC_READYTORUN_HELPER:
13955 /* We may be able to inline the field accessors in specific instantiations of generic
13957 compInlineResult->NoteFatal(InlineObservation::CALLSITE_STFLD_NEEDS_HELPER);
13965 impHandleAccessAllowed(fieldInfo.accessAllowed, &fieldInfo.accessCalloutHelper);
13967 if (tiVerificationNeeded)
13969 verVerifyField(&resolvedToken, fieldInfo, tiObj, TRUE);
13970 typeInfo fieldType = verMakeTypeInfo(ciType, fieldClsHnd);
13971 Verify(tiCompatibleWith(tiVal, fieldType.NormaliseForStack(), true), "type mismatch");
13975 // tiVerificationNeed is false.
13976 // Raise InvalidProgramException if static store accesses non-static field
13977 if (isStoreStatic && ((fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC) == 0))
13979 BADCODE("static access on an instance field");
13983 // We are using stfld on a static field.
13984 // We allow it, but need to eval any side-effects for obj
13985 if ((fieldInfo.fieldFlags & CORINFO_FLG_FIELD_STATIC) && obj != nullptr)
13987 if (obj->gtFlags & GTF_SIDE_EFFECT)
13989 obj = gtUnusedValNode(obj);
13990 impAppendTree(obj, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
13995 /* Preserve 'small' int types */
13996 if (!varTypeIsSmall(lclTyp))
13998 lclTyp = genActualType(lclTyp);
14001 switch (fieldInfo.fieldAccessor)
14003 case CORINFO_FIELD_INSTANCE:
14004 #ifdef FEATURE_READYTORUN_COMPILER
14005 case CORINFO_FIELD_INSTANCE_WITH_BASE:
14008 obj = impCheckForNullPointer(obj);
14010 /* Create the data member node */
14011 op1 = gtNewFieldRef(lclTyp, resolvedToken.hField, obj, fieldInfo.offset);
14012 DWORD typeFlags = info.compCompHnd->getClassAttribs(resolvedToken.hClass);
14013 if (StructHasOverlappingFields(typeFlags))
14015 op1->gtField.gtFldMayOverlap = true;
14018 #ifdef FEATURE_READYTORUN_COMPILER
14019 if (fieldInfo.fieldAccessor == CORINFO_FIELD_INSTANCE_WITH_BASE)
14021 op1->gtField.gtFieldLookup = fieldInfo.fieldLookup;
14025 op1->gtFlags |= (obj->gtFlags & GTF_GLOB_EFFECT);
14027 if (fgAddrCouldBeNull(obj))
14029 op1->gtFlags |= GTF_EXCEPT;
14032 // If gtFldObj is a BYREF then our target is a value class and
14033 // it could point anywhere, example a boxed class static int
14034 if (obj->gtType == TYP_BYREF)
14036 op1->gtFlags |= GTF_IND_TGTANYWHERE;
14039 if (compIsForInlining() &&
14040 impInlineIsGuaranteedThisDerefBeforeAnySideEffects(op2, obj, impInlineInfo->inlArgInfo))
14042 impInlineInfo->thisDereferencedFirst = true;
14047 case CORINFO_FIELD_STATIC_TLS:
14048 #ifdef _TARGET_X86_
14049 // Legacy TLS access is implemented as intrinsic on x86 only
14051 /* Create the data member node */
14052 op1 = gtNewFieldRef(lclTyp, resolvedToken.hField, NULL, fieldInfo.offset);
14053 op1->gtFlags |= GTF_IND_TLS_REF; // fgMorphField will handle the transformation
14057 fieldInfo.fieldAccessor = CORINFO_FIELD_STATIC_ADDR_HELPER;
14062 case CORINFO_FIELD_STATIC_ADDR_HELPER:
14063 case CORINFO_FIELD_INSTANCE_HELPER:
14064 case CORINFO_FIELD_INSTANCE_ADDR_HELPER:
14065 op1 = gtNewRefCOMfield(obj, &resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo, lclTyp,
14069 case CORINFO_FIELD_STATIC_ADDRESS:
14070 case CORINFO_FIELD_STATIC_RVA_ADDRESS:
14071 case CORINFO_FIELD_STATIC_SHARED_STATIC_HELPER:
14072 case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
14073 case CORINFO_FIELD_STATIC_READYTORUN_HELPER:
14074 op1 = impImportStaticFieldAccess(&resolvedToken, (CORINFO_ACCESS_FLAGS)aflags, &fieldInfo,
14079 assert(!"Unexpected fieldAccessor");
14082 // Create the member assignment, unless we have a struct.
14083 // TODO-1stClassStructs: This could be limited to TYP_STRUCT, to avoid extra copies.
14084 bool deferStructAssign = varTypeIsStruct(lclTyp);
14086 if (!deferStructAssign)
14088 if (prefixFlags & PREFIX_VOLATILE)
14090 assert((op1->OperGet() == GT_FIELD) || (op1->OperGet() == GT_IND));
14091 op1->gtFlags |= GTF_DONT_CSE; // Can't CSE a volatile
14092 op1->gtFlags |= GTF_ORDER_SIDEEFF; // Prevent this from being reordered
14093 op1->gtFlags |= GTF_IND_VOLATILE;
14095 if ((prefixFlags & PREFIX_UNALIGNED) && !varTypeIsByte(lclTyp))
14097 assert((op1->OperGet() == GT_FIELD) || (op1->OperGet() == GT_IND));
14098 op1->gtFlags |= GTF_IND_UNALIGNED;
14101 /* V4.0 allows assignment of i4 constant values to i8 type vars when IL verifier is bypassed (full
14102 trust apps). The reason this works is that JIT stores an i4 constant in Gentree union during
14103 importation and reads from the union as if it were a long during code generation. Though this
14104 can potentially read garbage, one can get lucky to have this working correctly.
14106 This code pattern is generated by Dev10 MC++ compiler while storing to fields when compiled with
14107 /O2 switch (default when compiling retail configs in Dev10) and a customer app has taken a
14108 dependency on it. To be backward compatible, we will explicitly add an upward cast here so that
14109 it works correctly always.
14111 Note that this is limited to x86 alone as there is no back compat to be addressed for Arm JIT
14114 CLANG_FORMAT_COMMENT_ANCHOR;
14116 #ifndef _TARGET_64BIT_
14117 // In UWP6.0 and beyond (post-.NET Core 2.0), we decided to let this cast from int to long be
14118 // generated for ARM as well as x86, so the following IR will be accepted:
14120 // | /--* CNS_INT int 2
14122 // \--* CLS_VAR long
14124 if ((op1->TypeGet() != op2->TypeGet()) && op2->OperIsConst() && varTypeIsIntOrI(op2->TypeGet()) &&
14125 varTypeIsLong(op1->TypeGet()))
14127 op2 = gtNewCastNode(op1->TypeGet(), op2, op1->TypeGet());
14131 #ifdef _TARGET_64BIT_
14132 // Automatic upcast for a GT_CNS_INT into TYP_I_IMPL
14133 if ((op2->OperGet() == GT_CNS_INT) && varTypeIsI(lclTyp) && !varTypeIsI(op2->gtType))
14135 op2->gtType = TYP_I_IMPL;
14139 // Allow a downcast of op2 from TYP_I_IMPL into a 32-bit Int for x86 JIT compatiblity
14141 if (varTypeIsI(op2->gtType) && (genActualType(lclTyp) == TYP_INT))
14143 op2 = gtNewCastNode(TYP_INT, op2, TYP_INT);
14145 // Allow an upcast of op2 from a 32-bit Int into TYP_I_IMPL for x86 JIT compatiblity
14147 if (varTypeIsI(lclTyp) && (genActualType(op2->gtType) == TYP_INT))
14149 op2 = gtNewCastNode(TYP_I_IMPL, op2, TYP_I_IMPL);
14154 #if !FEATURE_X87_DOUBLES
14155 // We can generate an assignment to a TYP_FLOAT from a TYP_DOUBLE
14156 // We insert a cast to the dest 'op1' type
14158 if ((op1->TypeGet() != op2->TypeGet()) && varTypeIsFloating(op1->gtType) &&
14159 varTypeIsFloating(op2->gtType))
14161 op2 = gtNewCastNode(op1->TypeGet(), op2, op1->TypeGet());
14163 #endif // !FEATURE_X87_DOUBLES
14165 op1 = gtNewAssignNode(op1, op2);
14167 /* Mark the expression as containing an assignment */
14169 op1->gtFlags |= GTF_ASG;
14172 /* Check if the class needs explicit initialization */
14174 if (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_INITCLASS)
14176 GenTreePtr helperNode = impInitClass(&resolvedToken);
14177 if (compDonotInline())
14181 if (helperNode != nullptr)
14183 op1 = gtNewOperNode(GT_COMMA, op1->TypeGet(), helperNode, op1);
14187 /* stfld can interfere with value classes (consider the sequence
14188 ldloc, ldloca, ..., stfld, stloc). We will be conservative and
14189 spill all value class references from the stack. */
14191 if (obj && ((obj->gtType == TYP_BYREF) || (obj->gtType == TYP_I_IMPL)))
14195 if (impIsValueType(tiObj))
14197 impSpillEvalStack();
14201 impSpillValueClasses();
14205 /* Spill any refs to the same member from the stack */
14207 impSpillLclRefs((ssize_t)resolvedToken.hField);
14209 /* stsfld also interferes with indirect accesses (for aliased
14210 statics) and calls. But don't need to spill other statics
14211 as we have explicitly spilled this particular static field. */
14213 impSpillSideEffects(false, (unsigned)CHECK_SPILL_ALL DEBUGARG("spill side effects before STFLD"));
14215 if (deferStructAssign)
14217 op1 = impAssignStruct(op1, op2, clsHnd, (unsigned)CHECK_SPILL_ALL);
14225 /* Get the class type index operand */
14227 _impResolveToken(CORINFO_TOKENKIND_Newarr);
14229 JITDUMP(" %08X", resolvedToken.token);
14231 if (!opts.IsReadyToRun())
14233 // Need to restore array classes before creating array objects on the heap
14234 op1 = impTokenToHandle(&resolvedToken, nullptr, TRUE /*mustRestoreHandle*/);
14235 if (op1 == nullptr)
14236 { // compDonotInline()
14241 if (tiVerificationNeeded)
14243 // As per ECMA 'numElems' specified can be either int32 or native int.
14244 Verify(impStackTop().seTypeInfo.IsIntOrNativeIntType(), "bad bound");
14246 CORINFO_CLASS_HANDLE elemTypeHnd;
14247 info.compCompHnd->getChildType(resolvedToken.hClass, &elemTypeHnd);
14248 Verify(elemTypeHnd == nullptr ||
14249 !(info.compCompHnd->getClassAttribs(elemTypeHnd) & CORINFO_FLG_CONTAINS_STACK_PTR),
14250 "array of byref-like type");
14253 tiRetVal = verMakeTypeInfo(resolvedToken.hClass);
14255 accessAllowedResult =
14256 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
14257 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
14259 /* Form the arglist: array class handle, size */
14260 op2 = impPopStack().val;
14261 assertImp(genActualTypeIsIntOrI(op2->gtType));
14263 #ifdef FEATURE_READYTORUN_COMPILER
14264 if (opts.IsReadyToRun())
14266 op1 = impReadyToRunHelperToTree(&resolvedToken, CORINFO_HELP_READYTORUN_NEWARR_1, TYP_REF,
14267 gtNewArgList(op2));
14268 usingReadyToRunHelper = (op1 != nullptr);
14270 if (!usingReadyToRunHelper)
14272 // TODO: ReadyToRun: When generic dictionary lookups are necessary, replace the lookup call
14273 // and the newarr call with a single call to a dynamic R2R cell that will:
14274 // 1) Load the context
14275 // 2) Perform the generic dictionary lookup and caching, and generate the appropriate stub
14276 // 3) Allocate the new array
14277 // Reason: performance (today, we'll always use the slow helper for the R2R generics case)
14279 // Need to restore array classes before creating array objects on the heap
14280 op1 = impTokenToHandle(&resolvedToken, nullptr, TRUE /*mustRestoreHandle*/);
14281 if (op1 == nullptr)
14282 { // compDonotInline()
14288 if (!usingReadyToRunHelper)
14291 args = gtNewArgList(op1, op2);
14293 /* Create a call to 'new' */
14295 // Note that this only works for shared generic code because the same helper is used for all
14296 // reference array types
14297 op1 = gtNewHelperCallNode(info.compCompHnd->getNewArrHelper(resolvedToken.hClass), TYP_REF, args);
14300 op1->gtCall.compileTimeHelperArgumentHandle = (CORINFO_GENERIC_HANDLE)resolvedToken.hClass;
14302 /* Remember that this basic block contains 'new' of an sd array */
14304 block->bbFlags |= BBF_HAS_NEWARRAY;
14305 optMethodFlags |= OMF_HAS_NEWARRAY;
14307 /* Push the result of the call on the stack */
14309 impPushOnStack(op1, tiRetVal);
14316 if (tiVerificationNeeded)
14318 Verify(false, "bad opcode");
14321 // We don't allow locallocs inside handlers
14322 if (block->hasHndIndex())
14324 BADCODE("Localloc can't be inside handler");
14327 setNeedsGSSecurityCookie();
14329 // Get the size to allocate
14331 op2 = impPopStack().val;
14332 assertImp(genActualTypeIsIntOrI(op2->gtType));
14334 if (verCurrentState.esStackDepth != 0)
14336 BADCODE("Localloc can only be used when the stack is empty");
14339 // If the localloc is not in a loop and its size is a small constant,
14340 // create a new local var of TYP_BLK and return its address.
14342 bool convertedToLocal = false;
14344 // Need to aggressively fold here, as even fixed-size locallocs
14345 // will have casts in the way.
14346 op2 = gtFoldExpr(op2);
14348 if (op2->IsIntegralConst())
14350 const ssize_t allocSize = op2->AsIntCon()->IconValue();
14352 if (allocSize == 0)
14354 // Result is nullptr
14355 JITDUMP("Converting stackalloc of 0 bytes to push null unmanaged pointer\n");
14356 op1 = gtNewIconNode(0, TYP_I_IMPL);
14357 convertedToLocal = true;
14359 else if ((allocSize > 0) && ((compCurBB->bbFlags & BBF_BACKWARD_JUMP) == 0))
14361 // Get the size threshold for local conversion
14362 ssize_t maxSize = DEFAULT_MAX_LOCALLOC_TO_LOCAL_SIZE;
14365 // Optionally allow this to be modified
14366 maxSize = JitConfig.JitStackAllocToLocalSize();
14369 if (allocSize <= maxSize)
14371 const unsigned stackallocAsLocal = lvaGrabTemp(false DEBUGARG("stackallocLocal"));
14372 JITDUMP("Converting stackalloc of %lld bytes to new local V%02u\n", allocSize,
14373 stackallocAsLocal);
14374 lvaTable[stackallocAsLocal].lvType = TYP_BLK;
14375 lvaTable[stackallocAsLocal].lvExactSize = (unsigned)allocSize;
14376 lvaTable[stackallocAsLocal].lvIsUnsafeBuffer = true;
14377 op1 = gtNewLclvNode(stackallocAsLocal, TYP_BLK);
14378 op1 = gtNewOperNode(GT_ADDR, TYP_I_IMPL, op1);
14379 convertedToLocal = true;
14380 compGSReorderStackLayout = true;
14385 if (!convertedToLocal)
14387 // Bail out if inlining and the localloc was not converted.
14389 // Note we might consider allowing the inline, if the call
14390 // site is not in a loop.
14391 if (compIsForInlining())
14393 InlineObservation obs = op2->IsIntegralConst()
14394 ? InlineObservation::CALLEE_LOCALLOC_TOO_LARGE
14395 : InlineObservation::CALLSITE_LOCALLOC_SIZE_UNKNOWN;
14396 compInlineResult->NoteFatal(obs);
14400 op1 = gtNewOperNode(GT_LCLHEAP, TYP_I_IMPL, op2);
14401 // May throw a stack overflow exception. Obviously, we don't want locallocs to be CSE'd.
14402 op1->gtFlags |= (GTF_EXCEPT | GTF_DONT_CSE);
14404 /* The FP register may not be back to the original value at the end
14405 of the method, even if the frame size is 0, as localloc may
14406 have modified it. So we will HAVE to reset it */
14407 compLocallocUsed = true;
14411 compLocallocOptimized = true;
14415 impPushOnStack(op1, tiRetVal);
14420 /* Get the type token */
14421 assertImp(sz == sizeof(unsigned));
14423 _impResolveToken(CORINFO_TOKENKIND_Casting);
14425 JITDUMP(" %08X", resolvedToken.token);
14427 if (!opts.IsReadyToRun())
14429 op2 = impTokenToHandle(&resolvedToken, nullptr, FALSE);
14430 if (op2 == nullptr)
14431 { // compDonotInline()
14436 if (tiVerificationNeeded)
14438 Verify(impStackTop().seTypeInfo.IsObjRef(), "obj reference needed");
14439 // Even if this is a value class, we know it is boxed.
14440 tiRetVal = typeInfo(TI_REF, resolvedToken.hClass);
14442 accessAllowedResult =
14443 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
14444 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
14446 op1 = impPopStack().val;
14448 GenTree* optTree = impOptimizeCastClassOrIsInst(op1, &resolvedToken, false);
14450 if (optTree != nullptr)
14452 impPushOnStack(optTree, tiRetVal);
14457 #ifdef FEATURE_READYTORUN_COMPILER
14458 if (opts.IsReadyToRun())
14460 GenTreeCall* opLookup =
14461 impReadyToRunHelperToTree(&resolvedToken, CORINFO_HELP_READYTORUN_ISINSTANCEOF, TYP_REF,
14462 gtNewArgList(op1));
14463 usingReadyToRunHelper = (opLookup != nullptr);
14464 op1 = (usingReadyToRunHelper ? opLookup : op1);
14466 if (!usingReadyToRunHelper)
14468 // TODO: ReadyToRun: When generic dictionary lookups are necessary, replace the lookup call
14469 // and the isinstanceof_any call with a single call to a dynamic R2R cell that will:
14470 // 1) Load the context
14471 // 2) Perform the generic dictionary lookup and caching, and generate the appropriate
14473 // 3) Perform the 'is instance' check on the input object
14474 // Reason: performance (today, we'll always use the slow helper for the R2R generics case)
14476 op2 = impTokenToHandle(&resolvedToken, nullptr, FALSE);
14477 if (op2 == nullptr)
14478 { // compDonotInline()
14484 if (!usingReadyToRunHelper)
14487 op1 = impCastClassOrIsInstToTree(op1, op2, &resolvedToken, false);
14489 if (compDonotInline())
14494 impPushOnStack(op1, tiRetVal);
14499 case CEE_REFANYVAL:
14501 // get the class handle and make a ICON node out of it
14503 _impResolveToken(CORINFO_TOKENKIND_Class);
14505 JITDUMP(" %08X", resolvedToken.token);
14507 op2 = impTokenToHandle(&resolvedToken);
14508 if (op2 == nullptr)
14509 { // compDonotInline()
14513 if (tiVerificationNeeded)
14515 Verify(typeInfo::AreEquivalent(impStackTop().seTypeInfo, verMakeTypeInfo(impGetRefAnyClass())),
14517 tiRetVal = verMakeTypeInfo(resolvedToken.hClass).MakeByRef();
14520 op1 = impPopStack().val;
14521 // make certain it is normalized;
14522 op1 = impNormStructVal(op1, impGetRefAnyClass(), (unsigned)CHECK_SPILL_ALL);
14524 // Call helper GETREFANY(classHandle, op1);
14525 args = gtNewArgList(op2, op1);
14526 op1 = gtNewHelperCallNode(CORINFO_HELP_GETREFANY, TYP_BYREF, args);
14528 impPushOnStack(op1, tiRetVal);
14531 case CEE_REFANYTYPE:
14533 if (tiVerificationNeeded)
14535 Verify(typeInfo::AreEquivalent(impStackTop().seTypeInfo, verMakeTypeInfo(impGetRefAnyClass())),
14539 op1 = impPopStack().val;
14541 // make certain it is normalized;
14542 op1 = impNormStructVal(op1, impGetRefAnyClass(), (unsigned)CHECK_SPILL_ALL);
14544 if (op1->gtOper == GT_OBJ)
14546 // Get the address of the refany
14547 op1 = op1->gtOp.gtOp1;
14549 // Fetch the type from the correct slot
14550 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1,
14551 gtNewIconNode(offsetof(CORINFO_RefAny, type), TYP_I_IMPL));
14552 op1 = gtNewOperNode(GT_IND, TYP_BYREF, op1);
14556 assertImp(op1->gtOper == GT_MKREFANY);
14558 // The pointer may have side-effects
14559 if (op1->gtOp.gtOp1->gtFlags & GTF_SIDE_EFFECT)
14561 impAppendTree(op1->gtOp.gtOp1, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
14563 impNoteLastILoffs();
14567 // We already have the class handle
14568 op1 = op1->gtOp.gtOp2;
14571 // convert native TypeHandle to RuntimeTypeHandle
14573 GenTreeArgList* helperArgs = gtNewArgList(op1);
14575 op1 = gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE_MAYBENULL, TYP_STRUCT, helperArgs);
14577 // The handle struct is returned in register
14578 op1->gtCall.gtReturnType = GetRuntimeHandleUnderlyingType();
14580 tiRetVal = typeInfo(TI_STRUCT, impGetTypeHandleClass());
14583 impPushOnStack(op1, tiRetVal);
14588 /* Get the Class index */
14589 assertImp(sz == sizeof(unsigned));
14590 lastLoadToken = codeAddr;
14591 _impResolveToken(CORINFO_TOKENKIND_Ldtoken);
14593 tokenType = info.compCompHnd->getTokenTypeAsHandle(&resolvedToken);
14595 op1 = impTokenToHandle(&resolvedToken, nullptr, TRUE);
14596 if (op1 == nullptr)
14597 { // compDonotInline()
14601 helper = CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE;
14602 assert(resolvedToken.hClass != nullptr);
14604 if (resolvedToken.hMethod != nullptr)
14606 helper = CORINFO_HELP_METHODDESC_TO_STUBRUNTIMEMETHOD;
14608 else if (resolvedToken.hField != nullptr)
14610 helper = CORINFO_HELP_FIELDDESC_TO_STUBRUNTIMEFIELD;
14613 GenTreeArgList* helperArgs = gtNewArgList(op1);
14615 op1 = gtNewHelperCallNode(helper, TYP_STRUCT, helperArgs);
14617 // The handle struct is returned in register
14618 op1->gtCall.gtReturnType = GetRuntimeHandleUnderlyingType();
14620 tiRetVal = verMakeTypeInfo(tokenType);
14621 impPushOnStack(op1, tiRetVal);
14626 case CEE_UNBOX_ANY:
14628 /* Get the Class index */
14629 assertImp(sz == sizeof(unsigned));
14631 _impResolveToken(CORINFO_TOKENKIND_Class);
14633 JITDUMP(" %08X", resolvedToken.token);
14635 BOOL runtimeLookup;
14636 op2 = impTokenToHandle(&resolvedToken, &runtimeLookup);
14637 if (op2 == nullptr)
14639 assert(compDonotInline());
14643 // Run this always so we can get access exceptions even with SkipVerification.
14644 accessAllowedResult =
14645 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
14646 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
14648 if (opcode == CEE_UNBOX_ANY && !eeIsValueClass(resolvedToken.hClass))
14650 if (tiVerificationNeeded)
14652 typeInfo tiUnbox = impStackTop().seTypeInfo;
14653 Verify(tiUnbox.IsObjRef(), "bad unbox.any arg");
14654 tiRetVal = verMakeTypeInfo(resolvedToken.hClass);
14655 tiRetVal.NormaliseForStack();
14657 JITDUMP("\n Importing UNBOX.ANY(refClass) as CASTCLASS\n");
14658 op1 = impPopStack().val;
14662 /* Pop the object and create the unbox helper call */
14663 /* You might think that for UNBOX_ANY we need to push a different */
14664 /* (non-byref) type, but here we're making the tiRetVal that is used */
14665 /* for the intermediate pointer which we then transfer onto the OBJ */
14666 /* instruction. OBJ then creates the appropriate tiRetVal. */
14667 if (tiVerificationNeeded)
14669 typeInfo tiUnbox = impStackTop().seTypeInfo;
14670 Verify(tiUnbox.IsObjRef(), "Bad unbox arg");
14672 tiRetVal = verMakeTypeInfo(resolvedToken.hClass);
14673 Verify(tiRetVal.IsValueClass(), "not value class");
14674 tiRetVal.MakeByRef();
14676 // We always come from an objref, so this is safe byref
14677 tiRetVal.SetIsPermanentHomeByRef();
14678 tiRetVal.SetIsReadonlyByRef();
14681 op1 = impPopStack().val;
14682 assertImp(op1->gtType == TYP_REF);
14684 helper = info.compCompHnd->getUnBoxHelper(resolvedToken.hClass);
14685 assert(helper == CORINFO_HELP_UNBOX || helper == CORINFO_HELP_UNBOX_NULLABLE);
14687 // Check legality and profitability of inline expansion for unboxing.
14688 const bool canExpandInline = (helper == CORINFO_HELP_UNBOX);
14689 const bool shouldExpandInline = !(compCurBB->isRunRarely() || opts.compDbgCode || opts.MinOpts());
14691 if (canExpandInline && shouldExpandInline)
14693 JITDUMP("\n Importing %s as inline sequence\n", opcode == CEE_UNBOX ? "UNBOX" : "UNBOX.ANY");
14694 // we are doing normal unboxing
14695 // inline the common case of the unbox helper
14696 // UNBOX(exp) morphs into
14697 // clone = pop(exp);
14698 // ((*clone == typeToken) ? nop : helper(clone, typeToken));
14699 // push(clone + TARGET_POINTER_SIZE)
14701 GenTreePtr cloneOperand;
14702 op1 = impCloneExpr(op1, &cloneOperand, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
14703 nullptr DEBUGARG("inline UNBOX clone1"));
14704 op1 = gtNewOperNode(GT_IND, TYP_I_IMPL, op1);
14706 GenTreePtr condBox = gtNewOperNode(GT_EQ, TYP_INT, op1, op2);
14708 op1 = impCloneExpr(cloneOperand, &cloneOperand, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL,
14709 nullptr DEBUGARG("inline UNBOX clone2"));
14710 op2 = impTokenToHandle(&resolvedToken);
14711 if (op2 == nullptr)
14712 { // compDonotInline()
14715 args = gtNewArgList(op2, op1);
14716 op1 = gtNewHelperCallNode(helper, TYP_VOID, args);
14718 op1 = new (this, GT_COLON) GenTreeColon(TYP_VOID, gtNewNothingNode(), op1);
14719 op1 = gtNewQmarkNode(TYP_VOID, condBox, op1);
14720 condBox->gtFlags |= GTF_RELOP_QMARK;
14722 // QMARK nodes cannot reside on the evaluation stack. Because there
14723 // may be other trees on the evaluation stack that side-effect the
14724 // sources of the UNBOX operation we must spill the stack.
14726 impAppendTree(op1, (unsigned)CHECK_SPILL_ALL, impCurStmtOffs);
14728 // Create the address-expression to reference past the object header
14729 // to the beginning of the value-type. Today this means adjusting
14730 // past the base of the objects vtable field which is pointer sized.
14732 op2 = gtNewIconNode(TARGET_POINTER_SIZE, TYP_I_IMPL);
14733 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, cloneOperand, op2);
14737 JITDUMP("\n Importing %s as helper call because %s\n", opcode == CEE_UNBOX ? "UNBOX" : "UNBOX.ANY",
14738 canExpandInline ? "want smaller code or faster jitting" : "inline expansion not legal");
14740 // Don't optimize, just call the helper and be done with it
14741 args = gtNewArgList(op2, op1);
14743 gtNewHelperCallNode(helper,
14744 (var_types)((helper == CORINFO_HELP_UNBOX) ? TYP_BYREF : TYP_STRUCT), args);
14747 assert(helper == CORINFO_HELP_UNBOX && op1->gtType == TYP_BYREF || // Unbox helper returns a byref.
14748 helper == CORINFO_HELP_UNBOX_NULLABLE &&
14749 varTypeIsStruct(op1) // UnboxNullable helper returns a struct.
14753 ----------------------------------------------------------------------
14756 | \ | CORINFO_HELP_UNBOX | CORINFO_HELP_UNBOX_NULLABLE |
14757 | \ | (which returns a BYREF) | (which returns a STRUCT) | |
14759 |---------------------------------------------------------------------
14760 | UNBOX | push the BYREF | spill the STRUCT to a local, |
14761 | | | push the BYREF to this local |
14762 |---------------------------------------------------------------------
14763 | UNBOX_ANY | push a GT_OBJ of | push the STRUCT |
14764 | | the BYREF | For Linux when the |
14765 | | | struct is returned in two |
14766 | | | registers create a temp |
14767 | | | which address is passed to |
14768 | | | the unbox_nullable helper. |
14769 |---------------------------------------------------------------------
14772 if (opcode == CEE_UNBOX)
14774 if (helper == CORINFO_HELP_UNBOX_NULLABLE)
14776 // Unbox nullable helper returns a struct type.
14777 // We need to spill it to a temp so than can take the address of it.
14778 // Here we need unsafe value cls check, since the address of struct is taken to be used
14779 // further along and potetially be exploitable.
14781 unsigned tmp = lvaGrabTemp(true DEBUGARG("UNBOXing a nullable"));
14782 lvaSetStruct(tmp, resolvedToken.hClass, true /* unsafe value cls check */);
14784 op2 = gtNewLclvNode(tmp, TYP_STRUCT);
14785 op1 = impAssignStruct(op2, op1, resolvedToken.hClass, (unsigned)CHECK_SPILL_ALL);
14786 assert(op1->gtType == TYP_VOID); // We must be assigning the return struct to the temp.
14788 op2 = gtNewLclvNode(tmp, TYP_STRUCT);
14789 op2 = gtNewOperNode(GT_ADDR, TYP_BYREF, op2);
14790 op1 = gtNewOperNode(GT_COMMA, TYP_BYREF, op1, op2);
14793 assert(op1->gtType == TYP_BYREF);
14794 assert(!tiVerificationNeeded || tiRetVal.IsByRef());
14798 assert(opcode == CEE_UNBOX_ANY);
14800 if (helper == CORINFO_HELP_UNBOX)
14802 // Normal unbox helper returns a TYP_BYREF.
14803 impPushOnStack(op1, tiRetVal);
14808 assert(helper == CORINFO_HELP_UNBOX_NULLABLE && "Make sure the helper is nullable!");
14810 #if FEATURE_MULTIREG_RET
14812 if (varTypeIsStruct(op1) && IsMultiRegReturnedType(resolvedToken.hClass))
14814 // Unbox nullable helper returns a TYP_STRUCT.
14815 // For the multi-reg case we need to spill it to a temp so that
14816 // we can pass the address to the unbox_nullable jit helper.
14818 unsigned tmp = lvaGrabTemp(true DEBUGARG("UNBOXing a register returnable nullable"));
14819 lvaTable[tmp].lvIsMultiRegArg = true;
14820 lvaSetStruct(tmp, resolvedToken.hClass, true /* unsafe value cls check */);
14822 op2 = gtNewLclvNode(tmp, TYP_STRUCT);
14823 op1 = impAssignStruct(op2, op1, resolvedToken.hClass, (unsigned)CHECK_SPILL_ALL);
14824 assert(op1->gtType == TYP_VOID); // We must be assigning the return struct to the temp.
14826 op2 = gtNewLclvNode(tmp, TYP_STRUCT);
14827 op2 = gtNewOperNode(GT_ADDR, TYP_BYREF, op2);
14828 op1 = gtNewOperNode(GT_COMMA, TYP_BYREF, op1, op2);
14830 // In this case the return value of the unbox helper is TYP_BYREF.
14831 // Make sure the right type is placed on the operand type stack.
14832 impPushOnStack(op1, tiRetVal);
14834 // Load the struct.
14837 assert(op1->gtType == TYP_BYREF);
14838 assert(!tiVerificationNeeded || tiRetVal.IsByRef());
14844 #endif // !FEATURE_MULTIREG_RET
14847 // If non register passable struct we have it materialized in the RetBuf.
14848 assert(op1->gtType == TYP_STRUCT);
14849 tiRetVal = verMakeTypeInfo(resolvedToken.hClass);
14850 assert(tiRetVal.IsValueClass());
14854 impPushOnStack(op1, tiRetVal);
14860 /* Get the Class index */
14861 assertImp(sz == sizeof(unsigned));
14863 _impResolveToken(CORINFO_TOKENKIND_Box);
14865 JITDUMP(" %08X", resolvedToken.token);
14867 if (tiVerificationNeeded)
14869 typeInfo tiActual = impStackTop().seTypeInfo;
14870 typeInfo tiBox = verMakeTypeInfo(resolvedToken.hClass);
14872 Verify(verIsBoxable(tiBox), "boxable type expected");
14874 // check the class constraints of the boxed type in case we are boxing an uninitialized value
14875 Verify(info.compCompHnd->satisfiesClassConstraints(resolvedToken.hClass),
14876 "boxed type has unsatisfied class constraints");
14878 Verify(tiCompatibleWith(tiActual, tiBox.NormaliseForStack(), true), "type mismatch");
14880 // Observation: the following code introduces a boxed value class on the stack, but,
14881 // according to the ECMA spec, one would simply expect: tiRetVal =
14882 // typeInfo(TI_REF,impGetObjectClass());
14884 // Push the result back on the stack,
14885 // even if clsHnd is a value class we want the TI_REF
14886 // we call back to the EE to get find out what hte type we should push (for nullable<T> we push T)
14887 tiRetVal = typeInfo(TI_REF, info.compCompHnd->getTypeForBox(resolvedToken.hClass));
14890 accessAllowedResult =
14891 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
14892 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
14894 // Note BOX can be used on things that are not value classes, in which
14895 // case we get a NOP. However the verifier's view of the type on the
14896 // stack changes (in generic code a 'T' becomes a 'boxed T')
14897 if (!eeIsValueClass(resolvedToken.hClass))
14899 JITDUMP("\n Importing BOX(refClass) as NOP\n");
14900 verCurrentState.esStack[verCurrentState.esStackDepth - 1].seTypeInfo = tiRetVal;
14904 // Look ahead for unbox.any
14905 if (codeAddr + (sz + 1 + sizeof(mdToken)) <= codeEndp && codeAddr[sz] == CEE_UNBOX_ANY)
14907 CORINFO_RESOLVED_TOKEN unboxResolvedToken;
14909 impResolveToken(codeAddr + (sz + 1), &unboxResolvedToken, CORINFO_TOKENKIND_Class);
14911 // See if the resolved tokens describe types that are equal.
14912 const TypeCompareState compare =
14913 info.compCompHnd->compareTypesForEquality(unboxResolvedToken.hClass, resolvedToken.hClass);
14915 // If so, box/unbox.any is a nop.
14916 if (compare == TypeCompareState::Must)
14918 JITDUMP("\n Importing BOX; UNBOX.ANY as NOP\n");
14919 // Skip the next unbox.any instruction
14920 sz += sizeof(mdToken) + 1;
14925 impImportAndPushBox(&resolvedToken);
14926 if (compDonotInline())
14935 /* Get the Class index */
14936 assertImp(sz == sizeof(unsigned));
14938 _impResolveToken(CORINFO_TOKENKIND_Class);
14940 JITDUMP(" %08X", resolvedToken.token);
14942 if (tiVerificationNeeded)
14944 tiRetVal = typeInfo(TI_INT);
14947 op1 = gtNewIconNode(info.compCompHnd->getClassSize(resolvedToken.hClass));
14948 impPushOnStack(op1, tiRetVal);
14951 case CEE_CASTCLASS:
14953 /* Get the Class index */
14955 assertImp(sz == sizeof(unsigned));
14957 _impResolveToken(CORINFO_TOKENKIND_Casting);
14959 JITDUMP(" %08X", resolvedToken.token);
14961 if (!opts.IsReadyToRun())
14963 op2 = impTokenToHandle(&resolvedToken, nullptr, FALSE);
14964 if (op2 == nullptr)
14965 { // compDonotInline()
14970 if (tiVerificationNeeded)
14972 Verify(impStackTop().seTypeInfo.IsObjRef(), "object ref expected");
14974 tiRetVal = typeInfo(TI_REF, resolvedToken.hClass);
14977 accessAllowedResult =
14978 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
14979 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
14981 op1 = impPopStack().val;
14983 /* Pop the address and create the 'checked cast' helper call */
14985 // At this point we expect typeRef to contain the token, op1 to contain the value being cast,
14986 // and op2 to contain code that creates the type handle corresponding to typeRef
14989 GenTree* optTree = impOptimizeCastClassOrIsInst(op1, &resolvedToken, true);
14991 if (optTree != nullptr)
14993 impPushOnStack(optTree, tiRetVal);
14998 #ifdef FEATURE_READYTORUN_COMPILER
14999 if (opts.IsReadyToRun())
15001 GenTreeCall* opLookup =
15002 impReadyToRunHelperToTree(&resolvedToken, CORINFO_HELP_READYTORUN_CHKCAST, TYP_REF,
15003 gtNewArgList(op1));
15004 usingReadyToRunHelper = (opLookup != nullptr);
15005 op1 = (usingReadyToRunHelper ? opLookup : op1);
15007 if (!usingReadyToRunHelper)
15009 // TODO: ReadyToRun: When generic dictionary lookups are necessary, replace the lookup call
15010 // and the chkcastany call with a single call to a dynamic R2R cell that will:
15011 // 1) Load the context
15012 // 2) Perform the generic dictionary lookup and caching, and generate the appropriate
15014 // 3) Check the object on the stack for the type-cast
15015 // Reason: performance (today, we'll always use the slow helper for the R2R generics case)
15017 op2 = impTokenToHandle(&resolvedToken, nullptr, FALSE);
15018 if (op2 == nullptr)
15019 { // compDonotInline()
15025 if (!usingReadyToRunHelper)
15028 op1 = impCastClassOrIsInstToTree(op1, op2, &resolvedToken, true);
15030 if (compDonotInline())
15035 /* Push the result back on the stack */
15036 impPushOnStack(op1, tiRetVal);
15043 if (compIsForInlining())
15045 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15046 // TODO: Will this be too strict, given that we will inline many basic blocks?
15047 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15049 /* Do we have just the exception on the stack ?*/
15051 if (verCurrentState.esStackDepth != 1)
15053 /* if not, just don't inline the method */
15055 compInlineResult->NoteFatal(InlineObservation::CALLEE_THROW_WITH_INVALID_STACK);
15060 if (tiVerificationNeeded)
15062 tiRetVal = impStackTop().seTypeInfo;
15063 Verify(tiRetVal.IsObjRef(), "object ref expected");
15064 if (verTrackObjCtorInitState && (verCurrentState.thisInitialized != TIS_Init))
15066 Verify(!tiRetVal.IsThisPtr(), "throw uninitialized this");
15070 block->bbSetRunRarely(); // any block with a throw is rare
15071 /* Pop the exception object and create the 'throw' helper call */
15073 op1 = gtNewHelperCallNode(CORINFO_HELP_THROW, TYP_VOID, gtNewArgList(impPopStack().val));
15076 if (verCurrentState.esStackDepth > 0)
15078 impEvalSideEffects();
15081 assert(verCurrentState.esStackDepth == 0);
15087 assert(!compIsForInlining());
15089 if (info.compXcptnsCount == 0)
15091 BADCODE("rethrow outside catch");
15094 if (tiVerificationNeeded)
15096 Verify(block->hasHndIndex(), "rethrow outside catch");
15097 if (block->hasHndIndex())
15099 EHblkDsc* HBtab = ehGetDsc(block->getHndIndex());
15100 Verify(!HBtab->HasFinallyOrFaultHandler(), "rethrow in finally or fault");
15101 if (HBtab->HasFilter())
15103 // we better be in the handler clause part, not the filter part
15104 Verify(jitIsBetween(compCurBB->bbCodeOffs, HBtab->ebdHndBegOffs(), HBtab->ebdHndEndOffs()),
15105 "rethrow in filter");
15110 /* Create the 'rethrow' helper call */
15112 op1 = gtNewHelperCallNode(CORINFO_HELP_RETHROW, TYP_VOID);
15118 assertImp(sz == sizeof(unsigned));
15120 _impResolveToken(CORINFO_TOKENKIND_Class);
15122 JITDUMP(" %08X", resolvedToken.token);
15124 if (tiVerificationNeeded)
15126 typeInfo tiTo = impStackTop().seTypeInfo;
15127 typeInfo tiInstr = verMakeTypeInfo(resolvedToken.hClass);
15129 Verify(tiTo.IsByRef(), "byref expected");
15130 Verify(!tiTo.IsReadonlyByRef(), "write to readonly byref");
15132 Verify(tiCompatibleWith(tiInstr, tiTo.DereferenceByRef(), false),
15133 "type operand incompatible with type of address");
15136 size = info.compCompHnd->getClassSize(resolvedToken.hClass); // Size
15137 op2 = gtNewIconNode(0); // Value
15138 op1 = impPopStack().val; // Dest
15139 op1 = gtNewBlockVal(op1, size);
15140 op1 = gtNewBlkOpNode(op1, op2, size, (prefixFlags & PREFIX_VOLATILE) != 0, false);
15145 if (tiVerificationNeeded)
15147 Verify(false, "bad opcode");
15150 op3 = impPopStack().val; // Size
15151 op2 = impPopStack().val; // Value
15152 op1 = impPopStack().val; // Dest
15154 if (op3->IsCnsIntOrI())
15156 size = (unsigned)op3->AsIntConCommon()->IconValue();
15157 op1 = new (this, GT_BLK) GenTreeBlk(GT_BLK, TYP_STRUCT, op1, size);
15161 op1 = new (this, GT_DYN_BLK) GenTreeDynBlk(op1, op3);
15164 op1 = gtNewBlkOpNode(op1, op2, size, (prefixFlags & PREFIX_VOLATILE) != 0, false);
15170 if (tiVerificationNeeded)
15172 Verify(false, "bad opcode");
15174 op3 = impPopStack().val; // Size
15175 op2 = impPopStack().val; // Src
15176 op1 = impPopStack().val; // Dest
15178 if (op3->IsCnsIntOrI())
15180 size = (unsigned)op3->AsIntConCommon()->IconValue();
15181 op1 = new (this, GT_BLK) GenTreeBlk(GT_BLK, TYP_STRUCT, op1, size);
15185 op1 = new (this, GT_DYN_BLK) GenTreeDynBlk(op1, op3);
15188 if (op2->OperGet() == GT_ADDR)
15190 op2 = op2->gtOp.gtOp1;
15194 op2 = gtNewOperNode(GT_IND, TYP_STRUCT, op2);
15197 op1 = gtNewBlkOpNode(op1, op2, size, (prefixFlags & PREFIX_VOLATILE) != 0, true);
15202 assertImp(sz == sizeof(unsigned));
15204 _impResolveToken(CORINFO_TOKENKIND_Class);
15206 JITDUMP(" %08X", resolvedToken.token);
15208 if (tiVerificationNeeded)
15210 typeInfo tiFrom = impStackTop().seTypeInfo;
15211 typeInfo tiTo = impStackTop(1).seTypeInfo;
15212 typeInfo tiInstr = verMakeTypeInfo(resolvedToken.hClass);
15214 Verify(tiFrom.IsByRef(), "expected byref source");
15215 Verify(tiTo.IsByRef(), "expected byref destination");
15217 Verify(tiCompatibleWith(tiFrom.DereferenceByRef(), tiInstr, false),
15218 "type of source address incompatible with type operand");
15219 Verify(!tiTo.IsReadonlyByRef(), "write to readonly byref");
15220 Verify(tiCompatibleWith(tiInstr, tiTo.DereferenceByRef(), false),
15221 "type operand incompatible with type of destination address");
15224 if (!eeIsValueClass(resolvedToken.hClass))
15226 op1 = impPopStack().val; // address to load from
15228 impBashVarAddrsToI(op1);
15230 assertImp(genActualType(op1->gtType) == TYP_I_IMPL || op1->gtType == TYP_BYREF);
15232 op1 = gtNewOperNode(GT_IND, TYP_REF, op1);
15233 op1->gtFlags |= GTF_EXCEPT | GTF_GLOB_REF;
15235 impPushOnStack(op1, typeInfo());
15236 opcode = CEE_STIND_REF;
15238 goto STIND_POST_VERIFY;
15241 op2 = impPopStack().val; // Src
15242 op1 = impPopStack().val; // Dest
15243 op1 = gtNewCpObjNode(op1, op2, resolvedToken.hClass, ((prefixFlags & PREFIX_VOLATILE) != 0));
15248 assertImp(sz == sizeof(unsigned));
15250 _impResolveToken(CORINFO_TOKENKIND_Class);
15252 JITDUMP(" %08X", resolvedToken.token);
15254 if (eeIsValueClass(resolvedToken.hClass))
15256 lclTyp = TYP_STRUCT;
15263 if (tiVerificationNeeded)
15266 typeInfo tiPtr = impStackTop(1).seTypeInfo;
15268 // Make sure we have a good looking byref
15269 Verify(tiPtr.IsByRef(), "pointer not byref");
15270 Verify(!tiPtr.IsReadonlyByRef(), "write to readonly byref");
15271 if (!tiPtr.IsByRef() || tiPtr.IsReadonlyByRef())
15273 compUnsafeCastUsed = true;
15276 typeInfo ptrVal = DereferenceByRef(tiPtr);
15277 typeInfo argVal = verMakeTypeInfo(resolvedToken.hClass);
15279 if (!tiCompatibleWith(impStackTop(0).seTypeInfo, NormaliseForStack(argVal), true))
15281 Verify(false, "type of value incompatible with type operand");
15282 compUnsafeCastUsed = true;
15285 if (!tiCompatibleWith(argVal, ptrVal, false))
15287 Verify(false, "type operand incompatible with type of address");
15288 compUnsafeCastUsed = true;
15293 compUnsafeCastUsed = true;
15296 if (lclTyp == TYP_REF)
15298 opcode = CEE_STIND_REF;
15299 goto STIND_POST_VERIFY;
15302 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(resolvedToken.hClass);
15303 if (impIsPrimitive(jitTyp))
15305 lclTyp = JITtype2varType(jitTyp);
15306 goto STIND_POST_VERIFY;
15309 op2 = impPopStack().val; // Value
15310 op1 = impPopStack().val; // Ptr
15312 assertImp(varTypeIsStruct(op2));
15314 op1 = impAssignStructPtr(op1, op2, resolvedToken.hClass, (unsigned)CHECK_SPILL_ALL);
15316 if (op1->OperIsBlkOp() && (prefixFlags & PREFIX_UNALIGNED))
15318 op1->gtFlags |= GTF_BLK_UNALIGNED;
15325 assert(!compIsForInlining());
15327 // Being lazy here. Refanys are tricky in terms of gc tracking.
15328 // Since it is uncommon, just don't perform struct promotion in any method that contains mkrefany.
15330 JITDUMP("disabling struct promotion because of mkrefany\n");
15331 fgNoStructPromotion = true;
15333 oper = GT_MKREFANY;
15334 assertImp(sz == sizeof(unsigned));
15336 _impResolveToken(CORINFO_TOKENKIND_Class);
15338 JITDUMP(" %08X", resolvedToken.token);
15340 op2 = impTokenToHandle(&resolvedToken, nullptr, TRUE);
15341 if (op2 == nullptr)
15342 { // compDonotInline()
15346 if (tiVerificationNeeded)
15348 typeInfo tiPtr = impStackTop().seTypeInfo;
15349 typeInfo tiInstr = verMakeTypeInfo(resolvedToken.hClass);
15351 Verify(!verIsByRefLike(tiInstr), "mkrefany of byref-like class");
15352 Verify(!tiPtr.IsReadonlyByRef(), "readonly byref used with mkrefany");
15353 Verify(typeInfo::AreEquivalent(tiPtr.DereferenceByRef(), tiInstr), "type mismatch");
15356 accessAllowedResult =
15357 info.compCompHnd->canAccessClass(&resolvedToken, info.compMethodHnd, &calloutHelper);
15358 impHandleAccessAllowed(accessAllowedResult, &calloutHelper);
15360 op1 = impPopStack().val;
15362 // @SPECVIOLATION: TYP_INT should not be allowed here by a strict reading of the spec.
15363 // But JIT32 allowed it, so we continue to allow it.
15364 assertImp(op1->TypeGet() == TYP_BYREF || op1->TypeGet() == TYP_I_IMPL || op1->TypeGet() == TYP_INT);
15366 // MKREFANY returns a struct. op2 is the class token.
15367 op1 = gtNewOperNode(oper, TYP_STRUCT, op1, op2);
15369 impPushOnStack(op1, verMakeTypeInfo(impGetRefAnyClass()));
15375 assertImp(sz == sizeof(unsigned));
15377 _impResolveToken(CORINFO_TOKENKIND_Class);
15379 JITDUMP(" %08X", resolvedToken.token);
15383 tiRetVal = verMakeTypeInfo(resolvedToken.hClass);
15385 if (tiVerificationNeeded)
15387 typeInfo tiPtr = impStackTop().seTypeInfo;
15389 // Make sure we have a byref
15390 if (!tiPtr.IsByRef())
15392 Verify(false, "pointer not byref");
15393 compUnsafeCastUsed = true;
15395 typeInfo tiPtrVal = DereferenceByRef(tiPtr);
15397 if (!tiCompatibleWith(tiPtrVal, tiRetVal, false))
15399 Verify(false, "type of address incompatible with type operand");
15400 compUnsafeCastUsed = true;
15402 tiRetVal.NormaliseForStack();
15406 compUnsafeCastUsed = true;
15409 if (eeIsValueClass(resolvedToken.hClass))
15411 lclTyp = TYP_STRUCT;
15416 opcode = CEE_LDIND_REF;
15417 goto LDIND_POST_VERIFY;
15420 op1 = impPopStack().val;
15422 assertImp(op1->TypeGet() == TYP_BYREF || op1->TypeGet() == TYP_I_IMPL);
15424 CorInfoType jitTyp = info.compCompHnd->asCorInfoType(resolvedToken.hClass);
15425 if (impIsPrimitive(jitTyp))
15427 op1 = gtNewOperNode(GT_IND, JITtype2varType(jitTyp), op1);
15429 // Could point anywhere, example a boxed class static int
15430 op1->gtFlags |= GTF_IND_TGTANYWHERE | GTF_GLOB_REF;
15431 assertImp(varTypeIsArithmetic(op1->gtType));
15435 // OBJ returns a struct
15436 // and an inline argument which is the class token of the loaded obj
15437 op1 = gtNewObjNode(resolvedToken.hClass, op1);
15439 op1->gtFlags |= GTF_EXCEPT;
15441 if (prefixFlags & PREFIX_UNALIGNED)
15443 op1->gtFlags |= GTF_IND_UNALIGNED;
15446 impPushOnStack(op1, tiRetVal);
15451 if (tiVerificationNeeded)
15453 typeInfo tiArray = impStackTop().seTypeInfo;
15454 Verify(verIsSDArray(tiArray), "bad array");
15455 tiRetVal = typeInfo(TI_INT);
15458 op1 = impPopStack().val;
15459 if (!opts.MinOpts() && !opts.compDbgCode)
15461 /* Use GT_ARR_LENGTH operator so rng check opts see this */
15462 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, op1, offsetof(CORINFO_Array, length));
15464 /* Mark the block as containing a length expression */
15466 if (op1->gtOper == GT_LCL_VAR)
15468 block->bbFlags |= BBF_HAS_IDX_LEN;
15475 /* Create the expression "*(array_addr + ArrLenOffs)" */
15476 op1 = gtNewOperNode(GT_ADD, TYP_BYREF, op1,
15477 gtNewIconNode(offsetof(CORINFO_Array, length), TYP_I_IMPL));
15478 op1 = gtNewIndir(TYP_INT, op1);
15479 op1->gtFlags |= GTF_IND_ARR_LEN;
15482 /* Push the result back on the stack */
15483 impPushOnStack(op1, tiRetVal);
15487 op1 = gtNewHelperCallNode(CORINFO_HELP_USER_BREAKPOINT, TYP_VOID);
15491 if (opts.compDbgCode)
15493 op1 = new (this, GT_NO_OP) GenTree(GT_NO_OP, TYP_VOID);
15498 /******************************** NYI *******************************/
15501 OutputDebugStringA("CLR: Invalid x86 breakpoint in IL stream\n");
15504 case CEE_MACRO_END:
15507 BADCODE3("unknown opcode", ": %02X", (int)opcode);
15511 prevOpcode = opcode;
15517 #undef _impResolveToken
15520 #pragma warning(pop)
15523 // Push a local/argument treeon the operand stack
15524 void Compiler::impPushVar(GenTree* op, typeInfo tiRetVal)
15526 tiRetVal.NormaliseForStack();
15528 if (verTrackObjCtorInitState && (verCurrentState.thisInitialized != TIS_Init) && tiRetVal.IsThisPtr())
15530 tiRetVal.SetUninitialisedObjRef();
15533 impPushOnStack(op, tiRetVal);
15536 // Load a local/argument on the operand stack
15537 // lclNum is an index into lvaTable *NOT* the arg/lcl index in the IL
15538 void Compiler::impLoadVar(unsigned lclNum, IL_OFFSET offset, typeInfo tiRetVal)
15542 if (lvaTable[lclNum].lvNormalizeOnLoad())
15544 lclTyp = lvaGetRealType(lclNum);
15548 lclTyp = lvaGetActualType(lclNum);
15551 impPushVar(gtNewLclvNode(lclNum, lclTyp, offset), tiRetVal);
15554 // Load an argument on the operand stack
15555 // Shared by the various CEE_LDARG opcodes
15556 // ilArgNum is the argument index as specified in IL.
15557 // It will be mapped to the correct lvaTable index
15558 void Compiler::impLoadArg(unsigned ilArgNum, IL_OFFSET offset)
15560 Verify(ilArgNum < info.compILargsCount, "bad arg num");
15562 if (compIsForInlining())
15564 if (ilArgNum >= info.compArgsCount)
15566 compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER);
15570 impPushVar(impInlineFetchArg(ilArgNum, impInlineInfo->inlArgInfo, impInlineInfo->lclVarInfo),
15571 impInlineInfo->lclVarInfo[ilArgNum].lclVerTypeInfo);
15575 if (ilArgNum >= info.compArgsCount)
15580 unsigned lclNum = compMapILargNum(ilArgNum); // account for possible hidden param
15582 if (lclNum == info.compThisArg)
15584 lclNum = lvaArg0Var;
15587 impLoadVar(lclNum, offset);
15591 // Load a local on the operand stack
15592 // Shared by the various CEE_LDLOC opcodes
15593 // ilLclNum is the local index as specified in IL.
15594 // It will be mapped to the correct lvaTable index
15595 void Compiler::impLoadLoc(unsigned ilLclNum, IL_OFFSET offset)
15597 if (tiVerificationNeeded)
15599 Verify(ilLclNum < info.compMethodInfo->locals.numArgs, "bad loc num");
15600 Verify(info.compInitMem, "initLocals not set");
15603 if (compIsForInlining())
15605 if (ilLclNum >= info.compMethodInfo->locals.numArgs)
15607 compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_LOCAL_NUMBER);
15611 // Get the local type
15612 var_types lclTyp = impInlineInfo->lclVarInfo[ilLclNum + impInlineInfo->argCnt].lclTypeInfo;
15614 typeInfo tiRetVal = impInlineInfo->lclVarInfo[ilLclNum + impInlineInfo->argCnt].lclVerTypeInfo;
15616 /* Have we allocated a temp for this local? */
15618 unsigned lclNum = impInlineFetchLocal(ilLclNum DEBUGARG("Inline ldloc first use temp"));
15620 // All vars of inlined methods should be !lvNormalizeOnLoad()
15622 assert(!lvaTable[lclNum].lvNormalizeOnLoad());
15623 lclTyp = genActualType(lclTyp);
15625 impPushVar(gtNewLclvNode(lclNum, lclTyp), tiRetVal);
15629 if (ilLclNum >= info.compMethodInfo->locals.numArgs)
15634 unsigned lclNum = info.compArgsCount + ilLclNum;
15636 impLoadVar(lclNum, offset);
15640 #ifdef _TARGET_ARM_
15641 /**************************************************************************************
15643 * When assigning a vararg call src to a HFA lcl dest, mark that we cannot promote the
15644 * dst struct, because struct promotion will turn it into a float/double variable while
15645 * the rhs will be an int/long variable. We don't code generate assignment of int into
15646 * a float, but there is nothing that might prevent us from doing so. The tree however
15647 * would like: (=, (typ_float, typ_int)) or (GT_TRANSFER, (typ_float, typ_int))
15649 * tmpNum - the lcl dst variable num that is a struct.
15650 * src - the src tree assigned to the dest that is a struct/int (when varargs call.)
15651 * hClass - the type handle for the struct variable.
15653 * TODO-ARM-CQ: [301608] This is a rare scenario with varargs and struct promotion coming into play,
15654 * however, we could do a codegen of transferring from int to float registers
15655 * (transfer, not a cast.)
15658 void Compiler::impMarkLclDstNotPromotable(unsigned tmpNum, GenTreePtr src, CORINFO_CLASS_HANDLE hClass)
15660 if (src->gtOper == GT_CALL && src->gtCall.IsVarargs() && IsHfa(hClass))
15662 int hfaSlots = GetHfaCount(hClass);
15663 var_types hfaType = GetHfaType(hClass);
15665 // If we have varargs we morph the method's return type to be "int" irrespective of its original
15666 // type: struct/float at importer because the ABI calls out return in integer registers.
15667 // We don't want struct promotion to replace an expression like this:
15668 // lclFld_int = callvar_int() into lclFld_float = callvar_int();
15669 // This means an int is getting assigned to a float without a cast. Prevent the promotion.
15670 if ((hfaType == TYP_DOUBLE && hfaSlots == sizeof(double) / REGSIZE_BYTES) ||
15671 (hfaType == TYP_FLOAT && hfaSlots == sizeof(float) / REGSIZE_BYTES))
15673 // Make sure this struct type stays as struct so we can receive the call in a struct.
15674 lvaTable[tmpNum].lvIsMultiRegRet = true;
15678 #endif // _TARGET_ARM_
15680 #if FEATURE_MULTIREG_RET
15681 GenTreePtr Compiler::impAssignMultiRegTypeToVar(GenTreePtr op, CORINFO_CLASS_HANDLE hClass)
15683 unsigned tmpNum = lvaGrabTemp(true DEBUGARG("Return value temp for multireg return."));
15684 impAssignTempGen(tmpNum, op, hClass, (unsigned)CHECK_SPILL_ALL);
15685 GenTreePtr ret = gtNewLclvNode(tmpNum, op->gtType);
15687 // TODO-1stClassStructs: Handle constant propagation and CSE-ing of multireg returns.
15688 ret->gtFlags |= GTF_DONT_CSE;
15690 assert(IsMultiRegReturnedType(hClass));
15692 // Mark the var so that fields are not promoted and stay together.
15693 lvaTable[tmpNum].lvIsMultiRegRet = true;
15697 #endif // FEATURE_MULTIREG_RET
15699 // do import for a return
15700 // returns false if inlining was aborted
15701 // opcode can be ret or call in the case of a tail.call
15702 bool Compiler::impReturnInstruction(BasicBlock* block, int prefixFlags, OPCODE& opcode)
15704 if (tiVerificationNeeded)
15706 verVerifyThisPtrInitialised();
15708 unsigned expectedStack = 0;
15709 if (info.compRetType != TYP_VOID)
15711 typeInfo tiVal = impStackTop().seTypeInfo;
15712 typeInfo tiDeclared =
15713 verMakeTypeInfo(info.compMethodInfo->args.retType, info.compMethodInfo->args.retTypeClass);
15715 Verify(!verIsByRefLike(tiDeclared) || verIsSafeToReturnByRef(tiVal), "byref return");
15717 Verify(tiCompatibleWith(tiVal, tiDeclared.NormaliseForStack(), true), "type mismatch");
15720 Verify(verCurrentState.esStackDepth == expectedStack, "stack non-empty on return");
15724 // If we are importing an inlinee and have GC ref locals we always
15725 // need to have a spill temp for the return value. This temp
15726 // should have been set up in advance, over in fgFindBasicBlocks.
15727 if (compIsForInlining() && impInlineInfo->HasGcRefLocals() && (info.compRetType != TYP_VOID))
15729 assert(lvaInlineeReturnSpillTemp != BAD_VAR_NUM);
15733 GenTree* op2 = nullptr;
15734 GenTree* op1 = nullptr;
15735 CORINFO_CLASS_HANDLE retClsHnd = nullptr;
15737 if (info.compRetType != TYP_VOID)
15739 StackEntry se = impPopStack();
15740 retClsHnd = se.seTypeInfo.GetClassHandle();
15743 if (!compIsForInlining())
15745 impBashVarAddrsToI(op2);
15746 op2 = impImplicitIorI4Cast(op2, info.compRetType);
15747 op2 = impImplicitR4orR8Cast(op2, info.compRetType);
15748 assertImp((genActualType(op2->TypeGet()) == genActualType(info.compRetType)) ||
15749 ((op2->TypeGet() == TYP_I_IMPL) && (info.compRetType == TYP_BYREF)) ||
15750 ((op2->TypeGet() == TYP_BYREF) && (info.compRetType == TYP_I_IMPL)) ||
15751 (varTypeIsFloating(op2->gtType) && varTypeIsFloating(info.compRetType)) ||
15752 (varTypeIsStruct(op2) && varTypeIsStruct(info.compRetType)));
15755 if (opts.compGcChecks && info.compRetType == TYP_REF)
15757 // DDB 3483 : JIT Stress: early termination of GC ref's life time in exception code path
15758 // VSW 440513: Incorrect gcinfo on the return value under COMPlus_JitGCChecks=1 for methods with
15761 assert(op2->gtType == TYP_REF);
15763 // confirm that the argument is a GC pointer (for debugging (GC stress))
15764 GenTreeArgList* args = gtNewArgList(op2);
15765 op2 = gtNewHelperCallNode(CORINFO_HELP_CHECK_OBJ, TYP_REF, args);
15769 printf("\ncompGcChecks tree:\n");
15777 // inlinee's stack should be empty now.
15778 assert(verCurrentState.esStackDepth == 0);
15783 printf("\n\n Inlinee Return expression (before normalization) =>\n");
15788 // Make sure the type matches the original call.
15790 var_types returnType = genActualType(op2->gtType);
15791 var_types originalCallType = impInlineInfo->inlineCandidateInfo->fncRetType;
15792 if ((returnType != originalCallType) && (originalCallType == TYP_STRUCT))
15794 originalCallType = impNormStructType(impInlineInfo->inlineCandidateInfo->methInfo.args.retTypeClass);
15797 if (returnType != originalCallType)
15799 compInlineResult->NoteFatal(InlineObservation::CALLSITE_RETURN_TYPE_MISMATCH);
15803 // Below, we are going to set impInlineInfo->retExpr to the tree with the return
15804 // expression. At this point, retExpr could already be set if there are multiple
15805 // return blocks (meaning fgNeedReturnSpillTemp() == true) and one of
15806 // the other blocks already set it. If there is only a single return block,
15807 // retExpr shouldn't be set. However, this is not true if we reimport a block
15808 // with a return. In that case, retExpr will be set, then the block will be
15809 // reimported, but retExpr won't get cleared as part of setting the block to
15810 // be reimported. The reimported retExpr value should be the same, so even if
15811 // we don't unconditionally overwrite it, it shouldn't matter.
15812 if (info.compRetNativeType != TYP_STRUCT)
15814 // compRetNativeType is not TYP_STRUCT.
15815 // This implies it could be either a scalar type or SIMD vector type or
15816 // a struct type that can be normalized to a scalar type.
15818 if (varTypeIsStruct(info.compRetType))
15820 noway_assert(info.compRetBuffArg == BAD_VAR_NUM);
15821 // adjust the type away from struct to integral
15822 // and no normalizing
15823 op2 = impFixupStructReturnType(op2, retClsHnd);
15827 // Do we have to normalize?
15828 var_types fncRealRetType = JITtype2varType(info.compMethodInfo->args.retType);
15829 if ((varTypeIsSmall(op2->TypeGet()) || varTypeIsSmall(fncRealRetType)) &&
15830 fgCastNeeded(op2, fncRealRetType))
15832 // Small-typed return values are normalized by the callee
15833 op2 = gtNewCastNode(TYP_INT, op2, fncRealRetType);
15837 if (fgNeedReturnSpillTemp())
15839 assert(info.compRetNativeType != TYP_VOID &&
15840 (fgMoreThanOneReturnBlock() || impInlineInfo->HasGcRefLocals()));
15842 // If this method returns a ref type, track the actual types seen
15844 if (info.compRetType == TYP_REF)
15846 bool isExact = false;
15847 bool isNonNull = false;
15848 CORINFO_CLASS_HANDLE returnClsHnd = gtGetClassHandle(op2, &isExact, &isNonNull);
15850 if (impInlineInfo->retExpr == nullptr)
15852 // This is the first return, so best known type is the type
15853 // of this return value.
15854 impInlineInfo->retExprClassHnd = returnClsHnd;
15855 impInlineInfo->retExprClassHndIsExact = isExact;
15857 else if (impInlineInfo->retExprClassHnd != returnClsHnd)
15859 // This return site type differs from earlier seen sites,
15860 // so reset the info and we'll fall back to using the method's
15861 // declared return type for the return spill temp.
15862 impInlineInfo->retExprClassHnd = nullptr;
15863 impInlineInfo->retExprClassHndIsExact = false;
15867 // This is a bit of a workaround...
15868 // If we are inlining a call that returns a struct, where the actual "native" return type is
15869 // not a struct (for example, the struct is composed of exactly one int, and the native
15870 // return type is thus an int), and the inlinee has multiple return blocks (thus,
15871 // fgNeedReturnSpillTemp() == true, and is the index of a local var that is set
15872 // to the *native* return type), and at least one of the return blocks is the result of
15873 // a call, then we have a problem. The situation is like this (from a failed test case):
15876 // // Note: valuetype plinq_devtests.LazyTests/LIX is a struct with only a single int
15877 // call !!0 [mscorlib]System.Threading.LazyInitializer::EnsureInitialized<valuetype
15878 // plinq_devtests.LazyTests/LIX>(!!0&, bool&, object&, class [mscorlib]System.Func`1<!!0>)
15882 // ldobj !!T // this gets bashed to a GT_LCL_FLD, type TYP_INT
15885 // call !!0 System.Threading.LazyInitializer::EnsureInitializedCore<!!0>(!!0&, bool&,
15886 // object&, class System.Func`1<!!0>)
15889 // In the code above, when we call impFixupStructReturnType(), we will change the op2 return type
15890 // of the inlinee return node, but we don't do that for GT_CALL nodes, which we delay until
15891 // morphing when we call fgFixupStructReturn(). We do this, apparently, to handle nested
15892 // inlining properly by leaving the correct type on the GT_CALL node through importing.
15894 // To fix this, for this case, we temporarily change the GT_CALL node type to the
15895 // native return type, which is what it will be set to eventually. We generate the
15896 // assignment to the return temp, using the correct type, and then restore the GT_CALL
15897 // node type. During morphing, the GT_CALL will get the correct, final, native return type.
15899 bool restoreType = false;
15900 if ((op2->OperGet() == GT_CALL) && (info.compRetType == TYP_STRUCT))
15902 noway_assert(op2->TypeGet() == TYP_STRUCT);
15903 op2->gtType = info.compRetNativeType;
15904 restoreType = true;
15907 impAssignTempGen(lvaInlineeReturnSpillTemp, op2, se.seTypeInfo.GetClassHandle(),
15908 (unsigned)CHECK_SPILL_ALL);
15910 GenTree* tmpOp2 = gtNewLclvNode(lvaInlineeReturnSpillTemp, op2->TypeGet());
15914 op2->gtType = TYP_STRUCT; // restore it to what it was
15920 if (impInlineInfo->retExpr)
15922 // Some other block(s) have seen the CEE_RET first.
15923 // Better they spilled to the same temp.
15924 assert(impInlineInfo->retExpr->gtOper == GT_LCL_VAR);
15925 assert(impInlineInfo->retExpr->gtLclVarCommon.gtLclNum == op2->gtLclVarCommon.gtLclNum);
15933 printf("\n\n Inlinee Return expression (after normalization) =>\n");
15938 // Report the return expression
15939 impInlineInfo->retExpr = op2;
15943 // compRetNativeType is TYP_STRUCT.
15944 // This implies that struct return via RetBuf arg or multi-reg struct return
15946 GenTreeCall* iciCall = impInlineInfo->iciCall->AsCall();
15948 // Assign the inlinee return into a spill temp.
15949 // spill temp only exists if there are multiple return points
15950 if (lvaInlineeReturnSpillTemp != BAD_VAR_NUM)
15952 // in this case we have to insert multiple struct copies to the temp
15953 // and the retexpr is just the temp.
15954 assert(info.compRetNativeType != TYP_VOID);
15955 assert(fgMoreThanOneReturnBlock() || impInlineInfo->HasGcRefLocals());
15957 impAssignTempGen(lvaInlineeReturnSpillTemp, op2, se.seTypeInfo.GetClassHandle(),
15958 (unsigned)CHECK_SPILL_ALL);
15961 #if defined(_TARGET_ARM_) || defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
15962 #if defined(_TARGET_ARM_)
15963 // TODO-ARM64-NYI: HFA
15964 // TODO-AMD64-Unix and TODO-ARM once the ARM64 functionality is implemented the
15965 // next ifdefs could be refactored in a single method with the ifdef inside.
15966 if (IsHfa(retClsHnd))
15968 // Same as !IsHfa but just don't bother with impAssignStructPtr.
15969 #else // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
15970 ReturnTypeDesc retTypeDesc;
15971 retTypeDesc.InitializeStructReturnType(this, retClsHnd);
15972 unsigned retRegCount = retTypeDesc.GetReturnRegCount();
15974 if (retRegCount != 0)
15976 // If single eightbyte, the return type would have been normalized and there won't be a temp var.
15977 // This code will be called only if the struct return has not been normalized (i.e. 2 eightbytes -
15979 assert(retRegCount == MAX_RET_REG_COUNT);
15980 // Same as !structDesc.passedInRegisters but just don't bother with impAssignStructPtr.
15981 CLANG_FORMAT_COMMENT_ANCHOR;
15982 #endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
15984 if (fgNeedReturnSpillTemp())
15986 if (!impInlineInfo->retExpr)
15988 #if defined(_TARGET_ARM_)
15989 impInlineInfo->retExpr = gtNewLclvNode(lvaInlineeReturnSpillTemp, info.compRetType);
15990 #else // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
15991 // The inlinee compiler has figured out the type of the temp already. Use it here.
15992 impInlineInfo->retExpr =
15993 gtNewLclvNode(lvaInlineeReturnSpillTemp, lvaTable[lvaInlineeReturnSpillTemp].lvType);
15994 #endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
15999 impInlineInfo->retExpr = op2;
16003 #elif defined(_TARGET_ARM64_)
16004 ReturnTypeDesc retTypeDesc;
16005 retTypeDesc.InitializeStructReturnType(this, retClsHnd);
16006 unsigned retRegCount = retTypeDesc.GetReturnRegCount();
16008 if (retRegCount != 0)
16010 assert(!iciCall->HasRetBufArg());
16011 assert(retRegCount >= 2);
16012 if (fgNeedReturnSpillTemp())
16014 if (!impInlineInfo->retExpr)
16016 // The inlinee compiler has figured out the type of the temp already. Use it here.
16017 impInlineInfo->retExpr =
16018 gtNewLclvNode(lvaInlineeReturnSpillTemp, lvaTable[lvaInlineeReturnSpillTemp].lvType);
16023 impInlineInfo->retExpr = op2;
16027 #endif // defined(_TARGET_ARM64_)
16029 assert(iciCall->HasRetBufArg());
16030 GenTreePtr dest = gtCloneExpr(iciCall->gtCallArgs->gtOp.gtOp1);
16031 // spill temp only exists if there are multiple return points
16032 if (fgNeedReturnSpillTemp())
16034 // if this is the first return we have seen set the retExpr
16035 if (!impInlineInfo->retExpr)
16037 impInlineInfo->retExpr =
16038 impAssignStructPtr(dest, gtNewLclvNode(lvaInlineeReturnSpillTemp, info.compRetType),
16039 retClsHnd, (unsigned)CHECK_SPILL_ALL);
16044 impInlineInfo->retExpr = impAssignStructPtr(dest, op2, retClsHnd, (unsigned)CHECK_SPILL_ALL);
16051 if (compIsForInlining())
16056 if (info.compRetType == TYP_VOID)
16059 op1 = new (this, GT_RETURN) GenTreeOp(GT_RETURN, TYP_VOID);
16061 else if (info.compRetBuffArg != BAD_VAR_NUM)
16063 // Assign value to return buff (first param)
16064 GenTreePtr retBuffAddr = gtNewLclvNode(info.compRetBuffArg, TYP_BYREF, impCurStmtOffs);
16066 op2 = impAssignStructPtr(retBuffAddr, op2, retClsHnd, (unsigned)CHECK_SPILL_ALL);
16067 impAppendTree(op2, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
16069 // There are cases where the address of the implicit RetBuf should be returned explicitly (in RAX).
16070 CLANG_FORMAT_COMMENT_ANCHOR;
16072 #if defined(_TARGET_AMD64_)
16074 // x64 (System V and Win64) calling convention requires to
16075 // return the implicit return buffer explicitly (in RAX).
16076 // Change the return type to be BYREF.
16077 op1 = gtNewOperNode(GT_RETURN, TYP_BYREF, gtNewLclvNode(info.compRetBuffArg, TYP_BYREF));
16078 #else // !defined(_TARGET_AMD64_)
16079 // In case of non-AMD64 targets the profiler hook requires to return the implicit RetBuf explicitly (in RAX).
16080 // In such case the return value of the function is changed to BYREF.
16081 // If profiler hook is not needed the return type of the function is TYP_VOID.
16082 if (compIsProfilerHookNeeded())
16084 op1 = gtNewOperNode(GT_RETURN, TYP_BYREF, gtNewLclvNode(info.compRetBuffArg, TYP_BYREF));
16089 op1 = new (this, GT_RETURN) GenTreeOp(GT_RETURN, TYP_VOID);
16091 #endif // !defined(_TARGET_AMD64_)
16093 else if (varTypeIsStruct(info.compRetType))
16095 #if !FEATURE_MULTIREG_RET
16096 // For both ARM architectures the HFA native types are maintained as structs.
16097 // Also on System V AMD64 the multireg structs returns are also left as structs.
16098 noway_assert(info.compRetNativeType != TYP_STRUCT);
16100 op2 = impFixupStructReturnType(op2, retClsHnd);
16102 op1 = gtNewOperNode(GT_RETURN, genActualType(info.compRetNativeType), op2);
16107 op1 = gtNewOperNode(GT_RETURN, genActualType(info.compRetType), op2);
16110 // We must have imported a tailcall and jumped to RET
16111 if (prefixFlags & PREFIX_TAILCALL)
16113 #if defined(FEATURE_CORECLR) || !defined(_TARGET_AMD64_)
16115 // This cannot be asserted on Amd64 since we permit the following IL pattern:
16119 assert(verCurrentState.esStackDepth == 0 && impOpcodeIsCallOpcode(opcode));
16120 #endif // FEATURE_CORECLR || !_TARGET_AMD64_
16122 opcode = CEE_RET; // To prevent trying to spill if CALL_SITE_BOUNDARIES
16124 // impImportCall() would have already appended TYP_VOID calls
16125 if (info.compRetType == TYP_VOID)
16131 impAppendTree(op1, (unsigned)CHECK_SPILL_NONE, impCurStmtOffs);
16133 // Remember at which BC offset the tree was finished
16134 impNoteLastILoffs();
16139 /*****************************************************************************
16140 * Mark the block as unimported.
16141 * Note that the caller is responsible for calling impImportBlockPending(),
16142 * with the appropriate stack-state
16145 inline void Compiler::impReimportMarkBlock(BasicBlock* block)
16148 if (verbose && (block->bbFlags & BBF_IMPORTED))
16150 printf("\nBB%02u will be reimported\n", block->bbNum);
16154 block->bbFlags &= ~BBF_IMPORTED;
16157 /*****************************************************************************
16158 * Mark the successors of the given block as unimported.
16159 * Note that the caller is responsible for calling impImportBlockPending()
16160 * for all the successors, with the appropriate stack-state.
16163 void Compiler::impReimportMarkSuccessors(BasicBlock* block)
16165 const unsigned numSuccs = block->NumSucc();
16166 for (unsigned i = 0; i < numSuccs; i++)
16168 impReimportMarkBlock(block->GetSucc(i));
16172 /*****************************************************************************
16174 * Filter wrapper to handle only passed in exception code
16178 LONG FilterVerificationExceptions(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam)
16180 if (pExceptionPointers->ExceptionRecord->ExceptionCode == SEH_VERIFICATION_EXCEPTION)
16182 return EXCEPTION_EXECUTE_HANDLER;
16185 return EXCEPTION_CONTINUE_SEARCH;
16188 void Compiler::impVerifyEHBlock(BasicBlock* block, bool isTryStart)
16190 assert(block->hasTryIndex());
16191 assert(!compIsForInlining());
16193 unsigned tryIndex = block->getTryIndex();
16194 EHblkDsc* HBtab = ehGetDsc(tryIndex);
16198 assert(block->bbFlags & BBF_TRY_BEG);
16200 // The Stack must be empty
16202 if (block->bbStkDepth != 0)
16204 BADCODE("Evaluation stack must be empty on entry into a try block");
16208 // Save the stack contents, we'll need to restore it later
16210 SavedStack blockState;
16211 impSaveStackState(&blockState, false);
16213 while (HBtab != nullptr)
16217 // Are we verifying that an instance constructor properly initializes it's 'this' pointer once?
16218 // We do not allow the 'this' pointer to be uninitialized when entering most kinds try regions
16220 if (verTrackObjCtorInitState && (verCurrentState.thisInitialized != TIS_Init))
16222 // We trigger an invalid program exception here unless we have a try/fault region.
16224 if (HBtab->HasCatchHandler() || HBtab->HasFinallyHandler() || HBtab->HasFilter())
16227 "The 'this' pointer of an instance constructor is not intialized upon entry to a try region");
16231 // Allow a try/fault region to proceed.
16232 assert(HBtab->HasFaultHandler());
16236 /* Recursively process the handler block */
16237 BasicBlock* hndBegBB = HBtab->ebdHndBeg;
16239 // Construct the proper verification stack state
16240 // either empty or one that contains just
16241 // the Exception Object that we are dealing with
16243 verCurrentState.esStackDepth = 0;
16245 if (handlerGetsXcptnObj(hndBegBB->bbCatchTyp))
16247 CORINFO_CLASS_HANDLE clsHnd;
16249 if (HBtab->HasFilter())
16251 clsHnd = impGetObjectClass();
16255 CORINFO_RESOLVED_TOKEN resolvedToken;
16257 resolvedToken.tokenContext = impTokenLookupContextHandle;
16258 resolvedToken.tokenScope = info.compScopeHnd;
16259 resolvedToken.token = HBtab->ebdTyp;
16260 resolvedToken.tokenType = CORINFO_TOKENKIND_Class;
16261 info.compCompHnd->resolveToken(&resolvedToken);
16263 clsHnd = resolvedToken.hClass;
16266 // push catch arg the stack, spill to a temp if necessary
16267 // Note: can update HBtab->ebdHndBeg!
16268 hndBegBB = impPushCatchArgOnStack(hndBegBB, clsHnd, false);
16271 // Queue up the handler for importing
16273 impImportBlockPending(hndBegBB);
16275 if (HBtab->HasFilter())
16277 /* @VERIFICATION : Ideally the end of filter state should get
16278 propagated to the catch handler, this is an incompleteness,
16279 but is not a security/compliance issue, since the only
16280 interesting state is the 'thisInit' state.
16283 verCurrentState.esStackDepth = 0;
16285 BasicBlock* filterBB = HBtab->ebdFilter;
16287 // push catch arg the stack, spill to a temp if necessary
16288 // Note: can update HBtab->ebdFilter!
16289 const bool isSingleBlockFilter = (filterBB->bbNext == hndBegBB);
16290 filterBB = impPushCatchArgOnStack(filterBB, impGetObjectClass(), isSingleBlockFilter);
16292 impImportBlockPending(filterBB);
16295 else if (verTrackObjCtorInitState && HBtab->HasFaultHandler())
16297 /* Recursively process the handler block */
16299 verCurrentState.esStackDepth = 0;
16301 // Queue up the fault handler for importing
16303 impImportBlockPending(HBtab->ebdHndBeg);
16306 // Now process our enclosing try index (if any)
16308 tryIndex = HBtab->ebdEnclosingTryIndex;
16309 if (tryIndex == EHblkDsc::NO_ENCLOSING_INDEX)
16315 HBtab = ehGetDsc(tryIndex);
16319 // Restore the stack contents
16320 impRestoreStackState(&blockState);
16323 //***************************************************************
16324 // Import the instructions for the given basic block. Perform
16325 // verification, throwing an exception on failure. Push any successor blocks that are enabled for the first
16326 // time, or whose verification pre-state is changed.
16329 #pragma warning(push)
16330 #pragma warning(disable : 21000) // Suppress PREFast warning about overly large function
16332 void Compiler::impImportBlock(BasicBlock* block)
16334 // BBF_INTERNAL blocks only exist during importation due to EH canonicalization. We need to
16335 // handle them specially. In particular, there is no IL to import for them, but we do need
16336 // to mark them as imported and put their successors on the pending import list.
16337 if (block->bbFlags & BBF_INTERNAL)
16339 JITDUMP("Marking BBF_INTERNAL block BB%02u as BBF_IMPORTED\n", block->bbNum);
16340 block->bbFlags |= BBF_IMPORTED;
16342 const unsigned numSuccs = block->NumSucc();
16343 for (unsigned i = 0; i < numSuccs; i++)
16345 impImportBlockPending(block->GetSucc(i));
16355 /* Make the block globaly available */
16360 /* Initialize the debug variables */
16361 impCurOpcName = "unknown";
16362 impCurOpcOffs = block->bbCodeOffs;
16365 /* Set the current stack state to the merged result */
16366 verResetCurrentState(block, &verCurrentState);
16368 /* Now walk the code and import the IL into GenTrees */
16370 struct FilterVerificationExceptionsParam
16375 FilterVerificationExceptionsParam param;
16377 param.pThis = this;
16378 param.block = block;
16380 PAL_TRY(FilterVerificationExceptionsParam*, pParam, ¶m)
16382 /* @VERIFICATION : For now, the only state propagation from try
16383 to it's handler is "thisInit" state (stack is empty at start of try).
16384 In general, for state that we track in verification, we need to
16385 model the possibility that an exception might happen at any IL
16386 instruction, so we really need to merge all states that obtain
16387 between IL instructions in a try block into the start states of
16390 However we do not allow the 'this' pointer to be uninitialized when
16391 entering most kinds try regions (only try/fault are allowed to have
16392 an uninitialized this pointer on entry to the try)
16394 Fortunately, the stack is thrown away when an exception
16395 leads to a handler, so we don't have to worry about that.
16396 We DO, however, have to worry about the "thisInit" state.
16397 But only for the try/fault case.
16399 The only allowed transition is from TIS_Uninit to TIS_Init.
16401 So for a try/fault region for the fault handler block
16402 we will merge the start state of the try begin
16403 and the post-state of each block that is part of this try region
16406 // merge the start state of the try begin
16408 if (pParam->block->bbFlags & BBF_TRY_BEG)
16410 pParam->pThis->impVerifyEHBlock(pParam->block, true);
16413 pParam->pThis->impImportBlockCode(pParam->block);
16415 // As discussed above:
16416 // merge the post-state of each block that is part of this try region
16418 if (pParam->block->hasTryIndex())
16420 pParam->pThis->impVerifyEHBlock(pParam->block, false);
16423 PAL_EXCEPT_FILTER(FilterVerificationExceptions)
16425 verHandleVerificationFailure(block DEBUGARG(false));
16429 if (compDonotInline())
16434 assert(!compDonotInline());
16436 markImport = false;
16440 unsigned baseTmp = NO_BASE_TMP; // input temps assigned to successor blocks
16441 bool reimportSpillClique = false;
16442 BasicBlock* tgtBlock = nullptr;
16444 /* If the stack is non-empty, we might have to spill its contents */
16446 if (verCurrentState.esStackDepth != 0)
16448 impBoxTemp = BAD_VAR_NUM; // if a box temp is used in a block that leaves something
16449 // on the stack, its lifetime is hard to determine, simply
16450 // don't reuse such temps.
16452 GenTreePtr addStmt = nullptr;
16454 /* Do the successors of 'block' have any other predecessors ?
16455 We do not want to do some of the optimizations related to multiRef
16456 if we can reimport blocks */
16458 unsigned multRef = impCanReimport ? unsigned(~0) : 0;
16460 switch (block->bbJumpKind)
16464 /* Temporarily remove the 'jtrue' from the end of the tree list */
16466 assert(impTreeLast);
16467 assert(impTreeLast->gtOper == GT_STMT);
16468 assert(impTreeLast->gtStmt.gtStmtExpr->gtOper == GT_JTRUE);
16470 addStmt = impTreeLast;
16471 impTreeLast = impTreeLast->gtPrev;
16473 /* Note if the next block has more than one ancestor */
16475 multRef |= block->bbNext->bbRefs;
16477 /* Does the next block have temps assigned? */
16479 baseTmp = block->bbNext->bbStkTempsIn;
16480 tgtBlock = block->bbNext;
16482 if (baseTmp != NO_BASE_TMP)
16487 /* Try the target of the jump then */
16489 multRef |= block->bbJumpDest->bbRefs;
16490 baseTmp = block->bbJumpDest->bbStkTempsIn;
16491 tgtBlock = block->bbJumpDest;
16495 multRef |= block->bbJumpDest->bbRefs;
16496 baseTmp = block->bbJumpDest->bbStkTempsIn;
16497 tgtBlock = block->bbJumpDest;
16501 multRef |= block->bbNext->bbRefs;
16502 baseTmp = block->bbNext->bbStkTempsIn;
16503 tgtBlock = block->bbNext;
16508 BasicBlock** jmpTab;
16511 /* Temporarily remove the GT_SWITCH from the end of the tree list */
16513 assert(impTreeLast);
16514 assert(impTreeLast->gtOper == GT_STMT);
16515 assert(impTreeLast->gtStmt.gtStmtExpr->gtOper == GT_SWITCH);
16517 addStmt = impTreeLast;
16518 impTreeLast = impTreeLast->gtPrev;
16520 jmpCnt = block->bbJumpSwt->bbsCount;
16521 jmpTab = block->bbJumpSwt->bbsDstTab;
16525 tgtBlock = (*jmpTab);
16527 multRef |= tgtBlock->bbRefs;
16529 // Thanks to spill cliques, we should have assigned all or none
16530 assert((baseTmp == NO_BASE_TMP) || (baseTmp == tgtBlock->bbStkTempsIn));
16531 baseTmp = tgtBlock->bbStkTempsIn;
16536 } while (++jmpTab, --jmpCnt);
16540 case BBJ_CALLFINALLY:
16541 case BBJ_EHCATCHRET:
16543 case BBJ_EHFINALLYRET:
16544 case BBJ_EHFILTERRET:
16546 NO_WAY("can't have 'unreached' end of BB with non-empty stack");
16550 noway_assert(!"Unexpected bbJumpKind");
16554 assert(multRef >= 1);
16556 /* Do we have a base temp number? */
16558 bool newTemps = (baseTmp == NO_BASE_TMP);
16562 /* Grab enough temps for the whole stack */
16563 baseTmp = impGetSpillTmpBase(block);
16566 /* Spill all stack entries into temps */
16567 unsigned level, tempNum;
16569 JITDUMP("\nSpilling stack entries into temps\n");
16570 for (level = 0, tempNum = baseTmp; level < verCurrentState.esStackDepth; level++, tempNum++)
16572 GenTreePtr tree = verCurrentState.esStack[level].val;
16574 /* VC generates code where it pushes a byref from one branch, and an int (ldc.i4 0) from
16575 the other. This should merge to a byref in unverifiable code.
16576 However, if the branch which leaves the TYP_I_IMPL on the stack is imported first, the
16577 successor would be imported assuming there was a TYP_I_IMPL on
16578 the stack. Thus the value would not get GC-tracked. Hence,
16579 change the temp to TYP_BYREF and reimport the successors.
16580 Note: We should only allow this in unverifiable code.
16582 if (tree->gtType == TYP_BYREF && lvaTable[tempNum].lvType == TYP_I_IMPL && !verNeedsVerification())
16584 lvaTable[tempNum].lvType = TYP_BYREF;
16585 impReimportMarkSuccessors(block);
16589 #ifdef _TARGET_64BIT_
16590 if (genActualType(tree->gtType) == TYP_I_IMPL && lvaTable[tempNum].lvType == TYP_INT)
16592 if (tiVerificationNeeded && tgtBlock->bbEntryState != nullptr &&
16593 (tgtBlock->bbFlags & BBF_FAILED_VERIFICATION) == 0)
16595 // Merge the current state into the entry state of block;
16596 // the call to verMergeEntryStates must have changed
16597 // the entry state of the block by merging the int local var
16598 // and the native-int stack entry.
16599 bool changed = false;
16600 if (verMergeEntryStates(tgtBlock, &changed))
16602 impRetypeEntryStateTemps(tgtBlock);
16603 impReimportBlockPending(tgtBlock);
16608 tgtBlock->bbFlags |= BBF_FAILED_VERIFICATION;
16613 // Some other block in the spill clique set this to "int", but now we have "native int".
16614 // Change the type and go back to re-import any blocks that used the wrong type.
16615 lvaTable[tempNum].lvType = TYP_I_IMPL;
16616 reimportSpillClique = true;
16618 else if (genActualType(tree->gtType) == TYP_INT && lvaTable[tempNum].lvType == TYP_I_IMPL)
16620 // Spill clique has decided this should be "native int", but this block only pushes an "int".
16621 // Insert a sign-extension to "native int" so we match the clique.
16622 verCurrentState.esStack[level].val = gtNewCastNode(TYP_I_IMPL, tree, TYP_I_IMPL);
16625 // Consider the case where one branch left a 'byref' on the stack and the other leaves
16626 // an 'int'. On 32-bit, this is allowed (in non-verifiable code) since they are the same
16627 // size. JIT64 managed to make this work on 64-bit. For compatibility, we support JIT64
16628 // behavior instead of asserting and then generating bad code (where we save/restore the
16629 // low 32 bits of a byref pointer to an 'int' sized local). If the 'int' side has been
16630 // imported already, we need to change the type of the local and reimport the spill clique.
16631 // If the 'byref' side has imported, we insert a cast from int to 'native int' to match
16632 // the 'byref' size.
16633 if (!tiVerificationNeeded)
16635 if (genActualType(tree->gtType) == TYP_BYREF && lvaTable[tempNum].lvType == TYP_INT)
16637 // Some other block in the spill clique set this to "int", but now we have "byref".
16638 // Change the type and go back to re-import any blocks that used the wrong type.
16639 lvaTable[tempNum].lvType = TYP_BYREF;
16640 reimportSpillClique = true;
16642 else if (genActualType(tree->gtType) == TYP_INT && lvaTable[tempNum].lvType == TYP_BYREF)
16644 // Spill clique has decided this should be "byref", but this block only pushes an "int".
16645 // Insert a sign-extension to "native int" so we match the clique size.
16646 verCurrentState.esStack[level].val = gtNewCastNode(TYP_I_IMPL, tree, TYP_I_IMPL);
16649 #endif // _TARGET_64BIT_
16651 #if FEATURE_X87_DOUBLES
16652 // X87 stack doesn't differentiate between float/double
16653 // so promoting is no big deal.
16654 // For everybody else keep it as float until we have a collision and then promote
16655 // Just like for x64's TYP_INT<->TYP_I_IMPL
16657 if (multRef > 1 && tree->gtType == TYP_FLOAT)
16659 verCurrentState.esStack[level].val = gtNewCastNode(TYP_DOUBLE, tree, TYP_DOUBLE);
16662 #else // !FEATURE_X87_DOUBLES
16664 if (tree->gtType == TYP_DOUBLE && lvaTable[tempNum].lvType == TYP_FLOAT)
16666 // Some other block in the spill clique set this to "float", but now we have "double".
16667 // Change the type and go back to re-import any blocks that used the wrong type.
16668 lvaTable[tempNum].lvType = TYP_DOUBLE;
16669 reimportSpillClique = true;
16671 else if (tree->gtType == TYP_FLOAT && lvaTable[tempNum].lvType == TYP_DOUBLE)
16673 // Spill clique has decided this should be "double", but this block only pushes a "float".
16674 // Insert a cast to "double" so we match the clique.
16675 verCurrentState.esStack[level].val = gtNewCastNode(TYP_DOUBLE, tree, TYP_DOUBLE);
16678 #endif // FEATURE_X87_DOUBLES
16680 /* If addStmt has a reference to tempNum (can only happen if we
16681 are spilling to the temps already used by a previous block),
16682 we need to spill addStmt */
16684 if (addStmt && !newTemps && gtHasRef(addStmt->gtStmt.gtStmtExpr, tempNum, false))
16686 GenTreePtr addTree = addStmt->gtStmt.gtStmtExpr;
16688 if (addTree->gtOper == GT_JTRUE)
16690 GenTreePtr relOp = addTree->gtOp.gtOp1;
16691 assert(relOp->OperIsCompare());
16693 var_types type = genActualType(relOp->gtOp.gtOp1->TypeGet());
16695 if (gtHasRef(relOp->gtOp.gtOp1, tempNum, false))
16697 unsigned temp = lvaGrabTemp(true DEBUGARG("spill addStmt JTRUE ref Op1"));
16698 impAssignTempGen(temp, relOp->gtOp.gtOp1, level);
16699 type = genActualType(lvaTable[temp].TypeGet());
16700 relOp->gtOp.gtOp1 = gtNewLclvNode(temp, type);
16703 if (gtHasRef(relOp->gtOp.gtOp2, tempNum, false))
16705 unsigned temp = lvaGrabTemp(true DEBUGARG("spill addStmt JTRUE ref Op2"));
16706 impAssignTempGen(temp, relOp->gtOp.gtOp2, level);
16707 type = genActualType(lvaTable[temp].TypeGet());
16708 relOp->gtOp.gtOp2 = gtNewLclvNode(temp, type);
16713 assert(addTree->gtOper == GT_SWITCH && genActualTypeIsIntOrI(addTree->gtOp.gtOp1->TypeGet()));
16715 unsigned temp = lvaGrabTemp(true DEBUGARG("spill addStmt SWITCH"));
16716 impAssignTempGen(temp, addTree->gtOp.gtOp1, level);
16717 addTree->gtOp.gtOp1 = gtNewLclvNode(temp, genActualType(addTree->gtOp.gtOp1->TypeGet()));
16721 /* Spill the stack entry, and replace with the temp */
16723 if (!impSpillStackEntry(level, tempNum
16726 true, "Spill Stack Entry"
16732 BADCODE("bad stack state");
16735 // Oops. Something went wrong when spilling. Bad code.
16736 verHandleVerificationFailure(block DEBUGARG(true));
16742 /* Put back the 'jtrue'/'switch' if we removed it earlier */
16746 impAppendStmt(addStmt, (unsigned)CHECK_SPILL_NONE);
16750 // Some of the append/spill logic works on compCurBB
16752 assert(compCurBB == block);
16754 /* Save the tree list in the block */
16755 impEndTreeList(block);
16757 // impEndTreeList sets BBF_IMPORTED on the block
16758 // We do *NOT* want to set it later than this because
16759 // impReimportSpillClique might clear it if this block is both a
16760 // predecessor and successor in the current spill clique
16761 assert(block->bbFlags & BBF_IMPORTED);
16763 // If we had a int/native int, or float/double collision, we need to re-import
16764 if (reimportSpillClique)
16766 // This will re-import all the successors of block (as well as each of their predecessors)
16767 impReimportSpillClique(block);
16769 // For blocks that haven't been imported yet, we still need to mark them as pending import.
16770 const unsigned numSuccs = block->NumSucc();
16771 for (unsigned i = 0; i < numSuccs; i++)
16773 BasicBlock* succ = block->GetSucc(i);
16774 if ((succ->bbFlags & BBF_IMPORTED) == 0)
16776 impImportBlockPending(succ);
16780 else // the normal case
16782 // otherwise just import the successors of block
16784 /* Does this block jump to any other blocks? */
16785 const unsigned numSuccs = block->NumSucc();
16786 for (unsigned i = 0; i < numSuccs; i++)
16788 impImportBlockPending(block->GetSucc(i));
16793 #pragma warning(pop)
16796 /*****************************************************************************/
16798 // Ensures that "block" is a member of the list of BBs waiting to be imported, pushing it on the list if
16799 // necessary (and ensures that it is a member of the set of BB's on the list, by setting its byte in
16800 // impPendingBlockMembers). Merges the current verification state into the verification state of "block"
16801 // (its "pre-state").
16803 void Compiler::impImportBlockPending(BasicBlock* block)
16808 printf("\nimpImportBlockPending for BB%02u\n", block->bbNum);
16812 // We will add a block to the pending set if it has not already been imported (or needs to be re-imported),
16813 // or if it has, but merging in a predecessor's post-state changes the block's pre-state.
16814 // (When we're doing verification, we always attempt the merge to detect verification errors.)
16816 // If the block has not been imported, add to pending set.
16817 bool addToPending = ((block->bbFlags & BBF_IMPORTED) == 0);
16819 // Initialize bbEntryState just the first time we try to add this block to the pending list
16820 // Just because bbEntryState is NULL, doesn't mean the pre-state wasn't previously set
16821 // We use NULL to indicate the 'common' state to avoid memory allocation
16822 if ((block->bbEntryState == nullptr) && ((block->bbFlags & (BBF_IMPORTED | BBF_FAILED_VERIFICATION)) == 0) &&
16823 (impGetPendingBlockMember(block) == 0))
16825 verInitBBEntryState(block, &verCurrentState);
16826 assert(block->bbStkDepth == 0);
16827 block->bbStkDepth = static_cast<unsigned short>(verCurrentState.esStackDepth);
16828 assert(addToPending);
16829 assert(impGetPendingBlockMember(block) == 0);
16833 // The stack should have the same height on entry to the block from all its predecessors.
16834 if (block->bbStkDepth != verCurrentState.esStackDepth)
16838 sprintf_s(buffer, sizeof(buffer),
16839 "Block at offset %4.4x to %4.4x in %s entered with different stack depths.\n"
16840 "Previous depth was %d, current depth is %d",
16841 block->bbCodeOffs, block->bbCodeOffsEnd, info.compFullName, block->bbStkDepth,
16842 verCurrentState.esStackDepth);
16843 buffer[400 - 1] = 0;
16846 NO_WAY("Block entered with different stack depths");
16850 // Additionally, if we need to verify, merge the verification state.
16851 if (tiVerificationNeeded)
16853 // Merge the current state into the entry state of block; if this does not change the entry state
16854 // by merging, do not add the block to the pending-list.
16855 bool changed = false;
16856 if (!verMergeEntryStates(block, &changed))
16858 block->bbFlags |= BBF_FAILED_VERIFICATION;
16859 addToPending = true; // We will pop it off, and check the flag set above.
16863 addToPending = true;
16865 JITDUMP("Adding BB%02u to pending set due to new merge result\n", block->bbNum);
16874 if (block->bbStkDepth > 0)
16876 // We need to fix the types of any spill temps that might have changed:
16877 // int->native int, float->double, int->byref, etc.
16878 impRetypeEntryStateTemps(block);
16881 // OK, we must add to the pending list, if it's not already in it.
16882 if (impGetPendingBlockMember(block) != 0)
16888 // Get an entry to add to the pending list
16892 if (impPendingFree)
16894 // We can reuse one of the freed up dscs.
16895 dsc = impPendingFree;
16896 impPendingFree = dsc->pdNext;
16900 // We have to create a new dsc
16901 dsc = new (this, CMK_Unknown) PendingDsc;
16905 dsc->pdSavedStack.ssDepth = verCurrentState.esStackDepth;
16906 dsc->pdThisPtrInit = verCurrentState.thisInitialized;
16908 // Save the stack trees for later
16910 if (verCurrentState.esStackDepth)
16912 impSaveStackState(&dsc->pdSavedStack, false);
16915 // Add the entry to the pending list
16917 dsc->pdNext = impPendingList;
16918 impPendingList = dsc;
16919 impSetPendingBlockMember(block, 1); // And indicate that it's now a member of the set.
16921 // Various assertions require us to now to consider the block as not imported (at least for
16922 // the final time...)
16923 block->bbFlags &= ~BBF_IMPORTED;
16928 printf("Added PendingDsc - %08p for BB%02u\n", dspPtr(dsc), block->bbNum);
16933 /*****************************************************************************/
16935 // Ensures that "block" is a member of the list of BBs waiting to be imported, pushing it on the list if
16936 // necessary (and ensures that it is a member of the set of BB's on the list, by setting its byte in
16937 // impPendingBlockMembers). Does *NOT* change the existing "pre-state" of the block.
16939 void Compiler::impReimportBlockPending(BasicBlock* block)
16941 JITDUMP("\nimpReimportBlockPending for BB%02u", block->bbNum);
16943 assert(block->bbFlags & BBF_IMPORTED);
16945 // OK, we must add to the pending list, if it's not already in it.
16946 if (impGetPendingBlockMember(block) != 0)
16951 // Get an entry to add to the pending list
16955 if (impPendingFree)
16957 // We can reuse one of the freed up dscs.
16958 dsc = impPendingFree;
16959 impPendingFree = dsc->pdNext;
16963 // We have to create a new dsc
16964 dsc = new (this, CMK_ImpStack) PendingDsc;
16969 if (block->bbEntryState)
16971 dsc->pdThisPtrInit = block->bbEntryState->thisInitialized;
16972 dsc->pdSavedStack.ssDepth = block->bbEntryState->esStackDepth;
16973 dsc->pdSavedStack.ssTrees = block->bbEntryState->esStack;
16977 dsc->pdThisPtrInit = TIS_Bottom;
16978 dsc->pdSavedStack.ssDepth = 0;
16979 dsc->pdSavedStack.ssTrees = nullptr;
16982 // Add the entry to the pending list
16984 dsc->pdNext = impPendingList;
16985 impPendingList = dsc;
16986 impSetPendingBlockMember(block, 1); // And indicate that it's now a member of the set.
16988 // Various assertions require us to now to consider the block as not imported (at least for
16989 // the final time...)
16990 block->bbFlags &= ~BBF_IMPORTED;
16995 printf("Added PendingDsc - %08p for BB%02u\n", dspPtr(dsc), block->bbNum);
17000 void* Compiler::BlockListNode::operator new(size_t sz, Compiler* comp)
17002 if (comp->impBlockListNodeFreeList == nullptr)
17004 return (BlockListNode*)comp->compGetMem(sizeof(BlockListNode), CMK_BasicBlock);
17008 BlockListNode* res = comp->impBlockListNodeFreeList;
17009 comp->impBlockListNodeFreeList = res->m_next;
17014 void Compiler::FreeBlockListNode(Compiler::BlockListNode* node)
17016 node->m_next = impBlockListNodeFreeList;
17017 impBlockListNodeFreeList = node;
17020 void Compiler::impWalkSpillCliqueFromPred(BasicBlock* block, SpillCliqueWalker* callback)
17024 noway_assert(!fgComputePredsDone);
17025 if (!fgCheapPredsValid)
17027 fgComputeCheapPreds();
17030 BlockListNode* succCliqueToDo = nullptr;
17031 BlockListNode* predCliqueToDo = new (this) BlockListNode(block);
17035 // Look at the successors of every member of the predecessor to-do list.
17036 while (predCliqueToDo != nullptr)
17038 BlockListNode* node = predCliqueToDo;
17039 predCliqueToDo = node->m_next;
17040 BasicBlock* blk = node->m_blk;
17041 FreeBlockListNode(node);
17043 const unsigned numSuccs = blk->NumSucc();
17044 for (unsigned succNum = 0; succNum < numSuccs; succNum++)
17046 BasicBlock* succ = blk->GetSucc(succNum);
17047 // If it's not already in the clique, add it, and also add it
17048 // as a member of the successor "toDo" set.
17049 if (impSpillCliqueGetMember(SpillCliqueSucc, succ) == 0)
17051 callback->Visit(SpillCliqueSucc, succ);
17052 impSpillCliqueSetMember(SpillCliqueSucc, succ, 1);
17053 succCliqueToDo = new (this) BlockListNode(succ, succCliqueToDo);
17058 // Look at the predecessors of every member of the successor to-do list.
17059 while (succCliqueToDo != nullptr)
17061 BlockListNode* node = succCliqueToDo;
17062 succCliqueToDo = node->m_next;
17063 BasicBlock* blk = node->m_blk;
17064 FreeBlockListNode(node);
17066 for (BasicBlockList* pred = blk->bbCheapPreds; pred != nullptr; pred = pred->next)
17068 BasicBlock* predBlock = pred->block;
17069 // If it's not already in the clique, add it, and also add it
17070 // as a member of the predecessor "toDo" set.
17071 if (impSpillCliqueGetMember(SpillCliquePred, predBlock) == 0)
17073 callback->Visit(SpillCliquePred, predBlock);
17074 impSpillCliqueSetMember(SpillCliquePred, predBlock, 1);
17075 predCliqueToDo = new (this) BlockListNode(predBlock, predCliqueToDo);
17082 // If this fails, it means we didn't walk the spill clique properly and somehow managed
17083 // miss walking back to include the predecessor we started from.
17084 // This most likely cause: missing or out of date bbPreds
17085 assert(impSpillCliqueGetMember(SpillCliquePred, block) != 0);
17088 void Compiler::SetSpillTempsBase::Visit(SpillCliqueDir predOrSucc, BasicBlock* blk)
17090 if (predOrSucc == SpillCliqueSucc)
17092 assert(blk->bbStkTempsIn == NO_BASE_TMP); // Should not already be a member of a clique as a successor.
17093 blk->bbStkTempsIn = m_baseTmp;
17097 assert(predOrSucc == SpillCliquePred);
17098 assert(blk->bbStkTempsOut == NO_BASE_TMP); // Should not already be a member of a clique as a predecessor.
17099 blk->bbStkTempsOut = m_baseTmp;
17103 void Compiler::ReimportSpillClique::Visit(SpillCliqueDir predOrSucc, BasicBlock* blk)
17105 // For Preds we could be a little smarter and just find the existing store
17106 // and re-type it/add a cast, but that is complicated and hopefully very rare, so
17107 // just re-import the whole block (just like we do for successors)
17109 if (((blk->bbFlags & BBF_IMPORTED) == 0) && (m_pComp->impGetPendingBlockMember(blk) == 0))
17111 // If we haven't imported this block and we're not going to (because it isn't on
17112 // the pending list) then just ignore it for now.
17114 // This block has either never been imported (EntryState == NULL) or it failed
17115 // verification. Neither state requires us to force it to be imported now.
17116 assert((blk->bbEntryState == nullptr) || (blk->bbFlags & BBF_FAILED_VERIFICATION));
17120 // For successors we have a valid verCurrentState, so just mark them for reimport
17121 // the 'normal' way
17122 // Unlike predecessors, we *DO* need to reimport the current block because the
17123 // initial import had the wrong entry state types.
17124 // Similarly, blocks that are currently on the pending list, still need to call
17125 // impImportBlockPending to fixup their entry state.
17126 if (predOrSucc == SpillCliqueSucc)
17128 m_pComp->impReimportMarkBlock(blk);
17130 // Set the current stack state to that of the blk->bbEntryState
17131 m_pComp->verResetCurrentState(blk, &m_pComp->verCurrentState);
17132 assert(m_pComp->verCurrentState.thisInitialized == blk->bbThisOnEntry());
17134 m_pComp->impImportBlockPending(blk);
17136 else if ((blk != m_pComp->compCurBB) && ((blk->bbFlags & BBF_IMPORTED) != 0))
17138 // As described above, we are only visiting predecessors so they can
17139 // add the appropriate casts, since we have already done that for the current
17140 // block, it does not need to be reimported.
17141 // Nor do we need to reimport blocks that are still pending, but not yet
17144 // For predecessors, we have no state to seed the EntryState, so we just have
17145 // to assume the existing one is correct.
17146 // If the block is also a successor, it will get the EntryState properly
17147 // updated when it is visited as a successor in the above "if" block.
17148 assert(predOrSucc == SpillCliquePred);
17149 m_pComp->impReimportBlockPending(blk);
17153 // Re-type the incoming lclVar nodes to match the varDsc.
17154 void Compiler::impRetypeEntryStateTemps(BasicBlock* blk)
17156 if (blk->bbEntryState != nullptr)
17158 EntryState* es = blk->bbEntryState;
17159 for (unsigned level = 0; level < es->esStackDepth; level++)
17161 GenTreePtr tree = es->esStack[level].val;
17162 if ((tree->gtOper == GT_LCL_VAR) || (tree->gtOper == GT_LCL_FLD))
17164 unsigned lclNum = tree->gtLclVarCommon.gtLclNum;
17165 noway_assert(lclNum < lvaCount);
17166 LclVarDsc* varDsc = lvaTable + lclNum;
17167 es->esStack[level].val->gtType = varDsc->TypeGet();
17173 unsigned Compiler::impGetSpillTmpBase(BasicBlock* block)
17175 if (block->bbStkTempsOut != NO_BASE_TMP)
17177 return block->bbStkTempsOut;
17183 printf("\n*************** In impGetSpillTmpBase(BB%02u)\n", block->bbNum);
17187 // Otherwise, choose one, and propagate to all members of the spill clique.
17188 // Grab enough temps for the whole stack.
17189 unsigned baseTmp = lvaGrabTemps(verCurrentState.esStackDepth DEBUGARG("IL Stack Entries"));
17190 SetSpillTempsBase callback(baseTmp);
17192 // We do *NOT* need to reset the SpillClique*Members because a block can only be the predecessor
17193 // to one spill clique, and similarly can only be the sucessor to one spill clique
17194 impWalkSpillCliqueFromPred(block, &callback);
17199 void Compiler::impReimportSpillClique(BasicBlock* block)
17204 printf("\n*************** In impReimportSpillClique(BB%02u)\n", block->bbNum);
17208 // If we get here, it is because this block is already part of a spill clique
17209 // and one predecessor had an outgoing live stack slot of type int, and this
17210 // block has an outgoing live stack slot of type native int.
17211 // We need to reset these before traversal because they have already been set
17212 // by the previous walk to determine all the members of the spill clique.
17213 impInlineRoot()->impSpillCliquePredMembers.Reset();
17214 impInlineRoot()->impSpillCliqueSuccMembers.Reset();
17216 ReimportSpillClique callback(this);
17218 impWalkSpillCliqueFromPred(block, &callback);
17221 // Set the pre-state of "block" (which should not have a pre-state allocated) to
17222 // a copy of "srcState", cloning tree pointers as required.
17223 void Compiler::verInitBBEntryState(BasicBlock* block, EntryState* srcState)
17225 if (srcState->esStackDepth == 0 && srcState->thisInitialized == TIS_Bottom)
17227 block->bbEntryState = nullptr;
17231 block->bbEntryState = (EntryState*)compGetMem(sizeof(EntryState));
17233 // block->bbEntryState.esRefcount = 1;
17235 block->bbEntryState->esStackDepth = srcState->esStackDepth;
17236 block->bbEntryState->thisInitialized = TIS_Bottom;
17238 if (srcState->esStackDepth > 0)
17240 block->bbSetStack(new (this, CMK_Unknown) StackEntry[srcState->esStackDepth]);
17241 unsigned stackSize = srcState->esStackDepth * sizeof(StackEntry);
17243 memcpy(block->bbEntryState->esStack, srcState->esStack, stackSize);
17244 for (unsigned level = 0; level < srcState->esStackDepth; level++)
17246 GenTreePtr tree = srcState->esStack[level].val;
17247 block->bbEntryState->esStack[level].val = gtCloneExpr(tree);
17251 if (verTrackObjCtorInitState)
17253 verSetThisInit(block, srcState->thisInitialized);
17259 void Compiler::verSetThisInit(BasicBlock* block, ThisInitState tis)
17261 assert(tis != TIS_Bottom); // Precondition.
17262 if (block->bbEntryState == nullptr)
17264 block->bbEntryState = new (this, CMK_Unknown) EntryState();
17267 block->bbEntryState->thisInitialized = tis;
17271 * Resets the current state to the state at the start of the basic block
17273 void Compiler::verResetCurrentState(BasicBlock* block, EntryState* destState)
17276 if (block->bbEntryState == nullptr)
17278 destState->esStackDepth = 0;
17279 destState->thisInitialized = TIS_Bottom;
17283 destState->esStackDepth = block->bbEntryState->esStackDepth;
17285 if (destState->esStackDepth > 0)
17287 unsigned stackSize = destState->esStackDepth * sizeof(StackEntry);
17289 memcpy(destState->esStack, block->bbStackOnEntry(), stackSize);
17292 destState->thisInitialized = block->bbThisOnEntry();
17297 ThisInitState BasicBlock::bbThisOnEntry()
17299 return bbEntryState ? bbEntryState->thisInitialized : TIS_Bottom;
17302 unsigned BasicBlock::bbStackDepthOnEntry()
17304 return (bbEntryState ? bbEntryState->esStackDepth : 0);
17307 void BasicBlock::bbSetStack(void* stackBuffer)
17309 assert(bbEntryState);
17310 assert(stackBuffer);
17311 bbEntryState->esStack = (StackEntry*)stackBuffer;
17314 StackEntry* BasicBlock::bbStackOnEntry()
17316 assert(bbEntryState);
17317 return bbEntryState->esStack;
17320 void Compiler::verInitCurrentState()
17322 verTrackObjCtorInitState = FALSE;
17323 verCurrentState.thisInitialized = TIS_Bottom;
17325 if (tiVerificationNeeded)
17327 // Track this ptr initialization
17328 if (!info.compIsStatic && (info.compFlags & CORINFO_FLG_CONSTRUCTOR) && lvaTable[0].lvVerTypeInfo.IsObjRef())
17330 verTrackObjCtorInitState = TRUE;
17331 verCurrentState.thisInitialized = TIS_Uninit;
17335 // initialize stack info
17337 verCurrentState.esStackDepth = 0;
17338 assert(verCurrentState.esStack != nullptr);
17340 // copy current state to entry state of first BB
17341 verInitBBEntryState(fgFirstBB, &verCurrentState);
17344 Compiler* Compiler::impInlineRoot()
17346 if (impInlineInfo == nullptr)
17352 return impInlineInfo->InlineRoot;
17356 BYTE Compiler::impSpillCliqueGetMember(SpillCliqueDir predOrSucc, BasicBlock* blk)
17358 if (predOrSucc == SpillCliquePred)
17360 return impInlineRoot()->impSpillCliquePredMembers.Get(blk->bbInd());
17364 assert(predOrSucc == SpillCliqueSucc);
17365 return impInlineRoot()->impSpillCliqueSuccMembers.Get(blk->bbInd());
17369 void Compiler::impSpillCliqueSetMember(SpillCliqueDir predOrSucc, BasicBlock* blk, BYTE val)
17371 if (predOrSucc == SpillCliquePred)
17373 impInlineRoot()->impSpillCliquePredMembers.Set(blk->bbInd(), val);
17377 assert(predOrSucc == SpillCliqueSucc);
17378 impInlineRoot()->impSpillCliqueSuccMembers.Set(blk->bbInd(), val);
17382 /*****************************************************************************
17384 * Convert the instrs ("import") into our internal format (trees). The
17385 * basic flowgraph has already been constructed and is passed in.
17388 void Compiler::impImport(BasicBlock* method)
17393 printf("*************** In impImport() for %s\n", info.compFullName);
17397 /* Allocate the stack contents */
17399 if (info.compMaxStack <= _countof(impSmallStack))
17401 /* Use local variable, don't waste time allocating on the heap */
17403 impStkSize = _countof(impSmallStack);
17404 verCurrentState.esStack = impSmallStack;
17408 impStkSize = info.compMaxStack;
17409 verCurrentState.esStack = new (this, CMK_ImpStack) StackEntry[impStkSize];
17412 // initialize the entry state at start of method
17413 verInitCurrentState();
17415 // Initialize stuff related to figuring "spill cliques" (see spec comment for impGetSpillTmpBase).
17416 Compiler* inlineRoot = impInlineRoot();
17417 if (this == inlineRoot) // These are only used on the root of the inlining tree.
17419 // We have initialized these previously, but to size 0. Make them larger.
17420 impPendingBlockMembers.Init(getAllocator(), fgBBNumMax * 2);
17421 impSpillCliquePredMembers.Init(getAllocator(), fgBBNumMax * 2);
17422 impSpillCliqueSuccMembers.Init(getAllocator(), fgBBNumMax * 2);
17424 inlineRoot->impPendingBlockMembers.Reset(fgBBNumMax * 2);
17425 inlineRoot->impSpillCliquePredMembers.Reset(fgBBNumMax * 2);
17426 inlineRoot->impSpillCliqueSuccMembers.Reset(fgBBNumMax * 2);
17427 impBlockListNodeFreeList = nullptr;
17430 impLastILoffsStmt = nullptr;
17431 impNestedStackSpill = false;
17433 impBoxTemp = BAD_VAR_NUM;
17435 impPendingList = impPendingFree = nullptr;
17437 /* Add the entry-point to the worker-list */
17439 // Skip leading internal blocks. There can be one as a leading scratch BB, and more
17440 // from EH normalization.
17441 // NOTE: It might be possible to always just put fgFirstBB on the pending list, and let everything else just fall
17443 for (; method->bbFlags & BBF_INTERNAL; method = method->bbNext)
17445 // Treat these as imported.
17446 assert(method->bbJumpKind == BBJ_NONE); // We assume all the leading ones are fallthrough.
17447 JITDUMP("Marking leading BBF_INTERNAL block BB%02u as BBF_IMPORTED\n", method->bbNum);
17448 method->bbFlags |= BBF_IMPORTED;
17451 impImportBlockPending(method);
17453 /* Import blocks in the worker-list until there are no more */
17455 while (impPendingList)
17457 /* Remove the entry at the front of the list */
17459 PendingDsc* dsc = impPendingList;
17460 impPendingList = impPendingList->pdNext;
17461 impSetPendingBlockMember(dsc->pdBB, 0);
17463 /* Restore the stack state */
17465 verCurrentState.thisInitialized = dsc->pdThisPtrInit;
17466 verCurrentState.esStackDepth = dsc->pdSavedStack.ssDepth;
17467 if (verCurrentState.esStackDepth)
17469 impRestoreStackState(&dsc->pdSavedStack);
17472 /* Add the entry to the free list for reuse */
17474 dsc->pdNext = impPendingFree;
17475 impPendingFree = dsc;
17477 /* Now import the block */
17479 if (dsc->pdBB->bbFlags & BBF_FAILED_VERIFICATION)
17482 #ifdef _TARGET_64BIT_
17483 // On AMD64, during verification we have to match JIT64 behavior since the VM is very tighly
17484 // coupled with the JIT64 IL Verification logic. Look inside verHandleVerificationFailure
17485 // method for further explanation on why we raise this exception instead of making the jitted
17486 // code throw the verification exception during execution.
17487 if (tiVerificationNeeded && opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IMPORT_ONLY))
17489 BADCODE("Basic block marked as not verifiable");
17492 #endif // _TARGET_64BIT_
17494 verConvertBBToThrowVerificationException(dsc->pdBB DEBUGARG(true));
17495 impEndTreeList(dsc->pdBB);
17500 impImportBlock(dsc->pdBB);
17502 if (compDonotInline())
17506 if (compIsForImportOnly() && !tiVerificationNeeded)
17514 if (verbose && info.compXcptnsCount)
17516 printf("\nAfter impImport() added block for try,catch,finally");
17517 fgDispBasicBlocks();
17521 // Used in impImportBlockPending() for STRESS_CHK_REIMPORT
17522 for (BasicBlock* block = fgFirstBB; block; block = block->bbNext)
17524 block->bbFlags &= ~BBF_VISITED;
17528 assert(!compIsForInlining() || !tiVerificationNeeded);
17531 // Checks if a typeinfo (usually stored in the type stack) is a struct.
17532 // The invariant here is that if it's not a ref or a method and has a class handle
17533 // it's a valuetype
17534 bool Compiler::impIsValueType(typeInfo* pTypeInfo)
17536 if (pTypeInfo && pTypeInfo->IsValueClassWithClsHnd())
17546 /*****************************************************************************
17547 * Check to see if the tree is the address of a local or
17548 the address of a field in a local.
17550 *lclVarTreeOut will contain the GT_LCL_VAR tree when it returns TRUE.
17554 BOOL Compiler::impIsAddressInLocal(GenTreePtr tree, GenTreePtr* lclVarTreeOut)
17556 if (tree->gtOper != GT_ADDR)
17561 GenTreePtr op = tree->gtOp.gtOp1;
17562 while (op->gtOper == GT_FIELD)
17564 op = op->gtField.gtFldObj;
17565 if (op && op->gtOper == GT_ADDR) // Skip static fields where op will be NULL.
17567 op = op->gtOp.gtOp1;
17575 if (op->gtOper == GT_LCL_VAR)
17577 *lclVarTreeOut = op;
17586 //------------------------------------------------------------------------
17587 // impMakeDiscretionaryInlineObservations: make observations that help
17588 // determine the profitability of a discretionary inline
17591 // pInlineInfo -- InlineInfo for the inline, or null for the prejit root
17592 // inlineResult -- InlineResult accumulating information about this inline
17595 // If inlining or prejitting the root, this method also makes
17596 // various observations about the method that factor into inline
17597 // decisions. It sets `compNativeSizeEstimate` as a side effect.
17599 void Compiler::impMakeDiscretionaryInlineObservations(InlineInfo* pInlineInfo, InlineResult* inlineResult)
17601 assert(pInlineInfo != nullptr && compIsForInlining() || // Perform the actual inlining.
17602 pInlineInfo == nullptr && !compIsForInlining() // Calculate the static inlining hint for ngen.
17605 // If we're really inlining, we should just have one result in play.
17606 assert((pInlineInfo == nullptr) || (inlineResult == pInlineInfo->inlineResult));
17608 // If this is a "forceinline" method, the JIT probably shouldn't have gone
17609 // to the trouble of estimating the native code size. Even if it did, it
17610 // shouldn't be relying on the result of this method.
17611 assert(inlineResult->GetObservation() == InlineObservation::CALLEE_IS_DISCRETIONARY_INLINE);
17613 // Note if the caller contains NEWOBJ or NEWARR.
17614 Compiler* rootCompiler = impInlineRoot();
17616 if ((rootCompiler->optMethodFlags & OMF_HAS_NEWARRAY) != 0)
17618 inlineResult->Note(InlineObservation::CALLER_HAS_NEWARRAY);
17621 if ((rootCompiler->optMethodFlags & OMF_HAS_NEWOBJ) != 0)
17623 inlineResult->Note(InlineObservation::CALLER_HAS_NEWOBJ);
17626 bool calleeIsStatic = (info.compFlags & CORINFO_FLG_STATIC) != 0;
17627 bool isSpecialMethod = (info.compFlags & CORINFO_FLG_CONSTRUCTOR) != 0;
17629 if (isSpecialMethod)
17631 if (calleeIsStatic)
17633 inlineResult->Note(InlineObservation::CALLEE_IS_CLASS_CTOR);
17637 inlineResult->Note(InlineObservation::CALLEE_IS_INSTANCE_CTOR);
17640 else if (!calleeIsStatic)
17642 // Callee is an instance method.
17644 // Check if the callee has the same 'this' as the root.
17645 if (pInlineInfo != nullptr)
17647 GenTreePtr thisArg = pInlineInfo->iciCall->gtCall.gtCallObjp;
17649 bool isSameThis = impIsThis(thisArg);
17650 inlineResult->NoteBool(InlineObservation::CALLSITE_IS_SAME_THIS, isSameThis);
17654 // Note if the callee's class is a promotable struct
17655 if ((info.compClassAttr & CORINFO_FLG_VALUECLASS) != 0)
17657 lvaStructPromotionInfo structPromotionInfo;
17658 lvaCanPromoteStructType(info.compClassHnd, &structPromotionInfo, false);
17659 if (structPromotionInfo.canPromote)
17661 inlineResult->Note(InlineObservation::CALLEE_CLASS_PROMOTABLE);
17665 #ifdef FEATURE_SIMD
17667 // Note if this method is has SIMD args or return value
17668 if (pInlineInfo != nullptr && pInlineInfo->hasSIMDTypeArgLocalOrReturn)
17670 inlineResult->Note(InlineObservation::CALLEE_HAS_SIMD);
17673 #endif // FEATURE_SIMD
17675 // Roughly classify callsite frequency.
17676 InlineCallsiteFrequency frequency = InlineCallsiteFrequency::UNUSED;
17678 // If this is a prejit root, or a maximally hot block...
17679 if ((pInlineInfo == nullptr) || (pInlineInfo->iciBlock->bbWeight >= BB_MAX_WEIGHT))
17681 frequency = InlineCallsiteFrequency::HOT;
17683 // No training data. Look for loop-like things.
17684 // We consider a recursive call loop-like. Do not give the inlining boost to the method itself.
17685 // However, give it to things nearby.
17686 else if ((pInlineInfo->iciBlock->bbFlags & BBF_BACKWARD_JUMP) &&
17687 (pInlineInfo->fncHandle != pInlineInfo->inlineCandidateInfo->ilCallerHandle))
17689 frequency = InlineCallsiteFrequency::LOOP;
17691 else if (pInlineInfo->iciBlock->hasProfileWeight() && (pInlineInfo->iciBlock->bbWeight > BB_ZERO_WEIGHT))
17693 frequency = InlineCallsiteFrequency::WARM;
17695 // Now modify the multiplier based on where we're called from.
17696 else if (pInlineInfo->iciBlock->isRunRarely() || ((info.compFlags & FLG_CCTOR) == FLG_CCTOR))
17698 frequency = InlineCallsiteFrequency::RARE;
17702 frequency = InlineCallsiteFrequency::BORING;
17705 // Also capture the block weight of the call site. In the prejit
17706 // root case, assume there's some hot call site for this method.
17707 unsigned weight = 0;
17709 if (pInlineInfo != nullptr)
17711 weight = pInlineInfo->iciBlock->bbWeight;
17715 weight = BB_MAX_WEIGHT;
17718 inlineResult->NoteInt(InlineObservation::CALLSITE_FREQUENCY, static_cast<int>(frequency));
17719 inlineResult->NoteInt(InlineObservation::CALLSITE_WEIGHT, static_cast<int>(weight));
17722 /*****************************************************************************
17723 This method makes STATIC inlining decision based on the IL code.
17724 It should not make any inlining decision based on the context.
17725 If forceInline is true, then the inlining decision should not depend on
17726 performance heuristics (code size, etc.).
17729 void Compiler::impCanInlineIL(CORINFO_METHOD_HANDLE fncHandle,
17730 CORINFO_METHOD_INFO* methInfo,
17732 InlineResult* inlineResult)
17734 unsigned codeSize = methInfo->ILCodeSize;
17736 // We shouldn't have made up our minds yet...
17737 assert(!inlineResult->IsDecided());
17739 if (methInfo->EHcount)
17741 inlineResult->NoteFatal(InlineObservation::CALLEE_HAS_EH);
17745 if ((methInfo->ILCode == nullptr) || (codeSize == 0))
17747 inlineResult->NoteFatal(InlineObservation::CALLEE_HAS_NO_BODY);
17751 // For now we don't inline varargs (import code can't handle it)
17753 if (methInfo->args.isVarArg())
17755 inlineResult->NoteFatal(InlineObservation::CALLEE_HAS_MANAGED_VARARGS);
17759 // Reject if it has too many locals.
17760 // This is currently an implementation limit due to fixed-size arrays in the
17761 // inline info, rather than a performance heuristic.
17763 inlineResult->NoteInt(InlineObservation::CALLEE_NUMBER_OF_LOCALS, methInfo->locals.numArgs);
17765 if (methInfo->locals.numArgs > MAX_INL_LCLS)
17767 inlineResult->NoteFatal(InlineObservation::CALLEE_TOO_MANY_LOCALS);
17771 // Make sure there aren't too many arguments.
17772 // This is currently an implementation limit due to fixed-size arrays in the
17773 // inline info, rather than a performance heuristic.
17775 inlineResult->NoteInt(InlineObservation::CALLEE_NUMBER_OF_ARGUMENTS, methInfo->args.numArgs);
17777 if (methInfo->args.numArgs > MAX_INL_ARGS)
17779 inlineResult->NoteFatal(InlineObservation::CALLEE_TOO_MANY_ARGUMENTS);
17783 // Note force inline state
17785 inlineResult->NoteBool(InlineObservation::CALLEE_IS_FORCE_INLINE, forceInline);
17787 // Note IL code size
17789 inlineResult->NoteInt(InlineObservation::CALLEE_IL_CODE_SIZE, codeSize);
17791 if (inlineResult->IsFailure())
17796 // Make sure maxstack is not too big
17798 inlineResult->NoteInt(InlineObservation::CALLEE_MAXSTACK, methInfo->maxStack);
17800 if (inlineResult->IsFailure())
17806 /*****************************************************************************
17809 void Compiler::impCheckCanInline(GenTreePtr call,
17810 CORINFO_METHOD_HANDLE fncHandle,
17812 CORINFO_CONTEXT_HANDLE exactContextHnd,
17813 InlineCandidateInfo** ppInlineCandidateInfo,
17814 InlineResult* inlineResult)
17816 // Either EE or JIT might throw exceptions below.
17817 // If that happens, just don't inline the method.
17823 CORINFO_METHOD_HANDLE fncHandle;
17825 CORINFO_CONTEXT_HANDLE exactContextHnd;
17826 InlineResult* result;
17827 InlineCandidateInfo** ppInlineCandidateInfo;
17829 memset(¶m, 0, sizeof(param));
17831 param.pThis = this;
17833 param.fncHandle = fncHandle;
17834 param.methAttr = methAttr;
17835 param.exactContextHnd = (exactContextHnd != nullptr) ? exactContextHnd : MAKE_METHODCONTEXT(fncHandle);
17836 param.result = inlineResult;
17837 param.ppInlineCandidateInfo = ppInlineCandidateInfo;
17839 bool success = eeRunWithErrorTrap<Param>(
17840 [](Param* pParam) {
17841 DWORD dwRestrictions = 0;
17842 CorInfoInitClassResult initClassResult;
17845 const char* methodName;
17846 const char* className;
17847 methodName = pParam->pThis->eeGetMethodName(pParam->fncHandle, &className);
17849 if (JitConfig.JitNoInline())
17851 pParam->result->NoteFatal(InlineObservation::CALLEE_IS_JIT_NOINLINE);
17856 /* Try to get the code address/size for the method */
17858 CORINFO_METHOD_INFO methInfo;
17859 if (!pParam->pThis->info.compCompHnd->getMethodInfo(pParam->fncHandle, &methInfo))
17861 pParam->result->NoteFatal(InlineObservation::CALLEE_NO_METHOD_INFO);
17866 forceInline = !!(pParam->methAttr & CORINFO_FLG_FORCEINLINE);
17868 pParam->pThis->impCanInlineIL(pParam->fncHandle, &methInfo, forceInline, pParam->result);
17870 if (pParam->result->IsFailure())
17872 assert(pParam->result->IsNever());
17876 // Speculatively check if initClass() can be done.
17877 // If it can be done, we will try to inline the method. If inlining
17878 // succeeds, then we will do the non-speculative initClass() and commit it.
17879 // If this speculative call to initClass() fails, there is no point
17880 // trying to inline this method.
17882 pParam->pThis->info.compCompHnd->initClass(nullptr /* field */, pParam->fncHandle /* method */,
17883 pParam->exactContextHnd /* context */,
17884 TRUE /* speculative */);
17886 if (initClassResult & CORINFO_INITCLASS_DONT_INLINE)
17888 pParam->result->NoteFatal(InlineObservation::CALLSITE_CLASS_INIT_FAILURE_SPEC);
17892 // Given the EE the final say in whether to inline or not.
17893 // This should be last since for verifiable code, this can be expensive
17895 /* VM Inline check also ensures that the method is verifiable if needed */
17896 CorInfoInline vmResult;
17897 vmResult = pParam->pThis->info.compCompHnd->canInline(pParam->pThis->info.compMethodHnd, pParam->fncHandle,
17900 if (vmResult == INLINE_FAIL)
17902 pParam->result->NoteFatal(InlineObservation::CALLSITE_IS_VM_NOINLINE);
17904 else if (vmResult == INLINE_NEVER)
17906 pParam->result->NoteFatal(InlineObservation::CALLEE_IS_VM_NOINLINE);
17909 if (pParam->result->IsFailure())
17911 // Make sure not to report this one. It was already reported by the VM.
17912 pParam->result->SetReported();
17916 // check for unsupported inlining restrictions
17917 assert((dwRestrictions & ~(INLINE_RESPECT_BOUNDARY | INLINE_NO_CALLEE_LDSTR | INLINE_SAME_THIS)) == 0);
17919 if (dwRestrictions & INLINE_SAME_THIS)
17921 GenTreePtr thisArg = pParam->call->gtCall.gtCallObjp;
17924 if (!pParam->pThis->impIsThis(thisArg))
17926 pParam->result->NoteFatal(InlineObservation::CALLSITE_REQUIRES_SAME_THIS);
17931 /* Get the method properties */
17933 CORINFO_CLASS_HANDLE clsHandle;
17934 clsHandle = pParam->pThis->info.compCompHnd->getMethodClass(pParam->fncHandle);
17936 clsAttr = pParam->pThis->info.compCompHnd->getClassAttribs(clsHandle);
17938 /* Get the return type */
17940 var_types fncRetType;
17941 fncRetType = pParam->call->TypeGet();
17944 var_types fncRealRetType;
17945 fncRealRetType = JITtype2varType(methInfo.args.retType);
17947 assert((genActualType(fncRealRetType) == genActualType(fncRetType)) ||
17948 // <BUGNUM> VSW 288602 </BUGNUM>
17949 // In case of IJW, we allow to assign a native pointer to a BYREF.
17950 (fncRetType == TYP_BYREF && methInfo.args.retType == CORINFO_TYPE_PTR) ||
17951 (varTypeIsStruct(fncRetType) && (fncRealRetType == TYP_STRUCT)));
17955 // Allocate an InlineCandidateInfo structure
17957 InlineCandidateInfo* pInfo;
17958 pInfo = new (pParam->pThis, CMK_Inlining) InlineCandidateInfo;
17960 pInfo->dwRestrictions = dwRestrictions;
17961 pInfo->methInfo = methInfo;
17962 pInfo->methAttr = pParam->methAttr;
17963 pInfo->clsHandle = clsHandle;
17964 pInfo->clsAttr = clsAttr;
17965 pInfo->fncRetType = fncRetType;
17966 pInfo->exactContextHnd = pParam->exactContextHnd;
17967 pInfo->ilCallerHandle = pParam->pThis->info.compMethodHnd;
17968 pInfo->initClassResult = initClassResult;
17970 *(pParam->ppInlineCandidateInfo) = pInfo;
17977 param.result->NoteFatal(InlineObservation::CALLSITE_COMPILATION_ERROR);
17981 //------------------------------------------------------------------------
17982 // impInlineRecordArgInfo: record information about an inline candidate argument
17985 // pInlineInfo - inline info for the inline candidate
17986 // curArgVal - tree for the caller actual argument value
17987 // argNum - logical index of this argument
17988 // inlineResult - result of ongoing inline evaluation
17992 // Checks for various inline blocking conditions and makes notes in
17993 // the inline info arg table about the properties of the actual. These
17994 // properties are used later by impFetchArg to determine how best to
17995 // pass the argument into the inlinee.
17997 void Compiler::impInlineRecordArgInfo(InlineInfo* pInlineInfo,
17998 GenTree* curArgVal,
18000 InlineResult* inlineResult)
18002 InlArgInfo* inlCurArgInfo = &pInlineInfo->inlArgInfo[argNum];
18004 if (curArgVal->gtOper == GT_MKREFANY)
18006 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_IS_MKREFANY);
18010 inlCurArgInfo->argNode = curArgVal;
18012 GenTreePtr lclVarTree;
18013 if (impIsAddressInLocal(curArgVal, &lclVarTree) && varTypeIsStruct(lclVarTree))
18015 inlCurArgInfo->argIsByRefToStructLocal = true;
18016 #ifdef FEATURE_SIMD
18017 if (lvaTable[lclVarTree->AsLclVarCommon()->gtLclNum].lvSIMDType)
18019 pInlineInfo->hasSIMDTypeArgLocalOrReturn = true;
18021 #endif // FEATURE_SIMD
18024 if (curArgVal->gtFlags & GTF_ALL_EFFECT)
18026 inlCurArgInfo->argHasGlobRef = (curArgVal->gtFlags & GTF_GLOB_REF) != 0;
18027 inlCurArgInfo->argHasSideEff = (curArgVal->gtFlags & (GTF_ALL_EFFECT & ~GTF_GLOB_REF)) != 0;
18030 if (curArgVal->gtOper == GT_LCL_VAR)
18032 inlCurArgInfo->argIsLclVar = true;
18034 /* Remember the "original" argument number */
18035 curArgVal->gtLclVar.gtLclILoffs = argNum;
18038 if ((curArgVal->OperKind() & GTK_CONST) ||
18039 ((curArgVal->gtOper == GT_ADDR) && (curArgVal->gtOp.gtOp1->gtOper == GT_LCL_VAR)))
18041 inlCurArgInfo->argIsInvariant = true;
18042 if (inlCurArgInfo->argIsThis && (curArgVal->gtOper == GT_CNS_INT) && (curArgVal->gtIntCon.gtIconVal == 0))
18044 // Abort inlining at this call site
18045 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_HAS_NULL_THIS);
18050 // If the arg is a local that is address-taken, we can't safely
18051 // directly substitute it into the inlinee.
18053 // Previously we'd accomplish this by setting "argHasLdargaOp" but
18054 // that has a stronger meaning: that the arg value can change in
18055 // the method body. Using that flag prevents type propagation,
18056 // which is safe in this case.
18058 // Instead mark the arg as having a caller local ref.
18059 if (!inlCurArgInfo->argIsInvariant && gtHasLocalsWithAddrOp(curArgVal))
18061 inlCurArgInfo->argHasCallerLocalRef = true;
18067 if (inlCurArgInfo->argIsThis)
18069 printf("thisArg:");
18073 printf("\nArgument #%u:", argNum);
18075 if (inlCurArgInfo->argIsLclVar)
18077 printf(" is a local var");
18079 if (inlCurArgInfo->argIsInvariant)
18081 printf(" is a constant");
18083 if (inlCurArgInfo->argHasGlobRef)
18085 printf(" has global refs");
18087 if (inlCurArgInfo->argHasCallerLocalRef)
18089 printf(" has caller local ref");
18091 if (inlCurArgInfo->argHasSideEff)
18093 printf(" has side effects");
18095 if (inlCurArgInfo->argHasLdargaOp)
18097 printf(" has ldarga effect");
18099 if (inlCurArgInfo->argHasStargOp)
18101 printf(" has starg effect");
18103 if (inlCurArgInfo->argIsByRefToStructLocal)
18105 printf(" is byref to a struct local");
18109 gtDispTree(curArgVal);
18115 //------------------------------------------------------------------------
18116 // impInlineInitVars: setup inline information for inlinee args and locals
18119 // pInlineInfo - inline info for the inline candidate
18122 // This method primarily adds caller-supplied info to the inlArgInfo
18123 // and sets up the lclVarInfo table.
18125 // For args, the inlArgInfo records properties of the actual argument
18126 // including the tree node that produces the arg value. This node is
18127 // usually the tree node present at the call, but may also differ in
18129 // - when the call arg is a GT_RET_EXPR, we search back through the ret
18130 // expr chain for the actual node. Note this will either be the original
18131 // call (which will be a failed inline by this point), or the return
18132 // expression from some set of inlines.
18133 // - when argument type casting is needed the necessary casts are added
18134 // around the argument node.
18135 // - if an argment can be simplified by folding then the node here is the
18138 // The method may make observations that lead to marking this candidate as
18139 // a failed inline. If this happens the initialization is abandoned immediately
18140 // to try and reduce the jit time cost for a failed inline.
18142 void Compiler::impInlineInitVars(InlineInfo* pInlineInfo)
18144 assert(!compIsForInlining());
18146 GenTreePtr call = pInlineInfo->iciCall;
18147 CORINFO_METHOD_INFO* methInfo = &pInlineInfo->inlineCandidateInfo->methInfo;
18148 unsigned clsAttr = pInlineInfo->inlineCandidateInfo->clsAttr;
18149 InlArgInfo* inlArgInfo = pInlineInfo->inlArgInfo;
18150 InlLclVarInfo* lclVarInfo = pInlineInfo->lclVarInfo;
18151 InlineResult* inlineResult = pInlineInfo->inlineResult;
18153 const bool hasRetBuffArg = impMethodInfo_hasRetBuffArg(methInfo);
18155 /* init the argument stuct */
18157 memset(inlArgInfo, 0, (MAX_INL_ARGS + 1) * sizeof(inlArgInfo[0]));
18159 /* Get hold of the 'this' pointer and the argument list proper */
18161 GenTreePtr thisArg = call->gtCall.gtCallObjp;
18162 GenTreePtr argList = call->gtCall.gtCallArgs;
18163 unsigned argCnt = 0; // Count of the arguments
18165 assert((methInfo->args.hasThis()) == (thisArg != nullptr));
18169 inlArgInfo[0].argIsThis = true;
18170 GenTree* actualThisArg = thisArg->gtRetExprVal();
18171 impInlineRecordArgInfo(pInlineInfo, actualThisArg, argCnt, inlineResult);
18173 if (inlineResult->IsFailure())
18178 /* Increment the argument count */
18182 /* Record some information about each of the arguments */
18183 bool hasTypeCtxtArg = (methInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE) != 0;
18185 #if USER_ARGS_COME_LAST
18186 unsigned typeCtxtArg = thisArg ? 1 : 0;
18187 #else // USER_ARGS_COME_LAST
18188 unsigned typeCtxtArg = methInfo->args.totalILArgs();
18189 #endif // USER_ARGS_COME_LAST
18191 for (GenTreePtr argTmp = argList; argTmp; argTmp = argTmp->gtOp.gtOp2)
18193 if (argTmp == argList && hasRetBuffArg)
18198 // Ignore the type context argument
18199 if (hasTypeCtxtArg && (argCnt == typeCtxtArg))
18201 pInlineInfo->typeContextArg = typeCtxtArg;
18202 typeCtxtArg = 0xFFFFFFFF;
18206 assert(argTmp->gtOper == GT_LIST);
18207 GenTree* arg = argTmp->gtOp.gtOp1;
18208 GenTree* actualArg = arg->gtRetExprVal();
18209 impInlineRecordArgInfo(pInlineInfo, actualArg, argCnt, inlineResult);
18211 if (inlineResult->IsFailure())
18216 /* Increment the argument count */
18220 /* Make sure we got the arg number right */
18221 assert(argCnt == methInfo->args.totalILArgs());
18223 #ifdef FEATURE_SIMD
18224 bool foundSIMDType = pInlineInfo->hasSIMDTypeArgLocalOrReturn;
18225 #endif // FEATURE_SIMD
18227 /* We have typeless opcodes, get type information from the signature */
18233 if (clsAttr & CORINFO_FLG_VALUECLASS)
18235 sigType = TYP_BYREF;
18242 lclVarInfo[0].lclVerTypeInfo = verMakeTypeInfo(pInlineInfo->inlineCandidateInfo->clsHandle);
18243 lclVarInfo[0].lclHasLdlocaOp = false;
18245 #ifdef FEATURE_SIMD
18246 // We always want to check isSIMDClass, since we want to set foundSIMDType (to increase
18247 // the inlining multiplier) for anything in that assembly.
18248 // But we only need to normalize it if it is a TYP_STRUCT
18249 // (which we need to do even if we have already set foundSIMDType).
18250 if ((!foundSIMDType || (sigType == TYP_STRUCT)) && isSIMDClass(&(lclVarInfo[0].lclVerTypeInfo)))
18252 if (sigType == TYP_STRUCT)
18254 sigType = impNormStructType(lclVarInfo[0].lclVerTypeInfo.GetClassHandle());
18256 foundSIMDType = true;
18258 #endif // FEATURE_SIMD
18259 lclVarInfo[0].lclTypeInfo = sigType;
18261 assert(varTypeIsGC(thisArg->gtType) || // "this" is managed
18262 (thisArg->gtType == TYP_I_IMPL && // "this" is unmgd but the method's class doesnt care
18263 (clsAttr & CORINFO_FLG_VALUECLASS)));
18265 if (genActualType(thisArg->gtType) != genActualType(sigType))
18267 if (sigType == TYP_REF)
18269 /* The argument cannot be bashed into a ref (see bug 750871) */
18270 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_NO_BASH_TO_REF);
18274 /* This can only happen with byrefs <-> ints/shorts */
18276 assert(genActualType(sigType) == TYP_I_IMPL || sigType == TYP_BYREF);
18277 assert(genActualType(thisArg->gtType) == TYP_I_IMPL || thisArg->gtType == TYP_BYREF);
18279 if (sigType == TYP_BYREF)
18281 lclVarInfo[0].lclVerTypeInfo = typeInfo(varType2tiType(TYP_I_IMPL));
18283 else if (thisArg->gtType == TYP_BYREF)
18285 assert(sigType == TYP_I_IMPL);
18287 /* If possible change the BYREF to an int */
18288 if (thisArg->IsVarAddr())
18290 thisArg->gtType = TYP_I_IMPL;
18291 lclVarInfo[0].lclVerTypeInfo = typeInfo(varType2tiType(TYP_I_IMPL));
18295 /* Arguments 'int <- byref' cannot be bashed */
18296 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_NO_BASH_TO_INT);
18303 /* Init the types of the arguments and make sure the types
18304 * from the trees match the types in the signature */
18306 CORINFO_ARG_LIST_HANDLE argLst;
18307 argLst = methInfo->args.args;
18310 for (i = (thisArg ? 1 : 0); i < argCnt; i++, argLst = info.compCompHnd->getArgNext(argLst))
18312 var_types sigType = (var_types)eeGetArgType(argLst, &methInfo->args);
18314 lclVarInfo[i].lclVerTypeInfo = verParseArgSigToTypeInfo(&methInfo->args, argLst);
18316 #ifdef FEATURE_SIMD
18317 if ((!foundSIMDType || (sigType == TYP_STRUCT)) && isSIMDClass(&(lclVarInfo[i].lclVerTypeInfo)))
18319 // If this is a SIMD class (i.e. in the SIMD assembly), then we will consider that we've
18320 // found a SIMD type, even if this may not be a type we recognize (the assumption is that
18321 // it is likely to use a SIMD type, and therefore we want to increase the inlining multiplier).
18322 foundSIMDType = true;
18323 if (sigType == TYP_STRUCT)
18325 var_types structType = impNormStructType(lclVarInfo[i].lclVerTypeInfo.GetClassHandle());
18326 sigType = structType;
18329 #endif // FEATURE_SIMD
18331 lclVarInfo[i].lclTypeInfo = sigType;
18332 lclVarInfo[i].lclHasLdlocaOp = false;
18334 /* Does the tree type match the signature type? */
18336 GenTreePtr inlArgNode = inlArgInfo[i].argNode;
18338 if (sigType != inlArgNode->gtType)
18340 /* In valid IL, this can only happen for short integer types or byrefs <-> [native] ints,
18341 but in bad IL cases with caller-callee signature mismatches we can see other types.
18342 Intentionally reject cases with mismatches so the jit is more flexible when
18343 encountering bad IL. */
18345 bool isPlausibleTypeMatch = (genActualType(sigType) == genActualType(inlArgNode->gtType)) ||
18346 (genActualTypeIsIntOrI(sigType) && inlArgNode->gtType == TYP_BYREF) ||
18347 (sigType == TYP_BYREF && genActualTypeIsIntOrI(inlArgNode->gtType));
18349 if (!isPlausibleTypeMatch)
18351 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_TYPES_INCOMPATIBLE);
18355 /* Is it a narrowing or widening cast?
18356 * Widening casts are ok since the value computed is already
18357 * normalized to an int (on the IL stack) */
18359 if (genTypeSize(inlArgNode->gtType) >= genTypeSize(sigType))
18361 if (sigType == TYP_BYREF)
18363 lclVarInfo[i].lclVerTypeInfo = typeInfo(varType2tiType(TYP_I_IMPL));
18365 else if (inlArgNode->gtType == TYP_BYREF)
18367 assert(varTypeIsIntOrI(sigType));
18369 /* If possible bash the BYREF to an int */
18370 if (inlArgNode->IsVarAddr())
18372 inlArgNode->gtType = TYP_I_IMPL;
18373 lclVarInfo[i].lclVerTypeInfo = typeInfo(varType2tiType(TYP_I_IMPL));
18377 /* Arguments 'int <- byref' cannot be changed */
18378 inlineResult->NoteFatal(InlineObservation::CALLSITE_ARG_NO_BASH_TO_INT);
18382 else if (genTypeSize(sigType) < EA_PTRSIZE)
18384 /* Narrowing cast */
18386 if (inlArgNode->gtOper == GT_LCL_VAR &&
18387 !lvaTable[inlArgNode->gtLclVarCommon.gtLclNum].lvNormalizeOnLoad() &&
18388 sigType == lvaGetRealType(inlArgNode->gtLclVarCommon.gtLclNum))
18390 /* We don't need to insert a cast here as the variable
18391 was assigned a normalized value of the right type */
18396 inlArgNode = inlArgInfo[i].argNode = gtNewCastNode(TYP_INT, inlArgNode, sigType);
18398 inlArgInfo[i].argIsLclVar = false;
18400 /* Try to fold the node in case we have constant arguments */
18402 if (inlArgInfo[i].argIsInvariant)
18404 inlArgNode = gtFoldExprConst(inlArgNode);
18405 inlArgInfo[i].argNode = inlArgNode;
18406 assert(inlArgNode->OperIsConst());
18409 #ifdef _TARGET_64BIT_
18410 else if (genTypeSize(genActualType(inlArgNode->gtType)) < genTypeSize(sigType))
18412 // This should only happen for int -> native int widening
18413 inlArgNode = inlArgInfo[i].argNode = gtNewCastNode(genActualType(sigType), inlArgNode, sigType);
18415 inlArgInfo[i].argIsLclVar = false;
18417 /* Try to fold the node in case we have constant arguments */
18419 if (inlArgInfo[i].argIsInvariant)
18421 inlArgNode = gtFoldExprConst(inlArgNode);
18422 inlArgInfo[i].argNode = inlArgNode;
18423 assert(inlArgNode->OperIsConst());
18426 #endif // _TARGET_64BIT_
18431 /* Init the types of the local variables */
18433 CORINFO_ARG_LIST_HANDLE localsSig;
18434 localsSig = methInfo->locals.args;
18436 for (i = 0; i < methInfo->locals.numArgs; i++)
18439 var_types type = (var_types)eeGetArgType(localsSig, &methInfo->locals, &isPinned);
18441 lclVarInfo[i + argCnt].lclHasLdlocaOp = false;
18442 lclVarInfo[i + argCnt].lclIsPinned = isPinned;
18443 lclVarInfo[i + argCnt].lclTypeInfo = type;
18445 if (varTypeIsGC(type))
18447 pInlineInfo->numberOfGcRefLocals++;
18452 // Pinned locals may cause inlines to fail.
18453 inlineResult->Note(InlineObservation::CALLEE_HAS_PINNED_LOCALS);
18454 if (inlineResult->IsFailure())
18460 lclVarInfo[i + argCnt].lclVerTypeInfo = verParseArgSigToTypeInfo(&methInfo->locals, localsSig);
18462 // If this local is a struct type with GC fields, inform the inliner. It may choose to bail
18463 // out on the inline.
18464 if (type == TYP_STRUCT)
18466 CORINFO_CLASS_HANDLE lclHandle = lclVarInfo[i + argCnt].lclVerTypeInfo.GetClassHandle();
18467 DWORD typeFlags = info.compCompHnd->getClassAttribs(lclHandle);
18468 if ((typeFlags & CORINFO_FLG_CONTAINS_GC_PTR) != 0)
18470 inlineResult->Note(InlineObservation::CALLEE_HAS_GC_STRUCT);
18471 if (inlineResult->IsFailure())
18476 // Do further notification in the case where the call site is rare; some policies do
18477 // not track the relative hotness of call sites for "always" inline cases.
18478 if (pInlineInfo->iciBlock->isRunRarely())
18480 inlineResult->Note(InlineObservation::CALLSITE_RARE_GC_STRUCT);
18481 if (inlineResult->IsFailure())
18490 localsSig = info.compCompHnd->getArgNext(localsSig);
18492 #ifdef FEATURE_SIMD
18493 if ((!foundSIMDType || (type == TYP_STRUCT)) && isSIMDClass(&(lclVarInfo[i + argCnt].lclVerTypeInfo)))
18495 foundSIMDType = true;
18496 if (featureSIMD && type == TYP_STRUCT)
18498 var_types structType = impNormStructType(lclVarInfo[i + argCnt].lclVerTypeInfo.GetClassHandle());
18499 lclVarInfo[i + argCnt].lclTypeInfo = structType;
18502 #endif // FEATURE_SIMD
18505 #ifdef FEATURE_SIMD
18506 if (!foundSIMDType && (call->AsCall()->gtRetClsHnd != nullptr) && isSIMDClass(call->AsCall()->gtRetClsHnd))
18508 foundSIMDType = true;
18510 pInlineInfo->hasSIMDTypeArgLocalOrReturn = foundSIMDType;
18511 #endif // FEATURE_SIMD
18514 //------------------------------------------------------------------------
18515 // impInlineFetchLocal: get a local var that represents an inlinee local
18518 // lclNum -- number of the inlinee local
18519 // reason -- debug string describing purpose of the local var
18522 // Number of the local to use
18525 // This method is invoked only for locals actually used in the
18528 // Allocates a new temp if necessary, and copies key properties
18529 // over from the inlinee local var info.
18531 unsigned Compiler::impInlineFetchLocal(unsigned lclNum DEBUGARG(const char* reason))
18533 assert(compIsForInlining());
18535 unsigned tmpNum = impInlineInfo->lclTmpNum[lclNum];
18537 if (tmpNum == BAD_VAR_NUM)
18539 const InlLclVarInfo& inlineeLocal = impInlineInfo->lclVarInfo[lclNum + impInlineInfo->argCnt];
18540 const var_types lclTyp = inlineeLocal.lclTypeInfo;
18542 // The lifetime of this local might span multiple BBs.
18543 // So it is a long lifetime local.
18544 impInlineInfo->lclTmpNum[lclNum] = tmpNum = lvaGrabTemp(false DEBUGARG(reason));
18546 // Copy over key info
18547 lvaTable[tmpNum].lvType = lclTyp;
18548 lvaTable[tmpNum].lvHasLdAddrOp = inlineeLocal.lclHasLdlocaOp;
18549 lvaTable[tmpNum].lvPinned = inlineeLocal.lclIsPinned;
18550 lvaTable[tmpNum].lvHasILStoreOp = inlineeLocal.lclHasStlocOp;
18551 lvaTable[tmpNum].lvHasMultipleILStoreOp = inlineeLocal.lclHasMultipleStlocOp;
18553 // Copy over class handle for ref types. Note this may be a
18554 // shared type -- someday perhaps we can get the exact
18555 // signature and pass in a more precise type.
18556 if (lclTyp == TYP_REF)
18558 lvaSetClass(tmpNum, inlineeLocal.lclVerTypeInfo.GetClassHandleForObjRef());
18561 if (inlineeLocal.lclVerTypeInfo.IsStruct())
18563 if (varTypeIsStruct(lclTyp))
18565 lvaSetStruct(tmpNum, inlineeLocal.lclVerTypeInfo.GetClassHandle(), true /* unsafe value cls check */);
18569 // This is a wrapped primitive. Make sure the verstate knows that
18570 lvaTable[tmpNum].lvVerTypeInfo = inlineeLocal.lclVerTypeInfo;
18575 // Sanity check that we're properly prepared for gc ref locals.
18576 if (varTypeIsGC(lclTyp))
18578 // Since there are gc locals we should have seen them earlier
18579 // and if there was a return value, set up the spill temp.
18580 assert(impInlineInfo->HasGcRefLocals());
18581 assert((info.compRetNativeType == TYP_VOID) || fgNeedReturnSpillTemp());
18585 // Make sure all pinned locals count as gc refs.
18586 assert(!inlineeLocal.lclIsPinned);
18594 //------------------------------------------------------------------------
18595 // impInlineFetchArg: return tree node for argument value in an inlinee
18598 // lclNum -- argument number in inlinee IL
18599 // inlArgInfo -- argument info for inlinee
18600 // lclVarInfo -- var info for inlinee
18603 // Tree for the argument's value. Often an inlinee-scoped temp
18604 // GT_LCL_VAR but can be other tree kinds, if the argument
18605 // expression from the caller can be directly substituted into the
18609 // Must be used only for arguments -- use impInlineFetchLocal for
18612 // Direct substitution is performed when the formal argument cannot
18613 // change value in the inlinee body (no starg or ldarga), and the
18614 // actual argument expression's value cannot be changed if it is
18615 // substituted it into the inlinee body.
18617 // Even if an inlinee-scoped temp is returned here, it may later be
18618 // "bashed" to a caller-supplied tree when arguments are actually
18619 // passed (see fgInlinePrependStatements). Bashing can happen if
18620 // the argument ends up being single use and other conditions are
18621 // met. So the contents of the tree returned here may not end up
18622 // being the ones ultimately used for the argument.
18624 // This method will side effect inlArgInfo. It should only be called
18625 // for actual uses of the argument in the inlinee.
18627 GenTreePtr Compiler::impInlineFetchArg(unsigned lclNum, InlArgInfo* inlArgInfo, InlLclVarInfo* lclVarInfo)
18629 // Cache the relevant arg and lcl info for this argument.
18630 // We will modify argInfo but not lclVarInfo.
18631 InlArgInfo& argInfo = inlArgInfo[lclNum];
18632 const InlLclVarInfo& lclInfo = lclVarInfo[lclNum];
18633 const bool argCanBeModified = argInfo.argHasLdargaOp || argInfo.argHasStargOp;
18634 const var_types lclTyp = lclInfo.lclTypeInfo;
18635 GenTreePtr op1 = nullptr;
18637 if (argInfo.argIsInvariant && !argCanBeModified)
18639 // Directly substitute constants or addresses of locals
18641 // Clone the constant. Note that we cannot directly use
18642 // argNode in the trees even if !argInfo.argIsUsed as this
18643 // would introduce aliasing between inlArgInfo[].argNode and
18644 // impInlineExpr. Then gtFoldExpr() could change it, causing
18645 // further references to the argument working off of the
18647 op1 = gtCloneExpr(argInfo.argNode);
18648 PREFIX_ASSUME(op1 != nullptr);
18649 argInfo.argTmpNum = BAD_VAR_NUM;
18651 else if (argInfo.argIsLclVar && !argCanBeModified && !argInfo.argHasCallerLocalRef)
18653 // Directly substitute unaliased caller locals for args that cannot be modified
18655 // Use the caller-supplied node if this is the first use.
18656 op1 = argInfo.argNode;
18657 argInfo.argTmpNum = op1->gtLclVarCommon.gtLclNum;
18659 // Use an equivalent copy if this is the second or subsequent
18660 // use, or if we need to retype.
18662 // Note argument type mismatches that prevent inlining should
18663 // have been caught in impInlineInitVars.
18664 if (argInfo.argIsUsed || (op1->TypeGet() != lclTyp))
18666 assert(op1->gtOper == GT_LCL_VAR);
18667 assert(lclNum == op1->gtLclVar.gtLclILoffs);
18669 var_types newTyp = lclTyp;
18671 if (!lvaTable[op1->gtLclVarCommon.gtLclNum].lvNormalizeOnLoad())
18673 newTyp = genActualType(lclTyp);
18676 // Create a new lcl var node - remember the argument lclNum
18677 op1 = gtNewLclvNode(op1->gtLclVarCommon.gtLclNum, newTyp, op1->gtLclVar.gtLclILoffs);
18680 else if (argInfo.argIsByRefToStructLocal && !argInfo.argHasStargOp)
18682 /* Argument is a by-ref address to a struct, a normed struct, or its field.
18683 In these cases, don't spill the byref to a local, simply clone the tree and use it.
18684 This way we will increase the chance for this byref to be optimized away by
18685 a subsequent "dereference" operation.
18687 From Dev11 bug #139955: Argument node can also be TYP_I_IMPL if we've bashed the tree
18688 (in impInlineInitVars()), if the arg has argHasLdargaOp as well as argIsByRefToStructLocal.
18689 For example, if the caller is:
18690 ldloca.s V_1 // V_1 is a local struct
18691 call void Test.ILPart::RunLdargaOnPointerArg(int32*)
18692 and the callee being inlined has:
18693 .method public static void RunLdargaOnPointerArg(int32* ptrToInts) cil managed
18695 call void Test.FourInts::NotInlined_SetExpectedValuesThroughPointerToPointer(int32**)
18696 then we change the argument tree (of "ldloca.s V_1") to TYP_I_IMPL to match the callee signature. We'll
18697 soon afterwards reject the inlining anyway, since the tree we return isn't a GT_LCL_VAR.
18699 assert(argInfo.argNode->TypeGet() == TYP_BYREF || argInfo.argNode->TypeGet() == TYP_I_IMPL);
18700 op1 = gtCloneExpr(argInfo.argNode);
18704 /* Argument is a complex expression - it must be evaluated into a temp */
18706 if (argInfo.argHasTmp)
18708 assert(argInfo.argIsUsed);
18709 assert(argInfo.argTmpNum < lvaCount);
18711 /* Create a new lcl var node - remember the argument lclNum */
18712 op1 = gtNewLclvNode(argInfo.argTmpNum, genActualType(lclTyp));
18714 /* This is the second or later use of the this argument,
18715 so we have to use the temp (instead of the actual arg) */
18716 argInfo.argBashTmpNode = nullptr;
18720 /* First time use */
18721 assert(!argInfo.argIsUsed);
18723 /* Reserve a temp for the expression.
18724 * Use a large size node as we may change it later */
18726 const unsigned tmpNum = lvaGrabTemp(true DEBUGARG("Inlining Arg"));
18728 lvaTable[tmpNum].lvType = lclTyp;
18730 // For ref types, determine the type of the temp.
18731 if (lclTyp == TYP_REF)
18733 if (!argCanBeModified)
18735 // If the arg can't be modified in the method
18736 // body, use the type of the value, if
18737 // known. Otherwise, use the declared type.
18738 lvaSetClass(tmpNum, argInfo.argNode, lclInfo.lclVerTypeInfo.GetClassHandleForObjRef());
18742 // Arg might be modified, use the declared type of
18744 lvaSetClass(tmpNum, lclInfo.lclVerTypeInfo.GetClassHandleForObjRef());
18748 assert(lvaTable[tmpNum].lvAddrExposed == 0);
18749 if (argInfo.argHasLdargaOp)
18751 lvaTable[tmpNum].lvHasLdAddrOp = 1;
18754 if (lclInfo.lclVerTypeInfo.IsStruct())
18756 if (varTypeIsStruct(lclTyp))
18758 lvaSetStruct(tmpNum, lclInfo.lclVerTypeInfo.GetClassHandle(), true /* unsafe value cls check */);
18762 // This is a wrapped primitive. Make sure the verstate knows that
18763 lvaTable[tmpNum].lvVerTypeInfo = lclInfo.lclVerTypeInfo;
18767 argInfo.argHasTmp = true;
18768 argInfo.argTmpNum = tmpNum;
18770 // If we require strict exception order, then arguments must
18771 // be evaluated in sequence before the body of the inlined method.
18772 // So we need to evaluate them to a temp.
18773 // Also, if arguments have global or local references, we need to
18774 // evaluate them to a temp before the inlined body as the
18775 // inlined body may be modifying the global ref.
18776 // TODO-1stClassStructs: We currently do not reuse an existing lclVar
18777 // if it is a struct, because it requires some additional handling.
18779 if (!varTypeIsStruct(lclTyp) && !argInfo.argHasSideEff && !argInfo.argHasGlobRef &&
18780 !argInfo.argHasCallerLocalRef)
18782 /* Get a *LARGE* LCL_VAR node */
18783 op1 = gtNewLclLNode(tmpNum, genActualType(lclTyp), lclNum);
18785 /* Record op1 as the very first use of this argument.
18786 If there are no further uses of the arg, we may be
18787 able to use the actual arg node instead of the temp.
18788 If we do see any further uses, we will clear this. */
18789 argInfo.argBashTmpNode = op1;
18793 /* Get a small LCL_VAR node */
18794 op1 = gtNewLclvNode(tmpNum, genActualType(lclTyp));
18795 /* No bashing of this argument */
18796 argInfo.argBashTmpNode = nullptr;
18801 // Mark this argument as used.
18802 argInfo.argIsUsed = true;
18807 /******************************************************************************
18808 Is this the original "this" argument to the call being inlined?
18810 Note that we do not inline methods with "starg 0", and so we do not need to
18814 BOOL Compiler::impInlineIsThis(GenTreePtr tree, InlArgInfo* inlArgInfo)
18816 assert(compIsForInlining());
18817 return (tree->gtOper == GT_LCL_VAR && tree->gtLclVarCommon.gtLclNum == inlArgInfo[0].argTmpNum);
18820 //-----------------------------------------------------------------------------
18821 // This function checks if a dereference in the inlinee can guarantee that
18822 // the "this" is non-NULL.
18823 // If we haven't hit a branch or a side effect, and we are dereferencing
18824 // from 'this' to access a field or make GTF_CALL_NULLCHECK call,
18825 // then we can avoid a separate null pointer check.
18827 // "additionalTreesToBeEvaluatedBefore"
18828 // is the set of pending trees that have not yet been added to the statement list,
18829 // and which have been removed from verCurrentState.esStack[]
18831 BOOL Compiler::impInlineIsGuaranteedThisDerefBeforeAnySideEffects(GenTreePtr additionalTreesToBeEvaluatedBefore,
18832 GenTreePtr variableBeingDereferenced,
18833 InlArgInfo* inlArgInfo)
18835 assert(compIsForInlining());
18836 assert(opts.OptEnabled(CLFLG_INLINING));
18838 BasicBlock* block = compCurBB;
18843 if (block != fgFirstBB)
18848 if (!impInlineIsThis(variableBeingDereferenced, inlArgInfo))
18853 if (additionalTreesToBeEvaluatedBefore &&
18854 GTF_GLOBALLY_VISIBLE_SIDE_EFFECTS(additionalTreesToBeEvaluatedBefore->gtFlags))
18859 for (stmt = impTreeList->gtNext; stmt; stmt = stmt->gtNext)
18861 expr = stmt->gtStmt.gtStmtExpr;
18863 if (GTF_GLOBALLY_VISIBLE_SIDE_EFFECTS(expr->gtFlags))
18869 for (unsigned level = 0; level < verCurrentState.esStackDepth; level++)
18871 unsigned stackTreeFlags = verCurrentState.esStack[level].val->gtFlags;
18872 if (GTF_GLOBALLY_VISIBLE_SIDE_EFFECTS(stackTreeFlags))
18881 //------------------------------------------------------------------------
18882 // impMarkInlineCandidate: determine if this call can be subsequently inlined
18885 // callNode -- call under scrutiny
18886 // exactContextHnd -- context handle for inlining
18887 // exactContextNeedsRuntimeLookup -- true if context required runtime lookup
18888 // callInfo -- call info from VM
18891 // If callNode is an inline candidate, this method sets the flag
18892 // GTF_CALL_INLINE_CANDIDATE, and ensures that helper methods have
18893 // filled in the associated InlineCandidateInfo.
18895 // If callNode is not an inline candidate, and the reason is
18896 // something that is inherent to the method being called, the
18897 // method may be marked as "noinline" to short-circuit any
18898 // future assessments of calls to this method.
18900 void Compiler::impMarkInlineCandidate(GenTreePtr callNode,
18901 CORINFO_CONTEXT_HANDLE exactContextHnd,
18902 bool exactContextNeedsRuntimeLookup,
18903 CORINFO_CALL_INFO* callInfo)
18905 // Let the strategy know there's another call
18906 impInlineRoot()->m_inlineStrategy->NoteCall();
18908 if (!opts.OptEnabled(CLFLG_INLINING))
18910 /* XXX Mon 8/18/2008
18911 * This assert is misleading. The caller does not ensure that we have CLFLG_INLINING set before
18912 * calling impMarkInlineCandidate. However, if this assert trips it means that we're an inlinee and
18913 * CLFLG_MINOPT is set. That doesn't make a lot of sense. If you hit this assert, work back and
18914 * figure out why we did not set MAXOPT for this compile.
18916 assert(!compIsForInlining());
18920 if (compIsForImportOnly())
18922 // Don't bother creating the inline candidate during verification.
18923 // Otherwise the call to info.compCompHnd->canInline will trigger a recursive verification
18924 // that leads to the creation of multiple instances of Compiler.
18928 GenTreeCall* call = callNode->AsCall();
18929 InlineResult inlineResult(this, call, nullptr, "impMarkInlineCandidate");
18931 // Don't inline if not optimizing root method
18932 if (opts.compDbgCode)
18934 inlineResult.NoteFatal(InlineObservation::CALLER_DEBUG_CODEGEN);
18938 // Don't inline if inlining into root method is disabled.
18939 if (InlineStrategy::IsNoInline(info.compCompHnd, info.compMethodHnd))
18941 inlineResult.NoteFatal(InlineObservation::CALLER_IS_JIT_NOINLINE);
18945 // Inlining candidate determination needs to honor only IL tail prefix.
18946 // Inlining takes precedence over implicit tail call optimization (if the call is not directly recursive).
18947 if (call->IsTailPrefixedCall())
18949 inlineResult.NoteFatal(InlineObservation::CALLSITE_EXPLICIT_TAIL_PREFIX);
18953 // Tail recursion elimination takes precedence over inlining.
18954 // TODO: We may want to do some of the additional checks from fgMorphCall
18955 // here to reduce the chance we don't inline a call that won't be optimized
18956 // as a fast tail call or turned into a loop.
18957 if (gtIsRecursiveCall(call) && call->IsImplicitTailCall())
18959 inlineResult.NoteFatal(InlineObservation::CALLSITE_IMPLICIT_REC_TAIL_CALL);
18963 if (call->IsVirtual())
18965 inlineResult.NoteFatal(InlineObservation::CALLSITE_IS_NOT_DIRECT);
18969 /* Ignore helper calls */
18971 if (call->gtCallType == CT_HELPER)
18973 inlineResult.NoteFatal(InlineObservation::CALLSITE_IS_CALL_TO_HELPER);
18977 /* Ignore indirect calls */
18978 if (call->gtCallType == CT_INDIRECT)
18980 inlineResult.NoteFatal(InlineObservation::CALLSITE_IS_NOT_DIRECT_MANAGED);
18984 /* I removed the check for BBJ_THROW. BBJ_THROW is usually marked as rarely run. This more or less
18985 * restricts the inliner to non-expanding inlines. I removed the check to allow for non-expanding
18986 * inlining in throw blocks. I should consider the same thing for catch and filter regions. */
18988 CORINFO_METHOD_HANDLE fncHandle = call->gtCallMethHnd;
18991 // Reuse method flags from the original callInfo if possible
18992 if (fncHandle == callInfo->hMethod)
18994 methAttr = callInfo->methodFlags;
18998 methAttr = info.compCompHnd->getMethodAttribs(fncHandle);
19002 if (compStressCompile(STRESS_FORCE_INLINE, 0))
19004 methAttr |= CORINFO_FLG_FORCEINLINE;
19008 // Check for COMPlus_AggressiveInlining
19009 if (compDoAggressiveInlining)
19011 methAttr |= CORINFO_FLG_FORCEINLINE;
19014 if (!(methAttr & CORINFO_FLG_FORCEINLINE))
19016 /* Don't bother inline blocks that are in the filter region */
19017 if (bbInCatchHandlerILRange(compCurBB))
19022 printf("\nWill not inline blocks that are in the catch handler region\n");
19027 inlineResult.NoteFatal(InlineObservation::CALLSITE_IS_WITHIN_CATCH);
19031 if (bbInFilterILRange(compCurBB))
19036 printf("\nWill not inline blocks that are in the filter region\n");
19040 inlineResult.NoteFatal(InlineObservation::CALLSITE_IS_WITHIN_FILTER);
19045 /* If the caller's stack frame is marked, then we can't do any inlining. Period. */
19047 if (opts.compNeedSecurityCheck)
19049 inlineResult.NoteFatal(InlineObservation::CALLER_NEEDS_SECURITY_CHECK);
19053 /* Check if we tried to inline this method before */
19055 if (methAttr & CORINFO_FLG_DONT_INLINE)
19057 inlineResult.NoteFatal(InlineObservation::CALLEE_IS_NOINLINE);
19061 /* Cannot inline synchronized methods */
19063 if (methAttr & CORINFO_FLG_SYNCH)
19065 inlineResult.NoteFatal(InlineObservation::CALLEE_IS_SYNCHRONIZED);
19069 /* Do not inline if callee needs security checks (since they would then mark the wrong frame) */
19071 if (methAttr & CORINFO_FLG_SECURITYCHECK)
19073 inlineResult.NoteFatal(InlineObservation::CALLEE_NEEDS_SECURITY_CHECK);
19077 InlineCandidateInfo* inlineCandidateInfo = nullptr;
19078 impCheckCanInline(call, fncHandle, methAttr, exactContextHnd, &inlineCandidateInfo, &inlineResult);
19080 if (inlineResult.IsFailure())
19085 // The old value should be NULL
19086 assert(call->gtInlineCandidateInfo == nullptr);
19088 // The new value should not be NULL.
19089 assert(inlineCandidateInfo != nullptr);
19090 inlineCandidateInfo->exactContextNeedsRuntimeLookup = exactContextNeedsRuntimeLookup;
19092 call->gtInlineCandidateInfo = inlineCandidateInfo;
19094 // Mark the call node as inline candidate.
19095 call->gtFlags |= GTF_CALL_INLINE_CANDIDATE;
19097 // Let the strategy know there's another candidate.
19098 impInlineRoot()->m_inlineStrategy->NoteCandidate();
19100 // Since we're not actually inlining yet, and this call site is
19101 // still just an inline candidate, there's nothing to report.
19102 inlineResult.SetReported();
19105 /******************************************************************************/
19106 // Returns true if the given intrinsic will be implemented by target-specific
19109 bool Compiler::IsTargetIntrinsic(CorInfoIntrinsics intrinsicId)
19111 #if defined(_TARGET_AMD64_) || (defined(_TARGET_X86_) && !defined(LEGACY_BACKEND))
19112 switch (intrinsicId)
19114 // AMD64/x86 has SSE2 instructions to directly compute sqrt/abs and SSE4.1
19115 // instructions to directly compute round/ceiling/floor.
19117 // TODO: Because the x86 backend only targets SSE for floating-point code,
19118 // it does not treat Sine, Cosine, or Round as intrinsics (JIT32
19119 // implemented those intrinsics as x87 instructions). If this poses
19120 // a CQ problem, it may be necessary to change the implementation of
19121 // the helper calls to decrease call overhead or switch back to the
19122 // x87 instructions. This is tracked by #7097.
19123 case CORINFO_INTRINSIC_Sqrt:
19124 case CORINFO_INTRINSIC_Abs:
19127 case CORINFO_INTRINSIC_Round:
19128 case CORINFO_INTRINSIC_Ceiling:
19129 case CORINFO_INTRINSIC_Floor:
19130 return compSupports(InstructionSet_SSE41);
19135 #elif defined(_TARGET_ARM64_)
19136 switch (intrinsicId)
19138 case CORINFO_INTRINSIC_Sqrt:
19139 case CORINFO_INTRINSIC_Abs:
19140 case CORINFO_INTRINSIC_Round:
19141 case CORINFO_INTRINSIC_Floor:
19142 case CORINFO_INTRINSIC_Ceiling:
19148 #elif defined(_TARGET_ARM_)
19149 switch (intrinsicId)
19151 case CORINFO_INTRINSIC_Sqrt:
19152 case CORINFO_INTRINSIC_Abs:
19153 case CORINFO_INTRINSIC_Round:
19159 #elif defined(_TARGET_X86_)
19160 switch (intrinsicId)
19162 case CORINFO_INTRINSIC_Sin:
19163 case CORINFO_INTRINSIC_Cos:
19164 case CORINFO_INTRINSIC_Sqrt:
19165 case CORINFO_INTRINSIC_Abs:
19166 case CORINFO_INTRINSIC_Round:
19173 // TODO: This portion of logic is not implemented for other arch.
19174 // The reason for returning true is that on all other arch the only intrinsic
19175 // enabled are target intrinsics.
19177 #endif //_TARGET_AMD64_
19180 /******************************************************************************/
19181 // Returns true if the given intrinsic will be implemented by calling System.Math
19184 bool Compiler::IsIntrinsicImplementedByUserCall(CorInfoIntrinsics intrinsicId)
19186 // Currently, if an math intrisic is not implemented by target-specific
19187 // intructions, it will be implemented by a System.Math call. In the
19188 // future, if we turn to implementing some of them with helper callers,
19189 // this predicate needs to be revisited.
19190 return !IsTargetIntrinsic(intrinsicId);
19193 bool Compiler::IsMathIntrinsic(CorInfoIntrinsics intrinsicId)
19195 switch (intrinsicId)
19197 case CORINFO_INTRINSIC_Sin:
19198 case CORINFO_INTRINSIC_Cbrt:
19199 case CORINFO_INTRINSIC_Sqrt:
19200 case CORINFO_INTRINSIC_Abs:
19201 case CORINFO_INTRINSIC_Cos:
19202 case CORINFO_INTRINSIC_Round:
19203 case CORINFO_INTRINSIC_Cosh:
19204 case CORINFO_INTRINSIC_Sinh:
19205 case CORINFO_INTRINSIC_Tan:
19206 case CORINFO_INTRINSIC_Tanh:
19207 case CORINFO_INTRINSIC_Asin:
19208 case CORINFO_INTRINSIC_Asinh:
19209 case CORINFO_INTRINSIC_Acos:
19210 case CORINFO_INTRINSIC_Acosh:
19211 case CORINFO_INTRINSIC_Atan:
19212 case CORINFO_INTRINSIC_Atan2:
19213 case CORINFO_INTRINSIC_Atanh:
19214 case CORINFO_INTRINSIC_Log10:
19215 case CORINFO_INTRINSIC_Pow:
19216 case CORINFO_INTRINSIC_Exp:
19217 case CORINFO_INTRINSIC_Ceiling:
19218 case CORINFO_INTRINSIC_Floor:
19225 bool Compiler::IsMathIntrinsic(GenTreePtr tree)
19227 return (tree->OperGet() == GT_INTRINSIC) && IsMathIntrinsic(tree->gtIntrinsic.gtIntrinsicId);
19230 //------------------------------------------------------------------------
19231 // impDevirtualizeCall: Attempt to change a virtual vtable call into a
19235 // call -- the call node to examine/modify
19236 // method -- [IN/OUT] the method handle for call. Updated iff call devirtualized.
19237 // methodFlags -- [IN/OUT] flags for the method to call. Updated iff call devirtualized.
19238 // contextHandle -- [IN/OUT] context handle for the call. Updated iff call devirtualized.
19239 // exactContextHnd -- [OUT] updated context handle iff call devirtualized
19242 // Virtual calls in IL will always "invoke" the base class method.
19244 // This transformation looks for evidence that the type of 'this'
19245 // in the call is exactly known, is a final class or would invoke
19246 // a final method, and if that and other safety checks pan out,
19247 // modifies the call and the call info to create a direct call.
19249 // This transformation is initially done in the importer and not
19250 // in some subsequent optimization pass because we want it to be
19251 // upstream of inline candidate identification.
19253 // However, later phases may supply improved type information that
19254 // can enable further devirtualization. We currently reinvoke this
19255 // code after inlining, if the return value of the inlined call is
19256 // the 'this obj' of a subsequent virtual call.
19258 // If devirtualization succeeds and the call's this object is the
19259 // result of a box, the jit will ask the EE for the unboxed entry
19260 // point. If this exists, the jit will see if it can rework the box
19261 // to instead make a local copy. If that is doable, the call is
19262 // updated to invoke the unboxed entry on the local copy.
19264 void Compiler::impDevirtualizeCall(GenTreeCall* call,
19265 CORINFO_METHOD_HANDLE* method,
19266 unsigned* methodFlags,
19267 CORINFO_CONTEXT_HANDLE* contextHandle,
19268 CORINFO_CONTEXT_HANDLE* exactContextHandle)
19270 assert(call != nullptr);
19271 assert(method != nullptr);
19272 assert(methodFlags != nullptr);
19273 assert(contextHandle != nullptr);
19275 // This should be a virtual vtable or virtual stub call.
19276 assert(call->IsVirtual());
19278 // Bail if not optimizing
19279 if (opts.MinOpts())
19284 // Bail if debuggable codegen
19285 if (opts.compDbgCode)
19291 // Bail if devirt is disabled.
19292 if (JitConfig.JitEnableDevirtualization() == 0)
19297 const bool doPrint = JitConfig.JitPrintDevirtualizedMethods() == 1;
19300 // Fetch information about the virtual method we're calling.
19301 CORINFO_METHOD_HANDLE baseMethod = *method;
19302 unsigned baseMethodAttribs = *methodFlags;
19304 if (baseMethodAttribs == 0)
19306 // For late devirt we may not have method attributes, so fetch them.
19307 baseMethodAttribs = info.compCompHnd->getMethodAttribs(baseMethod);
19312 // Validate that callInfo has up to date method flags
19313 const DWORD freshBaseMethodAttribs = info.compCompHnd->getMethodAttribs(baseMethod);
19315 // All the base method attributes should agree, save that
19316 // CORINFO_FLG_DONT_INLINE may have changed from 0 to 1
19317 // because of concurrent jitting activity.
19319 // Note we don't look at this particular flag bit below, and
19320 // later on (if we do try and inline) we will rediscover why
19321 // the method can't be inlined, so there's no danger here in
19322 // seeing this particular flag bit in different states between
19323 // the cached and fresh values.
19324 if ((freshBaseMethodAttribs & ~CORINFO_FLG_DONT_INLINE) != (baseMethodAttribs & ~CORINFO_FLG_DONT_INLINE))
19326 assert(!"mismatched method attributes");
19331 // In R2R mode, we might see virtual stub calls to
19332 // non-virtuals. For instance cases where the non-virtual method
19333 // is in a different assembly but is called via CALLVIRT. For
19334 // verison resilience we must allow for the fact that the method
19335 // might become virtual in some update.
19337 // In non-R2R modes CALLVIRT <nonvirtual> will be turned into a
19338 // regular call+nullcheck upstream, so we won't reach this
19340 if ((baseMethodAttribs & CORINFO_FLG_VIRTUAL) == 0)
19342 assert(call->IsVirtualStub());
19343 assert(opts.IsReadyToRun());
19344 JITDUMP("\nimpDevirtualizeCall: [R2R] base method not virtual, sorry\n");
19348 // See what we know about the type of 'this' in the call.
19349 GenTree* thisObj = call->gtCallObjp->gtEffectiveVal(false);
19350 GenTree* actualThisObj = nullptr;
19351 bool isExact = false;
19352 bool objIsNonNull = false;
19353 CORINFO_CLASS_HANDLE objClass = gtGetClassHandle(thisObj, &isExact, &objIsNonNull);
19355 // See if we have special knowlege that can get us a type or a better type.
19356 if ((objClass == nullptr) || !isExact)
19358 actualThisObj = thisObj;
19360 // Walk back through any return expression placeholders
19361 while (actualThisObj->OperGet() == GT_RET_EXPR)
19363 actualThisObj = actualThisObj->gtRetExpr.gtInlineCandidate;
19366 // See if we landed on a call to a special intrinsic method
19367 if (actualThisObj->IsCall())
19369 GenTreeCall* thisObjCall = actualThisObj->AsCall();
19370 if ((thisObjCall->gtCallMoreFlags & GTF_CALL_M_SPECIAL_INTRINSIC) != 0)
19372 assert(thisObjCall->gtCallType == CT_USER_FUNC);
19373 CORINFO_METHOD_HANDLE specialIntrinsicHandle = thisObjCall->gtCallMethHnd;
19374 CORINFO_CLASS_HANDLE specialObjClass = impGetSpecialIntrinsicExactReturnType(specialIntrinsicHandle);
19375 if (specialObjClass != nullptr)
19377 objClass = specialObjClass;
19379 objIsNonNull = true;
19385 // Bail if we know nothing.
19386 if (objClass == nullptr)
19388 JITDUMP("\nimpDevirtualizeCall: no type available (op=%s)\n", GenTree::OpName(thisObj->OperGet()));
19392 // Fetch information about the class that introduced the virtual method.
19393 CORINFO_CLASS_HANDLE baseClass = info.compCompHnd->getMethodClass(baseMethod);
19394 const DWORD baseClassAttribs = info.compCompHnd->getClassAttribs(baseClass);
19396 #if !defined(FEATURE_CORECLR)
19397 // If base class is not beforefieldinit then devirtualizing may
19398 // cause us to miss a base class init trigger. Spec says we don't
19399 // need a trigger for ref class callvirts but desktop seems to
19400 // have one anyways. So defer.
19401 if ((baseClassAttribs & CORINFO_FLG_BEFOREFIELDINIT) == 0)
19403 JITDUMP("\nimpDevirtualizeCall: base class has precise initialization, sorry\n");
19406 #endif // FEATURE_CORECLR
19408 // Is the call an interface call?
19409 const bool isInterface = (baseClassAttribs & CORINFO_FLG_INTERFACE) != 0;
19411 // If the objClass is sealed (final), then we may be able to devirtualize.
19412 const DWORD objClassAttribs = info.compCompHnd->getClassAttribs(objClass);
19413 const bool objClassIsFinal = (objClassAttribs & CORINFO_FLG_FINAL) != 0;
19416 const char* callKind = isInterface ? "interface" : "virtual";
19417 const char* objClassNote = "[?]";
19418 const char* objClassName = "?objClass";
19419 const char* baseClassName = "?baseClass";
19420 const char* baseMethodName = "?baseMethod";
19422 if (verbose || doPrint)
19424 objClassNote = isExact ? " [exact]" : objClassIsFinal ? " [final]" : "";
19425 objClassName = info.compCompHnd->getClassName(objClass);
19426 baseClassName = info.compCompHnd->getClassName(baseClass);
19427 baseMethodName = eeGetMethodName(baseMethod, nullptr);
19431 printf("\nimpDevirtualizeCall: Trying to devirtualize %s call:\n"
19432 " class for 'this' is %s%s (attrib %08x)\n"
19433 " base method is %s::%s\n",
19434 callKind, objClassName, objClassNote, objClassAttribs, baseClassName, baseMethodName);
19437 #endif // defined(DEBUG)
19439 // Bail if obj class is an interface.
19440 // See for instance System.ValueTuple`8::GetHashCode, where lcl 0 is System.IValueTupleInternal
19441 // IL_021d: ldloc.0
19442 // IL_021e: callvirt instance int32 System.Object::GetHashCode()
19443 if ((objClassAttribs & CORINFO_FLG_INTERFACE) != 0)
19445 JITDUMP("--- obj class is interface, sorry\n");
19451 assert(call->IsVirtualStub());
19452 JITDUMP("--- base class is interface\n");
19455 // Fetch the method that would be called based on the declared type of 'this'
19456 CORINFO_CONTEXT_HANDLE ownerType = *contextHandle;
19457 CORINFO_METHOD_HANDLE derivedMethod = info.compCompHnd->resolveVirtualMethod(baseMethod, objClass, ownerType);
19459 // If we failed to get a handle, we can't devirtualize. This can
19460 // happen when prejitting, if the devirtualization crosses
19461 // servicing bubble boundaries.
19462 if (derivedMethod == nullptr)
19464 JITDUMP("--- no derived method, sorry\n");
19468 // Fetch method attributes to see if method is marked final.
19469 const DWORD derivedMethodAttribs = info.compCompHnd->getMethodAttribs(derivedMethod);
19470 const bool derivedMethodIsFinal = ((derivedMethodAttribs & CORINFO_FLG_FINAL) != 0);
19473 const char* derivedClassName = "?derivedClass";
19474 const char* derivedMethodName = "?derivedMethod";
19476 const char* note = "speculative";
19481 else if (objClassIsFinal)
19483 note = "final class";
19485 else if (derivedMethodIsFinal)
19487 note = "final method";
19490 if (verbose || doPrint)
19492 derivedMethodName = eeGetMethodName(derivedMethod, &derivedClassName);
19495 printf(" devirt to %s::%s -- %s\n", derivedClassName, derivedMethodName, note);
19499 #endif // defined(DEBUG)
19501 if (!isExact && !objClassIsFinal && !derivedMethodIsFinal)
19503 // Type is not exact, and neither class or method is final.
19505 // We could speculatively devirtualize, but there's no
19506 // reason to believe the derived method is the one that
19507 // is likely to be invoked.
19509 // If there's currently no further overriding (that is, at
19510 // the time of jitting, objClass has no subclasses that
19511 // override this method), then perhaps we'd be willing to
19513 JITDUMP(" Class not final or exact, method not final, no devirtualization\n");
19517 // For interface calls we must have an exact type or final class.
19518 if (isInterface && !isExact && !objClassIsFinal)
19520 JITDUMP(" Class not final or exact for interface, no devirtualization\n");
19524 JITDUMP(" %s; can devirtualize\n", note);
19526 // Make the updates.
19527 call->gtFlags &= ~GTF_CALL_VIRT_VTABLE;
19528 call->gtFlags &= ~GTF_CALL_VIRT_STUB;
19529 call->gtCallMethHnd = derivedMethod;
19530 call->gtCallType = CT_USER_FUNC;
19532 // Virtual calls include an implicit null check, which we may
19533 // now need to make explicit.
19536 call->gtFlags |= GTF_CALL_NULLCHECK;
19539 // Clear the inline candidate info (may be non-null since
19540 // it's a union field used for other things by virtual
19542 call->gtInlineCandidateInfo = nullptr;
19547 printf("... after devirt...\n");
19553 printf("Devirtualized %s call to %s:%s; now direct call to %s:%s [%s]\n", callKind, baseClassName,
19554 baseMethodName, derivedClassName, derivedMethodName, note);
19556 #endif // defined(DEBUG)
19558 // If the 'this' object is a box, see if we can find the unboxed entry point for the call.
19559 if (thisObj->IsBoxedValue())
19561 JITDUMP("Now have direct call to boxed entry point, looking for unboxed entry point\n");
19563 // Note for some shared methods the unboxed entry point requires an extra parameter.
19564 // We defer optimizing if so.
19565 bool requiresInstMethodTableArg = false;
19566 CORINFO_METHOD_HANDLE unboxedEntryMethod =
19567 info.compCompHnd->getUnboxedEntry(derivedMethod, &requiresInstMethodTableArg);
19569 if (unboxedEntryMethod != nullptr)
19571 // Since the call is the only consumer of the box, we know the box can't escape
19572 // since it is being passed an interior pointer.
19574 // So, revise the box to simply create a local copy, use the address of that copy
19575 // as the this pointer, and update the entry point to the unboxed entry.
19577 // Ideally, we then inline the boxed method and and if it turns out not to modify
19578 // the copy, we can undo the copy too.
19579 if (requiresInstMethodTableArg)
19581 // We can likely handle this case by grabbing the argument passed to
19582 // the newobj in the box. But defer for now.
19583 JITDUMP("Found unboxed entry point, but it needs method table arg, deferring\n");
19587 JITDUMP("Found unboxed entry point, trying to simplify box to a local copy\n");
19588 GenTree* localCopyThis = gtTryRemoveBoxUpstreamEffects(thisObj, BR_MAKE_LOCAL_COPY);
19590 if (localCopyThis != nullptr)
19592 JITDUMP("Success! invoking unboxed entry point on local copy\n");
19593 call->gtCallObjp = localCopyThis;
19594 call->gtCallMethHnd = unboxedEntryMethod;
19595 derivedMethod = unboxedEntryMethod;
19599 JITDUMP("Sorry, failed to undo the box\n");
19605 // Many of the low-level methods on value classes won't have unboxed entries,
19606 // as they need access to the type of the object.
19608 // Note this may be a cue for us to stack allocate the boxed object, since
19609 // we probably know that these objects don't escape.
19610 JITDUMP("Sorry, failed to find unboxed entry point\n");
19614 // Fetch the class that introduced the derived method.
19616 // Note this may not equal objClass, if there is a
19617 // final method that objClass inherits.
19618 CORINFO_CLASS_HANDLE derivedClass = info.compCompHnd->getMethodClass(derivedMethod);
19620 // Need to update call info too. This is fragile
19621 // but hopefully the derived method conforms to
19622 // the base in most other ways.
19623 *method = derivedMethod;
19624 *methodFlags = derivedMethodAttribs;
19625 *contextHandle = MAKE_METHODCONTEXT(derivedMethod);
19627 // Update context handle.
19628 if ((exactContextHandle != nullptr) && (*exactContextHandle != nullptr))
19630 *exactContextHandle = MAKE_METHODCONTEXT(derivedMethod);
19633 #ifdef FEATURE_READYTORUN_COMPILER
19634 if (opts.IsReadyToRun())
19636 // For R2R, getCallInfo triggers bookkeeping on the zap
19637 // side so we need to call it here.
19639 // First, cons up a suitable resolved token.
19640 CORINFO_RESOLVED_TOKEN derivedResolvedToken = {};
19642 derivedResolvedToken.tokenScope = info.compScopeHnd;
19643 derivedResolvedToken.tokenContext = *contextHandle;
19644 derivedResolvedToken.token = info.compCompHnd->getMethodDefFromMethod(derivedMethod);
19645 derivedResolvedToken.tokenType = CORINFO_TOKENKIND_Method;
19646 derivedResolvedToken.hClass = derivedClass;
19647 derivedResolvedToken.hMethod = derivedMethod;
19649 // Look up the new call info.
19650 CORINFO_CALL_INFO derivedCallInfo;
19651 eeGetCallInfo(&derivedResolvedToken, nullptr, addVerifyFlag(CORINFO_CALLINFO_ALLOWINSTPARAM), &derivedCallInfo);
19653 // Update the call.
19654 call->gtCallMoreFlags &= ~GTF_CALL_M_VIRTSTUB_REL_INDIRECT;
19655 call->gtCallMoreFlags &= ~GTF_CALL_M_R2R_REL_INDIRECT;
19656 call->setEntryPoint(derivedCallInfo.codePointerLookup.constLookup);
19658 #endif // FEATURE_READYTORUN_COMPILER
19661 //------------------------------------------------------------------------
19662 // impGetSpecialIntrinsicExactReturnType: Look for special cases where a call
19663 // to an intrinsic returns an exact type
19666 // methodHnd -- handle for the special intrinsic method
19669 // Exact class handle returned by the intrinsic call, if known.
19670 // Nullptr if not known, or not likely to lead to beneficial optimization.
19672 CORINFO_CLASS_HANDLE Compiler::impGetSpecialIntrinsicExactReturnType(CORINFO_METHOD_HANDLE methodHnd)
19674 JITDUMP("Special intrinsic: looking for exact type returned by %s\n", eeGetMethodFullName(methodHnd));
19676 CORINFO_CLASS_HANDLE result = nullptr;
19678 // See what intrinisc we have...
19679 const NamedIntrinsic ni = lookupNamedIntrinsic(methodHnd);
19682 case NI_System_Collections_Generic_EqualityComparer_get_Default:
19684 // Expect one class generic parameter; figure out which it is.
19685 CORINFO_SIG_INFO sig;
19686 info.compCompHnd->getMethodSig(methodHnd, &sig);
19687 assert(sig.sigInst.classInstCount == 1);
19688 CORINFO_CLASS_HANDLE typeHnd = sig.sigInst.classInst[0];
19689 assert(typeHnd != nullptr);
19691 // Lookup can incorrect when we have __Canon as it won't appear
19692 // to implement any interface types.
19694 // And if we do not have a final type, devirt & inlining is
19695 // unlikely to result in much simplification.
19697 // We can use CORINFO_FLG_FINAL to screen out both of these cases.
19698 const DWORD typeAttribs = info.compCompHnd->getClassAttribs(typeHnd);
19699 const bool isFinalType = ((typeAttribs & CORINFO_FLG_FINAL) != 0);
19703 result = info.compCompHnd->getDefaultEqualityComparerClass(typeHnd);
19704 JITDUMP("Special intrinsic for type %s: return type is %s\n", eeGetClassName(typeHnd),
19705 result != nullptr ? eeGetClassName(result) : "unknown");
19709 JITDUMP("Special intrinsic for type %s: type not final, so deferring opt\n", eeGetClassName(typeHnd));
19717 JITDUMP("This special intrinsic not handled, sorry...\n");
19725 //------------------------------------------------------------------------
19726 // impAllocateToken: create CORINFO_RESOLVED_TOKEN into jit-allocated memory and init it.
19729 // token - init value for the allocated token.
19732 // pointer to token into jit-allocated memory.
19733 CORINFO_RESOLVED_TOKEN* Compiler::impAllocateToken(CORINFO_RESOLVED_TOKEN token)
19735 CORINFO_RESOLVED_TOKEN* memory = (CORINFO_RESOLVED_TOKEN*)compGetMem(sizeof(token));
19740 //------------------------------------------------------------------------
19741 // SpillRetExprHelper: iterate through arguments tree and spill ret_expr to local varibales.
19743 class SpillRetExprHelper
19746 SpillRetExprHelper(Compiler* comp) : comp(comp)
19750 void StoreRetExprResultsInArgs(GenTreeCall* call)
19752 GenTreePtr args = call->gtCallArgs;
19753 if (args != nullptr)
19755 comp->fgWalkTreePre(&args, SpillRetExprVisitor, this);
19757 GenTreePtr thisArg = call->gtCallObjp;
19758 if (thisArg != nullptr)
19760 comp->fgWalkTreePre(&thisArg, SpillRetExprVisitor, this);
19765 static Compiler::fgWalkResult SpillRetExprVisitor(GenTree** pTree, Compiler::fgWalkData* fgWalkPre)
19767 assert((pTree != nullptr) && (*pTree != nullptr));
19768 GenTreePtr tree = *pTree;
19769 if ((tree->gtFlags & GTF_CALL) == 0)
19771 // Trees with ret_expr are marked as GTF_CALL.
19772 return Compiler::WALK_SKIP_SUBTREES;
19774 if (tree->OperGet() == GT_RET_EXPR)
19776 SpillRetExprHelper* walker = static_cast<SpillRetExprHelper*>(fgWalkPre->pCallbackData);
19777 walker->StoreRetExprAsLocalVar(pTree);
19779 return Compiler::WALK_CONTINUE;
19782 void StoreRetExprAsLocalVar(GenTreePtr* pRetExpr)
19784 GenTreePtr retExpr = *pRetExpr;
19785 assert(retExpr->OperGet() == GT_RET_EXPR);
19786 JITDUMP("Store return expression %u as a local var.\n", retExpr->gtTreeID);
19787 unsigned tmp = comp->lvaGrabTemp(true DEBUGARG("spilling ret_expr"));
19788 comp->impAssignTempGen(tmp, retExpr, (unsigned)Compiler::CHECK_SPILL_NONE);
19789 *pRetExpr = comp->gtNewLclvNode(tmp, retExpr->TypeGet());
19796 //------------------------------------------------------------------------
19797 // addFatPointerCandidate: mark the call and the method, that they have a fat pointer candidate.
19798 // Spill ret_expr in the call node, because they can't be cloned.
19801 // call - fat calli candidate
19803 void Compiler::addFatPointerCandidate(GenTreeCall* call)
19805 setMethodHasFatPointer();
19806 call->SetFatPointerCandidate();
19807 SpillRetExprHelper helper(this);
19808 helper.StoreRetExprResultsInArgs(call);