summarylogtreecommitdiffstats
path: root/clangfix.patch
blob: 858556d288c3474c8033ce974d46e9592cdcd9ef (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
From e529586d062450007acf942fc7c73269db5a0fbe Mon Sep 17 00:00:00 2001
From: Pan Xiuli <xiuli.pan@intel.com>
Date: Wed, 27 Jan 2016 14:14:59 +0800
Subject: Backend: Add support for LLVM release 3.8

This is support for llvm releae 3.8

V2:add specific comment about bitcode library link about change in
LLVM3.8 of materialized check.

Signed-off-by: Pan Xiuli <xiuli.pan@intel.com>
Reviewed-by: Ruiling Song <ruiling.song@intel.com>

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index e3c2895..099116c 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -450,7 +450,11 @@ namespace gbe {
     using namespace gbe;
     char* errMsg;
     if(((GenProgram*)dst_program)->module == NULL){
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+      ((GenProgram*)dst_program)->module = llvm::CloneModule((llvm::Module*)((GenProgram*)src_program)->module).release();
+#else
       ((GenProgram*)dst_program)->module = llvm::CloneModule((llvm::Module*)((GenProgram*)src_program)->module);
+#endif
       errSize = 0;
     }else{
       llvm::Module* src = (llvm::Module*)((GenProgram*)src_program)->module;
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index 7d12f73..2b44025 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -124,7 +124,11 @@ namespace gbe {
     llvm::Module * cloned_module = NULL;
     bool ret = true;
     if(module){
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+      cloned_module = llvm::CloneModule((llvm::Module*)module).release();
+#else
       cloned_module = llvm::CloneModule((llvm::Module*)module);
+#endif
     }
     bool strictMath = true;
     if (fast_relaxed_math || !OCL_STRICT_CONFORMANCE)
diff --git a/backend/src/llvm/ExpandConstantExpr.cpp b/backend/src/llvm/ExpandConstantExpr.cpp
index c6f57b8..e9ec3ab 100644
--- a/backend/src/llvm/ExpandConstantExpr.cpp
+++ b/backend/src/llvm/ExpandConstantExpr.cpp
@@ -115,7 +115,7 @@ static Value *expandConstantVector(Instruction *InsertPt, ConstantVector *CV) {
   Type *IntTy = IntegerType::get(CV->getContext(), 32);
 
   BasicBlock::iterator InsertPos(InsertPt);
-  IRBuilder<> IRB(InsertPos);
+  IRBuilder<> IRB(&*InsertPos);
   Value *vec = UndefValue::get(CV->getType());
   for (int i = 0; i < elemNum; i++) {
     Value *idx = ConstantInt::get(IntTy, i);
@@ -177,7 +177,7 @@ bool ExpandConstantExpr::runOnFunction(Function &Func) {
     for (BasicBlock::InstListType::iterator Inst = BB->begin(), E = BB->end();
          Inst != E;
          ++Inst) {
-      Modified |= expandInstruction(Inst);
+      Modified |= expandInstruction(&*Inst);
     }
   }
   return Modified;
diff --git a/backend/src/llvm/ExpandLargeIntegers.cpp b/backend/src/llvm/ExpandLargeIntegers.cpp
index 20fdda9..00987cb 100644
--- a/backend/src/llvm/ExpandLargeIntegers.cpp
+++ b/backend/src/llvm/ExpandLargeIntegers.cpp
@@ -388,7 +388,7 @@ static void convertInstruction(Instruction *Inst, ConversionState &State,
   // Set the insert point *after* Inst, so that any instructions inserted here
   // will be visited again. That allows iterative expansion of types > i128.
   BasicBlock::iterator InsertPos(Inst);
-  IRBuilder<> IRB(++InsertPos);
+  IRBuilder<> IRB(&*++InsertPos);
   StringRef Name = Inst->getName();
 
   if (PHINode *Phi = dyn_cast<PHINode>(Inst)) {
diff --git a/backend/src/llvm/ExpandUtils.cpp b/backend/src/llvm/ExpandUtils.cpp
index 801f969..a09d990 100644
--- a/backend/src/llvm/ExpandUtils.cpp
+++ b/backend/src/llvm/ExpandUtils.cpp
@@ -101,7 +101,7 @@ namespace llvm {
   Function *RecreateFunction(Function *Func, FunctionType *NewType) {
     Function *NewFunc = Function::Create(NewType, Func->getLinkage());
     NewFunc->copyAttributesFrom(Func);
-    Func->getParent()->getFunctionList().insert(Func, NewFunc);
+    Func->getParent()->getFunctionList().insert(ilist_iterator<Function>(Func), NewFunc);
     NewFunc->takeName(Func);
     NewFunc->getBasicBlockList().splice(NewFunc->begin(),
                                         Func->getBasicBlockList());
diff --git a/backend/src/llvm/PromoteIntegers.cpp b/backend/src/llvm/PromoteIntegers.cpp
index b65440f..8759287 100644
--- a/backend/src/llvm/PromoteIntegers.cpp
+++ b/backend/src/llvm/PromoteIntegers.cpp
@@ -615,7 +615,7 @@ bool PromoteIntegers::runOnFunction(Function &F) {
   // Don't support changing the function arguments. This should not be
   // generated by clang.
   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
-    Value *Arg = I;
+    Value *Arg = &*I;
     if (shouldConvert(Arg)) {
       errs() << "Function " << F.getName() << ": " << *Arg << "\n";
       llvm_unreachable("Function has illegal integer/pointer argument");
@@ -626,7 +626,7 @@ bool PromoteIntegers::runOnFunction(Function &F) {
   bool Modified = false;
   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
     for (BasicBlock::iterator BBI = FI->begin(), BBE = FI->end(); BBI != BBE;) {
-      Instruction *Inst = BBI++;
+      Instruction *Inst = &*BBI++;
       // Only attempt to convert an instruction if its result or any of its
       // operands are illegal.
       bool ShouldConvert = shouldConvert(Inst);
diff --git a/backend/src/llvm/StripAttributes.cpp b/backend/src/llvm/StripAttributes.cpp
index e6df312..3bf3853 100644
--- a/backend/src/llvm/StripAttributes.cpp
+++ b/backend/src/llvm/StripAttributes.cpp
@@ -98,7 +98,7 @@ bool StripAttributes::runOnFunction(Function &Func) {
        BB != E; ++BB) {
     for (BasicBlock::iterator Inst = BB->begin(), E = BB->end();
          Inst != E; ++Inst) {
-      CallSite Call(Inst);
+      CallSite Call(&*Inst);
       if (Call)
         Call.setCallingConv(CallingConv::C);
     }
diff --git a/backend/src/llvm/llvm_bitcode_link.cpp b/backend/src/llvm/llvm_bitcode_link.cpp
index 56205bb..10fccf6 100644
--- a/backend/src/llvm/llvm_bitcode_link.cpp
+++ b/backend/src/llvm/llvm_bitcode_link.cpp
@@ -68,8 +68,9 @@ namespace gbe
     return oclLib;
   }
 
-  static bool materializedFuncCall(Module& src, Module& lib, llvm::Function &KF, std::set<std::string>& MFS)
-  {
+  static bool materializedFuncCall(Module& src, Module& lib, llvm::Function& KF,
+                                   std::set<std::string>& MFS,
+                                   std::vector<GlobalValue *>&Gvs) {
     bool fromSrc = false;
     for (llvm::Function::iterator B = KF.begin(), BE = KF.end(); B != BE; B++) {
       for (BasicBlock::iterator instI = B->begin(),
@@ -112,9 +113,10 @@ namespace gbe
             printf("Can not materialize the function: %s, because %s\n", fnName.c_str(), EC.message().c_str());
             return false;
           }
+          Gvs.push_back((GlobalValue *)newMF);
 #endif
         }
-        if (!materializedFuncCall(src, lib, *newMF, MFS))
+        if (!materializedFuncCall(src, lib, *newMF, MFS, Gvs))
           return false;
 
       }
@@ -128,6 +130,7 @@ namespace gbe
   {
     LLVMContext& ctx = mod->getContext();
     std::set<std::string> materializedFuncs;
+    std::vector<GlobalValue *> Gvs;
     Module* clonedLib = createOclBitCodeModule(ctx, strictMath);
     assert(clonedLib && "Can not create the beignet bitcode\n");
 
@@ -173,10 +176,11 @@ namespace gbe
       if (!isKernelFunction(*SF)) continue;
       kernels.push_back(SF->getName().data());
 
-      if (!materializedFuncCall(*mod, *clonedLib, *SF, materializedFuncs)) {
+      if (!materializedFuncCall(*mod, *clonedLib, *SF, materializedFuncs, Gvs)) {
         delete clonedLib;
         return NULL;
       }
+      Gvs.push_back((GlobalValue *)&*SF);
     }
 
     if (kernels.empty()) {
@@ -215,14 +219,43 @@ namespace gbe
       }
 #endif
 
-      if (!materializedFuncCall(*mod, *clonedLib, *newMF, materializedFuncs)) {
+      if (!materializedFuncCall(*mod, *clonedLib, *newMF, materializedFuncs, Gvs)) {
         delete clonedLib;
         return NULL;
       }
 
+      Gvs.push_back((GlobalValue *)newMF);
       kernels.push_back(f);
     }
 
+  /* The llvm 3.8 now has a strict materialized check for all value by checking
+   * module is materialized. If we want to use library as old style that just
+   * materialize what we need, we need to remove what we did not need before
+   * materialize all of the module. To do this, we need all of the builtin
+   * funcitons and what are needed from the kernel functions, these functions
+   * are materalized and are recorded in Gvs, the GlobalValue like PI are also
+   * needed and are added. Now we could not use use_empty to check if the GVs
+   * are needed before the module is marked as all materialized, so we just
+   * materialize all of them as there are only 7 GVs. Then we use GVExtraction
+   * pass to extract the functions and values in Gvs from the library module.
+   * After extract what we need and remove what we do not need, we use 
+   * materializeAll to mark the module as materialized. */
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >=8
+    /* Get all GlobalValue from module. */
+    Module::GlobalListType &GVlist = clonedLib->getGlobalList();
+    for(Module::global_iterator GVitr = GVlist.begin();GVitr != GVlist.end();++GVitr) {
+      GlobalValue * GV = &*GVitr;
+      clonedLib->materialize(GV);
+      Gvs.push_back(GV);
+    }
+    llvm::legacy::PassManager Extract;
+    /* Extract all values we need using GVExtractionPass. */
+    Extract.add(createGVExtractionPass(Gvs, false));
+    Extract.run(*clonedLib);
+    /* Mark the library module as materialized for later use. */
+    clonedLib->materializeAll();
+#endif
+
     /* the SPIR binary datalayout maybe different with beignet's bitcode */
     if(clonedLib->getDataLayout() != mod->getDataLayout())
       mod->setDataLayout(clonedLib->getDataLayout());
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index 886b6f3..1a2bbcd 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -1226,7 +1226,7 @@ namespace gbe
       }
       bool isImage = llvmInfo.isImageType();
       if (I->getType()->isPointerTy() || isImage) {
-        BtiMap.insert(std::make_pair(I, getNewBti(I, isImage)));
+        BtiMap.insert(std::make_pair(&*I, getNewBti(&*I, isImage)));
       }
     }
 
@@ -1340,7 +1340,7 @@ namespace gbe
     // function argument
     for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
       if (I->getType()->isPointerTy()) {
-        findPointerEscape(I, mixedPtr, true, revisit);
+        findPointerEscape(&*I, mixedPtr, true, revisit);
       }
     }
     // alloca
@@ -1389,7 +1389,7 @@ namespace gbe
       while (isa<AllocaInst>(bbIter)) ++bbIter;
 
       IRBuilder<> Builder(&entry);
-      Builder.SetInsertPoint(bbIter);
+      Builder.SetInsertPoint(&*bbIter);
 
       PointerType * AITy = cast<AllocaInst>(base)->getType();
       Value * btiArray = Builder.CreateAlloca(AITy->getElementType(), ArraySize, base->getName() + ".bti");
@@ -1814,7 +1814,7 @@ namespace gbe
   }
 
   void GenWriter::simplifyTerminator(BasicBlock *bb) {
-    Value *value = --bb->end();
+    Value *value = bb->getTerminator();
     BranchInst *I = NULL;
     if ((I = dyn_cast<BranchInst>(value)) != NULL) {
       if (I->isConditional() == false)
@@ -2097,12 +2097,12 @@ namespace gbe
         }
 
         // function arguments are uniform values.
-        this->newRegister(I, NULL, true);
+        this->newRegister(&*I, NULL, true);
 
         // add support for vector argument.
         if(type->isVectorTy()) {
           VectorType *vectorType = cast<VectorType>(type);
-          ir::Register reg = getRegister(I, 0);
+          ir::Register reg = getRegister(&*I, 0);
           Type *elemType = vectorType->getElementType();
           const uint32_t elemSize = getTypeByteSize(unit, elemType);
           const uint32_t elemNum = vectorType->getNumElements();
@@ -2112,7 +2112,7 @@ namespace gbe
           ir::Function& fn = ctx.getFunction();
           for(uint32_t i=1; i < elemNum; i++) {
             ir::PushLocation argLocation(fn, argID, elemSize*i);
-            reg = getRegister(I, i);
+            reg = getRegister(&*I, i);
             ctx.appendPushedConstant(reg, argLocation);  //add to push map for reg alloc
           }
           continue;
@@ -2120,10 +2120,10 @@ namespace gbe
 
         GBE_ASSERTM(isScalarType(type) == true,
                     "vector type in the function argument is not supported yet");
-        const ir::Register reg = getRegister(I);
+        const ir::Register reg = getRegister(&*I);
         if (llvmInfo.isImageType()) {
           ctx.input(argName, ir::FunctionArgument::IMAGE, reg, llvmInfo, 4, 4, 0);
-          ctx.getFunction().getImageSet()->append(reg, &ctx, BtiMap.find(I)->second);
+          ctx.getFunction().getImageSet()->append(reg, &ctx, BtiMap.find(&*I)->second);
           collectImageArgs(llvmInfo.accessQual, imageArgsInfo);
           continue;
         }
@@ -2156,7 +2156,7 @@ namespace gbe
             const uint32_t align = getAlignmentByte(unit, pointed);
               switch (addrSpace) {
               case ir::MEM_GLOBAL:
-                ctx.input(argName, ir::FunctionArgument::GLOBAL_POINTER, reg, llvmInfo, ptrSize, align, BtiMap.find(I)->second);
+                ctx.input(argName, ir::FunctionArgument::GLOBAL_POINTER, reg, llvmInfo, ptrSize, align, BtiMap.find(&*I)->second);
               break;
               case ir::MEM_LOCAL:
                 ctx.input(argName, ir::FunctionArgument::LOCAL_POINTER, reg,  llvmInfo, ptrSize, align, BTI_LOCAL);
@@ -2934,12 +2934,12 @@ namespace gbe
 
     // First create all the labels (one per block) ...
     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
-      this->newLabelIndex(BB);
+      this->newLabelIndex(&*BB);
 
     // Then, for all branch instructions that have conditions, see if we can
     // simplify the code by inverting condition code
     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
-      this->simplifyTerminator(BB);
+      this->simplifyTerminator(&*BB);
 
     // gather loop info, which is useful for liveness analysis
     gatherLoopInfo(fn);
@@ -2947,7 +2947,7 @@ namespace gbe
     // ... then, emit the instructions for all basic blocks
     pass = PASS_EMIT_INSTRUCTIONS;
     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
-      emitBasicBlock(BB);
+      emitBasicBlock(&*BB);
     ctx.endFunction();
 
     // Liveness can be shared when we optimized the immediates and the MOVs
@@ -3477,7 +3477,7 @@ namespace gbe
     Value *Callee = I.getCalledValue();
     GBE_ASSERT(ctx.getFunction().getProfile() == ir::PROFILE_OCL);
     GBE_ASSERT(isa<InlineAsm>(I.getCalledValue()) == false);
-    GBE_ASSERT(I.hasStructRetAttr() == false);
+    if(I.getNumArgOperands()) GBE_ASSERT(I.hasStructRetAttr() == false);
 
     // We only support a small number of intrinsics right now
     if (Function *F = I.getCalledFunction()) {
diff --git a/backend/src/llvm/llvm_includes.hpp b/backend/src/llvm/llvm_includes.hpp
index fed3a18..d2deb90 100644
--- a/backend/src/llvm/llvm_includes.hpp
+++ b/backend/src/llvm/llvm_includes.hpp
@@ -122,4 +122,9 @@
 
 #include <clang/CodeGen/CodeGenAction.h>
 
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >=8
+#include "llvm/Analysis/BasicAliasAnalysis.h"
+#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
+#endif
+
 #endif /* __GBE_IR_LLVM_INCLUDES_HPP__ */
diff --git a/backend/src/llvm/llvm_intrinsic_lowering.cpp b/backend/src/llvm/llvm_intrinsic_lowering.cpp
index b35d1e6..c26e96a 100644
--- a/backend/src/llvm/llvm_intrinsic_lowering.cpp
+++ b/backend/src/llvm/llvm_intrinsic_lowering.cpp
@@ -73,7 +73,7 @@ namespace gbe {
         Constant* FCache = M->getOrInsertFunction(NewFn,
                                         FunctionType::get(RetTy, ParamTys, false));
 
-        IRBuilder<> Builder(CI->getParent(), CI);
+        IRBuilder<> Builder(CI->getParent(), BasicBlock::iterator(CI));
         SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
         CallInst *NewCI = Builder.CreateCall(FCache, Args);
         NewCI->setName(CI->getName());
@@ -90,12 +90,12 @@ namespace gbe {
         DataLayout TD(M);
         LLVMContext &Context = BB.getContext();
         for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
-          Instruction *Inst = DI++;
+          Instruction *Inst = &*DI++;
           CallInst* CI = dyn_cast<CallInst>(Inst);
           if(CI == NULL)
             continue;
 
-          IRBuilder<> Builder(&BB, CI);
+          IRBuilder<> Builder(&BB, BasicBlock::iterator(CI));
           // only support memcpy and memset
           if (Function *F = CI->getCalledFunction()) {
             const Intrinsic::ID intrinsicID = (Intrinsic::ID) F->getIntrinsicID();
diff --git a/backend/src/llvm/llvm_loadstore_optimization.cpp b/backend/src/llvm/llvm_loadstore_optimization.cpp
index 698fdc2..121f53d 100644
--- a/backend/src/llvm/llvm_loadstore_optimization.cpp
+++ b/backend/src/llvm/llvm_loadstore_optimization.cpp
@@ -35,13 +35,22 @@ namespace gbe {
     GenLoadStoreOptimization() : BasicBlockPass(ID) {}
 
     void getAnalysisUsage(AnalysisUsage &AU) const {
+#if LLVM_VERSION_MAJOR == 3 &&  LLVM_VERSION_MINOR >= 8
+      AU.addRequired<ScalarEvolutionWrapperPass>();
+      AU.addPreserved<ScalarEvolutionWrapperPass>();
+#else
       AU.addRequired<ScalarEvolution>();
       AU.addPreserved<ScalarEvolution>();
+#endif
       AU.setPreservesCFG();
     }
 
     virtual bool runOnBasicBlock(BasicBlock &BB) {
+#if LLVM_VERSION_MAJOR == 3 &&  LLVM_VERSION_MINOR >= 8
+      SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
+#else
       SE = &getAnalysis<ScalarEvolution>();
+#endif
       #if LLVM_VERSION_MINOR >= 7
         TD = &BB.getModule()->getDataLayout();
       #elif LLVM_VERSION_MINOR >= 5
@@ -159,9 +168,9 @@ namespace gbe {
                             bool isLoad) {
 
     BasicBlock::iterator stepForward = start;
-    if(!isSimpleLoadStore(start)) return stepForward;
+    if(!isSimpleLoadStore(&*start)) return stepForward;
 
-    merged.push_back(start);
+    merged.push_back(&*start);
 
     BasicBlock::iterator E = BB.end();
     BasicBlock::iterator J = ++start;
@@ -170,8 +179,8 @@ namespace gbe {
 
     for(unsigned ss = 0; J != E && ss <= maxLimit; ++ss, ++J) {
       if((isLoad && isa<LoadInst>(*J)) || (!isLoad && isa<StoreInst>(*J))) {
-        if(isLoadStoreCompatible(merged[merged.size()-1], J)) {
-          merged.push_back(J);
+        if(isLoadStoreCompatible(merged[merged.size()-1], &*J)) {
+          merged.push_back(&*J);
           stepForward = ++J;
         }
       } else if((isLoad && isa<StoreInst>(*J)) || (!isLoad && isa<LoadInst>(*J))) {
@@ -217,7 +226,7 @@ namespace gbe {
     for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E;++BBI) {
       if(isa<LoadInst>(*BBI) || isa<StoreInst>(*BBI)) {
         bool isLoad = isa<LoadInst>(*BBI) ? true: false;
-        Type *ty = getValueType(BBI);
+        Type *ty = getValueType(&*BBI);
         if(ty->isVectorTy()) continue;
         // TODO Support DWORD/WORD/BYTE LOAD for store support DWORD only now.
         if (!(ty->isFloatTy() || ty->isIntegerTy(32) ||
diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp
index 422f16b..2bf3d04 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -618,7 +618,7 @@ error:
       Value* op0 = NULL;
       Value* val = NULL;
 
-      builder->SetInsertPoint(F.begin()->begin());// Insert the common var in the begin.
+      builder->SetInsertPoint(&*(F.begin()->begin()));// Insert the common var in the begin.
 
       /* FIXME: Because the OpenCL language do not support va macro, and we do not want
          to introduce the va_list, va_start and va_end into our code, we just simulate
diff --git a/backend/src/llvm/llvm_profiling.cpp b/backend/src/llvm/llvm_profiling.cpp
index 211aa43..96c95ee 100644
--- a/backend/src/llvm/llvm_profiling.cpp
+++ b/backend/src/llvm/llvm_profiling.cpp
@@ -135,7 +135,7 @@ namespace gbe
     builder = new IRBuilder<>(module->getContext());
 
     /* alloc a new buffer ptr to collect the timestamps. */
-    builder->SetInsertPoint(F.begin()->begin());
+    builder->SetInsertPoint(&*F.begin()->begin());
     llvm::Constant *profilingBuf = module->getGlobalVariable("__gen_ocl_profiling_buf");
     if (!profilingBuf) {
       profilingBuf = new GlobalVariable(*module, intTy, false,
@@ -174,7 +174,7 @@ namespace gbe
         continue;
 
       // Insert the first one at beginning of not PHI.
-      builder->SetInsertPoint(instI);
+      builder->SetInsertPoint(&*instI);
       /* Add the timestamp store function call. */
       // __gen_ocl_store_timestamp(int nth, int type);
       Value *Args[2] = {ConstantInt::get(intTy, pointNum++), ConstantInt::get(intTy, profilingType)};
@@ -190,7 +190,7 @@ namespace gbe
     BE--;
     BasicBlock::iterator retInst = BE->end();
     retInst--;
-    builder->SetInsertPoint(retInst);
+    builder->SetInsertPoint(&*retInst);
     Value *Args2[2] = {profilingBuf, ConstantInt::get(intTy, profilingType)};
 
     builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
diff --git a/backend/src/llvm/llvm_scalarize.cpp b/backend/src/llvm/llvm_scalarize.cpp
index 899a696..9efb897 100644
--- a/backend/src/llvm/llvm_scalarize.cpp
+++ b/backend/src/llvm/llvm_scalarize.cpp
@@ -197,7 +197,7 @@ namespace gbe {
     /* set to insert new instructions after the specified instruction.*/
     void setAppendPoint(Instruction *insn)  {
       BasicBlock::iterator next(insn);
-      builder->SetInsertPoint(++next);
+      builder->SetInsertPoint(&*++next);
     }
 
     DenseMap<Value*, VectorValues> vectorVals;
@@ -740,7 +740,7 @@ namespace gbe {
           while (isa<AllocaInst>(bbIter)) ++bbIter;
 
           IRBuilder<> allocBuilder(&entry);
-          allocBuilder.SetInsertPoint(bbIter);
+          allocBuilder.SetInsertPoint(&*bbIter);
 
           Alloc = allocBuilder.CreateAlloca(fooTy, nullptr, "");
           for (int i = 0; i < GetComponentCount(foo); ++i)
@@ -801,7 +801,7 @@ namespace gbe {
       return;
     ReversePostOrderTraversal<Function*> rpot(&F);
     BasicBlock::iterator instI = (*rpot.begin())->begin();
-    builder->SetInsertPoint(instI);
+    builder->SetInsertPoint(&*instI);
 
     Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
 
@@ -809,7 +809,7 @@ namespace gbe {
       Type *type = I->getType();
 
       if(type->isVectorTy())
-        extractFromVector(I);
+        extractFromVector(&*I);
     }
     return;
   }
@@ -846,11 +846,11 @@ namespace gbe {
     RPOTType rpot(&F);
     for (RPOTType::rpo_iterator bbI = rpot.begin(), bbE = rpot.end(); bbI != bbE; ++bbI) {
       for (BasicBlock::iterator instI = (*bbI)->begin(), instE = (*bbI)->end(); instI != instE; ++instI) {
-        bool scalarized = scalarize(instI);
+        bool scalarized = scalarize(&*instI);
         if (scalarized) {
           changed = true;
           // TODO: uncomment when done
-          deadList.push_back(instI);
+          deadList.push_back(&*instI);
         }
       }
     }
diff --git a/backend/src/llvm/llvm_to_gen.cpp b/backend/src/llvm/llvm_to_gen.cpp
index b8ab1dd..653fc09 100644
--- a/backend/src/llvm/llvm_to_gen.cpp
+++ b/backend/src/llvm/llvm_to_gen.cpp
@@ -45,7 +45,6 @@ namespace gbe
   using namespace llvm;
 
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
-  using namespace llvm::legacy;
   #define TARGETLIBRARY  TargetLibraryInfoImpl
 #else
   #define TARGETLIBRARY  TargetLibraryInfo
@@ -53,7 +52,11 @@ namespace gbe
 
   void runFuntionPass(Module &mod, TARGETLIBRARY *libraryInfo, const DataLayout &DL)
   {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
+    legacy::FunctionPassManager FPM(&mod);
+#else
     FunctionPassManager FPM(&mod);
+#endif
 
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
 #elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
@@ -74,8 +77,13 @@ namespace gbe
 #else
     FPM.add(new TargetLibraryInfo(*libraryInfo));
 #endif
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+    FPM.add(createTypeBasedAAWrapperPass());
+    FPM.add(createBasicAAWrapperPass());
+#else
     FPM.add(createTypeBasedAliasAnalysisPass());
     FPM.add(createBasicAliasAnalysisPass());
+#endif
     FPM.add(createCFGSimplificationPass());
     FPM.add(createSROAPass());
     FPM.add(createEarlyCSEPass());
@@ -91,7 +99,11 @@ namespace gbe
 
   void runModulePass(Module &mod, TARGETLIBRARY *libraryInfo, const DataLayout &DL, int optLevel, bool strictMath)
   {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
+    legacy::PassManager MPM;
+#else
     PassManager MPM;
+#endif
 
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
 #elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
@@ -107,8 +119,13 @@ namespace gbe
 #else
     MPM.add(new TargetLibraryInfo(*libraryInfo));
 #endif
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+    MPM.add(createTypeBasedAAWrapperPass());
+    MPM.add(createBasicAAWrapperPass());
+#else
     MPM.add(createTypeBasedAliasAnalysisPass());
     MPM.add(createBasicAliasAnalysisPass());
+#endif
     MPM.add(createIntrinsicLoweringPass());
     MPM.add(createStripAttributesPass());     // Strip unsupported attributes and calling conventions.
     MPM.add(createSamplerFixPass());
@@ -123,11 +140,19 @@ namespace gbe
     MPM.add(createBarrierNodupPass(false));   // remove noduplicate fnAttr before inlining.
     MPM.add(createFunctionInliningPass(20000));
     MPM.add(createBarrierNodupPass(true));    // restore noduplicate fnAttr after inlining.
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+    MPM.add(createPostOrderFunctionAttrsPass());       // Set readonly/readnone attrs
+#else
     MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
+#endif
 
     //MPM.add(createScalarReplAggregatesPass(64, true, -1, -1, 64))
     if(optLevel > 0)
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+      MPM.add(createSROAPass());
+#else
       MPM.add(createSROAPass(/*RequiresDomTree*/ false));
+#endif
     MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
     MPM.add(createJumpThreadingPass());         // Thread jumps.
     MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
@@ -146,7 +171,11 @@ namespace gbe
     MPM.add(createLoopDeletionPass());          // Delete dead loops
     MPM.add(createLoopUnrollPass(640)); //1024, 32, 1024, 512)); //Unroll loops
     if(optLevel > 0) {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+      MPM.add(createSROAPass());
+#else
       MPM.add(createSROAPass(/*RequiresDomTree*/ false));
+#endif
       MPM.add(createGVNPass());                 // Remove redundancies
     }
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
@@ -157,7 +186,11 @@ namespace gbe
       MPM.add(createCustomLoopUnrollPass()); //1024, 32, 1024, 512)); //Unroll loops
       MPM.add(createLoopUnrollPass()); //1024, 32, 1024, 512)); //Unroll loops
       if(optLevel > 0) {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 8
+        MPM.add(createSROAPass());
+#else
         MPM.add(createSROAPass(/*RequiresDomTree*/ false));
+#endif
         MPM.add(createGVNPass());                 // Remove redundancies
       }
     }
@@ -184,7 +217,15 @@ namespace gbe
   }
 
 
-#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
+#define OUTPUT_BITCODE(STAGE, MOD)  do {         \
+  legacy::PassManager passes__;           \
+   if (OCL_OUTPUT_LLVM_##STAGE) {                \
+     passes__.add(createPrintModulePass(*o));    \
+     passes__.run(MOD);                          \
+   }                                             \
+ }while(0)
+#elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
 #define OUTPUT_BITCODE(STAGE, MOD)  do {         \
    PassManager passes__;           \
    if (OCL_OUTPUT_LLVM_##STAGE) {                \
@@ -256,7 +297,11 @@ namespace gbe
 
     runFuntionPass(mod, libraryInfo, DL);
     runModulePass(mod, libraryInfo, DL, optLevel, strictMath);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
+    legacy::PassManager passes;
+#else
     PassManager passes;
+#endif
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
 #elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
     passes.add(new DataLayoutPass());
diff --git a/backend/src/llvm/llvm_unroll.cpp b/backend/src/llvm/llvm_unroll.cpp
index 6990e39..0f62bdc 100644
--- a/backend/src/llvm/llvm_unroll.cpp
+++ b/backend/src/llvm/llvm_unroll.cpp
@@ -47,8 +47,13 @@ namespace gbe {
         AU.addPreservedID(LoopSimplifyID);
         AU.addRequiredID(LCSSAID);
         AU.addPreservedID(LCSSAID);
+#if LLVM_VERSION_MAJOR == 3 &&  LLVM_VERSION_MINOR >= 8
+        AU.addRequired<ScalarEvolutionWrapperPass>();
+        AU.addPreserved<ScalarEvolutionWrapperPass>();
+#else
         AU.addRequired<ScalarEvolution>();
         AU.addPreserved<ScalarEvolution>();
+#endif
       // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info.
       // If loop unroll does not preserve dom info then LCSSA pass on next
       // loop will receive invalid dom info.
@@ -156,7 +161,12 @@ namespace gbe {
       // be unrolled.
       bool handleParentLoops(Loop *L, LPPassManager &LPM) {
         Loop *currL = L;
+#if LLVM_VERSION_MAJOR == 3 &&  LLVM_VERSION_MINOR >= 8
+        ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
+        LoopInfo &loopInfo = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+#else
         ScalarEvolution *SE = &getAnalysis<ScalarEvolution>();
+#endif
         BasicBlock *ExitBlock = currL->getLoopLatch();
         if (!ExitBlock || !L->isLoopExiting(ExitBlock))
           ExitBlock = currL->getExitingBlock();
@@ -183,7 +193,11 @@ namespace gbe {
               shouldUnroll = false;
             setUnrollID(currL, false);
             if (currL != L)
+#if LLVM_VERSION_MAJOR == 3 &&  LLVM_VERSION_MINOR >= 8
+              loopInfo.markAsRemoved(currL);
+#else
               LPM.deleteLoopFromQueue(currL);
+#endif
           }
           currL = parentL;
           currTripCount = parentTripCount;
-- 
cgit v0.10.2