[PATCH] D132555: [instcombine] Optimise for zero initialisation of integer product
Zain Jaffal via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 24 06:19:10 PDT 2022
zjaffal created this revision.
zjaffal added a reviewer: fhahn.
Herald added a subscriber: hiraditya.
Herald added a project: All.
zjaffal requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Similar to the work on `D131672` we optimise integer product if the accumlator value of a loop is initalised to zero
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132555
Files:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/remove-loop-phi-fastmul.ll
Index: llvm/test/Transforms/InstCombine/remove-loop-phi-fastmul.ll
===================================================================
--- llvm/test/Transforms/InstCombine/remove-loop-phi-fastmul.ll
+++ llvm/test/Transforms/InstCombine/remove-loop-phi-fastmul.ll
@@ -280,15 +280,11 @@
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.body:
; CHECK-NEXT: [[I_02:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
-; CHECK-NEXT: [[F_PROD_01:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[MUL:%.*]], [[FOR_BODY]] ]
-; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[ARR_D:%.*]], i64 [[I_02]]
-; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
-; CHECK-NEXT: [[MUL]] = mul nsw i32 [[F_PROD_01]], [[TMP0]]
; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
-; CHECK-NEXT: ret i32 [[MUL]]
+; CHECK-NEXT: ret i32 0
;
entry:
br label %for.body
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -424,6 +424,14 @@
I.setHasNoUnsignedWrap(true);
}
+ // Optimise loop multiplication when the inital accumulator value is zero.
+ // When we have a PHINode initalised to zero and we repeatedly multiply it
+ // with array elements we can remove the loop.
+ PHINode *PN = nullptr;
+ Value *Start = nullptr, *Step = nullptr;
+ if (matchSimpleRecurrence(&I, PN, Start, Step) && match(Start, m_Zero()))
+ return replaceInstUsesWith(I, Start);
+
return Changed ? &I : nullptr;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132555.455182.patch
Type: text/x-patch
Size: 1820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220824/34807d78/attachment.bin>
More information about the llvm-commits
mailing list