Advertisement
Guest User

Untitled

a guest
May 24th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc,
  2.                    const Value *DefUO) {
  3.     StoreInst *Store = dyn_cast<StoreInst>(Def->getMemoryInst());
  4.     MemSetInst *MemSet = dyn_cast<MemSetInst>(Def->getMemoryInst());
  5.     Constant *StoredConstant = nullptr;
  6.     if (Store)
  7.       StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
  8.     if (MemSet)
  9.       StoredConstant = dyn_cast<Constant>(MemSet->getValue());
  10.  
  11.     if (StoredConstant && StoredConstant->isNullValue()) {
  12.       Instruction *DefUOInst = const_cast<Instruction*>(dyn_cast<Instruction>(DefUO));
  13.       if (DefUOInst) {
  14.           if (isCallocLikeFn(DefUOInst, &TLI)) {
  15.             auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
  16.             // If UnderlyingDef is the clobbering access of Def, no instructions
  17.             // between them can modify the memory location.
  18.             auto *ClobberDef =
  19.                 MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
  20.             return UnderlyingDef == ClobberDef;
  21.         }
  22.  
  23.         if (isMallocLikeFn(DefUOInst, &TLI) && MemSet &&
  24.             cast<CallBase>(DefUOInst)->getOperand(0) == MemSet->getLength()) {
  25.           auto *Malloc = cast<CallInst>(DefUOInst);
  26.           if (DT.dominates(DefUOInst, cast<Instruction>(MemSet)) &&
  27.                   memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT)) {
  28.               IRBuilder<> IRB(Malloc);
  29.               const DataLayout &DL = Malloc->getModule()->getDataLayout();
  30.               IntegerType *SizeType = DL.getIntPtrType(IRB.GetInsertBlock()->getContext());
  31.               if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
  32.                             Malloc->getArgOperand(0), Malloc->getAttributes(), IRB, TLI)) {
  33.                   Malloc->replaceAllUsesWith(Calloc);
  34.                   Malloc->eraseFromParent();
  35.                   return true;
  36.               }
  37.               return false;
  38.           }
  39.         }
  40.       }
  41.     }
  42.  
  43.     if (!Store)
  44.       return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement