Guest User

Untitled

a guest
May 7th, 2021
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc, Value *DefUO) {
  2.     StoreInst *Store = dyn_cast<StoreInst>(Def->getMemoryInst());
  3.     MemSetInst *MemSet = dyn_cast<MemSetInst>(Def->getMemoryInst());
  4.     Constant *StoredConstant = nullptr;
  5.     if (Store)
  6.       StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
  7.     if (MemSet)
  8.       StoredConstant = dyn_cast<Constant>(MemSet->getValue());
  9.  
  10.     if (StoredConstant && StoredConstant->isNullValue()) {
  11.       auto *DefUOInst = dyn_cast<Instruction>(DefUO);
  12.       DefUOInst->dump();
  13.       if (DefUOInst) {
  14.         if (isCallocLikeFn(DefUOInst, &TLI)) {
  15.           auto *UnderlyingDef =
  16.               cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
  17.           // If UnderlyingDef is the clobbering access of Def, no instructions
  18.           // between them can modify the memory location.
  19.           auto *ClobberDef =
  20.               MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
  21.           return UnderlyingDef == ClobberDef;
  22.         }
  23.  
  24.         if (isMallocLikeFn(DefUOInst, &TLI) && MemSet &&
  25.             cast<CallBase>(DefUOInst)->getOperand(0) == MemSet->getLength()) {
  26.           auto *Malloc = cast<CallBase>(DefUOInst);
  27.           IRBuilder<> IRB(Malloc);
  28.           auto *UnderlyingDef =
  29.               cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
  30.           // If UnderlyingDef is the clobbering access of Def, no instructions
  31.           // between them can modify the memory location.
  32.           auto *ClobberDef =
  33.               MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
  34.           if (UnderlyingDef == ClobberDef) {
  35.             // malloc + memset => calloc
  36.             if (Value *Calloc = emitCalloc(
  37.                     ConstantInt::get(DL.getIntPtrType(IRB.getContext()), 1),
  38.                     Malloc->getOperand(0), IRB, TLI)) {
  39.               Malloc->replaceAllUsesWith(Calloc);
  40.               // Remove memset.
  41.               return true;
  42.             }
  43.           }
  44.         }
  45.       }
  46.     }
  47.  
  48.     if (!Store)
  49.       return false;
RAW Paste Data