llvm.org GIT mirror llvm / ff1e98c
Fix PR4882, by making MemCpyOpt not dereference removed stores to get the context for the newly created operations. Patch by Jakub Staszak! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81175 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
2 changed file(s) with 56 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
339339 bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
340340 if (SI->isVolatile()) return false;
341341
342 LLVMContext &Context = SI->getContext();
343
342344 // There are two cases that are interesting for this code to handle: memcpy
343345 // and memset. Right now we only handle memset.
344346
345347 // Ensure that the value being stored is something that can be memset'able a
346348 // byte at a time like "0" or "-1" or any width, as well as things like
347349 // 0xA0A0A0A0 and 0.0.
348 Value *ByteVal = isBytewiseValue(SI->getOperand(0), SI->getContext());
350 Value *ByteVal = isBytewiseValue(SI->getOperand(0), Context);
349351 if (!ByteVal)
350352 return false;
351353
386388 if (NextStore->isVolatile()) break;
387389
388390 // Check to see if this stored value is of the same byte-splattable value.
389 if (ByteVal != isBytewiseValue(NextStore->getOperand(0),
390 NextStore->getContext()))
391 if (ByteVal != isBytewiseValue(NextStore->getOperand(0), Context))
391392 break;
392393
393394 // Check to see if this store is to a constant offset from the start ptr.
407408 // store as well. We try to avoid this unless there is at least something
408409 // interesting as a small compile-time optimization.
409410 Ranges.addStore(0, SI);
410
411411
412412 Function *MemSetF = 0;
413413
431431 BasicBlock::iterator InsertPt = BI;
432432
433433 if (MemSetF == 0) {
434 const Type *Ty = Type::getInt64Ty(SI->getContext());
434 const Type *Ty = Type::getInt64Ty(Context);
435435 MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, &Ty, 1);
436 }
436 }
437437
438438 // Get the starting pointer of the block.
439439 StartPtr = Range.StartPtr;
440440
441441 // Cast the start ptr to be i8* as memset requires.
442 const Type *i8Ptr =
443 PointerType::getUnqual(Type::getInt8Ty(SI->getContext()));
442 const Type *i8Ptr = PointerType::getUnqual(Type::getInt8Ty(Context));
444443 if (StartPtr->getType() != i8Ptr)
445444 StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getName(),
446445 InsertPt);
448447 Value *Ops[] = {
449448 StartPtr, ByteVal, // Start, value
450449 // size
451 ConstantInt::get(Type::getInt64Ty(SI->getContext()),
452 Range.End-Range.Start),
450 ConstantInt::get(Type::getInt64Ty(Context), Range.End-Range.Start),
453451 // align
454 ConstantInt::get(Type::getInt32Ty(SI->getContext()), Range.Alignment)
452 ConstantInt::get(Type::getInt32Ty(Context), Range.Alignment)
455453 };
456454 Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
457455 DEBUG(errs() << "Replace stores:\n";
463461 BBI = BI;
464462
465463 // Zap all the stores.
466 for (SmallVector::const_iterator SI = Range.TheStores.begin(),
464 for (SmallVector::const_iterator
465 SI = Range.TheStores.begin(),
467466 SE = Range.TheStores.end(); SI != SE; ++SI)
468467 (*SI)->eraseFromParent();
469468 ++NumMemSetInfer;
0 ; RUN: opt %s -memcpyopt -disable-output
1 ; PR4882
2
3 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64"
4 target triple = "armv7-eabi"
5
6 %struct.qw = type { [4 x float] }
7 %struct.bar = type { %struct.qw, %struct.qw, %struct.qw, %struct.qw, %struct.qw, float, float}
8
9 define arm_aapcs_vfpcc void @test1(%struct.bar* %this) {
10 entry:
11 %0 = getelementptr inbounds %struct.bar* %this, i32 0, i32 0, i32 0, i32 0
12 store float 0.000000e+00, float* %0, align 4
13 %1 = getelementptr inbounds %struct.bar* %this, i32 0, i32 0, i32 0, i32 1
14 store float 0.000000e+00, float* %1, align 4
15 %2 = getelementptr inbounds %struct.bar* %this, i32 0, i32 0, i32 0, i32 2
16 store float 0.000000e+00, float* %2, align 4
17 %3 = getelementptr inbounds %struct.bar* %this, i32 0, i32 0, i32 0, i32 3
18 store float 0.000000e+00, float* %3, align 4
19 %4 = getelementptr inbounds %struct.bar* %this, i32 0, i32 1, i32 0, i32 0
20 store float 0.000000e+00, float* %4, align 4
21 %5 = getelementptr inbounds %struct.bar* %this, i32 0, i32 1, i32 0, i32 1
22 store float 0.000000e+00, float* %5, align 4
23 %6 = getelementptr inbounds %struct.bar* %this, i32 0, i32 1, i32 0, i32 2
24 store float 0.000000e+00, float* %6, align 4
25 %7 = getelementptr inbounds %struct.bar* %this, i32 0, i32 1, i32 0, i32 3
26 store float 0.000000e+00, float* %7, align 4
27 %8 = getelementptr inbounds %struct.bar* %this, i32 0, i32 3, i32 0, i32 1
28 store float 0.000000e+00, float* %8, align 4
29 %9 = getelementptr inbounds %struct.bar* %this, i32 0, i32 3, i32 0, i32 2
30 store float 0.000000e+00, float* %9, align 4
31 %10 = getelementptr inbounds %struct.bar* %this, i32 0, i32 3, i32 0, i32 3
32 store float 0.000000e+00, float* %10, align 4
33 %11 = getelementptr inbounds %struct.bar* %this, i32 0, i32 4, i32 0, i32 0
34 store float 0.000000e+00, float* %11, align 4
35 %12 = getelementptr inbounds %struct.bar* %this, i32 0, i32 4, i32 0, i32 1
36 store float 0.000000e+00, float* %12, align 4
37 %13 = getelementptr inbounds %struct.bar* %this, i32 0, i32 4, i32 0, i32 2
38 store float 0.000000e+00, float* %13, align 4
39 %14 = getelementptr inbounds %struct.bar* %this, i32 0, i32 4, i32 0, i32 3
40 store float 0.000000e+00, float* %14, align 4
41 %15 = getelementptr inbounds %struct.bar* %this, i32 0, i32 5
42 store float 0.000000e+00, float* %15, align 4
43 unreachable
44 }