[prev in list] [next in list] [prev in thread] [next in thread] 

List:       soot-list
Subject:    Re: [Soot-list] Constant Propagation
From:       Alefiya Lightwala <alefiya.lightwala90 () gmail ! com>
Date:       2013-09-23 18:39:10
Message-ID: CACqPjruQ0awMo6k4WE+xOoui9FxrBX1s4qU3gmVGBUWwkeSBMg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thankyou.. It solved the problem...

Regards
Alefiya


On Mon, Sep 23, 2013 at 11:28 PM, Bernhard Berger <berber@tzi.de> wrote:

> Hi Alefiya,
>
> I've not checked your code thoroughly but wanted to give you at least one
> pointer. Your copy function is broken since you do not update the
> destination parameter. You overwrite the parameter value. Hope that this
> helps.
>
> Bernhard
>
> Am 23.09.2013 um 19:37 schrieb Alefiya Lightwala <
> alefiya.lightwala90@gmail.com>:
>
> > package vasco.soot.examples;
> >
> > import java.util.HashMap;
> > import java.util.Map;
> >
> > import soot.Local;
> > import soot.Unit;
> > import soot.Value;
> > import soot.jimple.AssignStmt;
> > import soot.jimple.CastExpr;
> > import soot.jimple.Constant;
> > import soot.jimple.ReturnStmt;
> > import soot.jimple.internal.JimpleLocal;
> > import soot.toolkits.graph.DirectedGraph;
> > import soot.toolkits.graph.UnitGraph;
> > import soot.toolkits.scalar.ArraySparseSet;
> > import soot.toolkits.scalar.FlowSet;
> > import soot.toolkits.scalar.ForwardFlowAnalysis;
> >
> > public class constantprop extends
> ForwardFlowAnalysis<Unit,Map<Local,Constant>>{
> >     private static final Local RETURN_LOCAL = new JimpleLocal("@return",
> null);
> >     DirectedGraph<Unit> graph;
> >     public constantprop(DirectedGraph<Unit> graph) {
> >         super(graph);
> >         this.graph=graph;
> >         doAnalysis();
> >     }
> >     private void assign(Local lhs, Value rhs, Map<Local, Constant>
> input, Map<Local, Constant> output) {
> >         // First remove casts, if any.
> >         if (rhs instanceof CastExpr) {
> >             rhs = ((CastExpr) rhs).getOp();
> >         }
> >         // Then check if the RHS operand is a constant or local
> >         if (rhs instanceof Constant) {
> >             // If RHS is a constant, it is a direct gen
> >             output.put(lhs, (Constant) rhs);
> >         } else if (rhs instanceof Local) {
> >             // Copy constant-status of RHS to LHS (indirect gen), if
> exists
> >             if(input.containsKey(rhs)) {
> >                 output.put(lhs, input.get(rhs));
> >             }
> >         } else {
> >             // RHS is some compound expression, then LHS is non-constant
> (only kill)
> >             output.put(lhs, null);
> >         }
> >     }
> >
> >     @Override
> >     protected void flowThrough(Map<Local, Constant> inValue, Unit unit,
> >             Map<Local, Constant> outValue) {
> >         System.out.println("invalue="+inValue);
> >          copy(inValue,outValue);
> >         // Only statements assigning locals matter
> >         if (unit instanceof AssignStmt) {
> >             // Get operands
> >             Value lhsOp = ((AssignStmt) unit).getLeftOp();
> >             Value rhsOp = ((AssignStmt) unit).getRightOp();
> >             if (lhsOp instanceof Local) {
> >                 assign((Local) lhsOp, rhsOp, inValue, outValue);
> >             }
> >         } else if (unit instanceof ReturnStmt) {
> >             // Get operand
> >             Value rhsOp = ((ReturnStmt) unit).getOp();
> >             assign(RETURN_LOCAL, rhsOp, inValue, outValue);
> >         }
> >         // Return the data flow value at the OUT of the statement
> >
> >     }
> >
> >     @Override
> >     protected void merge(Map<Local, Constant> op1, Map<Local, Constant>
> op2,
> >             Map<Local, Constant> out) {
> >         //Map<Local, Constant> result;
> >         // First add everything in the first operand
> >         copy(op1,out);
> >         FlowSet l = null;
> >         l.add(op1);
> >
> >         // Then add everything in the second operand, bottoming out the
> common keys with different values
> >         for (Local x : op2.keySet()) {
> >             if (op1.containsKey(x)) {
> >                 // Check the values in both operands
> >                 Constant c1 = op1.get(x);
> >                 Constant c2 = op2.get(x);
> >                 if (c1 != null && c1.equals(c2) == false) {
> >                     // Set to non-constant
> >                     out.put(x, null);
> >                 }
> >             } else {
> >                 // Only in second operand, so add as-is
> >                 out.put(x, op2.get(x));
> >             }
> >         }
> >
> >     }
> >
> >     @Override
> >     protected void copy(Map<Local, Constant> source, Map<Local,
> Constant> dest) {
> >         dest = new HashMap<Local,Constant>(source);
> >     }
> >
> >     @Override
> >     protected Map<Local, Constant> entryInitialFlow() {
> >         return new HashMap<Local,Constant>();
> >     }
> >
> >
> >
> >     @Override
> >     protected Map<Local, Constant> newInitialFlow() {
> >         return new HashMap<Local,Constant>();
> >     }
> >
> >
> > }
> >
> > Here I attached code for constant propagation in soot. The values
> computed at OUT of one node are not propagated to IN of its predecessor
> node. IN for all the nodes is null. What is the problem with code?
> >
> > Regards
> > Alefiya
> > _______________________________________________
> > Soot-list mailing list
> > Soot-list@sable.mcgill.ca
> > http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>
>

