Verified Commit 6aa6d01c authored by 萝杨空队-梁韬's avatar 萝杨空队-梁韬
Browse files

opt/misc: fix bugs

1. 修复了丢异常时不会导致程序中止的异常
2. 修复了对非除法二元指令判断 RHS 是否为 0 的验证错误
3. 修复了优化不完全的 Bug
4. 更新了一些注释
Showing with 54 additions and 8 deletions
+54 -8
......@@ -242,6 +242,9 @@ def print_fail(subdir: str):
for prompt in fail_list:
console.log(prompt)
def link_run_on_rpi():
pass
def clear_up():
with console.status('[green bold]Clearing...') as status:
for subdir in sorted(os.listdir(rel('test-data'))):
......
import java.io.*;
import java.lang.Thread.UncaughtExceptionHandler;
import org.antlr.v4.runtime.*;
......@@ -27,13 +28,39 @@ public class Main {
}
}, "", 1 << 30);
final var exceptionSaver = new ExceptionSaver();
thread.setUncaughtExceptionHandler((t, exception) -> {
exceptionSaver.setException(exception);
});
thread.start();
thread.join();
if (exceptionSaver.hasException()) {
throw new RuntimeException("Exception in thread", exceptionSaver.getException());
}
} catch (InterruptedException e) {
throw new RuntimeException("Exception in thread", e);
}
}
static class ExceptionSaver {
public Throwable getException() {
return exception;
}
public void setException(final Throwable exception) {
this.exception = exception;
}
public boolean hasException() {
return exception != null;
}
private Throwable exception = null;
}
public static void runNormal(String target, String inputFileName, String outputFileName) throws IOException {
final var inputStream = openInput(inputFileName);
final var outputStream = openOutput(outputFileName);
......
......@@ -38,6 +38,7 @@ public abstract class Value {
final var oldUserList = new ArrayList<>(userList);
oldUserList.forEach(u -> u.replaceOperandCO(this, newValue));
ensure(userList.isEmpty(), "User list should be empty after RAUW");
GlobalModifitationStatus.current().markAsChanged();
}
// public void replaceAllUseWithInBBlock(Value newValue, BasicBlock bblock) {
......
......@@ -44,7 +44,10 @@ public class BinaryOpInst extends Instruction {
ensureNot(lhs instanceof Constant && rhs instanceof Constant,
"A BinaryOp shouldn't have two constants as its arguments");
ensureNot(rhs instanceof IntConst && ((IntConst) rhs).getValue() == 0,
"An IDiv shouldn't have a constant 0 as its RHS");
if (getKind().equals(InstKind.IDiv)) {
ensureNot(rhs instanceof IntConst && ((IntConst) rhs).getValue() == 0,
"An IDiv shouldn't have a constant 0 as its RHS");
}
}
}
......@@ -71,11 +71,11 @@ public class FunctionInline implements IRPass {
* │ │ └──────┬───────────────┘ │ │
* │ Other Inst... │ │ │ Blocks from callee│
* │ │ └────────────────────► │
* │ ┌──────────────────┐ │ 变成 │ ...... │
* │ ┌──────────────────┐ │ Become │ ...... │
* │ │ Call Inst │ │ ───────► BB_inline_exit │ │
* │ └──────────────x───┘ │ ┌────────────────────────┐ └───┬───┬──┬────────┘
* │ x │ │ │ │ │ │
* │ Other Inst... x │ 变成 │ ─────────────────────┐ ◄───────┘ │ │
* │ Other Inst... x │ Become │ ─────────────────────┐ ◄───────┘ │ │
* │ xxxxxxxxxxxxxxxxxxxxx Phi Inst for │ │ │ │
* │ │ │ │ different return │ ◄───────────┘ │
* └──────┬────────┬──────┘ │ └────────────────────┘ │ │
......
......@@ -21,6 +21,13 @@ public class IRPassManager {
runPass(new ConstructDominatorInfo());
runPass(new SimpleGVN());
runDefaultBlockClearUpPasses();
runMemoryOptimizePass();
runDefaultBlockClearUpPasses();
});
}
public void runMemoryOptimizePass() {
GlobalModifitationStatus.doUntilNoChange(() -> {
runPass(new ReplaceUnnecessaryLoad());
runDefaultBlockClearUpPasses();
runPass(new RemoveUnnecessaryArray());
......@@ -30,6 +37,8 @@ public class IRPassManager {
public void runDefaultBlockClearUpPasses() {
GlobalModifitationStatus.doUntilNoChange(() -> {
runDefaultInstructionClearUpPasses();
runPass(new ClearUnreachableBlock());
runDefaultInstructionClearUpPasses();
runPass(new ClearUnreachableBlock());
runDefaultInstructionClearUpPasses();
......
......@@ -11,18 +11,22 @@ import java.util.List;
public class RemoveUnnecessaryArray implements IRPass {
@Override
public void runPass(final Module module) {
module.getVariables().forEach(this::dealWithGlobalArray);
currModule = module;
IRPass.copyForChange(module.getVariables())
.forEach(this::dealWithGlobalArray);
module.getNonExternalFunction().stream()
.flatMap(List<BasicBlock>::stream)
.forEach(this::dealWithLocalArray);
}
Module currModule;
void dealWithGlobalArray(GlobalVar gv) {
final var hasNoLoad = gv.getUserList().stream()
.noneMatch(gv.isArray() ? this::isGlobalArrayLoad : this::isGlobalVariableLoad);
if (hasNoLoad) {
removeUserTree(gv);
currModule.getVariables().remove(gv);
}
}
......@@ -68,10 +72,9 @@ public class RemoveUnnecessaryArray implements IRPass {
/** 递归删除所有 user 以及 user 的 user 以及 ... */
void removeUserTree(Value value) {
IRPass.copyForChange(value.getUserList()).forEach(this::removeUserTree);
if (value instanceof User) {
final var user = (User) value;
IRPass.copyForChange(user.getUserList()).forEach(this::removeUserTree);
user.freeFromUseDef();
((User) value).freeFromUseDef();
}
if (value instanceof INodeOwner) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment