Search in sources :

Example 1 with BOUND

use of com.dat3m.dartagnan.configuration.OptionNames.BOUND in project Dat3M by hernanponcedeleon.

the class LoopUnrolling method unrollLoop.

private void unrollLoop(CondJump loopBackJump, int bound) {
    Label loopBegin = loopBackJump.getLabel();
    Preconditions.checkArgument(bound >= 1, "Positive unrolling bound expected.");
    Preconditions.checkArgument(loopBegin.getOId() < loopBackJump.getOId(), "The jump does not belong to a loop.");
    Preconditions.checkArgument(loopBackJump.getUId() < 0, "The loop has already been unrolled");
    // (1) Collect continue points of the loop
    List<CondJump> continues = new ArrayList<>();
    for (Event e = loopBegin; e != null && e != loopBackJump; e = e.getSuccessor()) {
        if (e instanceof CondJump && ((CondJump) e).getLabel() == loopBegin) {
            continues.add((CondJump) e);
        }
    }
    continues.add(loopBackJump);
    // (2) Collect forward jumps from the outside into the loop
    List<CondJump> enterJumps = new ArrayList<>();
    for (Event e = loopBegin; e != null && e != loopBackJump; e = e.getSuccessor()) {
        if (e instanceof Label) {
            Label label = (Label) e;
            label.getJumpSet().stream().filter(j -> j.getOId() < loopBegin.getOId()).forEach(enterJumps::add);
        }
    }
    int iterCounter = 0;
    while (--bound >= 0) {
        iterCounter++;
        if (bound == 0) {
            Label exit = (Label) loopBackJump.getThread().getExit();
            loopBegin.setName(loopBegin.getName() + "_" + iterCounter);
            for (CondJump cont : continues) {
                if (!cont.isGoto()) {
                    logger.warn("Conditional jump {} was replaced by unconditional bound event", cont);
                }
                CondJump boundEvent = EventFactory.newGoto(exit);
                // Keep tags of original jump.
                boundEvent.addFilters(cont.getFilters());
                boundEvent.addFilters(Tag.BOUND, Tag.NOOPT);
                cont.getPredecessor().setSuccessor(boundEvent);
                boundEvent.setSuccessor(cont.getSuccessor());
                cont.delete();
            }
        } else {
            Map<Event, Event> copyCtx = new HashMap<>();
            List<Event> copies = copyPath(loopBegin, loopBackJump, copyCtx);
            ((Label) copyCtx.get(loopBegin)).setName(loopBegin.getName() + "_" + iterCounter);
            // Insert copies at right place
            loopBegin.getPredecessor().setSuccessor(copies.get(0));
            copies.get(copies.size() - 1).setSuccessor(loopBegin);
            // Update entering jumps to go to the copies.
            for (CondJump enterJump : enterJumps) {
                enterJump.updateReferences(copyCtx);
            }
            enterJumps.clear();
            // All "continues" that were copied need to get updated to jump forward to the next iteration.
            for (CondJump cont : continues) {
                if (cont == loopBackJump) {
                    continue;
                }
                CondJump copy = (CondJump) copyCtx.get(cont);
                copy.updateReferences(Map.of(copy.getLabel(), loopBegin));
                enterJumps.add(cont);
            }
        }
    }
}
Also used : Printer(com.dat3m.dartagnan.utils.printer.Printer) PRINT_PROGRAM_AFTER_UNROLLING(com.dat3m.dartagnan.configuration.OptionNames.PRINT_PROGRAM_AFTER_UNROLLING) HashMap(java.util.HashMap) Thread(com.dat3m.dartagnan.program.Thread) Label(com.dat3m.dartagnan.program.event.core.Label) org.sosy_lab.common.configuration(org.sosy_lab.common.configuration) ArrayList(java.util.ArrayList) Tag(com.dat3m.dartagnan.program.event.Tag) EventFactory(com.dat3m.dartagnan.program.event.EventFactory) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Map(java.util.Map) Program(com.dat3m.dartagnan.program.Program) Event(com.dat3m.dartagnan.program.event.core.Event) Preconditions(com.google.common.base.Preconditions) BOUND(com.dat3m.dartagnan.configuration.OptionNames.BOUND) CondJump(com.dat3m.dartagnan.program.event.core.CondJump) LogManager(org.apache.logging.log4j.LogManager) HashMap(java.util.HashMap) Label(com.dat3m.dartagnan.program.event.core.Label) ArrayList(java.util.ArrayList) Event(com.dat3m.dartagnan.program.event.core.Event) CondJump(com.dat3m.dartagnan.program.event.core.CondJump)

Aggregations

BOUND (com.dat3m.dartagnan.configuration.OptionNames.BOUND)1 PRINT_PROGRAM_AFTER_UNROLLING (com.dat3m.dartagnan.configuration.OptionNames.PRINT_PROGRAM_AFTER_UNROLLING)1 Program (com.dat3m.dartagnan.program.Program)1 Thread (com.dat3m.dartagnan.program.Thread)1 EventFactory (com.dat3m.dartagnan.program.event.EventFactory)1 Tag (com.dat3m.dartagnan.program.event.Tag)1 CondJump (com.dat3m.dartagnan.program.event.core.CondJump)1 Event (com.dat3m.dartagnan.program.event.core.Event)1 Label (com.dat3m.dartagnan.program.event.core.Label)1 Printer (com.dat3m.dartagnan.utils.printer.Printer)1 Preconditions (com.google.common.base.Preconditions)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 org.sosy_lab.common.configuration (org.sosy_lab.common.configuration)1