Skip to content

Commit 4414586

Browse files
committed
Merge 81175 from mainline.
Fix PR4882, by making MemCpyOpt not dereference removed stores to get the context for the newly created operations. llvm-svn: 81693
1 parent 54bacab commit 4414586

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,15 @@ static RegisterPass<MemCpyOpt> X("memcpyopt",
338338
bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
339339
if (SI->isVolatile()) return false;
340340

341+
LLVMContext &Context = SI->getContext();
342+
341343
// There are two cases that are interesting for this code to handle: memcpy
342344
// and memset. Right now we only handle memset.
343345

344346
// Ensure that the value being stored is something that can be memset'able a
345347
// byte at a time like "0" or "-1" or any width, as well as things like
346348
// 0xA0A0A0A0 and 0.0.
347-
Value *ByteVal = isBytewiseValue(SI->getOperand(0), SI->getContext());
349+
Value *ByteVal = isBytewiseValue(SI->getOperand(0), Context);
348350
if (!ByteVal)
349351
return false;
350352

@@ -385,8 +387,7 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
385387
if (NextStore->isVolatile()) break;
386388

387389
// Check to see if this stored value is of the same byte-splattable value.
388-
if (ByteVal != isBytewiseValue(NextStore->getOperand(0),
389-
NextStore->getContext()))
390+
if (ByteVal != isBytewiseValue(NextStore->getOperand(0), Context))
390391
break;
391392

392393
// Check to see if this store is to a constant offset from the start ptr.
@@ -406,7 +407,6 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
406407
// store as well. We try to avoid this unless there is at least something
407408
// interesting as a small compile-time optimization.
408409
Ranges.addStore(0, SI);
409-
410410

411411
Function *MemSetF = 0;
412412

@@ -430,28 +430,25 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
430430
BasicBlock::iterator InsertPt = BI;
431431

432432
if (MemSetF == 0) {
433-
const Type *Tys[] = {Type::getInt64Ty(SI->getContext())};
434-
MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset,
435-
Tys, 1);
436-
}
433+
const Type *Ty = Type::getInt64Ty(Context);
434+
MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, &Ty, 1);
435+
}
437436

438437
// Get the starting pointer of the block.
439438
StartPtr = Range.StartPtr;
440439

441440
// Cast the start ptr to be i8* as memset requires.
442-
const Type *i8Ptr =
443-
PointerType::getUnqual(Type::getInt8Ty(SI->getContext()));
441+
const Type *i8Ptr = PointerType::getUnqual(Type::getInt8Ty(Context));
444442
if (StartPtr->getType() != i8Ptr)
445443
StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getName(),
446444
InsertPt);
447445

448446
Value *Ops[] = {
449447
StartPtr, ByteVal, // Start, value
450448
// size
451-
ConstantInt::get(Type::getInt64Ty(SI->getContext()),
452-
Range.End-Range.Start),
449+
ConstantInt::get(Type::getInt64Ty(Context), Range.End-Range.Start),
453450
// align
454-
ConstantInt::get(Type::getInt32Ty(SI->getContext()), Range.Alignment)
451+
ConstantInt::get(Type::getInt32Ty(Context), Range.Alignment)
455452
};
456453
Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
457454
DEBUG(cerr << "Replace stores:\n";
@@ -463,7 +460,8 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
463460
BBI = BI;
464461

465462
// Zap all the stores.
466-
for (SmallVector<StoreInst*, 16>::const_iterator SI = Range.TheStores.begin(),
463+
for (SmallVector<StoreInst*, 16>::const_iterator
464+
SI = Range.TheStores.begin(),
467465
SE = Range.TheStores.end(); SI != SE; ++SI)
468466
(*SI)->eraseFromParent();
469467
++NumMemSetInfer;

0 commit comments

Comments
 (0)