summaryrefslogtreecommitdiff
path: root/libre/clamav/Add-support-for-LLVM-3.7.patch
blob: 8bcd1d7ea67cb869776d8a2e93b53b0d09f27748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
From 3b1b2e757d0d1f13a3f234abf05d6e5015f52744 Mon Sep 17 00:00:00 2001
From: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
Date: Fri, 14 Oct 2016 20:24:39 +0200
Subject: Add support for LLVM 3.7

Main changes:
The DataLayoutPass is no longer necessary.
The LoopInfo pass is now a WrapperPass.
Before creating TargetLibraryInfo one needs to create a
TargetLibraryInfoImpl.
PassManager is now in the legacy:: namespace.
GetElementPtrInst::getIndexedType changed behavior causing segfaults in
the testsuite; emulating the old behavior now.
CreateCallX functions for fixed number X of elements got removed.
JITEmitDebugInfo Option was removed.
DIDescriptor was removed.

Patch-Name: Add-support-for-LLVM-3.7.patch
---
 libclamav/c++/ClamBCRTChecks.cpp  |  34 ++++++-
 libclamav/c++/PointerTracking.cpp |  44 ++++++++-
 libclamav/c++/bytecode2llvm.cpp   | 181 +++++++++++++++++++++++++++++++++++---
 libclamav/c++/m4/llvm-flags.m4    |   4 +-
 4 files changed, 244 insertions(+), 19 deletions(-)

