[sil-simplify] Add a peephole for XOR

Use the following equality: (x xor y) xor y == x.
With this in mind (a xor b) xor c can be replaced by:
 a - if b and c are the same
 b - if a and c are the same
 c - if a and b are the same

rdar://20280322

Swift SVN r26568
This commit is contained in:
Roman Levenstein
2015-03-26 00:37:08 +00:00
parent 381e9ebe40
commit f8a1aaff96
2 changed files with 67 additions and 0 deletions

View File

@@ -488,6 +488,29 @@ static SILValue simplifyBuiltin(BuiltinInst *BI) {
}
return SILValue();
}
case BuiltinValueKind::Xor: {
SILValue val1, val2, val3;
// xor (xor (val1, val2), val3) == val1
if (BI->getNumOperands() == 2 &&
(match(BI,
m_BuiltinInst(BuiltinValueKind::Xor,
m_BuiltinInst(BuiltinValueKind::Xor,
m_SILValue(val1), m_SILValue(val2)),
m_SILValue(val3))) ||
match(BI, m_BuiltinInst(BuiltinValueKind::Xor, m_SILValue(val3),
m_BuiltinInst(BuiltinValueKind::Xor,
m_SILValue(val1),
m_SILValue(val2)))))) {
if (val2 == val3)
return val1.getDef();
if (val1 == val3)
return val2.getDef();
if (val1 == val2)
return val3.getDef();
}
}
}
return SILValue();
}