Search in sources :

Example 6 with Register

use of com.dat3m.dartagnan.program.Register in project Dat3M by hernanponcedeleon.

the class ConstantPropagation method evaluate.

// TODO Once we have a lattice class this should be moved there.
private IExpr evaluate(IExpr input, Map<Register, IExpr> map) {
    if (input instanceof INonDet) {
        return new ITop();
    }
    if (input instanceof IConst) {
        return input;
    }
    if (input instanceof Register) {
        // have the key in the map.
        return map.getOrDefault(input, input);
    }
    if (input instanceof IExprUn) {
        IExprUn un = (IExprUn) input;
        IOpUn op = un.getOp();
        // These two can cause problems
        if (op.equals(BV2INT) || op.equals(BV2UINT)) {
            return input;
        }
        IExpr inner = evaluate(un.getInner(), map);
        return inner instanceof ITop ? inner : new IExprUn(op, inner);
    }
    if (input instanceof IExprBin) {
        IExprBin bin = (IExprBin) input;
        IExpr lhs = evaluate(bin.getLHS(), map);
        IExpr rhs = evaluate(bin.getRHS(), map);
        return lhs instanceof ITop ? lhs : rhs instanceof ITop ? rhs : new IExprBin(lhs, bin.getOp(), rhs);
    }
    if (input instanceof IfExpr) {
        IfExpr ife = (IfExpr) input;
        IExpr tbranch = evaluate(ife.getTrueBranch(), map);
        IExpr fbranch = evaluate(ife.getFalseBranch(), map);
        return tbranch instanceof ITop ? tbranch : fbranch instanceof ITop ? fbranch : new IfExpr(ife.getGuard(), tbranch, fbranch);
    }
    throw new UnsupportedOperationException(String.format("IExpr %s not supported", input));
}
Also used : Register(com.dat3m.dartagnan.program.Register) IOpUn(com.dat3m.dartagnan.expression.op.IOpUn)

Example 7 with Register

use of com.dat3m.dartagnan.program.Register in project Dat3M by hernanponcedeleon.

the class ConstantPropagation method merge.

private Map<Register, IExpr> merge(Map<Register, IExpr> x, Map<Register, IExpr> y) {
    Preconditions.checkNotNull(x);
    Preconditions.checkNotNull(y);
    Map<Register, IExpr> merged = new HashMap<>(x);
    for (Register reg : y.keySet()) {
        if (!merged.containsKey(reg)) {
            merged.put(reg, y.get(reg));
        } else if (!merged.get(reg).equals(y.get(reg))) {
            merged.put(reg, new ITop());
        }
    }
    return merged;
}
Also used : Register(com.dat3m.dartagnan.program.Register) HashMap(java.util.HashMap)

Example 8 with Register

use of com.dat3m.dartagnan.program.Register in project Dat3M by hernanponcedeleon.

the class ConstantPropagation method getSimplifiedCopy.

