Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -1169,6 +1169,12 @@ if (Op0 == Op1) return Constant::getNullValue(Op0->getType()); + // ((X % Y) % Y) -> (X % Y) + Value *X, *Y; + if (match(Op1, m_Value(Y)) && match(Op0, m_SRem(m_Value(X), m_Specific(Y)))) { + 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(Op0) || isa(Op1)) Index: test/Transforms/InstSimplify/rem.ll =================================================================== --- test/Transforms/InstSimplify/rem.ll +++ test/Transforms/InstSimplify/rem.ll @@ -15,3 +15,12 @@ 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 +}