-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optimize switch fallthrough (PR #2054)
* cache post dom map between switch cases * cache post dom map of whole methods * calculate full post dom tree, fix switch out block detection --------- Co-authored-by: Skylot <[email protected]>
- Loading branch information
Showing
12 changed files
with
237 additions
and
236 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
65 changes: 65 additions & 0 deletions
65
jadx-core/src/main/java/jadx/core/dex/visitors/blocks/PostDominatorTree.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package jadx.core.dex.visitors.blocks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.BitSet; | ||
import java.util.List; | ||
|
||
import jadx.core.dex.nodes.BlockNode; | ||
import jadx.core.dex.nodes.MethodNode; | ||
import jadx.core.utils.BlockUtils; | ||
import jadx.core.utils.EmptyBitSet; | ||
|
||
public class PostDominatorTree { | ||
|
||
public static void compute(MethodNode mth) { | ||
try { | ||
int mthBlocksCount = mth.getBasicBlocks().size(); | ||
List<BlockNode> sorted = new ArrayList<>(mthBlocksCount); | ||
BlockUtils.visitReverseDFS(mth, sorted::add); | ||
// temporary set block ids to match reverse sorted order | ||
// save old ids for later remapping | ||
int blocksCount = sorted.size(); | ||
int[] ids = new int[mthBlocksCount]; | ||
for (int i = 0; i < blocksCount; i++) { | ||
ids[i] = sorted.get(i).getId(); | ||
} | ||
mth.updateBlockIds(sorted); | ||
|
||
BlockNode[] postDoms = DominatorTree.build(sorted, BlockNode::getSuccessors); | ||
BlockNode firstBlock = sorted.get(0); | ||
firstBlock.setPostDoms(EmptyBitSet.EMPTY); | ||
firstBlock.setIPostDom(null); | ||
for (int i = 1; i < blocksCount; i++) { | ||
BlockNode block = sorted.get(i); | ||
BlockNode iPostDom = postDoms[i]; | ||
block.setIPostDom(iPostDom); | ||
BitSet postDomBS = DominatorTree.collectDoms(postDoms, iPostDom); | ||
block.setPostDoms(postDomBS); | ||
} | ||
for (int i = 1; i < blocksCount; i++) { | ||
BlockNode block = sorted.get(i); | ||
BitSet bs = new BitSet(blocksCount); | ||
block.getPostDoms().stream().forEach(n -> bs.set(ids[n])); | ||
bs.clear(ids[i]); | ||
block.setPostDoms(bs); | ||
} | ||
// check for missing blocks in 'sorted' list | ||
// can be caused by infinite loops | ||
int blocksDelta = mthBlocksCount - blocksCount; | ||
if (blocksDelta != 0) { | ||
int insnsCount = 0; | ||
for (BlockNode block : mth.getBasicBlocks()) { | ||
if (block.getPostDoms() == null) { | ||
block.setPostDoms(EmptyBitSet.EMPTY); | ||
block.setIPostDom(null); | ||
insnsCount += block.getInstructions().size(); | ||
} | ||
} | ||
mth.addInfoComment("Infinite loop detected, blocks: " + blocksDelta + ", insns: " + insnsCount); | ||
} | ||
} finally { | ||
// revert block ids change | ||
mth.updateBlockIds(mth.getBasicBlocks()); | ||
} | ||
} | ||
} |
Oops, something went wrong.