From 26b8cd84b3c762dc6fbd7d83bebe42b3df2f8d02 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sat, 31 May 2008 17:59:52 +0000 Subject: [PATCH] Add more i1 optimizations. add, sub, mul, s/udiv on i1 are now simplified away. llvm-svn: 51817 --- .../Scalar/InstructionCombining.cpp | 20 ++++++++++++---- .../InstCombine/2008-05-31-Bools.ll | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 llvm/test/Transforms/InstCombine/2008-05-31-Bools.ll diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index 5199578b1f81..6222d2ae49bf 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2551,9 +2551,6 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { bool Changed = SimplifyCommutative(I); Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); - if (I.getType() == Type::Int1Ty) - return BinaryOperator::CreateXor(LHS, RHS); - if (Constant *RHSC = dyn_cast(RHS)) { // X + undef -> undef if (isa(RHS)) @@ -2637,8 +2634,11 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { } } + if (I.getType() == Type::Int1Ty) + return BinaryOperator::CreateXor(LHS, RHS); + // X + X --> X << 1 - if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { + if (I.getType()->isInteger()) { if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; if (Instruction *RHSI = dyn_cast(RHS)) { @@ -2976,6 +2976,9 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { return NV; } + if (I.getType() == Type::Int1Ty) + return BinaryOperator::CreateXor(Op0, Op1); + if (BinaryOperator *Op1I = dyn_cast(Op1)) { if (Op1I->getOpcode() == Instruction::Add && !Op0->getType()->isFPOrFPVector()) { @@ -3170,12 +3173,15 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) return BinaryOperator::CreateMul(Op0v, Op1v); + if (I.getType() == Type::Int1Ty) + return BinaryOperator::CreateAnd(Op0, I.getOperand(1)); + // If one of the operands of the multiply is a cast from a boolean value, then // we know the bool is either zero or one, so this is a 'masking' multiply. // See if we can simplify things based on how the boolean was originally // formed. CastInst *BoolCast = 0; - if (ZExtInst *CI = dyn_cast(I.getOperand(0))) + if (ZExtInst *CI = dyn_cast(Op0)) if (CI->getOperand(0)->getType() == Type::Int1Ty) BoolCast = CI; if (!BoolCast) @@ -3331,6 +3337,10 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { if (LHS->equalsInt(0)) return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + // It can't be division by zero, hence it must be division by one. + if (I.getType() == Type::Int1Ty) + return ReplaceInstUsesWith(I, Op0); + return 0; } diff --git a/llvm/test/Transforms/InstCombine/2008-05-31-Bools.ll b/llvm/test/Transforms/InstCombine/2008-05-31-Bools.ll new file mode 100644 index 000000000000..f3df49b352c3 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/2008-05-31-Bools.ll @@ -0,0 +1,24 @@ +; RUN: llvm-as < %s | opt -instcombine | llvm-dis > %t +; RUN: grep {xor} %t +; RUN: grep {and} %t +; RUN: not grep {div} %t + +define i1 @foo1(i1 %a, i1 %b) { + %A = sub i1 %a, %b + ret i1 %A +} + +define i1 @foo2(i1 %a, i1 %b) { + %A = mul i1 %a, %b + ret i1 %A +} + +define i1 @foo3(i1 %a, i1 %b) { + %A = udiv i1 %a, %b + ret i1 %A +} + +define i1 @foo4(i1 %a, i1 %b) { + %A = sdiv i1 %a, %b + ret i1 %A +}