diff --git a/libclamav/c++/ClamBCRTChecks.cpp b/libclamav/c++/ClamBCRTChecks.cpp
index 97099b2..35f7272 100644
--- a/libclamav/c++/ClamBCRTChecks.cpp
+++ b/libclamav/c++/ClamBCRTChecks.cpp
@@ -201,9 +201,11 @@ namespace llvm {
           TD = &getAnalysis<TargetData>();
 #elif LLVM_VERSION < 35
           TD = &getAnalysis<DataLayout>();
-#else
+#elif LLVM_VERSION < 37
           DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
           TD = DLP ? &DLP->getDataLayout() : 0;
+#else
+          TD = &F.getEntryBlock().getModule()->getDataLayout();
 #endif
           SE = &getAnalysis<ScalarEvolution>();
           PT = &getAnalysis<PointerTracking>();
@@ -212,7 +214,11 @@ namespace llvm {
 #else
           DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
 #endif
+#if LLVM_VERSION < 37
           expander = new SCEVExpander(*SE OPT("SCEVexpander"));
+#else
+          expander = new SCEVExpander(*SE, *TD OPT("SCEVexpander"));
+#endif
 
           std::vector<Instruction*> insns;
 
@@ -351,8 +357,10 @@ namespace llvm {
           AU.addRequired<TargetData>();
 #elif LLVM_VERSION < 35
           AU.addRequired<DataLayout>();
-#else
+#elif LLVM_VERSION < 37
           AU.addRequired<DataLayoutPass>();
+#else
+    // No DataLayout pass needed anymore.
 #endif
 #if LLVM_VERSION < 35
           AU.addRequired<DominatorTree>();
@@ -406,7 +414,11 @@ namespace llvm {
           if (BaseMap.count(P)) {
               return BaseMap[Ptr] = BaseMap[P];
           }
+#if LLVM_VERSION < 37
           Value *P2 = GetUnderlyingObject(P, TD);
+#else
+          Value *P2 = GetUnderlyingObject(P, *TD);
+#endif
           if (P2 != P) {
               Value *V = getPointerBase(P2);
               return BaseMap[Ptr] = V;
@@ -524,7 +536,11 @@ namespace llvm {
               }
           }
           if (LoadInst *LI = dyn_cast<LoadInst>(Base)) {
+#if LLVM_VERSION < 37
               Value *V = GetUnderlyingObject(LI->getPointerOperand()->stripPointerCasts(), TD);
+#else
+              Value *V = GetUnderlyingObject(LI->getPointerOperand()->stripPointerCasts(), *TD);
+#endif
               if (Argument *A = dyn_cast<Argument>(V)) {
                   if (A->getArgNo() == 0) {
                       // pointers from hidden ctx are trusted to be at least the
@@ -651,7 +667,11 @@ namespace llvm {
           }
           BasicBlock *BB = I->getParent();
           BasicBlock::iterator It = I;
+#if LLVM_VERSION < 37
           BasicBlock *newBB = SplitBlock(BB, &*It, this);
+#else
+          BasicBlock *newBB = SplitBlock(BB, &*It);
+#endif
           PHINode *PN;
           unsigned MDDbgKind = I->getContext().getMDKindID("dbg");
           //verifyFunction(*BB->getParent());
@@ -696,9 +716,15 @@ namespace llvm {
           unsigned locationid = 0;
           bool Approximate;
           if (MDNode *Dbg = getLocation(I, Approximate, MDDbgKind)) {
+#if LLVM_VERSION < 37
               DILocation Loc(Dbg);
               locationid = Loc.getLineNumber() << 8;
               unsigned col = Loc.getColumnNumber();
+#else
+              DebugLoc Loc(Dbg);
+              locationid = Loc.getLine() << 8;
+              unsigned col = Loc.getCol();
+#endif
               if (col > 254)
                   col = 254;
               if (Approximate)
@@ -912,7 +938,11 @@ INITIALIZE_PASS_DEPENDENCY(TargetData)
 #elif LLVM_VERSION < 35
 INITIALIZE_PASS_DEPENDENCY(DataLayout)
 #else
+#if LLVM_VERSION < 37
 INITIALIZE_PASS_DEPENDENCY(DataLayoutPass)
+#else
+// No DataLayout pass needed anymore.
+#endif
 #endif
 #if LLVM_VERSION < 35
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
diff --git a/libclamav/c++/PointerTracking.cpp b/libclamav/c++/PointerTracking.cpp
index 8e17a4a..67340e8 100644
--- a/libclamav/c++/PointerTracking.cpp
+++ b/libclamav/c++/PointerTracking.cpp
@@ -30,7 +30,11 @@
 #include "llvm/IR/InstIterator.h"
 #endif
 #include "llvm/Support/raw_ostream.h"
+#if LLVM_VERSION < 37
 #include "llvm/Target/TargetLibraryInfo.h"
+#else
+#include <llvm/Analysis/TargetLibraryInfo.h>
+#endif
 
 #if LLVM_VERSION < 32
 #include "llvm/Target/TargetData.h"
@@ -70,7 +74,11 @@ INITIALIZE_PASS_DEPENDENCY(DominatorTree)
 #else
 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
 #endif
+#if LLVM_VERSION < 37
 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
+#else
+INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
+#endif
 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
 #if LLVM_VERSION < 35
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
@@ -96,12 +104,18 @@ bool PointerTracking::runOnFunction(Function &F) {
   TD = getAnalysisIfAvailable<TargetData>();
 #elif LLVM_VERSION < 35
   TD = getAnalysisIfAvailable<DataLayout>();
-#else
+#elif LLVM_VERSION < 37
   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
   TD = DLP ? &DLP->getDataLayout() : 0;
+#else
+  TD = &F.getEntryBlock().getModule()->getDataLayout();
 #endif
   SE = &getAnalysis<ScalarEvolution>();
+#if LLVM_VERSION < 37
   LI = &getAnalysis<LoopInfo>();
+#else
+ LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+#endif
 #if LLVM_VERSION < 35
   DT = &getAnalysis<DominatorTree>();
 #else
@@ -116,7 +130,11 @@ void PointerTracking::getAnalysisUsage(AnalysisUsage &AU) const {
 #else
   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
 #endif
+#if LLVM_VERSION < 37
   AU.addRequiredTransitive<LoopInfo>();
+#else
+  AU.addRequiredTransitive<LoopInfoWrapperPass>();
+#endif
   AU.addRequiredTransitive<ScalarEvolution>();
   AU.setPreservesAll();
 }
@@ -178,12 +196,19 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
   if (CallInst *CI = extractMallocCall(V)) {
     Value *arraySize = getMallocArraySize(CI, TD);
     constType* AllocTy = getMallocAllocatedType(CI);
-#else
+#elif LLVM_VERSION < 37
   TargetLibraryInfo* TLI = new TargetLibraryInfo();
 
   if (CallInst *CI = extractMallocCall(V, TLI)) {
     Value *arraySize = getMallocArraySize(CI, TD, TLI);
     constType* AllocTy = getMallocAllocatedType(CI, TLI);
+#else
+  TargetLibraryInfoImpl* TLII = new TargetLibraryInfoImpl();
+  TargetLibraryInfo* TLI = new TargetLibraryInfo(*TLII);
+
+  if (CallInst *CI = extractMallocCall(V, TLI)) {
+    Value *arraySize = getMallocArraySize(CI, *TD, TLI);
+    constType* AllocTy = getMallocAllocatedType(CI, TLI);
 #endif
     if (!AllocTy || !arraySize) return SE->getCouldNotCompute();
     Ty = AllocTy;
@@ -240,7 +265,7 @@ Value *PointerTracking::computeAllocationCountValue(Value *P, constType *&Ty) co
     if (!Ty)
       return 0;
     Value *arraySize = getMallocArraySize(CI, TD);
-#else
+#elif LLVM_VERSION < 37
   TargetLibraryInfo* TLI = new TargetLibraryInfo();
 
   if (CallInst *CI = extractMallocCall(V, TLI)) {
@@ -248,6 +273,15 @@ Value *PointerTracking::computeAllocationCountValue(Value *P, constType *&Ty) co
     if (!Ty)
       return 0;
     Value *arraySize = getMallocArraySize(CI, TD, TLI);
+#else
+  TargetLibraryInfoImpl* TLII = new TargetLibraryInfoImpl();
+  TargetLibraryInfo* TLI = new TargetLibraryInfo(*TLII);
+
+  if (CallInst *CI = extractMallocCall(V, TLI)) {
+    Ty = getMallocAllocatedType(CI, TLI);
+    if (!Ty)
+      return 0;
+    Value *arraySize = getMallocArraySize(CI, *TD, TLI);
 #endif
     if (!arraySize) {
       Ty = Type::getInt8Ty(P->getContext());
@@ -351,7 +385,11 @@ void PointerTracking::getPointerOffset(Value *Pointer, Value *&Base,
                                        const SCEV *&Offset) const
 {
     Pointer = Pointer->stripPointerCasts();
+#if LLVM_VERSION < 37
     Base = GetUnderlyingObject(Pointer, TD);
+#else
+    Base = GetUnderlyingObject(Pointer, *TD);
+#endif
     Limit = getAllocationSizeInBytes(Base);
     if (isa<SCEVCouldNotCompute>(Limit)) {
       Base = 0;
diff --git a/libclamav/c++/bytecode2llvm.cpp b/libclamav/c++/bytecode2llvm.cpp
index e5aea03..2171993 100644
--- a/libclamav/c++/bytecode2llvm.cpp
+++ b/libclamav/c++/bytecode2llvm.cpp
@@ -63,7 +63,11 @@
 #include "llvm/Object/ObjectFile.h"
 #endif
 #include "llvm/ExecutionEngine/JITEventListener.h"
+#if LLVM_VERSION < 37
 #include "llvm/PassManager.h"
+#else
+#include "llvm/IR/LegacyPassManager.h"
+#endif
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/CommandLine.h"
@@ -231,7 +235,9 @@ namespace {
 #define llvm_report_error(x) report_fatal_error(x)
 #define llvm_install_error_handler(x) install_fatal_error_handler(x)
 #define DwarfExceptionHandling JITExceptionHandling
+#if LLVM_VERSION < 37
 #define SetCurrentDebugLocation(x) SetCurrentDebugLocation(DebugLoc::getFromDILocation(x))
+#endif
 #define DEFINEPASS(passname) passname() : FunctionPass(ID)
 #else
 #define DEFINEPASS(passname) passname() : FunctionPass(&ID)
@@ -718,7 +724,11 @@ class RuntimeLimits : public FunctionPass {
 	    BBMap[BB] = apicalls;
 	}
 	if (!BackedgeTargets.empty()) {
+#if LLVM_VERSION < 37
 	    LoopInfo &LI = getAnalysis<LoopInfo>();
+#else
+        LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+#endif
 	    ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
 
 	    // Now check whether any of these backedge targets are part of a loop
@@ -802,7 +812,11 @@ class RuntimeLimits : public FunctionPass {
 #endif
 	    // Load Flag that tells us we timed out (first byte in bc_ctx)
 	    Value *Cond = Builder.CreateLoad(Flag, true);
+#if LLVM_VERSION < 37
 	    BasicBlock *newBB = SplitBlock(BB, BB->getTerminator(), this);
+#else
+        BasicBlock *newBB = SplitBlock(BB, BB->getTerminator());
+#endif
 	    TerminatorInst *TI = BB->getTerminator();
 	    BranchInst::Create(AbrtBB, newBB, Cond, TI);
 	    TI->eraseFromParent();
@@ -823,7 +837,11 @@ class RuntimeLimits : public FunctionPass {
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.setPreservesAll();
+#if LLVM_VERSION < 37
       AU.addRequired<LoopInfo>();
+#else
+      AU.addRequired<LoopInfoWrapperPass>();
+#endif
       AU.addRequired<ScalarEvolution>();
 #if LLVM_VERSION < 35
       AU.addRequired<DominatorTree>();
@@ -916,7 +934,11 @@ class LLVMCodegen {
     Module *M;
     LLVMContext &Context;
     ExecutionEngine *EE;
+#if LLVM_VERSION < 37
     FunctionPassManager &PM, &PMUnsigned;
+#else
+    legacy::FunctionPassManager &PM, &PMUnsigned;
+#endif
     LLVMTypeMapper *TypeMap;
 
     Function **apiFuncs;
@@ -1089,7 +1111,11 @@ class LLVMCodegen {
 	  Constant *C = ConstantExpr::getPointerCast(GV, IP8Ty);
 	  //TODO: check constant bounds here
 	  return ConstantExpr::getPointerCast(
+#if LLVM_VERSION < 37
 	      ConstantExpr::getInBoundsGetElementPtr(C, ARRAYREF(Value*, idxs, 1)),
+#else
+	      ConstantExpr::getInBoundsGetElementPtr(Ty, C, ARRAYREF(Value*, idxs, 1)),
+#endif
 	      PTy);
         }
 	if (isa<IntegerType>(Ty)) {
@@ -1118,15 +1144,21 @@ class LLVMCodegen {
 
 public:
     LLVMCodegen(const struct cli_bc *bc, Module *M, struct CommonFunctions *CF, FunctionMapTy &cFuncs,
+#if LLVM_VERSION < 37
 		ExecutionEngine *EE, FunctionPassManager &PM, FunctionPassManager &PMUnsigned,
+#else
+        ExecutionEngine *EE, legacy::FunctionPassManager &PM, legacy::FunctionPassManager &PMUnsigned,
+#endif
 		Function **apiFuncs, LLVMTypeMapper &apiMap)
 	: bc(bc), M(M), Context(M->getContext()), EE(EE),
 	PM(PM),PMUnsigned(PMUnsigned), TypeMap(), apiFuncs(apiFuncs),apiMap(apiMap),
 	compiledFunctions(cFuncs), BytecodeID("bc"+Twine(bc->id)),
 #if LLVM_VERSION < 32
 	Folder(EE->getTargetData()), Builder(Context, Folder), Values(), CF(CF) {
-#else
+#elif LLVM_VERSION < 37
 	Folder(EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) {
+#else
+    Folder(*EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) {
 #endif
 
 	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
@@ -1141,7 +1173,17 @@ class LLVMCodegen {
     template <typename InputIterator>
 #endif
     Value* createGEP(Value *Base, constType *ETy, ARRAYREFPARAM(Value*,InputIterator Start,InputIterator End,ARef)) {
+#if LLVM_VERSION < 37
 	constType *Ty = GetElementPtrInst::getIndexedType(Base->getType(),ARRAYREFP(Start,End,ARef));
+#else
+    Type *Ty = NULL;
+    // This used to be done internally in LLVM's getIndexedTypeInternal.
+    PointerType *PTy = dyn_cast<PointerType>(Base->getType()->getScalarType());
+    if (PTy) {
+        Type *Agg = PTy->getElementType();
+        Ty = GetElementPtrInst::getIndexedType(Agg,ARRAYREFP(Start,End,ARef));
+    }
+#endif
 	if (!Ty || (ETy && (Ty != ETy && (!isa<IntegerType>(Ty) || !isa<IntegerType>(ETy))))) {
 	    if (cli_debug_flag) {
 		std::string str;
@@ -1457,7 +1499,11 @@ class LLVMCodegen {
 			if (func->dbgnodes[c] != ~0u) {
 			unsigned j = func->dbgnodes[c];
 			assert(j < mdnodes.size());
+#if LLVM_VERSION < 37
 			Builder.SetCurrentDebugLocation(mdnodes[j]);
+#else
+            Builder.SetCurrentDebugLocation(DebugLoc(mdnodes[j]));
+#endif
 			} else
 			    Builder.SetCurrentDebugLocation(0);
 		    }
@@ -1767,11 +1813,16 @@ class LLVMCodegen {
 #if LLVM_VERSION < 29
 			    CallInst *c = Builder.CreateCall4(CF->FMemset, Dst, Val, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1));
-#else
+#elif LLVM_VERSION < 37
 			    CallInst *c = Builder.CreateCall5(CF->FMemset, Dst, Val, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1),
 								ConstantInt::get(Type::getInt1Ty(Context), 0)
 								);
+#else
+                Value *args[] = { Dst, Val, Len,
+                                  ConstantInt::get(Type::getInt32Ty(Context), 1),
+                                  ConstantInt::get(Type::getInt1Ty(Context), 0)};
+                CallInst *c = Builder.CreateCall(CF->FMemset, ARRAYREF(Value*, args, args + 5));
 #endif
 			    c->setTailCall(true);
 			    c->setDoesNotThrow();
@@ -1788,11 +1839,16 @@ class LLVMCodegen {
 #if LLVM_VERSION < 29
 			    CallInst *c = Builder.CreateCall4(CF->FMemcpy, Dst, Src, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1));
-#else
+#elif LLVM_VERSION < 37
 			    CallInst *c = Builder.CreateCall5(CF->FMemcpy, Dst, Src, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1),
 								ConstantInt::get(Type::getInt1Ty(Context), 0)
 								);
+#else
+                Value *args[] = { Dst, Src, Len,
+                                  ConstantInt::get(Type::getInt32Ty(Context), 1),
+                                  ConstantInt::get(Type::getInt1Ty(Context), 0)};
+                CallInst *c = Builder.CreateCall(CF->FMemcpy, ARRAYREF(Value*, args, args + 5));
 #endif
 			    c->setTailCall(true);
 			    c->setDoesNotThrow();
@@ -1809,10 +1865,15 @@ class LLVMCodegen {
 #if LLVM_VERSION < 29
 			    CallInst *c = Builder.CreateCall4(CF->FMemmove, Dst, Src, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1));
-#else
+#elif LLVM_VERSION < 37
 			    CallInst *c = Builder.CreateCall5(CF->FMemmove, Dst, Src, Len,
 								ConstantInt::get(Type::getInt32Ty(Context), 1),
 								ConstantInt::get(Type::getInt1Ty(Context), 0));
+#else
+                Value *args[] = {Dst, Src, Len,
+                                 ConstantInt::get(Type::getInt32Ty(Context), 1),
+                                 ConstantInt::get(Type::getInt1Ty(Context), 0)};
+                CallInst *c = Builder.CreateCall(CF->FMemmove, args);
 #endif
 			    c->setTailCall(true);
 			    c->setDoesNotThrow();
@@ -1830,7 +1891,12 @@ class LLVMCodegen {
 #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);
+#else
+                Value *args[] = {Dst, Src, Len};
+                CallInst *c = Builder.CreateCall(CF->FRealmemcmp, ARRAYREF(Value*, args, args + 3));
+#endif
 			    c->setTailCall(true);
 			    c->setDoesNotThrow();
 			    Store(inst->dest, c);
@@ -2211,7 +2277,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
 }
 #if LLVM_VERSION >= 29
 INITIALIZE_PASS_BEGIN(RuntimeLimits, "rl", "Runtime Limits", false, false)
+#if LLVM_VERSION < 37
 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
+#else
+INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
+#endif
 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
 #if LLVM_VERSION < 35
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
@@ -2437,8 +2507,10 @@ static void addFPasses(FunctionPassManager &FPM, bool trusted, const TargetData
 static void addFPasses(FunctionPassManager &FPM, bool trusted, const DataLayout *TD)
 #elif LLVM_VERSION < 36
 static void addFPasses(FunctionPassManager &FPM, bool trusted, const Module *M)
-#else
+#elif LLVM_VERSION < 37
 static void addFPasses(FunctionPassManager &FPM, bool trusted, Module *M)
+#else
+static void addFPasses(legacy::FunctionPassManager &FPM, bool trusted, Module *M)
 #endif
 {
     // Set up the optimizer pipeline.  Start with registering info about how
@@ -2449,10 +2521,12 @@ static void addFPasses(FunctionPassManager &FPM, bool trusted, Module *M)
     FPM.add(new DataLayout(*TD));
 #elif LLVM_VERSION < 36
     FPM.add(new DataLayoutPass(M));
-#else
+#elif LLVM_VERSION < 37
     DataLayoutPass *DLP = new DataLayoutPass();
     DLP->doInitialization(*M);
     FPM.add(DLP);
+#else
+    // No DataLayout pass needed anymore.
 #endif
     // Promote allocas to registers.
     FPM.add(createPromoteMemoryToRegisterPass());
@@ -2482,6 +2556,8 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
 
 #if LLVM_VERSION >= 31
 	TargetOptions Options;
+#if LLVM_VERSION < 37
+    // This option was removed.
 #ifdef CL_DEBUG
 	//disable this for now, it leaks
 	Options.JITEmitDebugInfo = false;
@@ -2489,6 +2565,7 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
 #else
 	Options.JITEmitDebugInfo = false;
 #endif
+#endif
 #if LLVM_VERSION < 34
 	Options.DwarfExceptionHandling = false;
 #else
@@ -2525,7 +2602,11 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
 	struct CommonFunctions CF;
 	addFunctionProtos(&CF, EE, M);
 
+#if LLVM_VERSION < 37
 	FunctionPassManager OurFPM(M), OurFPMUnsigned(M);
+#else
+    legacy::FunctionPassManager OurFPM(M), OurFPMUnsigned(M);
+#endif
 #if LLVM_VERSION < 32
 	M->setDataLayout(EE->getTargetData()->getStringRepresentation());
 #else
@@ -2665,17 +2746,23 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
 		break;
 	    }
 	}
+#if LLVM_VERSION < 37
 	PassManager PM;
+#else
+	legacy::PassManager PM;
+#endif
 #if LLVM_VERSION < 32
 	PM.add(new TargetData(*EE->getTargetData()));
 #elif LLVM_VERSION < 35
 	PM.add(new DataLayout(*EE->getDataLayout()));
 #elif LLVM_VERSION < 36
 	PM.add(new DataLayoutPass(M));
-#else
+#elif LLVM_VERSION < 37
     DataLayoutPass *DLP = new DataLayoutPass();
     DLP->doInitialization(*M);
     PM.add(DLP);
+#else
+    // No DataLayout pass needed anymore.
 #endif
 	// TODO: only run this on the untrusted bytecodes, not all of them...
 	if (has_untrusted)
@@ -2987,11 +3074,19 @@ static Metadata *findDbgGlobalDeclare(GlobalVariable *V) {
     return 0;
 
   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
+#if LLVM_VERSION < 37
     DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
     if (!DIG.isGlobalVariable())
       continue;
     if (DIGlobalVariable(DIG).getGlobal() == V)
       return DIG;
+#else
+    MDNode *DIG = NMD->getOperand(i);
+    if (!DIGlobalVariable::classof(DIG))
+      continue;
+    if ((cast<DIGlobalVariable>(DIG))->getVariable() == V)
+      return DIG;
+#endif
   }
   return 0;
 }
@@ -3008,11 +3103,19 @@ static Metadata *findDbgSubprogramDeclare(Function *V) {
     return 0;
 
   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
+#if LLVM_VERSION < 37
     DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
     if (!DIG.isSubprogram())
       continue;
     if (DISubprogram(DIG).getFunction() == V)
       return DIG;
+#else
+    MDNode *DIG = NMD->getOperand(i);
+    if (!DISubprogram::classof(DIG))
+      continue;
+    if ((cast<DISubprogram>(DIG))->getFunction() == V)
+      return DIG;
+#endif
   }
   return 0;
 }
@@ -3061,22 +3164,39 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
     Metadata *DIGV = findDbgGlobalDeclare(GV);
 #endif
     if (!DIGV) return false;
+#if LLVM_VERSION < 37
     DIGlobalVariable Var(cast<MDNode>(DIGV));
+#else
+    DIGlobalVariable *Var = cast<DIGlobalVariable>(DIGV);
+#endif
 
+#if LLVM_VERSION < 37
     StringRef D = Var.getDisplayName();
+#else
+    StringRef D = Var->getDisplayName();
+#endif
     if (!D.empty())
       DisplayName = D;
+#if LLVM_VERSION < 37
     LineNo = Var.getLineNumber();
+#else
+    LineNo = Var->getLine();
+#endif
 #if LLVM_VERSION < 33
     Unit = Var.getCompileUnit();
-#else
+#elif LLVM_VERSION < 37
     G = Var.getFilename();
     H = Var.getDirectory();
+#else
+    G = Var->getFilename();
+    H = Var->getDirectory();
 #endif
 #if LLVM_VERSION < 35
     TypeD = Var.getType();
-#else
+#elif LLVM_VERSION < 37
     T = Var.getType().getName();
+#else
+    T = (cast<DIType>(*Var->getType())).getName();
 #endif
   } else if (Function *F = dyn_cast<Function>(const_cast<Value*>(V))){
 #if LLVM_VERSION < 36
@@ -3085,32 +3205,61 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
     Metadata *DIF = findDbgSubprogramDeclare(F);
 #endif
     if (!DIF) return false;
+#if LLVM_VERSION < 37
     DISubprogram Var(cast<MDNode>(DIF));
+#else
+    DISubprogram *Var = cast<DISubprogram>(DIF);
+#endif
 
+#if LLVM_VERSION < 37
     StringRef D = Var.getDisplayName();
+#else
+    StringRef D = Var->getDisplayName();
+#endif
     if (!D.empty())
       DisplayName = D;
+#if LLVM_VERSION < 37
     LineNo = Var.getLineNumber();
+#else
+    LineNo = Var->getLine();
+#endif
 #if LLVM_VERSION < 33
     Unit = Var.getCompileUnit();
-#else
+#elif LLVM_VERSION < 37
     G = Var.getFilename();
     H = Var.getDirectory();
+#else
+    G = Var->getFilename();
+    H = Var->getDirectory();
 #endif
 #if LLVM_VERSION < 35
     TypeD = Var.getType();
-#else
+#elif LLVM_VERSION < 37
     T = Var.getType().getName();
+#else
+    T = Var->getType()->getName();
 #endif
   } else {
     const DbgDeclareInst *DDI = findDbgDeclare(V);
     if (!DDI) return false;
+#if LLVM_VERSION < 37
     DIVariable Var(cast<MDNode>(DDI->getVariable()));
+#else
+    DIVariable* Var = DDI->getVariable();
+#endif
 
+#if LLVM_VERSION < 37
     StringRef D = Var.getName();
+#else
+    StringRef D = Var->getName();
+#endif
     if (!D.empty())
       DisplayName = D;
+#if LLVM_VERSION < 37
     LineNo = Var.getLineNumber();
+#else
+    LineNo = Var->getLine();
+#endif
 #if LLVM_VERSION < 33
     Unit = Var.getCompileUnit();
 #else
@@ -3120,8 +3269,10 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
 #endif
 #if LLVM_VERSION < 35
     TypeD = Var.getType();
-#else
+#elif LLVM_VERSION < 37
     T = Var.getType().getName();
+#else
+    T = (cast<DIType>(*Var->getType())).getName();
 #endif
   }
 
@@ -3157,9 +3308,15 @@ void printValue(llvm::Value *V, bool a, bool b) {
 
 void printLocation(llvm::Instruction *I, bool a, bool b) {
     if (MDNode *N = I->getMetadata("dbg")) {
+#if LLVM_VERSION < 37
 	DILocation Loc(N);
 	errs() << Loc.getFilename() << ":" << Loc.getLineNumber();
 	if (unsigned Col = Loc.getColumnNumber()) {
+#else
+    DebugLoc Loc(N);
+    errs() << Loc.get()->getFilename() << ":" << Loc.getLine();
+    if (unsigned Col = Loc.getCol()) {
+#endif
   	    errs() << ":" << Col;
   	}
   	errs() << ": ";
diff --git a/libclamav/c++/m4/llvm-flags.m4 b/libclamav/c++/m4/llvm-flags.m4
index dce4e10..04d6833 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 370; then
+elif test $llvmver_test -lt 380; 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.7 required, but "$llvmver"($llvmver_test) found])
+    AC_MSG_ERROR([LLVM < 3.8 required, but "$llvmver"($llvmver_test) found])
 fi
 
 dnl aquire the required flags to properly link in external LLVM