Ejemplo n.º 1
0
bool LLPE::runOnModule(Module &M) {
  
	vector<int> argv = readInputFile();

	for (Module::iterator F = M.begin(), F_end = M.end(); F != F_end; ++F) {

		for (Function::arg_iterator A = F->arg_begin(), A_end = F->arg_end(); A != A_end; ++A) {
		
			//Search for variables referencing argv
			if (A->getName() == "argv") {
				
				//Iterate through uses of argv
				for (Value::use_iterator U = A->use_begin(), U_end = A->use_end(); U != U_end; ++U) {
					Instruction *User = dyn_cast<Instruction>(*U);
					
					StoreInst *SI = dyn_cast<StoreInst>(User);
					AllocaInst *OrigAlloca = dyn_cast<AllocaInst>(SI->getOperand(1));
					
					for (Value::use_iterator U2 = OrigAlloca->use_begin(), U2_end = OrigAlloca->use_end(); U2 != U2_end; ++U2) {
						Instruction *User2 = dyn_cast<Instruction>(*U2);

						for (Value::use_iterator U3 = User2->use_begin(), U3_end = OrigAlloca->use_end(); U3 != U3_end; ++U3) {
							searchForStoreInstruction(dyn_cast<Instruction>(*U3)->getParent(), argv);
						}
					}

				}
			}
			
		}

	}
   
	return true;
}
Ejemplo n.º 2
0
AllocaInst* Variables::changeLocal(Value* value, ArrayType* newType) {

  AllocaInst* oldTarget = dyn_cast<AllocaInst>(value);
  PointerType* oldPointerType = dyn_cast<PointerType>(oldTarget->getType());
  ArrayType* oldType = dyn_cast<ArrayType>(oldPointerType->getElementType());
  AllocaInst* newTarget = NULL;

  errs() << "Changing the precision of variable \"" << oldTarget->getName() << "\" from " << *oldType 
	 << " to " << *newType << ".\n";

  if (newType->getElementType()->getTypeID() != oldType->getElementType()->getTypeID()) {

    newTarget = new AllocaInst(newType, getInt32(1), "", oldTarget);
    
    // we are not calling getAlignment because in this case double requires 16. Investigate further.
    unsigned alignment;
    switch(newType->getElementType()->getTypeID()) {
    case Type::FloatTyID: 
      alignment = 4;
      break;
    case Type::DoubleTyID:
      alignment = 16;
      break;
    case Type::X86_FP80TyID:
      alignment = 16;
      break;
    default:
      alignment = 0;
    } 
    
    newTarget->setAlignment(alignment); // depends on type? 8 for float? 16 for double?
    newTarget->takeName(oldTarget);
    
    // iterating through instructions using old AllocaInst
    vector<Instruction*> erase;
    Value::use_iterator it = oldTarget->use_begin();
    for(; it != oldTarget->use_end(); it++) {
      bool is_erased = Transformer::transform(it, newTarget, oldTarget, newType, oldType, alignment);

      if (!is_erased)
        erase.push_back(dyn_cast<Instruction>(*it));
    }	  
    
    // erasing uses of old instructions
    for(unsigned int i = 0; i < erase.size(); i++) {
      erase[i]->eraseFromParent();
    }
    // erase old instruction
    //oldTarget->eraseFromParent();
  }
  else {
    errs() << "\tNo changes required.\n";    
  }

  return newTarget;
}
Ejemplo n.º 3
0
/// performCallSlotOptzn - takes a memcpy and a call that it depends on,
/// and checks for the possibility of a call slot optimization by having
/// the call write its result directly into the destination of the memcpy.
bool MemCpyOpt::performCallSlotOptzn(Instruction *cpy,
                                     Value *cpyDest, Value *cpySrc,
                                     uint64_t cpyLen, unsigned cpyAlign,
                                     CallInst *C) {
    // The general transformation to keep in mind is
    //
    //   call @func(..., src, ...)
    //   memcpy(dest, src, ...)
    //
    // ->
    //
    //   memcpy(dest, src, ...)
    //   call @func(..., dest, ...)
    //
    // Since moving the memcpy is technically awkward, we additionally check that
    // src only holds uninitialized values at the moment of the call, meaning that
    // the memcpy can be discarded rather than moved.

    // Deliberately get the source and destination with bitcasts stripped away,
    // because we'll need to do type comparisons based on the underlying type.
    CallSite CS(C);

    // Require that src be an alloca.  This simplifies the reasoning considerably.
    AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc);
    if (!srcAlloca)
        return false;

    // Check that all of src is copied to dest.
    if (TD == 0) return false;

    ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
    if (!srcArraySize)
        return false;

    uint64_t srcSize = TD->getTypeAllocSize(srcAlloca->getAllocatedType()) *
                       srcArraySize->getZExtValue();

    if (cpyLen < srcSize)
        return false;

    // Check that dest points to memory that is at least as aligned as src.
    unsigned srcAlign = srcAlloca->getAlignment();
    if (!srcAlign)
        srcAlign = TD->getABITypeAlignment(srcAlloca->getAllocatedType());
    bool isDestSufficientlyAligned = srcAlign <= cpyAlign;
    // If dest is not aligned enough and we can't increase its alignment then
    // bail out.
    if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest))
        return false;

    // Check that accessing the first srcSize bytes of dest will not cause a
    // trap.  Otherwise the transform is invalid since it might cause a trap
    // to occur earlier than it otherwise would.
    if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) {
        // The destination is an alloca.  Check it is larger than srcSize.
        ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize());
        if (!destArraySize)
            return false;

        uint64_t destSize = TD->getTypeAllocSize(A->getAllocatedType()) *
                            destArraySize->getZExtValue();

        if (destSize < srcSize)
            return false;
    } else if (Argument *A = dyn_cast<Argument>(cpyDest)) {
        // If the destination is an sret parameter then only accesses that are
        // outside of the returned struct type can trap.
        if (!A->hasStructRetAttr())
            return false;

        Type *StructTy = cast<PointerType>(A->getType())->getElementType();
        uint64_t destSize = TD->getTypeAllocSize(StructTy);

        if (destSize < srcSize)
            return false;
    } else {
        return false;
    }

    // Check that src is not accessed except via the call and the memcpy.  This
    // guarantees that it holds only undefined values when passed in (so the final
    // memcpy can be dropped), that it is not read or written between the call and
    // the memcpy, and that writing beyond the end of it is undefined.
    SmallVector<User*, 8> srcUseList(srcAlloca->use_begin(),
                                     srcAlloca->use_end());
    while (!srcUseList.empty()) {
        User *UI = srcUseList.pop_back_val();

        if (isa<BitCastInst>(UI)) {
            for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
                    I != E; ++I)
                srcUseList.push_back(*I);
        } else if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(UI)) {
            if (G->hasAllZeroIndices())
                for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
                        I != E; ++I)
                    srcUseList.push_back(*I);
            else
                return false;
        } else if (UI != C && UI != cpy) {
            return false;
        }
    }

    // Since we're changing the parameter to the callsite, we need to make sure
    // that what would be the new parameter dominates the callsite.
    DominatorTree &DT = getAnalysis<DominatorTree>();
    if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest))
        if (!DT.dominates(cpyDestInst, C))
            return false;

    // In addition to knowing that the call does not access src in some
    // unexpected manner, for example via a global, which we deduce from
    // the use analysis, we also need to know that it does not sneakily
    // access dest.  We rely on AA to figure this out for us.
    AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
    AliasAnalysis::ModRefResult MR = AA.getModRefInfo(C, cpyDest, srcSize);
    // If necessary, perform additional analysis.
    if (MR != AliasAnalysis::NoModRef)
        MR = AA.callCapturesBefore(C, cpyDest, srcSize, &DT);
    if (MR != AliasAnalysis::NoModRef)
        return false;

    // All the checks have passed, so do the transformation.
    bool changedArgument = false;
    for (unsigned i = 0; i < CS.arg_size(); ++i)
        if (CS.getArgument(i)->stripPointerCasts() == cpySrc) {
            Value *Dest = cpySrc->getType() == cpyDest->getType() ?  cpyDest
                          : CastInst::CreatePointerCast(cpyDest, cpySrc->getType(),
                                  cpyDest->getName(), C);
            changedArgument = true;
            if (CS.getArgument(i)->getType() == Dest->getType())
                CS.setArgument(i, Dest);
            else
                CS.setArgument(i, CastInst::CreatePointerCast(Dest,
                               CS.getArgument(i)->getType(), Dest->getName(), C));
        }

    if (!changedArgument)
        return false;

    // If the destination wasn't sufficiently aligned then increase its alignment.
    if (!isDestSufficientlyAligned) {
        assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!");
        cast<AllocaInst>(cpyDest)->setAlignment(srcAlign);
    }

    // Drop any cached information about the call, because we may have changed
    // its dependence information by changing its parameter.
    MD->removeInstruction(C);

    // Remove the memcpy.
    MD->removeInstruction(cpy);
    ++NumMemCpyInstr;

    return true;
}
Ejemplo n.º 4
0
bool SpDefUseInstrumenter::runOnModule(Module &M) {

  cerr << "instrument: --- Def-Use pair Spectrum ---\n";

  Function *Main = M.getFunction("main");
  LLVMContext &C = M.getContext();
  
  if (Main == 0) {
    cerr << "WARNING: cannot insert def-use instrumentation into a module"
         << " with no main function!\n";
    return false;  // No main, no instrumentation!
  }

  // Add library function prototype
  Constant *SpFn = M.getOrInsertFunction("_updateSpectrum", 
                          Type::getVoidTy(C), 
                          Type::getInt32Ty(C),  // spectrum index
                          Type::getInt32Ty(C),  // component index
                          NULL);


  unsigned spectrumIndex = IndexManager::getSpectrumIndex();
  unsigned nDefs = 0;
  unsigned nUses = 0;

  // Loop through all functions within module
  for (Module::iterator F = M.begin(), ME = M.end(); F != ME; ++F) {

    // skip function declarations
    if(F->isDeclaration()) 
      continue;

    // skip the _registerAll function
    if(F->getName()=="_registerAll")
      continue;
    
    // Loop through all basic blocks within function
    for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
      //skip dead blocks
      //is this really safe??
      BasicBlock *bb = B;
      if (B!=F->begin() && (pred_begin(bb)==pred_end(bb))) continue; //skip dead blocks

      // Loop through all instructions within basic block
      for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE; I++) {
    
        if(isa<DbgDeclareInst>(*I)) {

          // extract source file information from debug intrinsic
          DbgDeclareInst &DDI = cast<DbgDeclareInst>(*I);
          std::string file, dir;
          std::string name;
          GlobalVariable *gv = cast<GlobalVariable>(DDI.getVariable());
          if(!gv->hasInitializer()) continue;
          ConstantStruct *cs = cast<ConstantStruct>(gv->getInitializer());
          llvm::GetConstantStringInfo(cs->getOperand(2), name);
          unsigned int line = unsigned(cast<ConstantInt>(cs->getOperand(4))->getZExtValue());
          Value *V = cast<Value>(cs->getOperand(3));
          GlobalVariable *gv2 = cast<GlobalVariable>(cast<ConstantExpr>(V)->getOperand(0));
          if(!gv2->hasInitializer()) continue;
          ConstantStruct *cs2 = cast<ConstantStruct>(gv2->getInitializer());
          llvm::GetConstantStringInfo(cs2->getOperand(3), file);
          llvm::GetConstantStringInfo(cs2->getOperand(4), dir);

          // get the allocation instruction of the variable definition
          AllocaInst *AI;
          if(isa<AllocaInst>(DDI.getAddress())) {
            AI = cast<AllocaInst>(DDI.getAddress());
          } else if (isa<BitCastInst>(DDI.getAddress())) {
            AI = cast<AllocaInst>(cast<BitCastInst>(DDI.getAddress())->getOperand(0));
          } else {
            continue;
          }

          nDefs++;

          // add calls to lib function for each use of the variable
          int currUses = 0;
          for(AllocaInst::use_iterator U = AI->use_begin(), UE = AI->use_end(); U != UE; ++U) {
            if(isa<Instruction>(*U)) {

              User *user = *U;
              Instruction *insertInst = (Instruction*)user;

              // find most likely context location of use
              int useline = line;
              std::string usefile = file, usedir = dir;
              BasicBlock *parent = insertInst->getParent();
              BasicBlock::iterator inst = parent->begin();
              while(((Instruction *)inst) != insertInst  &&  inst != parent->end()) {
            	  /*TODO: solve DbgStopPointInst problem*/
            	/*if(isa<DbgStopPointInst>(*inst)) {
                  DbgStopPointInst &DSPI = cast<DbgStopPointInst>(*inst);
                  llvm::GetConstantStringInfo(DSPI.getDirectory(), usedir);
                  llvm::GetConstantStringInfo(DSPI.getFileName(), usefile);
                  useline = DSPI.getLine();
                }*/
                inst++;
              }

              std::stringstream usename;
              usename << name << "(use_" << currUses << ")";
              // add source context of this invariant to context file
              ContextManager::addSpectrumContext(
                spectrumIndex,  // spectrumIndex
                nUses,          // componentIndex
                usedir,         // path
                usefile,        // file
                useline,        // line
                usename.str()); // name
              currUses++;

              std::vector<Value*> Args(2);
              Args[0] = ConstantInt::get(Type::getInt32Ty(C), spectrumIndex);
              Args[1] = ConstantInt::get(Type::getInt32Ty(C), nUses++);

              CallInst::Create(SpFn, Args.begin(), Args.end(), "", insertInst);
            }
          }
        }
      }
    }
  }

  // add the registration of the instrumented spectrum points in the _registerAll() function
  addSpectrumRegistration(M, spectrumIndex, nUses, "Def-Use_Pairs");
  
  std::cerr << "instrument: " << nDefs << " defines with a total number of " << nUses << " uses instrumented\n";

  // notify change of program 
  return true;
}