Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 12b807e

Browse files
committed
[MemCpyOpt] Fix wrong merging adjacent nontemporal stores into memset calls.
Pass MemCpyOpt doesn't check if a store instruction is nontemporal. As a consequence, adjacent nontemporal stores are always merged into a memset call. Example: ;;; define void @foo(<4 x float>* nocapture %p) { entry: store <4 x float> zeroinitializer, <4 x float>* %p, align 16, !nontemporal !0 %p1 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 1 store <4 x float> zeroinitializer, <4 x float>* %p1, align 16, !nontemporal !0 ret void } !0 = !{i32 1} ;;; In this example, the two nontemporal stores are combined to a memset of zero which does not preserve the nontemporal hint. Later on the backend (tested on a x86-64 corei7) expands that memset call into a sequence of two normal 16-byte aligned vector stores. opt -memcpyopt example.ll -S -o - | llc -mcpu=corei7 -o - Before: xorps %xmm0, %xmm0 movaps %xmm0, 16(%rdi) movaps %xmm0, (%rdi) With this patch, we no longer merge nontemporal stores into calls to memset. In this example, llc correctly expands the two stores into two movntps: xorps %xmm0, %xmm0 movntps %xmm0, 16(%rdi) movntps %xmm0, (%rdi) In theory, we could extend the usage of !nontemporal metadata to memcpy/memset calls. However a change like that would only have the effect of forcing the backend to expand !nontemporal memsets back to sequences of store instructions. A memset library call would not have exactly the same semantic of a builtin !nontemporal memset call. So, SelectionDAG will have to conservatively expand it back to a sequence of !nontemporal stores (effectively undoing the merging). Differential Revision: http://reviews.llvm.org/D13519 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249820 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 4d9ce58 commit 12b807e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,16 @@ Instruction *MemCpyOpt::tryMergingIntoMemset(Instruction *StartInst,
488488

489489
bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
490490
if (!SI->isSimple()) return false;
491+
492+
// Avoid merging nontemporal stores since the resulting
493+
// memcpy/memset would not be able to preserve the nontemporal hint.
494+
// In theory we could teach how to propagate the !nontemporal metadata to
495+
// memset calls. However, that change would force the backend to
496+
// conservatively expand !nontemporal memset calls back to sequences of
497+
// store instructions (effectively undoing the merging).
498+
if (SI->getMetadata(LLVMContext::MD_nontemporal))
499+
return false;
500+
491501
const DataLayout &DL = SI->getModule()->getDataLayout();
492502

493503
// Detect cases where we're performing call slot forwarding, but
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; RUN: opt < %s -memcpyopt -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
5+
; Verify that we don't combine nontemporal stores into memset calls.
6+
7+
define void @nontemporal_stores_1(<4 x float>* nocapture %dst) {
8+
; CHECK-LABEL: @nontemporal_stores_1
9+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %dst, align 16, !nontemporal !0
10+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr1, align 16, !nontemporal !0
11+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr2, align 16, !nontemporal !0
12+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr3, align 16, !nontemporal !0
13+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr4, align 16, !nontemporal !0
14+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr5, align 16, !nontemporal !0
15+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr6, align 16, !nontemporal !0
16+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr7, align 16, !nontemporal !0
17+
; CHECK-NEXT: ret void
18+
entry:
19+
store <4 x float> zeroinitializer, <4 x float>* %dst, align 16, !nontemporal !0
20+
%ptr1 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 1
21+
store <4 x float> zeroinitializer, <4 x float>* %ptr1, align 16, !nontemporal !0
22+
%ptr2 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 2
23+
store <4 x float> zeroinitializer, <4 x float>* %ptr2, align 16, !nontemporal !0
24+
%ptr3 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 3
25+
store <4 x float> zeroinitializer, <4 x float>* %ptr3, align 16, !nontemporal !0
26+
%ptr4 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 4
27+
store <4 x float> zeroinitializer, <4 x float>* %ptr4, align 16, !nontemporal !0
28+
%ptr5 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 5
29+
store <4 x float> zeroinitializer, <4 x float>* %ptr5, align 16, !nontemporal !0
30+
%ptr6 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 6
31+
store <4 x float> zeroinitializer, <4 x float>* %ptr6, align 16, !nontemporal !0
32+
%ptr7 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 7
33+
store <4 x float> zeroinitializer, <4 x float>* %ptr7, align 16, !nontemporal !0
34+
ret void
35+
}
36+
37+
define void @nontemporal_stores_2(<4 x float>* nocapture %dst) {
38+
; CHECK-LABEL: @nontemporal_stores_2
39+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %dst, align 16, !nontemporal !0
40+
; CHECK: store <4 x float> zeroinitializer, <4 x float>* %ptr1, align 16, !nontemporal !0
41+
; CHECK-NEXT: ret void
42+
entry:
43+
store <4 x float> zeroinitializer, <4 x float>* %dst, align 16, !nontemporal !0
44+
%ptr1 = getelementptr inbounds <4 x float>, <4 x float>* %dst, i64 1
45+
store <4 x float> zeroinitializer, <4 x float>* %ptr1, align 16, !nontemporal !0
46+
ret void
47+
}
48+
49+
!0 = !{i32 1}

0 commit comments

Comments
 (0)