[llvm] r188936 - Teach BaseIndexOffset::match to identify base pointers in loops.
Juergen Ributzka
juergen at apple.com
Wed Aug 21 14:53:38 PDT 2013
Author: ributzka
Date: Wed Aug 21 16:53:38 2013
New Revision: 188936
URL: http://llvm.org/viewvc/llvm-project?rev=188936&view=rev
Log:
Teach BaseIndexOffset::match to identify base pointers in loops.
The small utility function that pattern matches Base + Index +
Offset patterns for loads and stores fails to recognize the base
pointer for loads/stores from/into an array at offset 0 inside a
loop. As a result DAGCombiner::MergeConsecutiveStores was not able
to merge all stores.
This commit fixes the issue by adding an additional pattern match
and also a test case.
Reviewer: Nadav
Added:
llvm/trunk/test/CodeGen/X86/merge_store.ll
Modified:
llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=188936&r1=188935&r2=188936&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Wed Aug 21 16:53:38 2013
@@ -7859,17 +7859,29 @@ struct BaseIndexOffset {
static BaseIndexOffset match(SDValue Ptr) {
bool IsIndexSignExt = false;
- // Just Base or possibly anything else.
+ // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
+ // instruction, then it could be just the BASE or everything else we don't
+ // know how to handle. Just use Ptr as BASE and give up.
if (Ptr->getOpcode() != ISD::ADD)
return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
- // Base + offset.
+ // We know that we have at least an ADD instruction. Try to pattern match
+ // the simple case of BASE + OFFSET.
if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
IsIndexSignExt);
}
+ // Inside a loop the current BASE pointer is calculated using an ADD and a
+ // MUL insruction. In this case Ptr is the actual BASE pointer.
+ // (i64 add (i64 %array_ptr)
+ // (i64 mul (i64 %induction_var)
+ // (i64 %element_size)))
+ if (Ptr->getOperand(1)->getOpcode() == ISD::MUL) {
+ return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
+ }
+
// Look at Base + Index + Offset cases.
SDValue Base = Ptr->getOperand(0);
SDValue IndexOffset = Ptr->getOperand(1);
Added: llvm/trunk/test/CodeGen/X86/merge_store.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/merge_store.ll?rev=188936&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/merge_store.ll (added)
+++ llvm/trunk/test/CodeGen/X86/merge_store.ll Wed Aug 21 16:53:38 2013
@@ -0,0 +1,30 @@
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=x86-64 | FileCheck %s
+
+define void @merge_store(i32* nocapture %a) {
+; CHECK-LABEL: merge_store:
+; CHECK: movq
+; CHECK: movq
+entry:
+ br label %for.body
+
+ for.body:
+ %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
+ %arrayidx = getelementptr inbounds i32* %a, i64 %indvars.iv
+ store i32 1, i32* %arrayidx, align 4
+ %0 = or i64 %indvars.iv, 1
+ %arrayidx2 = getelementptr inbounds i32* %a, i64 %0
+ store i32 1, i32* %arrayidx2, align 4
+ %1 = or i64 %indvars.iv, 2
+ %arrayidx5 = getelementptr inbounds i32* %a, i64 %1
+ store i32 1, i32* %arrayidx5, align 4
+ %2 = or i64 %indvars.iv, 3
+ %arrayidx8 = getelementptr inbounds i32* %a, i64 %2
+ store i32 1, i32* %arrayidx8, align 4
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 4
+ %3 = trunc i64 %indvars.iv.next to i32
+ %cmp = icmp slt i32 %3, 1000
+ br i1 %cmp, label %for.body, label %for.end
+
+ for.end:
+ ret void
+}
More information about the llvm-commits
mailing list