Changes in directory llvm/lib/Transforms/Scalar:
InstructionCombining.cpp updated: 1.520.2.10 -> 1.520.2.11 --- Log message: --- Diffs of the changes: (+54 -102) InstructionCombining.cpp | 156 ++++++++++++++++------------------------------- 1 files changed, 54 insertions(+), 102 deletions(-) Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.520.2.10 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.520.2.11 --- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.520.2.10 Wed Oct 25 20:58:05 2006 +++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp Sat Oct 28 21:46:08 2006 @@ -2426,33 +2426,8 @@ return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); if (isa<UndefValue>(Op1)) return ReplaceInstUsesWith(I, Op1); // X % undef -> undef - return 0; -} - -Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { - Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); - - if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { - // X % 0 == undef, we don't need to preserve faults! - if (RHS->equalsInt(0)) - return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); - - if (RHS->equalsInt(1)) // X % 1 == 0 - return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); - if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { - if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { - if (Instruction *R = FoldOpIntoSelect(I, SI, this)) - return R; - } else if (isa<PHINode>(Op0I)) { - if (Instruction *NV = FoldOpIntoPhi(I)) - return NV; - } - } - } - - // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, - // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. + // Handle cases involving: rem X, (select Cond, Y, Z) if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in // the same basic block, then we replace the select with Y, and the @@ -2482,37 +2457,43 @@ UpdateValueUsesWith(SI, SI->getOperand(1)); return &I; } - - - if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) - if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) - if (STO->getType()->isUnsigned() && SFO->getType()->isUnsigned()) { - // STO == 0 and SFO == 0 handled above. - if (isPowerOf2_64(STO->getZExtValue()) && - isPowerOf2_64(SFO->getZExtValue())) { - Value *TrueAnd = InsertNewInstBefore( - BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), - I); - Value *FalseAnd = InsertNewInstBefore( - BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), - I); - return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); - } - } } return 0; } -Instruction *InstCombiner::visitURem(BinaryOperator &I) { +Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); - Instruction* common = commonRemTransforms(I); - if (common) + if (Instruction *common = commonRemTransforms(I)) return common; - common = commonIRemTransforms(I); - if (common) + if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { + // X % 0 == undef, we don't need to preserve faults! + if (RHS->equalsInt(0)) + return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); + + if (RHS->equalsInt(1)) // X % 1 == 0 + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + + if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { + if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { + if (Instruction *R = FoldOpIntoSelect(I, SI, this)) + return R; + } else if (isa<PHINode>(Op0I)) { + if (Instruction *NV = FoldOpIntoPhi(I)) + return NV; + } + } + } + + return 0; +} + +Instruction *InstCombiner::visitURem(BinaryOperator &I) { + Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); + + if (Instruction *common = commonIRemTransforms(I)) return common; if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { @@ -2542,7 +2523,26 @@ return BinaryOperator::createAnd(Op0, Add); } } - + } + + // urem X, (Select Cond, C1, C2) --> Select Cond, (and X, C1), (and X, C2) + // where C1&C2 are powers of two. + if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { + if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) + if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) + if (STO->getType()->isUnsigned() && SFO->getType()->isUnsigned()) { + // STO == 0 and SFO == 0 handled above. + if (isPowerOf2_64(STO->getZExtValue()) && + isPowerOf2_64(SFO->getZExtValue())) { + Value *TrueAnd = InsertNewInstBefore( + BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), + I); + Value *FalseAnd = InsertNewInstBefore( + BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), + I); + return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); + } + } } return 0; @@ -2551,12 +2551,7 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); - Instruction *common = commonRemTransforms(I); - if (common) - return common; - - common = commonIRemTransforms(I); - if (common) + if (Instruction *common = commonIRemTransforms(I)) return common; if (Value *RHSNeg = dyn_castNegVal(Op1)) @@ -2572,17 +2567,7 @@ // unsigned inputs), turn this into a urem. uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { - const Type *NTy = Op0->getType()->getUnsignedVersion(); - Instruction *LHS = new CastInst(Op0, NTy, Op0->getName()); - InsertNewInstBefore(LHS, I); - Value *RHS; - if (Constant *R = dyn_cast<Constant>(Op1)) - RHS = ConstantExpr::getCast(R, NTy); - else - RHS = InsertNewInstBefore(new CastInst(Op1, NTy, Op1->getName()), I); - Instruction *URem = BinaryOperator::createURem(LHS, RHS, I.getName()); - InsertNewInstBefore(URem, I); - return new CastInst(URem, I.getType()); + return BinaryOperator::createURem(Op0, Op1, I.getName());; } if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { @@ -2599,42 +2584,7 @@ Instruction *InstCombiner::visitFRem(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); - Instruction *common = commonRemTransforms(I); - if (common) - return common; - - if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { - // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in - // the same basic block, then we replace the select with Y, and the - // condition of the select with false (if the cond value is in the same - // BB). If the select has uses other than the div, this allows them to be - // simplified also. - if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) - if (ST->isNullValue()) { - Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); - if (CondI && CondI->getParent() == I.getParent()) - UpdateValueUsesWith(CondI, ConstantBool::getFalse()); - else if (I.getParent() != SI->getParent() || SI->hasOneUse()) - I.setOperand(1, SI->getOperand(2)); - else - UpdateValueUsesWith(SI, SI->getOperand(2)); - return &I; - } - // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y - if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) - if (ST->isNullValue()) { - Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); - if (CondI && CondI->getParent() == I.getParent()) - UpdateValueUsesWith(CondI, ConstantBool::getTrue()); - else if (I.getParent() != SI->getParent() || SI->hasOneUse()) - I.setOperand(1, SI->getOperand(1)); - else - UpdateValueUsesWith(SI, SI->getOperand(1)); - return &I; - } - } - - return 0; + return commonRemTransforms(I); } // isMaxValueMinusOne - return true if this is Max-1 @@ -5843,6 +5793,8 @@ break; case Instruction::SDiv: case Instruction::UDiv: + case Instruction::SRem: + case Instruction::URem: // If we are just changing the sign, rewrite. if (DestBitSize == SrcBitSize) { // Don't insert two casts if they cannot be eliminated. We allow two _______________________________________________ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits