Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc,
- const Value *DefUO) {
- StoreInst *Store = dyn_cast<StoreInst>(Def->getMemoryInst());
- MemSetInst *MemSet = dyn_cast<MemSetInst>(Def->getMemoryInst());
- Constant *StoredConstant = nullptr;
- if (Store)
- StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
- if (MemSet)
- StoredConstant = dyn_cast<Constant>(MemSet->getValue());
- if (StoredConstant && StoredConstant->isNullValue()) {
- Instruction *DefUOInst = const_cast<Instruction*>(dyn_cast<Instruction>(DefUO));
- if (DefUOInst) {
- if (isCallocLikeFn(DefUOInst, &TLI)) {
- auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
- // If UnderlyingDef is the clobbering access of Def, no instructions
- // between them can modify the memory location.
- auto *ClobberDef =
- MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
- return UnderlyingDef == ClobberDef;
- }
- if (isMallocLikeFn(DefUOInst, &TLI) && MemSet &&
- cast<CallBase>(DefUOInst)->getOperand(0) == MemSet->getLength()) {
- auto *Malloc = cast<CallInst>(DefUOInst);
- if (DT.dominates(DefUOInst, cast<Instruction>(MemSet)) &&
- memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT)) {
- IRBuilder<> IRB(Malloc);
- const DataLayout &DL = Malloc->getModule()->getDataLayout();
- IntegerType *SizeType = DL.getIntPtrType(IRB.GetInsertBlock()->getContext());
- if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
- Malloc->getArgOperand(0), Malloc->getAttributes(), IRB, TLI)) {
- Malloc->replaceAllUsesWith(Calloc);
- Malloc->eraseFromParent();
- return true;
- }
- return false;
- }
- }
- }
- }
- if (!Store)
- return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement