llvm.org GIT mirror llvm / f28f8bc
Correctly lower memset / memcpy of undef. It should be a nop. PR6767. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100208 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 9 years ago
8 changed file(s) with 94 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
632632 }
633633
634634 /// getOptimalMemOpType - Returns the target specific optimal type for load
635 /// and store operations as a result of memset, memcpy, and memmove lowering.
636 /// If DstAlign is zero that means it's safe to destination alignment can
637 /// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
638 /// a need to check it against alignment requirement, probably because the
639 /// source does not need to be loaded. It returns EVT::Other if SelectionDAG
640 /// should be responsible for determining it.
635 /// and store operations as a result of memset, memcpy, and memmove
636 /// lowering. If DstAlign is zero that means it's safe to destination
637 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
638 /// means there isn't a need to check it against alignment requirement,
639 /// probably because the source does not need to be loaded. If
640 /// 'NonScalarIntSafe' is true, that means it's safe to return a
641 /// non-scalar-integer type, e.g. empty string source, constant, or loaded
642 /// from memory. It returns EVT::Other if SelectionDAG should be responsible
643 /// for determining it.
641644 virtual EVT getOptimalMemOpType(uint64_t Size,
642645 unsigned DstAlign, unsigned SrcAlign,
643 bool SafeToUseFP, SelectionDAG &DAG) const {
646 bool NonScalarIntSafe,
647 SelectionDAG &DAG) const {
644648 return MVT::Other;
645649 }
646650
30933093 /// operand.
30943094 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
30953095 DebugLoc dl) {
3096 assert(Value.getOpcode() != ISD::UNDEF);
3097
30963098 unsigned NumBits = VT.getScalarType().getSizeInBits();
30973099 if (ConstantSDNode *C = dyn_cast(Value)) {
30983100 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
31963198 static bool FindOptimalMemOpLowering(std::vector &MemOps,
31973199 unsigned Limit, uint64_t Size,
31983200 unsigned DstAlign, unsigned SrcAlign,
3199 bool SafeToUseFP,
3201 bool NonScalarIntSafe,
32003202 SelectionDAG &DAG,
32013203 const TargetLowering &TLI) {
32023204 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
32063208 // the inferred alignment of the source. 'DstAlign', on the other hand, is the
32073209 // specified alignment of the memory operation. If it is zero, that means
32083210 // it's possible to change the alignment of the destination.
3209 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign, SafeToUseFP, DAG);
3211 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
3212 NonScalarIntSafe, DAG);
32103213
32113214 if (VT == MVT::Other) {
32123215 VT = TLI.getPointerTy();
32653268 unsigned Align, bool AlwaysInline,
32663269 const Value *DstSV, uint64_t DstSVOff,
32673270 const Value *SrcSV, uint64_t SrcSVOff) {
3268 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3271 // Turn a memcpy of undef to nop.
3272 if (Src.getOpcode() == ISD::UNDEF)
3273 return Chain;
32693274
32703275 // Expand memcpy to a series of load and store ops if the size operand falls
32713276 // below a certain threshold.
3277 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
32723278 std::vector MemOps;
32733279 uint64_t Limit = -1ULL;
32743280 if (!AlwaysInline)
33513357 unsigned Align,bool AlwaysInline,
33523358 const Value *DstSV, uint64_t DstSVOff,
33533359 const Value *SrcSV, uint64_t SrcSVOff) {
3354 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3360 // Turn a memmove of undef to nop.
3361 if (Src.getOpcode() == ISD::UNDEF)
3362 return Chain;
33553363
33563364 // Expand memmove to a series of load and store ops if the size operand falls
33573365 // below a certain threshold.
3366 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
33583367 std::vector MemOps;
33593368 uint64_t Limit = -1ULL;
33603369 if (!AlwaysInline)
34253434 SDValue Src, uint64_t Size,
34263435 unsigned Align,
34273436 const Value *DstSV, uint64_t DstSVOff) {
3428 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3437 // Turn a memset of undef to nop.
3438 if (Src.getOpcode() == ISD::UNDEF)
3439 return Chain;
34293440
34303441 // Expand memset to a series of load/store ops if the size operand
34313442 // falls below a certain threshold.
3443 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
34323444 std::vector MemOps;
34333445 bool DstAlignCanChange = false;
34343446 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
34353447 FrameIndexSDNode *FI = dyn_cast(Dst);
34363448 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
34373449 DstAlignCanChange = true;
3438 bool IsZero = isa(Src) &&
3439 cast(Src)->isNullValue();
3450 bool NonScalarIntSafe =
3451 isa(Src) && cast(Src)->isNullValue();
34403452 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(),
34413453 Size, (DstAlignCanChange ? 0 : Align), 0,
3442 IsZero, DAG, TLI))
3454 NonScalarIntSafe, DAG, TLI))
34433455 return SDValue();
34443456
34453457 if (DstAlignCanChange) {
35913603 if (ConstantSize->isNullValue())
35923604 return Chain;
35933605
3594 SDValue Result =
3595 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
3596 Align, DstSV, DstSVOff);
3606 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
3607 ConstantSize->getZExtValue(),
3608 Align, DstSV, DstSVOff);
35973609 if (Result.getNode())
35983610 return Result;
35993611 }
55395539 }
55405540
55415541 /// getOptimalMemOpType - Returns the target specific optimal type for load
5542 /// and store operations as a result of memset, memcpy, and memmove lowering.
5543 /// If DstAlign is zero that means it's safe to destination alignment can
5544 /// satisfy any constraint. Similarly if SrcAlign is zero it means there
5545 /// isn't a need to check it against alignment requirement, probably because
5546 /// the source does not need to be loaded. It returns EVT::Other if
5547 /// SelectionDAG should be responsible for determining it.
5542 /// and store operations as a result of memset, memcpy, and memmove
5543 /// lowering. If DstAlign is zero that means it's safe to destination
5544 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
5545 /// means there isn't a need to check it against alignment requirement,
5546 /// probably because the source does not need to be loaded. If
5547 /// 'NonScalarIntSafe' is true, that means it's safe to return a
5548 /// non-scalar-integer type, e.g. empty string source, constant, or loaded
5549 /// from memory. It returns EVT::Other if SelectionDAG should be responsible
5550 /// for determining it.
55485551 EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
55495552 unsigned DstAlign, unsigned SrcAlign,
5550 bool SafeToUseFP,
5553 bool NonScalarIntSafe,
55515554 SelectionDAG &DAG) const {
55525555 if (this->PPCSubTarget.isPPC64()) {
55535556 return MVT::i64;
347347 virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
348348
349349 /// getOptimalMemOpType - Returns the target specific optimal type for load
350 /// and store operations as a result of memset, memcpy, and memmove lowering.
351 /// If DstAlign is zero that means it's safe to destination alignment can
352 /// satisfy any constraint. Similarly if SrcAlign is zero it means there
353 /// isn't a need to check it against alignment requirement, probably because
354 /// the source does not need to be loaded. It returns EVT::Other if
355 /// SelectionDAG should be responsible for determining it.
356 virtual EVT getOptimalMemOpType(uint64_t Size,
357 unsigned DstAlign, unsigned SrcAlign,
358 bool SafeToUseFP, SelectionDAG &DAG) const;
350 /// and store operations as a result of memset, memcpy, and memmove
351 /// lowering. If DstAlign is zero that means it's safe to destination
352 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
353 /// means there isn't a need to check it against alignment requirement,
354 /// probably because the source does not need to be loaded. If
355 /// 'NonScalarIntSafe' is true, that means it's safe to return a
356 /// non-scalar-integer type, e.g. empty string source, constant, or loaded
357 /// from memory. It returns EVT::Other if SelectionDAG should be responsible
358 /// for determining it.
359 virtual EVT
360 getOptimalMemOpType(uint64_t Size,
361 unsigned DstAlign, unsigned SrcAlign,
362 bool NonScalarIntSafe, SelectionDAG &DAG) const;
359363
360364 /// getFunctionAlignment - Return the Log2 alignment of this function.
361365 virtual unsigned getFunctionAlignment(const Function *F) const;
10701070 /// If DstAlign is zero that means it's safe to destination alignment can
10711071 /// satisfy any constraint. Similarly if SrcAlign is zero it means there
10721072 /// isn't a need to check it against alignment requirement, probably because
1073 /// the source does not need to be loaded. It returns EVT::Other if
1074 /// SelectionDAG should be responsible for determining it.
1073 /// the source does not need to be loaded. If 'NonScalarIntSafe' is true, that
1074 /// means it's safe to return a non-scalar-integer type, e.g. constant string
1075 /// source or loaded from memory. It returns EVT::Other if SelectionDAG should
1076 /// be responsible for determining it.
10751077 EVT
10761078 X86TargetLowering::getOptimalMemOpType(uint64_t Size,
10771079 unsigned DstAlign, unsigned SrcAlign,
1078 bool SafeToUseFP,
1080 bool NonScalarIntSafe,
10791081 SelectionDAG &DAG) const {
10801082 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
10811083 // linux. This is because the stack realignment code can't handle certain
10821084 // cases like PR2962. This should be removed when PR2962 is fixed.
10831085 const Function *F = DAG.getMachineFunction().getFunction();
1084 if (!F->hasFnAttr(Attribute::NoImplicitFloat)) {
1086 if (NonScalarIntSafe &&
1087 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
10851088 if (Size >= 16 &&
10861089 (Subtarget->isUnalignedMemAccessFast() ||
10871090 ((DstAlign == 0 || DstAlign >= 16) &&
10891092 Subtarget->getStackAlignment() >= 16) {
10901093 if (Subtarget->hasSSE2())
10911094 return MVT::v4i32;
1092 if (SafeToUseFP && Subtarget->hasSSE1())
1095 if (Subtarget->hasSSE1())
10931096 return MVT::v4f32;
1094 } else if (SafeToUseFP &&
1095 Size >= 8 &&
1097 } else if (Size >= 8 &&
10961098 !Subtarget->is64Bit() &&
10971099 Subtarget->getStackAlignment() >= 8 &&
10981100 Subtarget->hasSSE2())
416416 virtual unsigned getByValTypeAlignment(const Type *Ty) const;
417417
418418 /// getOptimalMemOpType - Returns the target specific optimal type for load
419 /// and store operations as a result of memset, memcpy, and memmove lowering.
420 /// If DstAlign is zero that means it's safe to destination alignment can
421 /// satisfy any constraint. Similarly if SrcAlign is zero it means there
422 /// isn't a need to check it against alignment requirement, probably because
423 /// the source does not need to be loaded. It returns EVT::Other if
424 /// SelectionDAG should be responsible for determining it.
425 virtual EVT getOptimalMemOpType(uint64_t Size,
426 unsigned DstAlign, unsigned SrcAlign,
427 bool SafeToUseFP, SelectionDAG &DAG) const;
419 /// and store operations as a result of memset, memcpy, and memmove
420 /// lowering. If DstAlign is zero that means it's safe to destination
421 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
422 /// means there isn't a need to check it against alignment requirement,
423 /// probably because the source does not need to be loaded. If
424 /// 'NonScalarIntSafe' is true, that means it's safe to return a
425 /// non-scalar-integer type, e.g. empty string source, constant, or loaded
426 /// from memory. It returns EVT::Other if SelectionDAG should be responsible
427 /// for determining it.
428 virtual EVT
429 getOptimalMemOpType(uint64_t Size,
430 unsigned DstAlign, unsigned SrcAlign,
431 bool NonScalarIntSafe, SelectionDAG &DAG) const;
428432
429433 /// allowsUnalignedMemoryAccesses - Returns true if the target allows
430434 /// unaligned memory accesses. of the specified type.
None ; RUN: llc < %s | FileCheck %s
1
2 target triple = "i386"
0 ; RUN: llc -mtriple=i386-apple-darwin < %s | FileCheck %s
31
42 declare void @llvm.memset.i32(i8*, i8, i32, i32) nounwind
53
64 define fastcc void @t1() nounwind {
75 entry:
86 ; CHECK: t1:
9 ; CHECK: call memset
7 ; CHECK: call _memset
108 call void @llvm.memset.i32( i8* null, i8 0, i32 188, i32 1 ) nounwind
119 unreachable
1210 }
1412 define fastcc void @t2(i8 signext %c) nounwind {
1513 entry:
1614 ; CHECK: t2:
17 ; CHECK: call memset
15 ; CHECK: call _memset
1816 call void @llvm.memset.i32( i8* undef, i8 %c, i32 76, i32 1 ) nounwind
1917 unreachable
2018 }
0 ; RUN: llc -mtriple=i386-apple-darwin < %s | not grep memset
1 ; PR6767
2
3 define void @t() nounwind ssp {
4 entry:
5 %buf = alloca [512 x i8], align 1
6 %ptr = getelementptr inbounds [512 x i8]* %buf, i32 0, i32 0
7 call void @llvm.memset.i32(i8* %ptr, i8 undef, i32 512, i32 1)
8 unreachable
9 }
10
11 declare void @llvm.memset.i32(i8* nocapture, i8, i32, i32) nounwind