
main.java.com.debughelper.tools.r8.ir.code.IRCode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of debughelper Show documentation
Show all versions of debughelper Show documentation
A gradle plugin help to debug android lib module include native code.
The newest version!
// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.debughelper.tools.r8.ir.code;
import com.debughelper.tools.r8.graph.DebugLocalInfo;
import com.debughelper.tools.r8.graph.DexEncodedMethod;
import com.debughelper.tools.r8.ir.code.MoveException;
import com.debughelper.tools.r8.utils.CfgPrinter;
import com.debughelper.tools.r8.utils.InternalOptions;
import com.google.common.collect.ImmutableList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
public class IRCode {
// Stack marker to denote when all successors of a block have been processed when topologically
// sorting.
private static class BlockMarker {
final BasicBlock block;
BlockMarker(BasicBlock block) {
this.block = block;
}
}
// When numbering instructions we number instructions only with even numbers. This allows us to
// use odd instruction numbers for the insertion of moves during spilling.
public static final int INSTRUCTION_NUMBER_DELTA = 2;
public final DexEncodedMethod method;
public LinkedList blocks;
public final ValueNumberGenerator valueNumberGenerator;
private int usedMarkingColors = 0;
private boolean numbered = false;
private int nextInstructionNumber = 0;
// Initial value indicating if the code does have actual positions on all throwing instructions.
// If this is the case, which holds for javac code, then we want to ensure that it remains so.
private boolean allThrowingInstructionsHavePositions;
public final boolean hasDebugPositions;
public final InternalOptions options;
public IRCode(
InternalOptions options,
DexEncodedMethod method,
LinkedList blocks,
ValueNumberGenerator valueNumberGenerator,
boolean hasDebugPositions) {
this.options = options;
this.method = method;
this.blocks = blocks;
this.valueNumberGenerator = valueNumberGenerator;
this.hasDebugPositions = hasDebugPositions;
allThrowingInstructionsHavePositions = computeAllThrowingInstructionsHavePositions();
}
/**
* Compute the set of live values at the entry to each block using a backwards data-flow analysis.
*/
public Map> computeLiveAtEntrySets() {
Map> liveAtEntrySets = new IdentityHashMap<>();
Queue worklist = new ArrayDeque<>();
// Since this is a backwards data-flow analysis we process the blocks in reverse
// topological order to reduce the number of iterations.
ImmutableList sorted = topologicallySortedBlocks();
worklist.addAll(sorted.reverse());
while (!worklist.isEmpty()) {
BasicBlock block = worklist.poll();
Set live = new HashSet<>();
for (BasicBlock succ : block.getSuccessors()) {
Set succLiveAtEntry = liveAtEntrySets.get(succ);
if (succLiveAtEntry != null) {
live.addAll(succLiveAtEntry);
}
int predIndex = succ.getPredecessors().indexOf(block);
for (Phi phi : succ.getPhis()) {
live.add(phi.getOperand(predIndex));
assert phi.getDebugValues().stream().allMatch(Value::needsRegister);
live.addAll(phi.getDebugValues());
}
}
ListIterator iterator =
block.getInstructions().listIterator(block.getInstructions().size());
while (iterator.hasPrevious()) {
Instruction instruction = iterator.previous();
if (instruction.outValue() != null) {
live.remove(instruction.outValue());
}
for (Value use : instruction.inValues()) {
if (use.needsRegister()) {
live.add(use);
}
}
assert instruction.getDebugValues().stream().allMatch(Value::needsRegister);
live.addAll(instruction.getDebugValues());
}
for (Phi phi : block.getPhis()) {
live.remove(phi);
}
Set previousLiveAtEntry = liveAtEntrySets.put(block, live);
// If the live-at-entry set changed, add the predecessors to the worklist if they are not
// already there.
if (previousLiveAtEntry == null || !previousLiveAtEntry.equals(live)) {
for (BasicBlock pred : block.getPredecessors()) {
if (!worklist.contains(pred)) {
worklist.add(pred);
}
}
}
}
assert liveAtEntrySets.get(sorted.get(0)).size() == 0
: "Unexpected values live at entry to first block: " + liveAtEntrySets.get(sorted.get(0));
return liveAtEntrySets;
}
public void splitCriticalEdges() {
List newBlocks = new ArrayList<>();
int nextBlockNumber = getHighestBlockNumber() + 1;
for (BasicBlock block : blocks) {
// We are using a spilling register allocator that might need to insert moves at
// all critical edges, so we always split them all.
List predecessors = block.getPredecessors();
if (predecessors.size() <= 1) {
continue;
}
// If any of the edges to the block are critical, we need to insert new blocks on each
// containing the move-exception instruction which must remain the first instruction.
if (block.entry() instanceof com.debughelper.tools.r8.ir.code.MoveException) {
nextBlockNumber = block.splitCriticalExceptionEdges(
nextBlockNumber, valueNumberGenerator, newBlocks::add);
continue;
}
for (int predIndex = 0; predIndex < predecessors.size(); predIndex++) {
BasicBlock pred = predecessors.get(predIndex);
if (!pred.hasOneNormalExit()) {
// Critical edge: split it and inject a new block into which the
// phi moves can be inserted. The new block is created with the
// correct predecessor and successor structure. It is inserted
// at the end of the list of blocks disregarding branching
// structure.
BasicBlock newBlock = BasicBlock.createGotoBlock(nextBlockNumber++, block);
newBlocks.add(newBlock);
pred.replaceSuccessor(block, newBlock);
newBlock.getPredecessors().add(pred);
predecessors.set(predIndex, newBlock);
}
}
}
blocks.addAll(newBlocks);
}
/**
* Trace blocks and attempt to put fallthrough blocks immediately after the block that
* falls through. When we fail to do that we create a new fallthrough block with an explicit
* goto to the actual fallthrough block.
*/
public void traceBlocks() {
// Get the blocks first, as calling topologicallySortedBlocks also sets marks.
ImmutableList sorted = topologicallySortedBlocks();
int color = reserveMarkingColor();
int nextBlockNumber = blocks.size();
LinkedList tracedBlocks = new LinkedList<>();
for (BasicBlock block : sorted) {
if (!block.isMarked(color)) {
block.mark(color);
tracedBlocks.add(block);
BasicBlock current = block;
BasicBlock fallthrough = block.exit().fallthroughBlock();
while (fallthrough != null && !fallthrough.isMarked(color)) {
fallthrough.mark(color);
tracedBlocks.add(fallthrough);
current = fallthrough;
fallthrough = fallthrough.exit().fallthroughBlock();
}
if (fallthrough != null) {
BasicBlock newFallthrough = BasicBlock.createGotoBlock(nextBlockNumber++, fallthrough);
current.exit().setFallthroughBlock(newFallthrough);
newFallthrough.getPredecessors().add(current);
fallthrough.replacePredecessor(current, newFallthrough);
newFallthrough.mark(color);
tracedBlocks.add(newFallthrough);
}
}
}
blocks = tracedBlocks;
returnMarkingColor(color);
assert verifyNoColorsInUse();
}
private void ensureBlockNumbering() {
if (!numbered) {
numbered = true;
int blockNumber = 0;
for (BasicBlock block : topologicallySortedBlocks()) {
block.setNumber(blockNumber++);
}
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("blocks:\n");
for (BasicBlock block : blocks) {
builder.append(block.toDetailedString());
builder.append("\n");
}
return builder.toString();
}
public void clearMarks(int color) {
for (BasicBlock block : blocks) {
block.clearMark(color);
}
}
public void removeMarkedBlocks(int color) {
ListIterator blockIterator = listIterator();
while (blockIterator.hasNext()) {
BasicBlock block = blockIterator.next();
if (block.isMarked(color)) {
blockIterator.remove();
}
}
}
private boolean verifyNoBlocksMarked(int color) {
for (BasicBlock block : blocks) {
if (block.isMarked(color)) {
return false;
}
}
return true;
}
public void removeBlocks(List blocksToRemove) {
blocks.removeAll(blocksToRemove);
}
/**
* Compute quasi topologically sorted list of the basic blocks using depth first search.
*
* TODO(ager): We probably want to compute strongly connected components and topologically
* sort strongly connected components instead. However, this is much better than having
* no sorting.
*/
public ImmutableList topologicallySortedBlocks() {
ImmutableList ordered = depthFirstSorting();
return options.testing.placeExceptionalBlocksLast
? reorderExceptionalBlocksLastForTesting(ordered)
: ordered;
}
private ImmutableList depthFirstSorting() {
ArrayList reverseOrdered = new ArrayList<>(blocks.size());
Set visitedBlocks = new HashSet<>(blocks.size());
Deque
© 2015 - 2025 Weber Informatics LLC | Privacy Policy