From 3111db3d1a3f2b20a007673f69a53ad69afec2f8 Mon Sep 17 00:00:00 2001 From: Andreas Cadhalpun Date: Fri, 14 Oct 2016 20:24:48 +0200 Subject: Add support for LLVM 3.8 Main changes: llvm/Config/config.h was removed. The ScalarEvolution pass is now a WrapperPass. Iterators are no longer automatically converted to and from pointers. The GVNPass causes the test_bswap_jit test to fail; replaced with ConstantPropagationPass. LLVMIsMultithreaded is from the C API, while llvm_is_multithreaded is the corresponding C++ API. Patch-Name: Add-support-for-LLVM-3.8.patch --- libclamav/c++/ClamBCRTChecks.cpp | 50 ++++++++++++++++++++++++++++++ libclamav/c++/PointerTracking.cpp | 12 ++++++++ libclamav/c++/bytecode2llvm.cpp | 65 +++++++++++++++++++++++++++++++-------- libclamav/c++/detect.cpp | 2 ++ libclamav/c++/m4/llvm-flags.m4 | 4 +-- 5 files changed, 119 insertions(+), 14 deletions(-) diff --git a/libclamav/c++/ClamBCRTChecks.cpp b/libclamav/c++/ClamBCRTChecks.cpp index 35f7272..a254c40 100644 --- a/libclamav/c++/ClamBCRTChecks.cpp +++ b/libclamav/c++/ClamBCRTChecks.cpp @@ -54,7 +54,9 @@ #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/Analysis/ScalarEvolutionExpander.h" +#if LLVM_VERSION < 38 #include "llvm/Config/config.h" +#endif #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #if LLVM_VERSION < 35 @@ -207,7 +209,11 @@ namespace llvm { #else TD = &F.getEntryBlock().getModule()->getDataLayout(); #endif +#if LLVM_VERSION < 38 SE = &getAnalysis(); +#else + SE = &getAnalysis().getSE(); +#endif PT = &getAnalysis(); #if LLVM_VERSION < 35 DT = &getAnalysis(); @@ -332,7 +338,11 @@ namespace llvm { AbrtC->setDoesNotThrow(); #endif // remove all instructions from entry +#if LLVM_VERSION < 38 BasicBlock::iterator BBI = I, BBE=BB->end(); +#else + BasicBlock::iterator BBI = I->getIterator(), BBE=BB->end(); +#endif while (BBI != BBE) { if (!BBI->use_empty()) BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); @@ -367,7 +377,11 @@ namespace llvm { #else AU.addRequired(); #endif +#if LLVM_VERSION < 38 AU.addRequired(); +#else + AU.addRequired(); +#endif AU.addRequired(); #if LLVM_VERSION < 35 AU.addRequired(); @@ -398,9 +412,17 @@ namespace llvm { Instruction *getInsertPoint(Value *V) { +#if LLVM_VERSION < 38 BasicBlock::iterator It = EP; +#else + BasicBlock::iterator It = EP->getIterator(); +#endif if (Instruction *I = dyn_cast(V)) { +#if LLVM_VERSION < 38 It = I; +#else + It = I->getIterator(); +#endif ++It; } return &*It; @@ -427,7 +449,11 @@ namespace llvm { constType *P8Ty = PointerType::getUnqual(Type::getInt8Ty(Ptr->getContext())); if (PHINode *PN = dyn_cast(Ptr)) { +#if LLVM_VERSION < 38 BasicBlock::iterator It = PN; +#else + BasicBlock::iterator It = PN->getIterator(); +#endif ++It; PHINode *newPN = PHINode::Create(P8Ty, HINT(PN->getNumIncomingValues()) ".verif.base", &*It); Changed = true; @@ -441,7 +467,11 @@ namespace llvm { return newPN; } if (SelectInst *SI = dyn_cast(Ptr)) { +#if LLVM_VERSION < 38 BasicBlock::iterator It = SI; +#else + BasicBlock::iterator It = SI->getIterator(); +#endif ++It; Value *TrueB = getPointerBase(SI->getTrueValue()); Value *FalseB = getPointerBase(SI->getFalseValue()); @@ -552,7 +582,11 @@ namespace llvm { } #endif if (PHINode *PN = dyn_cast(Base)) { +#if LLVM_VERSION < 38 BasicBlock::iterator It = PN; +#else + BasicBlock::iterator It = PN->getIterator(); +#endif ++It; PHINode *newPN = PHINode::Create(I64Ty, HINT(PN->getNumIncomingValues()) ".verif.bounds", &*It); Changed = true; @@ -575,7 +609,11 @@ namespace llvm { return BoundsMap[Base] = newPN; } if (SelectInst *SI = dyn_cast(Base)) { +#if LLVM_VERSION < 38 BasicBlock::iterator It = SI; +#else + BasicBlock::iterator It = SI->getIterator(); +#endif ++It; Value *TrueB = getPointerBounds(SI->getTrueValue()); Value *FalseB = getPointerBounds(SI->getFalseValue()); @@ -632,7 +670,11 @@ namespace llvm { if (!MDDbgKind) return 0; Approximate = true; +#if LLVM_VERSION < 38 BasicBlock::iterator It = I; +#else + BasicBlock::iterator It = I->getIterator(); +#endif while (It != I->getParent()->begin()) { --It; if (MDNode *Dbg = It->getMetadata(MDDbgKind)) @@ -666,7 +708,11 @@ namespace llvm { return false; } BasicBlock *BB = I->getParent(); +#if LLVM_VERSION < 38 BasicBlock::iterator It = I; +#else + BasicBlock::iterator It = I->getIterator(); +#endif #if LLVM_VERSION < 37 BasicBlock *newBB = SplitBlock(BB, &*It, this); #else @@ -949,7 +995,11 @@ INITIALIZE_PASS_DEPENDENCY(DominatorTree) #else INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) #endif +#if LLVM_VERSION < 38 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) +#else +INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) +#endif #if LLVM_VERSION < 34 INITIALIZE_AG_DEPENDENCY(CallGraph) #elif LLVM_VERSION < 35 diff --git a/libclamav/c++/PointerTracking.cpp b/libclamav/c++/PointerTracking.cpp index 67340e8..ad5b93f 100644 --- a/libclamav/c++/PointerTracking.cpp +++ b/libclamav/c++/PointerTracking.cpp @@ -79,7 +79,11 @@ INITIALIZE_PASS_DEPENDENCY(LoopInfo) #else INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) #endif +#if LLVM_VERSION < 38 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) +#else +INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) +#endif #if LLVM_VERSION < 35 INITIALIZE_PASS_DEPENDENCY(DominatorTree) #else @@ -110,7 +114,11 @@ bool PointerTracking::runOnFunction(Function &F) { #else TD = &F.getEntryBlock().getModule()->getDataLayout(); #endif +#if LLVM_VERSION < 38 SE = &getAnalysis(); +#else + SE = &getAnalysis().getSE(); +#endif #if LLVM_VERSION < 37 LI = &getAnalysis(); #else @@ -135,7 +143,11 @@ void PointerTracking::getAnalysisUsage(AnalysisUsage &AU) const { #else AU.addRequiredTransitive(); #endif +#if LLVM_VERSION < 38 AU.addRequiredTransitive(); +#else + AU.addRequiredTransitive(); +#endif AU.setPreservesAll(); } diff --git a/libclamav/c++/bytecode2llvm.cpp b/libclamav/c++/bytecode2llvm.cpp index 2171993..213847c 100644 --- a/libclamav/c++/bytecode2llvm.cpp +++ b/libclamav/c++/bytecode2llvm.cpp @@ -170,7 +170,9 @@ void LLVMInitializePowerPCAsmPrinter(); //#define TIMING #undef TIMING +#if LLVM_VERSION < 38 #include "llvm/Config/config.h" +#endif #ifdef ENABLE_THREADS #if !ENABLE_THREADS #error "Thread support was explicitly disabled. Cannot continue" @@ -729,7 +731,11 @@ class RuntimeLimits : public FunctionPass { #else LoopInfo &LI = getAnalysis().getLoopInfo(); #endif +#if LLVM_VERSION < 38 ScalarEvolution &SE = getAnalysis(); +#else + ScalarEvolution &SE = getAnalysis().getSE(); +#endif // Now check whether any of these backedge targets are part of a loop // with a small constant trip count @@ -783,7 +789,11 @@ class RuntimeLimits : public FunctionPass { new UnreachableInst(F.getContext(), AbrtBB); IRBuilder Builder(F.getContext()); +#if LLVM_VERSION < 38 Value *Flag = F.arg_begin(); +#else + Value *Flag = &*F.arg_begin(); +#endif #if LLVM_VERSION < 30 Function *LSBarrier = Intrinsic::getDeclaration(F.getParent(), Intrinsic::memory_barrier); @@ -797,13 +807,21 @@ class RuntimeLimits : public FunctionPass { #endif verifyFunction(F); BasicBlock *BB = &F.getEntryBlock(); +#if LLVM_VERSION < 38 Builder.SetInsertPoint(BB, BB->getTerminator()); +#else + Builder.SetInsertPoint(BB, BB->getTerminator()->getIterator()); +#endif Flag = Builder.CreatePointerCast(Flag, PointerType::getUnqual( Type::getInt1Ty(F.getContext()))); for (BBSetTy::iterator I=needsTimeoutCheck.begin(), E=needsTimeoutCheck.end(); I != E; ++I) { BasicBlock *BB = *I; +#if LLVM_VERSION < 38 Builder.SetInsertPoint(BB, BB->getTerminator()); +#else + Builder.SetInsertPoint(BB, BB->getTerminator()->getIterator()); +#endif #if LLVM_VERSION < 30 // store-load barrier: will be a no-op on x86 but not other arches Builder.CreateCall(LSBarrier, ARRAYREF(Value*, MBArgs, MBArgs+5)); @@ -842,7 +860,11 @@ class RuntimeLimits : public FunctionPass { #else AU.addRequired(); #endif +#if LLVM_VERSION < 38 AU.addRequired(); +#else + AU.addRequired(); +#endif #if LLVM_VERSION < 35 AU.addRequired(); #else @@ -1157,8 +1179,10 @@ class LLVMCodegen { Folder(EE->getTargetData()), Builder(Context, Folder), Values(), CF(CF) { #elif LLVM_VERSION < 37 Folder(EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) { -#else +#elif LLVM_VERSION < 38 Folder(*EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) { +#else + Folder(EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) { #endif for (unsigned i=0;igetTargetData()->getPointerSize() == 8) { -#else +#elif LLVM_VERSION < 38 if (EE->getDataLayout()->getPointerSize() == 8) { +#else + if (EE->getDataLayout().getPointerSize() == 8) { #endif // eliminate useless trunc, GEP can take i64 too if (TruncInst *I = dyn_cast(V)) { @@ -1440,7 +1466,11 @@ class LLVMCodegen { numArgs = func->numArgs; if (FakeGVs.any()) { +#if LLVM_VERSION < 38 Argument *Ctx = F->arg_begin(); +#else + Argument *Ctx = &*F->arg_begin(); +#endif for (unsigned i=0;inum_globals;i++) { if (!FakeGVs[i]) continue; @@ -1888,8 +1918,10 @@ class LLVMCodegen { Src = Builder.CreatePointerCast(Src, PointerType::getUnqual(Type::getInt8Ty(Context))); #if LLVM_VERSION < 32 Value *Len = convertOperand(func, EE->getTargetData()->getIntPtrType(Context), inst->u.three[2]); -#else +#elif LLVM_VERSION < 38 Value *Len = convertOperand(func, EE->getDataLayout()->getIntPtrType(Context), inst->u.three[2]); +#else + Value *Len = convertOperand(func, EE->getDataLayout().getIntPtrType(Context), inst->u.three[2]); #endif #if LLVM_VERSION < 37 CallInst *c = Builder.CreateCall3(CF->FRealmemcmp, Dst, Src, Len); @@ -2028,6 +2060,7 @@ class LLVMCodegen { PMUnsigned.run(*F); PMUnsigned.doFinalization(); } + apiMap.pmTimer.stopTimer(); apiMap.irgenTimer.startTimer(); } @@ -2260,8 +2293,10 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context))); #if LLVM_VERSION < 32 args.push_back(EE->getTargetData()->getIntPtrType(Context)); -#else +#elif LLVM_VERSION < 38 args.push_back(EE->getDataLayout()->getIntPtrType(Context)); +#else + args.push_back(EE->getDataLayout().getIntPtrType(Context)); #endif FuncTy_5 = FunctionType::get(Type::getInt32Ty(Context), args, false); @@ -2282,7 +2317,11 @@ INITIALIZE_PASS_DEPENDENCY(LoopInfo) #else INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) #endif +#if LLVM_VERSION < 38 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) +#else +INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) +#endif #if LLVM_VERSION < 35 INITIALIZE_PASS_DEPENDENCY(DominatorTree) #else @@ -2609,8 +2648,10 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs) #endif #if LLVM_VERSION < 32 M->setDataLayout(EE->getTargetData()->getStringRepresentation()); -#else +#elif LLVM_VERSION < 38 M->setDataLayout(EE->getDataLayout()->getStringRepresentation()); +#else + M->setDataLayout(EE->getDataLayout().getStringRepresentation()); #endif #if LLVM_VERSION < 31 M->setTargetTriple(sys::getHostTriple()); @@ -2767,7 +2808,11 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs) // TODO: only run this on the untrusted bytecodes, not all of them... if (has_untrusted) PM.add(createClamBCRTChecks()); -#if LLVM_VERSION >= 36 +#if LLVM_VERSION >= 38 + // With LLVM 3.8 the test_bswap_jit test fails with the GVNPass enabled. + // To prevent the segfaults mentioned below, replace it with the ConstantPropagationPass. + PM.add(createConstantPropagationPass()); +#elif LLVM_VERSION >= 36 // With LLVM 3.6 (MCJIT) this Pass is required to work around // a crash in LLVM caused by the SCCP Pass: // Pass 'Sparse Conditional Constant Propagation' is not initialized. @@ -2841,7 +2886,7 @@ int bytecode_init(void) return CL_EARG; } #else - if (!LLVMIsMultithreaded()) { + if (!llvm_is_multithreaded()) { cli_warnmsg("bytecode_init: LLVM is compiled without multithreading support\n"); } #endif @@ -2890,11 +2935,7 @@ int bytecode_init(void) InitializeAllTargets(); #endif -#if LLVM_VERSION < 35 if (!llvm_is_multithreaded()) { -#else - if (!LLVMIsMultithreaded()) { -#endif //TODO:cli_dbgmsg DEBUG(errs() << "WARNING: ClamAV JIT built w/o atomic builtins\n" << "On x86 for best performance ClamAV should be built for i686, not i386!\n"); @@ -3113,7 +3154,7 @@ static Metadata *findDbgSubprogramDeclare(Function *V) { MDNode *DIG = NMD->getOperand(i); if (!DISubprogram::classof(DIG)) continue; - if ((cast(DIG))->getFunction() == V) + if ((cast(DIG))->describes(V)) return DIG; #endif } diff --git a/libclamav/c++/detect.cpp b/libclamav/c++/detect.cpp index 17348af..95ba2f7 100644 --- a/libclamav/c++/detect.cpp +++ b/libclamav/c++/detect.cpp @@ -22,7 +22,9 @@ */ #include "llvm/ADT/Triple.h" +#if LLVM_VERSION < 38 #include "llvm/Config/config.h" +#endif #include "llvm/Support/raw_ostream.h" #if LLVM_VERSION < 29 #include "llvm/System/DataTypes.h" diff --git a/libclamav/c++/m4/llvm-flags.m4 b/libclamav/c++/m4/llvm-flags.m4 index 04d6833..345c7ae 100644 --- a/libclamav/c++/m4/llvm-flags.m4 +++ b/libclamav/c++/m4/llvm-flags.m4 @@ -98,14 +98,14 @@ elif test $llvmver_test -lt 290; then elif test $llvmver_test -lt 360; then llvmcomp="jit nativecodegen scalaropts ipo" AC_MSG_RESULT([ok ($llvmver)]) -elif test $llvmver_test -lt 380; then +elif test $llvmver_test -lt 390; then dnl LLVM 3.6.0 removed jit, so we have to use mcjit dnl and we're using InitializeNativeTargetAsmParser, so we need the architecture specific parsers llvmcomp="mcjit nativecodegen scalaropts ipo x86asmparser powerpcasmparser" AC_MSG_RESULT([ok ($llvmver)]) else AC_MSG_RESULT([no ($llvmver)]) - AC_MSG_ERROR([LLVM < 3.8 required, but "$llvmver"($llvmver_test) found]) + AC_MSG_ERROR([LLVM < 3.9 required, but "$llvmver"($llvmver_test) found]) fi dnl aquire the required flags to properly link in external LLVM