Skip to content

Commit e8eb03e

Browse files
morriiswailrst
morriisw
authored andcommitted
Change simplification logic for two's complement negation and fix applyTransform method to generate IL.
1 parent 88e373b commit e8eb03e

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

scripts/tnum_test_z3.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ def subset(self, other):
4747
val_cmp = ((self.v & ~other.m) == other.v)
4848
return mask_cmp & val_cmp
4949

50-
# self_m_extended = ZeroExt(self.w - self.m.size(), self.m)
51-
# other_m_extended = ZeroExt(self.w - other.m.size(), other.m)
52-
# self_v_extended = ZeroExt(self.w - self.v.size(), self.v)
53-
# other_v_extended = ZeroExt(self.w - other.v.size(), other.v)
54-
# mask_cmp = (self_m_extended & ~other_m_extended) == BitVecVal(0, self.w)
55-
# val_cmp = (self_v_extended & ~other_m_extended) == other_v_extended
56-
# return mask_cmp & val_cmp
57-
5850
def union(self,other):
5951
mask = self.m | other.m | (self.v ^ other.v)
6052
val = self.v & ~mask
@@ -64,12 +56,9 @@ def __invert__(self):
6456
return TNum(~self.v & ~self.m, self.m)
6557

6658
def __neg__(self):
67-
return TNum(-self.v & ~self.m, self.m)
59+
tnum = TNum(BitVecVal(0, self.w), BitVecVal(0, self.w))
60+
return tnum - self
6861

69-
# val = If(-self.v & ~self.m == self.v, ZeroExt(1, -self.v & ~self.m), ZeroExt(1, -self.v & ~self.m))
70-
# mask = If(-self.v & ~self.m == self.v, ZeroExt(1, self.m), ZeroExt(1, self.m))
71-
# return TNum(val, mask)
72-
7362
def __and__(self, other):
7463
alpha = self.v | self.m
7564
beta = other.v | other.m
@@ -398,7 +387,6 @@ def test_uop(name, tf, tf_c=None, prec=False, width=4):
398387
test_bop("TXNOR", lambda x, y: xnor_tnum(x, y), xnor_bitvec, prec=True)
399388
test_bop("TNAND", lambda x, y: nand_tnum(x, y), nand_bitvec, prec=True)
400389
test_bop("TADD", lambda x, y: x + y, prec=True)
401-
402390
test_bop("TSUB", lambda x, y: x - y, prec=True)
403391
test_bop("TSHL", lambda x, y: x << y, prec=True)
404392
test_bop("TASHR", lambda x, y: x >> y, prec=True)
@@ -408,5 +396,5 @@ def test_uop(name, tf, tf_c=None, prec=False, width=4):
408396
test_bop("CONCAT", lambda x, y: concat_tnum(x, y), concat_bitvec, prec=True)
409397

410398
test_uop("INV", lambda x: ~x, prec=True)
411-
# test_uop("NEG", lambda x: -x, prec=True)
399+
test_uop("NEG", lambda x: -x, prec=True)
412400
# test_bop("TMUL", lambda x, y: x * y, prec=True)

src/main/scala/ir/transforms/SimplifyKnownBits.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ir.transforms
22
import ir.*
3+
import util.writeToFile
34

45
// A = definite 1 bits, B = unknown bits
56
// A_[i] = 1, B_[i] = 0 -> Bit i of TNum _ is definitely 1
@@ -78,6 +79,7 @@ case class TNumValue(value: BigInt, mask: BigInt) extends TNum {
7879
override def toString() = {
7980
"v.%#016x m.%#016x".format(value, mask)
8081
}
82+
8183
// Bitwise AND
8284
def TAND(that: TNumValue): TNumValue = {
8385
val alpha = this.value | this.mask
@@ -428,7 +430,7 @@ case class TNumValue(value: BigInt, mask: BigInt) extends TNum {
428430

429431
// Two's complement negation
430432
def TNEG(): TNumValue = {
431-
TNumValue(-this.value & ~this.mask, this.mask)
433+
TNumValue(BigInt(0), BigInt(0)).TSUB(this)
432434
}
433435

434436
// Bitwise Not
@@ -596,7 +598,6 @@ class TNumDomain extends AbstractDomain[Map[Variable, TNum]] {
596598
case IntLE => tn1.TSLE(tn2)
597599
case IntGT => tn1.TSGT(tn2)
598600
case IntGE => tn1.TSGE(tn2)
599-
case _ => TNumValue(BigInt(0), BigInt(-1))
600601
}
601602
}
602603

@@ -805,7 +806,7 @@ class SimplifyKnownBits() {
805806

806807
def applyTransform(procedure: Procedure): Unit = {
807808
val (beforeIn, afterIn) = solver.solveProc(procedure, backwards = false)
808-
util.writeToFile(
809+
writeToFile(
809810
translating.PrettyPrinter.pp_proc_with_analysis_results(beforeIn, afterIn, procedure, x => x.toString),
810811
s"${procedure.name}_known_bits.il"
811812
)

0 commit comments

Comments
 (0)