This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improve div/mod by const power of 2 #5871
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1280,42 +1280,30 @@ void CodeGen::genCodeForDivMod(GenTreeOp* treeNode) | |
| gcInfo.gcMarkRegSetNpt(RBM_RDX); | ||
| } | ||
|
|
||
| if (divisor->isContainedIntOrIImmed()) | ||
| { | ||
| GenTreeIntConCommon* divImm = divisor->AsIntConCommon(); | ||
| assert(divImm->IsIntCnsFitsInI32()); | ||
| ssize_t imm = divImm->IconValue(); | ||
| assert(isPow2(abs(imm))); | ||
| genCodeForPow2Div(treeNode->AsOp()); | ||
| } | ||
| // Perform the 'targetType' (64-bit or 32-bit) divide instruction | ||
| instruction ins; | ||
| if (oper == GT_UMOD || oper == GT_UDIV) | ||
| ins = INS_div; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs braces (i.e.) and I would also add a comment says that INS_div is an unsigned divide (as I always found this mnemonic to be confusing on x86) |
||
| else | ||
| { | ||
| // Perform the 'targetType' (64-bit or 32-bit) divide instruction | ||
| instruction ins; | ||
| if (oper == GT_UMOD || oper == GT_UDIV) | ||
| ins = INS_div; | ||
| else | ||
| ins = INS_idiv; | ||
| ins = INS_idiv; | ||
|
|
||
| emit->emitInsBinary(ins, size, treeNode, divisor); | ||
| emit->emitInsBinary(ins, size, treeNode, divisor); | ||
|
|
||
| // Signed divide RDX:RAX by r/m64, with result | ||
| // stored in RAX := Quotient, RDX := Remainder. | ||
| // Move the result to the desired register, if necessary | ||
| if (oper == GT_DIV || oper == GT_UDIV) | ||
| // DIV/IDIV instructions always store the quotient in RAX and the remainder in RDX. | ||
| // Move the result to the desired register, if necessary | ||
| if (oper == GT_DIV || oper == GT_UDIV) | ||
| { | ||
| if (targetReg != REG_RAX) | ||
| { | ||
| if (targetReg != REG_RAX) | ||
| { | ||
| inst_RV_RV(INS_mov, targetReg, REG_RAX, targetType); | ||
| } | ||
| inst_RV_RV(INS_mov, targetReg, REG_RAX, targetType); | ||
| } | ||
| else | ||
| } | ||
| else | ||
| { | ||
| assert((oper == GT_MOD) || (oper == GT_UMOD)); | ||
| if (targetReg != REG_RDX) | ||
| { | ||
| assert((oper == GT_MOD) || (oper == GT_UMOD)); | ||
| if (targetReg != REG_RDX) | ||
| { | ||
| inst_RV_RV(INS_mov, targetReg, REG_RDX, targetType); | ||
| } | ||
| inst_RV_RV(INS_mov, targetReg, REG_RDX, targetType); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2888,120 +2876,6 @@ CodeGen::genMultiRegCallStoreToLocal(GenTreePtr treeNode) | |
| #endif // !FEATURE_UNIX_AMD64_STRUCT_PASSING | ||
| } | ||
|
|
||
| // Generate code for division (or mod) by power of two | ||
| // or negative powers of two. (meaning -1 * a power of two, not 2^(-1)) | ||
| // Op2 must be a contained integer constant. | ||
| void | ||
| CodeGen::genCodeForPow2Div(GenTreeOp* tree) | ||
| { | ||
| GenTree *dividend = tree->gtOp.gtOp1; | ||
| GenTree *divisor = tree->gtOp.gtOp2; | ||
| genTreeOps oper = tree->OperGet(); | ||
| emitAttr size = emitTypeSize(tree); | ||
| emitter *emit = getEmitter(); | ||
| regNumber targetReg = tree->gtRegNum; | ||
| var_types targetType = tree->TypeGet(); | ||
|
|
||
| bool isSigned = oper == GT_MOD || oper == GT_DIV; | ||
|
|
||
| // precondition: extended dividend is in RDX:RAX | ||
| // which means it is either all zeros or all ones | ||
|
|
||
| noway_assert(divisor->isContained()); | ||
| GenTreeIntConCommon* divImm = divisor->AsIntConCommon(); | ||
| ssize_t imm = divImm->IconValue(); | ||
| ssize_t abs_imm = abs(imm); | ||
| noway_assert(isPow2(abs_imm)); | ||
|
|
||
|
|
||
| if (isSigned) | ||
| { | ||
| if (imm == 1) | ||
| { | ||
| if (oper == GT_DIV) | ||
| { | ||
| if (targetReg != REG_RAX) | ||
| inst_RV_RV(INS_mov, targetReg, REG_RAX, targetType); | ||
| } | ||
| else | ||
| { | ||
| assert(oper == GT_MOD); | ||
| instGen_Set_Reg_To_Zero(size, targetReg); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (abs_imm == 2) | ||
| { | ||
| if (oper == GT_MOD) | ||
| { | ||
| emit->emitIns_R_I(INS_and, size, REG_RAX, 1); // result is 0 or 1 | ||
| // xor with rdx will flip all bits if negative | ||
| emit->emitIns_R_R(INS_xor, size, REG_RAX, REG_RDX); // 111.11110 or 0 | ||
| } | ||
| else | ||
| { | ||
| assert(oper == GT_DIV); | ||
| // add 1 if it's negative | ||
| emit->emitIns_R_R(INS_sub, size, REG_RAX, REG_RDX); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // add imm-1 if negative | ||
| emit->emitIns_R_I(INS_and, size, REG_RDX, abs_imm - 1); | ||
| emit->emitIns_R_R(INS_add, size, REG_RAX, REG_RDX); | ||
| } | ||
|
|
||
| if (oper == GT_DIV) | ||
| { | ||
| unsigned shiftAmount = genLog2(unsigned(abs_imm)); | ||
| inst_RV_SH(INS_sar, size, REG_RAX, shiftAmount); | ||
|
|
||
| if (imm < 0) | ||
| { | ||
| emit->emitIns_R(INS_neg, size, REG_RAX); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| assert(oper == GT_MOD); | ||
| if (abs_imm > 2) | ||
| { | ||
| emit->emitIns_R_I(INS_and, size, REG_RAX, abs_imm - 1); | ||
| } | ||
| // RDX contains 'imm-1' if negative | ||
| emit->emitIns_R_R(INS_sub, size, REG_RAX, REG_RDX); | ||
| } | ||
|
|
||
| if (targetReg != REG_RAX) | ||
| { | ||
| inst_RV_RV(INS_mov, targetReg, REG_RAX, targetType); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| assert (imm > 0); | ||
|
|
||
| if (targetReg != dividend->gtRegNum) | ||
| { | ||
| inst_RV_RV(INS_mov, targetReg, dividend->gtRegNum, targetType); | ||
| } | ||
|
|
||
| if (oper == GT_UDIV) | ||
| { | ||
| inst_RV_SH(INS_shr, size, targetReg, genLog2(unsigned(imm))); | ||
| } | ||
| else | ||
| { | ||
| assert(oper == GT_UMOD); | ||
|
|
||
| emit->emitIns_R_I(INS_and, size, targetReg, imm -1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /*********************************************************************************************** | ||
| * Generate code for localloc | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for also removing this here