[InstCombine] Fold (C - X) ^ signmask -> (C + signmask - X).

llvm-svn: 310186
This commit is contained in:
Craig Topper 2017-08-05 20:00:44 +00:00
parent 65dd32afbc
commit 9ffda5ab86
2 changed files with 20 additions and 6 deletions

View File

@ -2422,11 +2422,17 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
// ~(c-X) == X-c-1 == X+(-c-1)
if (Op0I->getOpcode() == Instruction::Sub && RHSC->isMinusOne())
if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
return BinaryOperator::CreateAdd(Op0I->getOperand(1),
SubOne(NegOp0I0C));
if (Op0I->getOpcode() == Instruction::Sub)
if (ConstantInt *Op0I0C = dyn_cast<ConstantInt>(Op0I->getOperand(0))) {
if (RHSC->isMinusOne()) {
Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
return BinaryOperator::CreateAdd(Op0I->getOperand(1),
SubOne(NegOp0I0C));
} else if (RHSC->getValue().isSignMask()) {
// (C - X) ^ signmask -> (C + signmask - X)
Constant *C = Builder.getInt(RHSC->getValue() + Op0I0C->getValue());
return BinaryOperator::CreateSub(C, Op0I->getOperand(1));
}
}
if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
@ -2440,7 +2446,6 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
// (X + C) ^ signmask -> (X + C + signmask)
Constant *C = Builder.getInt(RHSC->getValue() + Op0CI->getValue());
return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
}
} else if (Op0I->getOpcode() == Instruction::Or) {
// (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0

View File

@ -353,6 +353,15 @@ define i32 @test28(i32 %indvar) {
ret i32 %t214
}
define i32 @test28_sub(i32 %indvar) {
; CHECK-LABEL: @test28_sub(
; CHECK-NEXT: ret i32 -2147483648
;
%t7 = sub i32 -2147483647, %indvar
%t214 = xor i32 %t7, -2147483648
ret i32 %t214
}
define i32 @test29(i1 %C) {
; CHECK-LABEL: @test29(
; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], i32 915, i32 113