LLVMColumnMapProjectionBuilder::LLVMColumnMapProjectionBuilder(const ColumnMapContext& context, llvm::Module& module,
        llvm::TargetMachine* target, const std::string& name)
        : FunctionBuilder(module, target, buildReturnTy(module.getContext()), buildParamTy(module.getContext()), name),
          mContext(context),
          mMainPageStructTy(getColumnMapMainPageTy(module.getContext())),
          mHeapEntryStructTy(getColumnMapHeapEntriesTy(module.getContext())) {
    // Set noalias hints (data pointers are not allowed to overlap)
    mFunction->setDoesNotAlias(1);
    mFunction->setOnlyReadsMemory(1);
    mFunction->setDoesNotAlias(3);
}
Ejemplo n.º 2
0
bool TimeProfiler::runOnModule(llvm::Module &M)
{
  Function *Main = M.getFunction("main");
  if (Main == 0) {
    errs() << "WARNING: cannot insert edge profiling into a module"
           << " with no main function!\n";
    return false;  // No main, no instrumentation!
  }

  std::vector<CallInst*> Traped;
  Function* wtime = NULL;
  for(auto F = M.begin(), E = M.end(); F!=E; ++F){
     if((*F).getName() == "mpi_wtime_")
        wtime = &*F;
     for(auto I = inst_begin(*F), IE = inst_end(*F); I!=IE; ++I){
        CallInst* CI = dyn_cast<CallInst>(&*I);
        if(CI == NULL) continue;
        Value* CV = const_cast<CallInst*>(CI)->getCalledValue();
        Function* func = dyn_cast<Function>(castoff(CV));
        if(func == NULL)
          errs()<<"No func!\n";
        StringRef str = func->getName();
        if(str.startswith("mpi_"))
        {
           if(str.startswith("mpi_init_")||str.startswith("mpi_comm_rank_")||str.startswith("mpi_comm_size_"))
              continue;
           Traped.push_back(CI);
        }
     }
  }

  IRBuilder<> Builder(M.getContext());

  Type* DoubleTy = Type::getDoubleTy(M.getContext());

  Type*ATy = ArrayType::get(DoubleTy, Traped.size());
  GlobalVariable* Counters = new GlobalVariable(M, ATy, false,
        GlobalVariable::InternalLinkage, Constant::getNullValue(ATy),
        "TimeCounters");

  unsigned I=0;
  for(auto P : Traped){
     ArrayRef<Value*> args;
     CallInst* callTime = CallInst::Create(wtime, args, "", P);

     IncrementTimeCounter(callTime, wtime, I++, Counters, Builder, P);
  }

  InsertPredProfilingInitCall(Main, "llvm_start_time_profiling", Counters);
  return true;
}
Ejemplo n.º 3
0
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
                         const CodeGenOptions &CGO,
                         const LangOptions &Features, MangleContext &MContext)
  : Context(Ctx), Module(M), CodeGenOpts(CGO),
    Features(Features), MContext(MContext), MDHelper(M.getContext()),
    Root(nullptr), Char(nullptr)
{}
Ejemplo n.º 4
0
void InstructionReplace::cloneFunctions(llvm::Module& M)
{
	for(llvm::Module::iterator F = M.begin(), ME = M.end(); F != ME; ++F) {
		//if(!F->getFnAttributes().hasAttribute(Attributes::AttrVal::MaskedCopy)) { continue; }
		cerr << "Masking " << F->getName().str();
		assert(F->arg_size() == 1);
		Type* rettype = F->getReturnType();
		auto args = F->arg_begin();
		Argument* a1 = args++;
		Type* paramtype = a1->getType();
		assert(isa<IntegerType>(rettype));
		assert(isa<IntegerType>(paramtype));
		stringstream ss("");
		ss << "__masked__" << F->getName().str();
		llvm::LLVMContext& Ctx = M.getContext();
		llvm::Constant* FunSym;
		std::vector<Type*> paramtypes;
		for(unsigned int i = 0; i <= MaskingOrder; i++) { paramtypes.push_back(paramtype); } //TODO riducibile?
		for(unsigned int i = 0; i <= MaskingOrder; i++) { paramtypes.push_back(rettype->getPointerTo()); }
		llvm::FunctionType* ftype = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), llvm::ArrayRef<Type*>(paramtypes), false);
		FunSym = M.getOrInsertFunction(ss.str(), ftype);
		llvm::Function* newF = llvm::cast<llvm::Function>(FunSym);
		maskedfn[F] = newF;
		SmallVector<llvm::ReturnInst*, 4> rets;
		ValueToValueMapTy vmap;
		llvm::BasicBlock* Entry = llvm::BasicBlock::Create(Ctx, "entry", newF);
		llvm::IRBuilder<> ib_entry = llvm::IRBuilder<>(Entry->getContext());
		ib_entry.SetInsertPoint(Entry);
		NoCryptoFA::InstructionMetadata* md = new NoCryptoFA::InstructionMetadata();
		md->hasBeenMasked = true;
		auto arg = newF->arg_begin();
		for(unsigned int i = 0; i <= MaskingOrder; i++) { md->MaskedValues.push_back(arg++); }
		Value* fakevalue = ib_entry.CreateAdd(md->MaskedValues[0], md->MaskedValues[1]);
		md->my_instruction = cast<Instruction>(fakevalue);
		NoCryptoFA::known[ md->my_instruction] = md;
		deletionqueue.insert(md->my_instruction);
		vmap.insert(std::make_pair(a1, fakevalue));
		CloneFunctionInto(newF, F, vmap, true, rets);
		ib_entry.CreateBr(cast<BasicBlock>(vmap[&F->getEntryBlock()]));
		/*
		AttrBuilder toremove;
		//toremove.addAttribute(Attributes::AttrVal::MaskedCopy);
		toremove.addAttribute(Attributes::AttrVal::ZExt);
		toremove.addAttribute(Attributes::AttrVal::SExt);
		toremove.addAttribute(Attributes::AttrVal::NoAlias);
		toremove.addAttribute(Attributes::AttrVal::NoCapture);
		toremove.addAttribute(Attributes::AttrVal::StructRet);
		toremove.addAttribute(Attributes::AttrVal::ByVal);
		toremove.addAttribute(Attributes::AttrVal::Nest);
		newF->removeFnAttr(Attributes::get(Ctx, toremove));
		newF->removeAttribute(0, Attributes::get(Ctx, toremove)); //Thr..ehm,Zero is a magic number! Toglie gli attributi zeroext e simili dal valore di ritorno.
		*/
		TaggedData& td = getAnalysis<TaggedData>(*F);
		//td.markFunction(newF);
		//setFullyMasked(newF);
	}
}
Ejemplo n.º 5
0
bool DeclareAssumePass::runOnModule(llvm::Module &M){
  bool modified_M = false;
  CheckModule::check_assume(&M);
  llvm::Function *F_assume = M.getFunction("__VERIFIER_assume");
  if(!F_assume){
    llvm::FunctionType *assumeTy;
    {
      llvm::Type *voidTy = llvm::Type::getVoidTy(M.getContext());
      llvm::Type *i1Ty = llvm::Type::getInt1Ty(M.getContext());
      assumeTy = llvm::FunctionType::get(voidTy,{i1Ty},false);
    }
    llvm::AttributeSet assumeAttrs =
      llvm::AttributeSet::get(M.getContext(),llvm::AttributeSet::FunctionIndex,
                              std::vector<llvm::Attribute::AttrKind>({llvm::Attribute::NoUnwind}));
    F_assume = llvm::dyn_cast<llvm::Function>(M.getOrInsertFunction("__VERIFIER_assume",assumeTy,assumeAttrs));
    assert(F_assume);
    modified_M = true;
  }
  return modified_M;
}
Ejemplo n.º 6
0
 Compiler(llvm::Module& module_) :
   module(module_),
   builder(module_.getContext()),
   endLoopBlock(nullptr),
   result(nullptr)
 {}
