SEXP R_optimizeFunction(SEXP r_func, SEXP r_passMgr) { llvm::legacy::FunctionPassManager *mgr = GET_REF(r_passMgr, legacy::FunctionPassManager); llvm::Function *func = GET_REF(r_func, Function); return(ScalarLogical(mgr->run(*func))); }
SEXP R_getPassManager(SEXP r_module, SEXP r_ee) { llvm::Module *TheModule = GET_REF(r_module, Module); llvm::ExecutionEngine *TheExecutionEngine = NULL; llvm::FunctionPassManager *mgr = new llvm::FunctionPassManager(TheModule); if(r_ee != R_NilValue) { TheExecutionEngine = GET_REF(r_ee, ExecutionEngine); // Set up the optimizer pipeline. Start with registering info about how the // target lays out data structures. mgr->add(new llvm::TargetData(*TheExecutionEngine->getTargetData())); } // Promote allocas to registers. mgr->add(llvm::createPromoteMemoryToRegisterPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. mgr->add(llvm::createInstructionCombiningPass()); // Reassociate expressions. mgr->add(llvm::createReassociatePass()); // Eliminate Common SubExpressions. mgr->add(llvm::createGVNPass()); // Simplify the control flow graph (deleting unreachable blocks, etc). mgr->add(llvm::createCFGSimplificationPass()); mgr->doInitialization(); return(R_createRef(mgr, "FunctionPassManager")); }
SEXP R_IRBuilder_createLocalVariable(SEXP r_builder, SEXP r_type, SEXP r_size, SEXP r_id, SEXP r_beforeTerminator) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Type *type = GET_TYPE(r_type); llvm::AllocaInst *ans; if(Rf_length(r_size)) { llvm::Value *size = GET_REF(r_size, Value); ans = new llvm::AllocaInst(type, size, makeTwine(r_id)); } else ans = new llvm::AllocaInst(type, makeTwine(r_id)); if(LOGICAL(r_beforeTerminator)[0]) { llvm::BasicBlock *block; block = builder->GetInsertBlock(); llvm::TerminatorInst *inst = block->getTerminator(); if(inst) block->getInstList().insert(inst, ans); else builder->Insert(ans); } else ans = builder->Insert(ans); if(Rf_length(r_id)) ans->setName(makeTwine(r_id)); return(R_createRef(ans, "AllocaInst")); }
SEXP R_PassManager_run(SEXP r_passMgr, SEXP r_module) { llvm::legacy::PassManager *mgr = GET_REF(r_passMgr, legacy::PassManager); llvm::Module *module = GET_REF(r_module, Module); return(ScalarLogical(mgr->run(*module))); }
SEXP R_new_DIBuilder_Function(SEXP r_dibuilder, SEXP r_cu, SEXP r_func, SEXP r_type, SEXP r_lineno) { llvm::DIBuilder *builder; builder = GET_REF(r_dibuilder, DIBuilder); llvm::Function *func = GET_REF(r_func, Function); llvm::DICompileUnit *cu; cu = GET_REF(r_cu, DICompileUnit); llvm::DIFile* Unit = builder->createFile(cu->getFilename(), cu->getDirectory()); llvm::DISubroutineType *type; type = GET_REF(r_type, DISubroutineType); int lineNo = asInteger(r_lineno); llvm::DISubprogram* SP = builder->createFunction( Unit, func->getName(), llvm::StringRef(), Unit, lineNo, type, false /* internal linkage */, true /* definition */, lineNo, llvm::DINode::FlagPrototyped, false #if LLVM_VERSION == 3 && LLVM_MINOR_VERSION < 8 , func #endif ); return(R_createRef(SP, "DISubprogram")); }
SEXP R_DIBuilder_CreateFunctionType(SEXP r_builder, SEXP args, SEXP r_cu) { llvm::SmallVector<llvm::Metadata *, 8> EltTys; llvm::DIBuilder *builder = GET_REF(r_builder, DIBuilder); for(R_len_t i = 0; i < Rf_length(args); ++i) { llvm::DIType *element=GET_REF(VECTOR_ELT(args,i), DIType); //llvm::DITypeRef *element2 = llvm::cast<llvm::DITypeRef >(element); //llvm::Value *element3 = llvm::cast<llvm::Value>(element2); EltTys.push_back(element); //EltTys.push_back(const_cast<llvm::Value*>( (llvm::MDNodeContext, MDs *)element)); } llvm::DITypeRefArray EltTypeArray = builder->getOrCreateTypeArray(EltTys); #if LLVM_VERSION == 3 && LLVM_MINOR_VERSION < 8 llvm::DICompileUnit* cu = GET_REF(r_cu, DICompileUnit); llvm::DIFile* file = builder->createFile(cu->getFilename(), cu->getDirectory()); llvm::DISubroutineType* ans = builder->createSubroutineType(file, EltTypeArray); #else llvm::DISubroutineType* ans = builder->createSubroutineType(EltTypeArray); #endif return R_createRef(ans,"DISubroutineType"); }
SEXP R_ParseAssemblyString(SEXP r_str, SEXP r_module, SEXP r_context) { llvm::Module *module; llvm::SMDiagnostic err; llvm::LLVMContext *context; if(Rf_length(r_context)) context = (GET_REF(r_context, LLVMContext)); // llvm::cast<llvm::LLVMContext> else context = & llvm::getGlobalContext(); if(length(r_module)) module = GET_REF(r_module, Module); else module = NULL; const char *text = CHAR(STRING_ELT(r_str, 0)); #if LLVM_VERSION ==3 && LLVM_MINOR_VERSION >= 6 module = llvm::parseAssemblyString(text, /* module, */ err, *context).release(); #else module = llvm::ParseAssemblyString(text, module, err, *context); #endif if(!module) { Rprintf("%s\n", err.getLineContents()); Rprintf("%s\n", err.getMessage()); PROBLEM "problem reading assembler code" ERROR; } return(R_createRef(module, "Module")); }
SEXP R_PassManagerBase_Add(SEXP r_mgr, SEXP r_pass) { llvm::legacy::PassManagerBase *mgr = GET_REF(r_mgr, legacy::PassManagerBase); llvm::Pass *pass = GET_REF(r_pass, Pass); mgr->add(pass); return(ScalarLogical(TRUE)); }
SEXP R_Value_replaceAllUsesWith(SEXP r_val, SEXP r_other) { llvm::Value *val = GET_REF(r_val, Value); llvm::Value *other = GET_REF(r_other, Value); val->replaceAllUsesWith(other); return(R_NilValue); }
SEXP R_GlobalVariable_setInitializer(SEXP r_var, SEXP r_val) { llvm::GlobalVariable *ans = GET_REF(r_var, GlobalVariable); llvm::Constant *val = GET_REF(r_val, Constant); ans->setInitializer(val); return(ScalarLogical(TRUE)); }
SEXP R_IRBuilder_CreateFNeg(SEXP r_builder, SEXP r_val) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *val = GET_REF(r_val, Value); llvm::Value * ans = builder->CreateFNeg(val); return(R_createRef(ans, "Value")); }
SEXP R_IRBuilder_CreateRet(SEXP r_builder, SEXP r_val) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *val = GET_REF(r_val, Value); llvm::ReturnInst *ret = builder->CreateRet(val); return(R_createRef(ret, "ReturnInst")); }
SEXP R_IRBuilder_SetInsertPoint(SEXP r_builder, SEXP r_block) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::BasicBlock *block = GET_REF(r_block, BasicBlock); builder->SetInsertPoint(block); return(R_NilValue); }
SEXP R_IRBuilder_CreateBr(SEXP r_builder, SEXP r_block) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::BasicBlock *block = GET_REF(r_block, BasicBlock); llvm::BranchInst *ans = builder->CreateBr(block); return(R_createRef(ans, "BranchInst")); }
SEXP R_IRBuilder_SetInsertPointInstruction(SEXP r_builder, SEXP r_block) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Instruction *ins = GET_REF(r_block, Instruction); builder->SetInsertPoint(ins); return(R_NilValue); }
SEXP R_IRBuilder_CreateExtractElement(SEXP r_builder, SEXP r_vec, SEXP r_idx) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *val = GET_REF(r_vec, Value); llvm::Value *idx = GET_REF(r_idx, Value); llvm::Value *ret = builder->CreateExtractElement(val, idx); return(R_createRef(ret, "Value")); }
SEXP R_DIBuilder_CreatePointerType(SEXP r_builder, SEXP r_type, SEXP r_name) { llvm::DIBuilder *builder = GET_REF(r_builder, DIBuilder); llvm::DIType *baseType = GET_REF(r_type, DIType); const char * name = CHAR(STRING_ELT(r_name, 0)); llvm::DIType* ans = builder->createPointerType(baseType,sizeof(void*),0,llvm::StringRef(name)); return(R_createRef(ans,"DIType")); }
SEXP R_IRBuilder_CreateSwitch(SEXP r_builder, SEXP r_val, SEXP r_dest, SEXP numCases, SEXP r_id) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *val = GET_REF(r_val, Value); llvm::BasicBlock *dest = GET_REF(r_dest, BasicBlock); llvm::SwitchInst *ans = builder->CreateSwitch(val, dest, INTEGER(numCases)[0]); return(R_createRef(ans, "SwitchInst")); }
SEXP R_SwitchInst_addCases(SEXP r_inst, SEXP r_vals, SEXP r_block) { llvm::SwitchInst *sw = GET_REF(r_inst, SwitchInst); for(int i = 0 ; i < Rf_length(r_vals) ; i++) { llvm::ConstantInt *val = GET_REF(VECTOR_ELT(r_vals, i), ConstantInt); llvm::BasicBlock *block = GET_REF(VECTOR_ELT(r_block, i), BasicBlock); sw->addCase(val, block); } return(ScalarLogical(TRUE)); }
SEXP R_IRBuilder_CreateSIToFP(SEXP r_builder, SEXP r_val, SEXP r_type) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *val = GET_REF(r_val, Value); llvm::Type *type = GET_REF(r_type, Type); llvm::Value * ans = builder->CreateSIToFP(val, type); return(R_createRef(ans, "Value")); }
SEXP R_clang_getRange(SEXP r_begin, SEXP r_end) { SEXP r_ans = R_NilValue; CXSourceLocation begin = * GET_REF(r_begin, CXSourceLocation); CXSourceLocation end = * GET_REF(r_end, CXSourceLocation); CXSourceRange ans; ans = clang_getRange(begin, end); r_ans = R_makeCXSourceRange(ans) ; return(r_ans); }
SEXP R_clang_equalCursors(SEXP r_arg1, SEXP r_arg2) { SEXP r_ans = R_NilValue; CXCursor arg1 = * GET_REF(r_arg1, CXCursor); CXCursor arg2 = * GET_REF(r_arg2, CXCursor); unsigned int ans; ans = clang_equalCursors(arg1, arg2); r_ans = ScalarReal(ans) ; return(r_ans); }
SEXP R_IRBuilder_CreatePHI(SEXP r_builder, SEXP r_type, SEXP r_numReservedValues, SEXP r_id) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Type *type = GET_REF(r_type, Type); llvm::PHINode *ret = builder->CreatePHI(type, INTEGER(r_numReservedValues)[0]); if(Rf_length(r_id)) ret->setName(makeTwine(r_id)); return(R_createRef(ret, "PHINode")); }
SEXP R_IRBuilder_CreateStructGEP(SEXP r_builder, SEXP r_value, SEXP r_index, SEXP r_id) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *ans, *value; value = GET_REF(r_builder, Value); ans = builder->CreateStructGEP(NULL, value, INTEGER(r_index)[0]); if(Rf_length(r_id)) ans->setName(makeTwine(r_id)); return(R_createRef(ans, "Value")); }
SEXP R_clang_equalLocations(SEXP r_loc1, SEXP r_loc2) { SEXP r_ans = R_NilValue; CXSourceLocation loc1 = * GET_REF(r_loc1, CXSourceLocation); CXSourceLocation loc2 = * GET_REF(r_loc2, CXSourceLocation); unsigned int ans; ans = clang_equalLocations(loc1, loc2); r_ans = ScalarReal(ans) ; return(r_ans); }
SEXP R_IRBuilder_CreateCondBr(SEXP r_builder, SEXP r_cond, SEXP r_true, SEXP r_false) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *cond = GET_REF(r_cond, Value); llvm::BasicBlock *block1 = GET_REF(r_true, BasicBlock); llvm::BasicBlock *block2 = GET_REF(r_false, BasicBlock); llvm::BranchInst *ans = builder->CreateCondBr(cond, block1, block2); return(R_createRef(ans, "BranchInst")); }
SEXP R_clang_equalRanges(SEXP r_range1, SEXP r_range2) { SEXP r_ans = R_NilValue; CXSourceRange range1 = * GET_REF(r_range1, CXSourceRange); CXSourceRange range2 = * GET_REF(r_range2, CXSourceRange); unsigned int ans; ans = clang_equalRanges(range1, range2); r_ans = ScalarReal(ans) ; return(r_ans); }
SEXP R_clang_getDiagnosticOption(SEXP r_Diag, SEXP r_Disable) { SEXP r_ans = R_NilValue; CXDiagnostic Diag = GET_REF(r_Diag, CXDiagnostic); CXString * Disable = GET_REF(r_Disable, CXString); CXString ans; ans = clang_getDiagnosticOption(Diag, Disable); r_ans = CXStringToSEXP(ans) ; return(r_ans); }
SEXP R_Target_createTargetMachine(SEXP r_target, SEXP r_triple, SEXP r_cpu, SEXP r_features, SEXP r_opts) { const llvm::Target *tgt = GET_REF(r_target, Target); // llvm::Module *mod = GET_REF(r_module, Module); std::string triple(CHAR(STRING_ELT(r_triple, 0))); llvm::TargetOptions *opts = NULL, defaultOpts; llvm::TargetMachine *ans; if(Rf_length(r_opts)) opts = GET_REF(r_opts, TargetOptions); else { /* taken from Halide's CodeGen.cpp */ defaultOpts.LessPreciseFPMADOption = true; //defaultOpts.NoFramePointerElim = false; #ifdef LLVM_HAS_NOFRAMEPOINTERELIMNONLEAF defaultOpts.NoFramePointerElimNonLeaf = false; #endif defaultOpts.AllowFPOpFusion = llvm::FPOpFusion::Fast; defaultOpts.UnsafeFPMath = true; defaultOpts.NoInfsFPMath = true; defaultOpts.NoNaNsFPMath = true; defaultOpts.HonorSignDependentRoundingFPMathOption = false; //defaultOpts.UseSoftFloat = false; defaultOpts.FloatABIType = llvm::FloatABI::Soft; defaultOpts.NoZerosInBSS = false; defaultOpts.GuaranteedTailCallOpt = false; //defaultOpts.DisableTailCalls = false; defaultOpts.StackAlignmentOverride = 32; #ifdef LLVM_HAS_REALIGNSTACK defaultOpts.RealignStack = true; #endif //defaultOpts.TrapFuncName = ""; defaultOpts.PositionIndependentExecutable = true; #if LLVM_VERSION == 3 && LLVM_MINOR_VERSION < 5 defaultOpts.EnableSegmentedStacks = false; #endif defaultOpts.UseInitArray = false; #ifdef LLVM_HAS_SSPBUFFERSIZE defaultOpts.SSPBufferSize = 0; #endif opts = &defaultOpts; } ans = tgt->createTargetMachine(triple, std::string(CHAR(STRING_ELT(r_cpu, 0))), std::string(CHAR(STRING_ELT(r_features, 0))), *opts); return(R_createRef(ans, "TargetMachine")); }
SEXP R_IRBuilder_CreateTrunc(SEXP r_builder, SEXP r_value, SEXP r_type, SEXP r_id) { llvm::IRBuilder<> *builder; builder = GET_REF(r_builder, IRBuilder<>); llvm::Value *value = GET_REF(r_value, Value); llvm::Type *type = GET_REF(r_type, Type); llvm::Value *ans = builder->CreateTrunc(value, type); if(Rf_length(r_id)) ans->setName(makeTwine(r_id)); return(R_createRef(ans, "TruncInst")); }