[Attachment #5 (text/html)]

<div dir="ltr"><div>Thankyou.. It solved the \
problem...<br><br></div>Regards<br>Alefiya<br></div><div \
class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Sep 23, 2013 at 11:28 \
PM, Bernhard Berger <span dir="ltr">&lt;<a href="mailto:berber@tzi.de" \
target="_blank">berber@tzi.de</a>&gt;</span> wrote:<br> <blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Hi Alefiya,<br> <br>
I&#39;ve not checked your code thoroughly but wanted to give you at least one \
pointer. Your copy function is broken since you do not update the destination \
parameter. You overwrite the parameter value. Hope that this helps.<br>

<br>
Bernhard<br>
<br>
Am 23.09.2013 um 19:37 schrieb Alefiya Lightwala &lt;<a \
href="mailto:alefiya.lightwala90@gmail.com">alefiya.lightwala90@gmail.com</a>&gt;:<br>
 <div><div class="h5"><br>
&gt; package vasco.soot.examples;<br>
&gt;<br>
&gt; import java.util.HashMap;<br>
&gt; import java.util.Map;<br>
&gt;<br>
&gt; import soot.Local;<br>
&gt; import soot.Unit;<br>
&gt; import soot.Value;<br>
&gt; import soot.jimple.AssignStmt;<br>
&gt; import soot.jimple.CastExpr;<br>
&gt; import soot.jimple.Constant;<br>
&gt; import soot.jimple.ReturnStmt;<br>
&gt; import soot.jimple.internal.JimpleLocal;<br>
&gt; import soot.toolkits.graph.DirectedGraph;<br>
&gt; import soot.toolkits.graph.UnitGraph;<br>
&gt; import soot.toolkits.scalar.ArraySparseSet;<br>
&gt; import soot.toolkits.scalar.FlowSet;<br>
&gt; import soot.toolkits.scalar.ForwardFlowAnalysis;<br>
&gt;<br>
&gt; public class constantprop extends \
ForwardFlowAnalysis&lt;Unit,Map&lt;Local,Constant&gt;&gt;{<br> &gt;     private \
static final Local RETURN_LOCAL = new JimpleLocal(&quot;@return&quot;, null);<br> \
&gt;     DirectedGraph&lt;Unit&gt; graph;<br> &gt;     public \
constantprop(DirectedGraph&lt;Unit&gt; graph) {<br> &gt;         super(graph);<br>
&gt;         this.graph=graph;<br>
&gt;         doAnalysis();<br>
&gt;     }<br>
&gt;     private void assign(Local lhs, Value rhs, Map&lt;Local, Constant&gt; input, \
Map&lt;Local, Constant&gt; output) {<br> &gt;         // First remove casts, if \
any.<br> &gt;         if (rhs instanceof CastExpr) {<br>
&gt;             rhs = ((CastExpr) rhs).getOp();<br>
&gt;         }<br>
&gt;         // Then check if the RHS operand is a constant or local<br>
&gt;         if (rhs instanceof Constant) {<br>
&gt;             // If RHS is a constant, it is a direct gen<br>
&gt;             output.put(lhs, (Constant) rhs);<br>
&gt;         } else if (rhs instanceof Local) {<br>
&gt;             // Copy constant-status of RHS to LHS (indirect gen), if exists<br>
&gt;             if(input.containsKey(rhs)) {<br>
&gt;                 output.put(lhs, input.get(rhs));<br>
&gt;             }<br>
&gt;         } else {<br>
&gt;             // RHS is some compound expression, then LHS is non-constant (only \
kill)<br> &gt;             output.put(lhs, null);<br>
&gt;         }<br>
&gt;     }<br>
&gt;<br>
&gt;     @Override<br>
&gt;     protected void flowThrough(Map&lt;Local, Constant&gt; inValue, Unit \
unit,<br> &gt;             Map&lt;Local, Constant&gt; outValue) {<br>
&gt;         System.out.println(&quot;invalue=&quot;+inValue);<br>
&gt;          copy(inValue,outValue);<br>
&gt;         // Only statements assigning locals matter<br>
&gt;         if (unit instanceof AssignStmt) {<br>
&gt;             // Get operands<br>
&gt;             Value lhsOp = ((AssignStmt) unit).getLeftOp();<br>
&gt;             Value rhsOp = ((AssignStmt) unit).getRightOp();<br>
&gt;             if (lhsOp instanceof Local) {<br>
&gt;                 assign((Local) lhsOp, rhsOp, inValue, outValue);<br>
&gt;             }<br>
&gt;         } else if (unit instanceof ReturnStmt) {<br>
&gt;             // Get operand<br>
&gt;             Value rhsOp = ((ReturnStmt) unit).getOp();<br>
&gt;             assign(RETURN_LOCAL, rhsOp, inValue, outValue);<br>
&gt;         }<br>
&gt;         // Return the data flow value at the OUT of the statement<br>
&gt;<br>
&gt;     }<br>
&gt;<br>
&gt;     @Override<br>
&gt;     protected void merge(Map&lt;Local, Constant&gt; op1, Map&lt;Local, \
Constant&gt; op2,<br> &gt;             Map&lt;Local, Constant&gt; out) {<br>
&gt;         //Map&lt;Local, Constant&gt; result;<br>
&gt;         // First add everything in the first operand<br>
&gt;         copy(op1,out);<br>
&gt;         FlowSet l = null;<br>
&gt;         l.add(op1);<br>
&gt;<br>
&gt;         // Then add everything in the second operand, bottoming out the common \
keys with different values<br> &gt;         for (Local x : op2.keySet()) {<br>
&gt;             if (op1.containsKey(x)) {<br>
&gt;                 // Check the values in both operands<br>
&gt;                 Constant c1 = op1.get(x);<br>
&gt;                 Constant c2 = op2.get(x);<br>
&gt;                 if (c1 != null &amp;&amp; c1.equals(c2) == false) {<br>
&gt;                     // Set to non-constant<br>
&gt;                     out.put(x, null);<br>
&gt;                 }<br>
&gt;             } else {<br>
&gt;                 // Only in second operand, so add as-is<br>
&gt;                 out.put(x, op2.get(x));<br>
&gt;             }<br>
&gt;         }<br>
&gt;<br>
&gt;     }<br>
&gt;<br>
&gt;     @Override<br>
&gt;     protected void copy(Map&lt;Local, Constant&gt; source, Map&lt;Local, \
Constant&gt; dest) {<br> &gt;         dest = new \
HashMap&lt;Local,Constant&gt;(source);<br> &gt;     }<br>
&gt;<br>
&gt;     @Override<br>
&gt;     protected Map&lt;Local, Constant&gt; entryInitialFlow() {<br>
&gt;         return new HashMap&lt;Local,Constant&gt;();<br>
&gt;     }<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;     @Override<br>
&gt;     protected Map&lt;Local, Constant&gt; newInitialFlow() {<br>
&gt;         return new HashMap&lt;Local,Constant&gt;();<br>
&gt;     }<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt; Here I attached code for constant propagation in soot. The values computed at \
OUT of one node are not propagated to IN of its predecessor node. IN for all the \
nodes is null. What is the problem with code?<br> &gt;<br>
&gt; Regards<br>
&gt; Alefiya<br>
</div></div>&gt; _______________________________________________<br>
&gt; Soot-list mailing list<br>
&gt; <a href="mailto:Soot-list@sable.mcgill.ca">Soot-list@sable.mcgill.ca</a><br>
&gt; <a href="http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list" \
target="_blank">http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list</a><br> <br>
</blockquote></div><br></div>



_______________________________________________
Soot-list mailing list
Soot-list@sable.mcgill.ca
http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic