Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc, 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()) {
- auto *DefUOInst = dyn_cast<Instruction>(DefUO);
- DefUOInst->dump();
- 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<CallBase>(DefUOInst);
- IRBuilder<> IRB(Malloc);
- 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);
- if (UnderlyingDef == ClobberDef) {
- // malloc + memset => calloc
- if (Value *Calloc = emitCalloc(
- ConstantInt::get(DL.getIntPtrType(IRB.getContext()), 1),
- Malloc->getOperand(0), IRB, TLI)) {
- Malloc->replaceAllUsesWith(Calloc);
- // Remove memset.
- return true;
- }
- }
- }
- }
- }
- if (!Store)
- return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement