InstSimplify: ((X % Y) % Y) -> (X % Y)
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 17 Sep 2014 03:34:34 +0000 (03:34 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 17 Sep 2014 03:34:34 +0000 (03:34 +0000)
Patch by Sonam Kumari!

Differential Revision: http://reviews.llvm.org/D5350

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217937 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/rem.ll

index a745d14ca4283c25f9914963656c8ae7b6fef233..039d8286ecb803a01b904da17df48324a89772d7 100644 (file)
@@ -1171,6 +1171,11 @@ static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
   if (Op0 == Op1)
     return Constant::getNullValue(Op0->getType());
 
+  // ((X % Y) % Y) -> (X % Y)
+  if (match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) {
+    return Op0;
+  }
+
   // If the operation is with the result of a select instruction, check whether
   // operating on either branch of the select always yields the same value.
   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
index 80fa8e7b48315f1a37c6dd95ce3b4f5451a71aab..b757ccd6e4dd76f9728feecb3485dfd99d2c284b 100644 (file)
@@ -15,3 +15,12 @@ define i32 @select2(i32 %x, i1 %b) {
   ret i32 %rem
 ; CHECK: ret i32 0
 }
+
+define i32 @select3(i32 %x, i32 %n) {
+; CHECK-LABEL: @select3(
+; CHECK-NEXT: %mod = srem i32 %x, %n
+; CHECK-NEXT: ret i32 %mod
+ %mod = srem i32 %x, %n
+ %mod1 = srem i32 %mod, %n
+ ret i32 %mod1
+}