[llvm] r290927 - [InstCombine] Combine adds across a zext
David Majnemer via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 3 18:21:31 PST 2017
Author: majnemer
Date: Tue Jan 3 20:21:31 2017
New Revision: 290927
URL: http://llvm.org/viewvc/llvm-project?rev=290927&view=rev
Log:
[InstCombine] Combine adds across a zext
We can perform the following:
(add (zext (add nuw X, C1)), C2) -> (zext (add nuw X, C1+C2))
This is only possible if C2 is negative and C2 is greater than or equal to negative C1.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/test/Transforms/InstCombine/add.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=290927&r1=290926&r2=290927&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Tue Jan 3 20:21:31 2017
@@ -1057,6 +1057,18 @@ Instruction *InstCombiner::visitAdd(Bina
// add(zext(xor i16 X, -32768), -32768) --> sext X
return CastInst::Create(Instruction::SExt, X, LHS->getType());
}
+
+ if (Val->isNegative() &&
+ match(LHS, m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C)))) &&
+ Val->sge(-C->sext(Val->getBitWidth()))) {
+ // (add (zext (add nuw X, C)), Val) -> (zext (add nuw X, C+Val))
+ return CastInst::Create(
+ Instruction::ZExt,
+ Builder->CreateNUWAdd(
+ X, Constant::getIntegerValue(X->getType(),
+ *C + Val->trunc(C->getBitWidth()))),
+ I.getType());
+ }
}
// FIXME: Use the match above instead of dyn_cast to allow these transforms
Modified: llvm/trunk/test/Transforms/InstCombine/add.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/add.ll?rev=290927&r1=290926&r2=290927&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/add.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/add.ll Tue Jan 3 20:21:31 2017
@@ -507,3 +507,15 @@ define i1 @test40(i32 %a, i32 %b) {
%cmp = icmp eq i32 %add, %b
ret i1 %cmp
}
+
+define i64 @test41(i32 %a) {
+; CHECK-LABEL: @test41(
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 %a, 15
+; CHECK-NEXT: [[EXT:%.*]] = zext i32 [[ADD]] to i64
+; CHECK-NEXT: ret i64 [[EXT]]
+;
+ %add = add nuw i32 %a, 16
+ %zext = zext i32 %add to i64
+ %sub = add i64 %zext, -1
+ ret i64 %sub
+}
More information about the llvm-commits
mailing list