// Creates a copy of the provided event, using the <propagationMap> to simplify expressions.
// Can return the original event if no simplifications are performed
private Event getSimplifiedCopy(Event ev, Map<Register, IExpr> propagationMap) {
    Event copy = ev;
    if (ev instanceof MemEvent && !ev.is(Tag.C11.PTHREAD) && !ev.is(Tag.C11.LOCK)) {
        MemEvent m = (MemEvent) ev;
        String mo = m.getMo();
        // All events for which we use reg are RegWriters
        Register reg = ev instanceof RegWriter ? ((RegWriter) ev).getResultRegister() : null;
        IExpr oldAddress = m.getAddress();
        IExpr newAddress = evaluate(oldAddress, propagationMap);
        newAddress = newAddress instanceof ITop ? oldAddress : newAddress;
        Verify.verifyNotNull(newAddress, "Expression %s got no value after constant propagation analysis", oldAddress);
        IExpr oldValue = (IExpr) ((MemEvent) ev).getMemValue();
        IExpr newValue = evaluate(oldValue, propagationMap);
        newValue = newValue instanceof ITop ? oldValue : newValue;
        Verify.verifyNotNull(newValue, "Expression %s got no value after constant propagation analysis", oldValue);
        // Atomic Events
        if (ev instanceof AtomicLoad) {
            copy = Atomic.newLoad(reg, newAddress, mo);
        } else if (ev instanceof AtomicStore) {
            copy = Atomic.newStore(newAddress, newValue, mo);
        } else if (ev instanceof AtomicCmpXchg) {
            IExpr oldExpectedAddr = ((AtomicCmpXchg) ev).getExpectedAddr();
            IExpr newExpectedAddr = evaluate(oldExpectedAddr, propagationMap);
            Verify.verifyNotNull(newExpectedAddr, "Register %s got no value after constant propagation analysis", oldExpectedAddr);
            copy = Atomic.newCompareExchange(reg, newAddress, newExpectedAddr, newValue, mo, ev.is(Tag.STRONG));
        } else if (ev instanceof AtomicXchg) {
            copy = Atomic.newExchange(reg, newAddress, newValue, mo);
        } else if (ev instanceof AtomicFetchOp) {
            copy = Atomic.newFetchOp(reg, newAddress, newValue, ((AtomicFetchOp) ev).getOp(), mo);
        } else // Linux Events
        if (ev instanceof RMWAddUnless) {
            copy = Linux.newRMWAddUnless(newAddress, reg, ((RMWAddUnless) ev).getCmp(), newValue);
        } else if (ev instanceof RMWCmpXchg) {
            copy = Linux.newRMWCompareExchange(newAddress, reg, ((RMWCmpXchg) ev).getCmp(), newValue, mo);
        } else if (ev instanceof RMWFetchOp) {
            copy = Linux.newRMWFetchOp(newAddress, reg, newValue, ((RMWFetchOp) ev).getOp(), mo);
        } else if (ev instanceof RMWOp) {
            copy = Linux.newRMWOp(newAddress, reg, newValue, ((RMWOp) ev).getOp());
        } else if (ev instanceof RMWOpAndTest) {
            copy = Linux.newRMWOpAndTest(newAddress, reg, newValue, ((RMWOpAndTest) ev).getOp());
        } else if (ev instanceof RMWOpReturn) {
            copy = Linux.newRMWOpReturn(newAddress, reg, newValue, ((RMWOpReturn) ev).getOp(), mo);
        } else if (ev instanceof RMWXchg) {
            copy = Linux.newRMWExchange(newAddress, reg, newValue, mo);
        } else // Exclusive events
        if (ev.is(Tag.EXCL)) {
            if (ev instanceof Load) {
                copy = EventFactory.newRMWLoadExclusive(reg, newAddress, mo);
            } else if (ev instanceof StoreExclusive) {
                copy = AArch64.newExclusiveStore(reg, newAddress, newValue, mo);
            } else {
                // Other EXCL events are generated during compilation (which have not yet occurred)
                throw new UnsupportedOperationException(String.format("Exclusive event %s not supported by %s", ev.getClass().getSimpleName(), getClass().getSimpleName()));
            }
        } else // Basic Events
        if (ev instanceof Load) {
            copy = EventFactory.newLoad(reg, newAddress, mo);
        } else if (ev instanceof Store) {
            copy = EventFactory.newStore(newAddress, newValue, mo);
        }
    } else // Local.initialise() which is never the case for the new Event e below.
    if (ev instanceof Local && ((Local) ev).getExpr() instanceof IExpr && !ev.is(Tag.ASSERTION)) {
        Register reg = ((Local) ev).getResultRegister();
        IExpr oldValue = (IExpr) ((Local) ev).getExpr();
        IExpr newValue = evaluate(oldValue, propagationMap);
        newValue = newValue instanceof ITop ? oldValue : newValue;
        Verify.verify(newValue != null, String.format("Expression %s got no value after constant propagation analysis", oldValue));
        copy = EventFactory.newLocal(reg, newValue);
    }
    if (copy != ev) {
        // We made a real copy
        copy.setOId(ev.getOId());
        copy.setUId(ev.getUId());
        copy.setCId(ev.getCId());
        copy.setCLine(ev.getCLine());
        copy.setThread(ev.getThread());
    }
    return copy;
}
Also used : StoreExclusive(com.dat3m.dartagnan.program.event.arch.aarch64.StoreExclusive) Register(com.dat3m.dartagnan.program.Register) RegWriter(com.dat3m.dartagnan.program.event.core.utils.RegWriter)

