diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2008-05-31 17:59:52 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2008-05-31 17:59:52 +0000 |
commit | 9419ddb2898e2ae7a0c22da63cd1a38783200d17 (patch) | |
tree | 69806c667a2c562856cee19442abf72faf1793f9 | |
parent | fd12a0b20b12b0199011fb3903aa1f8ca4b387f5 (diff) |
Add more i1 optimizations. add, sub, mul, s/udiv on i1 are now simplified away.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51817 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 20 | ||||
-rw-r--r-- | test/Transforms/InstCombine/2008-05-31-Bools.ll | 24 |
2 files changed, 39 insertions, 5 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 5199578b1f..6222d2ae49 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/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<Constant>(RHS)) { // X + undef -> undef if (isa<UndefValue>(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<Instruction>(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<BinaryOperator>(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<ZExtInst>(I.getOperand(0))) + if (ZExtInst *CI = dyn_cast<ZExtInst>(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/test/Transforms/InstCombine/2008-05-31-Bools.ll b/test/Transforms/InstCombine/2008-05-31-Bools.ll new file mode 100644 index 0000000000..f3df49b352 --- /dev/null +++ b/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 +} |