Implement InstCombine/add.ll:test20

Canonicalize add of sign bit constant into a xor

llvm-svn: 12819
This commit is contained in:
Chris Lattner 2004-04-10 22:01:55 +00:00
parent 825a00195d
commit cf4a996cba
1 changed files with 14 additions and 4 deletions

View File

@ -540,11 +540,21 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
bool Changed = SimplifyCommutative(I);
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
// X + 0 --> X
if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop
RHS == Constant::getNullValue(I.getType()))
RHSC->isNullValue())
return ReplaceInstUsesWith(I, LHS);
// X + (signbit) --> X ^ signbit
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
unsigned NumBits = CI->getType()->getPrimitiveSize()*8;
uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1;
if (Val == (1ULL << NumBits-1))
return BinaryOperator::create(Instruction::Xor, LHS, RHS);
}
}
// X + X --> X << 1
if (I.getType()->isInteger())
if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;