Example 9 with Register

use of com.dat3m.dartagnan.program.Register in project Dat3M by hernanponcedeleon.

the class VisitorNone method visitAtomicFetchOp.

@Override
public List<Event> visitAtomicFetchOp(AtomicFetchOp e) {
    Register resultRegister = e.getResultRegister();
    IOpBin op = e.getOp();
    IExpr address = e.getAddress();
    String mo = e.getMo();
    Register dummyReg = new Register(null, resultRegister.getThreadId(), resultRegister.getPrecision());
    Load load = newRMWLoad(resultRegister, address, mo);
    return eventSequence(load, newLocal(dummyReg, new IExprBin(resultRegister, op, (IExpr) e.getMemValue())), newRMWStore(load, address, dummyReg, mo));
}
Also used : IOpBin(com.dat3m.dartagnan.expression.op.IOpBin) Register(com.dat3m.dartagnan.program.Register)

Example 10 with Register

use of com.dat3m.dartagnan.program.Register in project Dat3M by hernanponcedeleon.

the class VisitorNone method visitRMWCmpXchg.

@Override
public List<Event> visitRMWCmpXchg(RMWCmpXchg e) {
    Register resultRegister = e.getResultRegister();
    ExprInterface cmp = e.getCmp();
    ExprInterface value = e.getMemValue();
    IExpr address = e.getAddress();
    String mo = e.getMo();
    Register dummy = resultRegister;
    if (resultRegister == value || resultRegister == cmp) {
        dummy = new Register(null, resultRegister.getThreadId(), resultRegister.getPrecision());
    }
    RMWReadCondCmp load = Linux.newRMWReadCondCmp(dummy, cmp, address, Tag.Linux.loadMO(mo));
    Local optionalUpdateReg = dummy != resultRegister ? newLocal(resultRegister, dummy) : null;
    Fence optionalMbBefore = mo.equals(Tag.Linux.MO_MB) ? Linux.newConditionalMemoryBarrier(load) : null;
    Fence optionalMbAfter = mo.equals(Tag.Linux.MO_MB) ? Linux.newConditionalMemoryBarrier(load) : null;
    return eventSequence(optionalMbBefore, load, Linux.newRMWStoreCond(load, address, value, Tag.Linux.storeMO(mo)), optionalUpdateReg, optionalMbAfter);
}
Also used : Register(com.dat3m.dartagnan.program.Register) RMWReadCondCmp(com.dat3m.dartagnan.program.event.lang.linux.cond.RMWReadCondCmp)

Aggregations

Register (com.dat3m.dartagnan.program.Register)154 MemoryObject (com.dat3m.dartagnan.program.memory.MemoryObject)29 Event (com.dat3m.dartagnan.program.event.core.Event)25 IExpr (com.dat3m.dartagnan.expression.IExpr)16 RegWriter (com.dat3m.dartagnan.program.event.core.utils.RegWriter)15 ProgramBuilder (com.dat3m.dartagnan.parsers.program.utils.ProgramBuilder)13 AliasAnalysis (com.dat3m.dartagnan.program.analysis.AliasAnalysis)12 Label (com.dat3m.dartagnan.program.event.core.Label)11 ParsingException (com.dat3m.dartagnan.exception.ParsingException)10 BigInteger (java.math.BigInteger)8 IConst (com.dat3m.dartagnan.expression.IConst)7 IExprBin (com.dat3m.dartagnan.expression.IExprBin)6 IValue (com.dat3m.dartagnan.expression.IValue)6 IOpBin (com.dat3m.dartagnan.expression.op.IOpBin)6 Program (com.dat3m.dartagnan.program.Program)6 Dependency (com.dat3m.dartagnan.program.analysis.Dependency)6 ExprInterface (com.dat3m.dartagnan.expression.ExprInterface)5 Thread (com.dat3m.dartagnan.program.Thread)5 java.util (java.util)5 Atom (com.dat3m.dartagnan.expression.Atom)4