[llvm-commits] [llvm] r75539 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/udivrem-change-width.ll
Eli Friedman
eli.friedman at gmail.com
Mon Jul 13 15:46:03 PDT 2009
Author: efriedma
Date: Mon Jul 13 17:46:01 2009
New Revision: 75539
URL: http://llvm.org/viewvc/llvm-project?rev=75539&view=rev
Log:
PR4548: optimize zext+udiv+trunc to udiv.
Added:
llvm/trunk/test/Transforms/InstCombine/udivrem-change-width.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=75539&r1=75538&r2=75539&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Mon Jul 13 17:46:01 2009
@@ -7974,6 +7974,23 @@
CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
NumCastsRemoved);
+ case Instruction::UDiv:
+ case Instruction::URem: {
+ // UDiv and URem can be truncated if all the truncated bits are zero.
+ uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
+ uint32_t BitWidth = Ty->getScalarSizeInBits();
+ if (BitWidth < OrigBitWidth) {
+ APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
+ if (MaskedValueIsZero(I->getOperand(0), Mask) &&
+ MaskedValueIsZero(I->getOperand(1), Mask)) {
+ return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
+ NumCastsRemoved) &&
+ CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
+ NumCastsRemoved);
+ }
+ }
+ break;
+ }
case Instruction::Shl:
// If we are truncating the result of this SHL, and if it's a shift of a
// constant amount, we can always perform a SHL in a smaller type.
@@ -8060,7 +8077,9 @@
case Instruction::Xor:
case Instruction::AShr:
case Instruction::LShr:
- case Instruction::Shl: {
+ case Instruction::Shl:
+ case Instruction::UDiv:
+ case Instruction::URem: {
Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
Added: llvm/trunk/test/Transforms/InstCombine/udivrem-change-width.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/udivrem-change-width.ll?rev=75539&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/udivrem-change-width.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/udivrem-change-width.ll Mon Jul 13 17:46:01 2009
@@ -0,0 +1,19 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep zext
+; PR4548
+
+define i8 @udiv_i8(i8 %a, i8 %b) nounwind {
+ %conv = zext i8 %a to i32
+ %conv2 = zext i8 %b to i32
+ %div = udiv i32 %conv, %conv2
+ %conv3 = trunc i32 %div to i8
+ ret i8 %conv3
+}
+
+define i8 @urem_i8(i8 %a, i8 %b) nounwind {
+ %conv = zext i8 %a to i32
+ %conv2 = zext i8 %b to i32
+ %div = urem i32 %conv, %conv2
+ %conv3 = trunc i32 %div to i8
+ ret i8 %conv3
+}
+
More information about the llvm-commits
mailing list