Ejemplo n.º 7
0
CodeGenASTVisitor::CodeGenASTVisitor(llvm::Module& targetModule)
    : Context(targetModule.getContext())
    , TargetModule(targetModule)
    , Builder(TargetModule.getContext())
{ }
/// ValueEnumerator - Enumerate module-level information.
ValueEnumerator::ValueEnumerator(const llvm::Module &M,
                                 bool ShouldPreserveUseListOrder)
    : HasMDString(false), HasDILocation(false),
    ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
  assert(!ShouldPreserveUseListOrder &&
         "not supported UseListOrders = predictUseListOrder(M)");

  // Enumerate the global variables.
  for (llvm::Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    EnumerateValue(I);

  // Enumerate the functions.
  for (llvm::Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
    EnumerateValue(I);
    EnumerateAttributes(cast<Function>(I)->getAttributes());
  }

  // Enumerate the aliases.
  for (llvm::Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I);

  // Remember what is the cutoff between globalvalue's and other constants.
  unsigned FirstConstant = Values.size();

  // Enumerate the global variable initializers.
  for (llvm::Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    if (I->hasInitializer())
      EnumerateValue(I->getInitializer());

  // Enumerate the aliasees.
  for (llvm::Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I->getAliasee());

  // Enumerate the metadata type.
  //
  // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
  // only encodes the metadata type when it's used as a value.
  EnumerateType(Type::getMetadataTy(M.getContext()));

  // Insert constants and metadata that are named at module level into the slot
  // pool so that the module symbol table can refer to them...
  EnumerateValueSymbolTable(M.getValueSymbolTable());
  EnumerateNamedMetadata(M);

  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;

  // Enumerate types used by function bodies and argument lists.
  for (const Function &F : M) {
    for (const Argument &A : F.args())
      EnumerateType(A.getType());

    for (const BasicBlock &BB : F)
      for (const Instruction &I : BB) {
        for (const Use &Op : I.operands()) {
          auto *MD = dyn_cast<MetadataAsValue>(&Op);
          if (!MD) {
            EnumerateOperandType(Op);
            continue;
          }

          // Local metadata is enumerated during function-incorporation.
          if (isa<LocalAsMetadata>(MD->getMetadata()))
            continue;

          EnumerateMetadata(MD->getMetadata());
        }
        EnumerateType(I.getType());
        if (const CallInst *CI = dyn_cast<CallInst>(&I))
          EnumerateAttributes(CI->getAttributes());
        else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
          EnumerateAttributes(II->getAttributes());

        // Enumerate metadata attached with this instruction.
        MDs.clear();
        I.getAllMetadataOtherThanDebugLoc(MDs);
        for (unsigned i = 0, e = MDs.size(); i != e; ++i)
          EnumerateMetadata(MDs[i].second);

#if LLVM_VERSION >= 37
        if (I.getDebugLoc()) {
          MDNode* Scope = I.getDebugLoc().getScope();
          if (Scope) EnumerateMetadata(Scope);
          DILocation *IA = I.getDebugLoc().getInlinedAt();
          if (IA) EnumerateMetadata(IA);
        }
#else
        if (!I.getDebugLoc().isUnknown()) {
          MDNode *Scope, *IA;
          I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
          if (Scope) EnumerateMetadata(Scope);
          if (IA) EnumerateMetadata(IA);
        }
#endif
      }
  }

  // Optimize constant ordering.
  OptimizeConstants(FirstConstant, Values.size());
}
Ejemplo n.º 9
0
bool CheckCandidateMap(llvm::Module &Mod, CandidateMap &M, Solver *S,
                       InstContext &IC) {
  if (!S) {
    llvm::errs() << "Solver required in -check mode\n";
    return false;
  }

  unsigned ExpectedID = Mod.getContext().getMDKindID("expected");

  bool OK = true;
  for (auto &Cand : M) {
    Inst *RHS;
    if (std::error_code EC =
            S->infer(Cand.PCs, Cand.Mapping.LHS, RHS, &Cand.Origin, IC)) {
      llvm::errs() << "Unable to query solver: " << EC.message() << '\n';
      return false;
    }
    if (RHS) {
      Cand.Mapping.RHS = RHS;
      if (Cand.Mapping.RHS->K != Inst::Const) {
        llvm::errs() << "found replacement:\n";
        Cand.printFunction(llvm::errs());
        Cand.print(llvm::errs());
        llvm::errs() << "but cannot yet analyze non-constant replacements\n";
        OK = false;
        continue;
      }
      llvm::APInt ActualVal = Cand.Mapping.RHS->Val;

      llvm::Instruction *Inst = Cand.Origin.getInstruction();
      llvm::MDNode *ExpectedMD = Inst->getMetadata(ExpectedID);
      if (!ExpectedMD) {
        llvm::errs() << "instruction:\n";
        Inst->dump();
        llvm::errs() << "unexpected simplification:\n";
        Cand.printFunction(llvm::errs());
        Cand.print(llvm::errs());
        OK = false;
        continue;
      }
      if (ExpectedMD->getNumOperands() != 1 ||
          !isa<llvm::ConstantInt>(ExpectedMD->getOperand(0))) {
        llvm::errs() << "instruction:\n";
        Inst->dump();
        llvm::errs() << "invalid metadata\n";
        OK = false;
        continue;
      }
      llvm::APInt ExpectedVal =
        cast<llvm::ConstantInt>(ExpectedMD->getOperand(0))->getValue();
      Inst->setMetadata(ExpectedID, 0);
      if (ExpectedVal != ActualVal) {
        llvm::errs() << "instruction:\n";
        Inst->dump();
        llvm::errs() << "unexpected simplification, wanted " << ExpectedVal
                     << ":\n";
        Cand.printFunction(llvm::errs());
        Cand.print(llvm::errs());
        OK = false;
        continue;
      }
    }
  }

  for (const auto &F : Mod) {
    for (const auto &BB : F) {
      for (const auto &Inst : BB) {
        llvm::MDNode *ExpectedMD = Inst.getMetadata(ExpectedID);
        if (ExpectedMD) {
          llvm::errs() << "instruction:\n";
          Inst.dump();
          llvm::errs() << "expected simplification, none found\n";
          OK = false;
          continue;
        }
      }
    }
  }

  return OK;
}