3131
3232using namespace llvm ;
3333
34- namespace {
35- #if JL_LLVM_VERSION < 50000
36- static void appendToUsedList (Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
37- GlobalVariable *GV = M.getGlobalVariable (Name);
38- SmallPtrSet<Constant *, 16 > InitAsSet;
39- SmallVector<Constant *, 16 > Init;
40- if (GV) {
41- ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer ());
42- for (auto &Op : CA->operands ()) {
43- Constant *C = cast_or_null<Constant>(Op);
44- if (InitAsSet.insert (C).second )
45- Init.push_back (C);
46- }
47- GV->eraseFromParent ();
48- }
49-
50- Type *Int8PtrTy = llvm::Type::getInt8PtrTy (M.getContext ());
51- for (auto *V : Values) {
52- Constant *C = ConstantExpr::getBitCast (V, Int8PtrTy);
53- if (InitAsSet.insert (C).second )
54- Init.push_back (C);
55- }
56-
57- if (Init.empty ())
58- return ;
59-
60- ArrayType *ATy = ArrayType::get (Int8PtrTy, Init.size ());
61- GV = new llvm::GlobalVariable (M, ATy, false , GlobalValue::AppendingLinkage,
62- ConstantArray::get (ATy, Init), Name);
63- GV->setSection (" llvm.metadata" );
64- }
65-
66- static void append_to_compiler_used (Module &M, ArrayRef<GlobalValue *> Values) {
67- appendToUsedList (M, " llvm.compiler.used" , Values);
68- }
69- #else
70- static void append_to_compiler_used (Module &M, ArrayRef<GlobalValue *> Values)
71- {
72- appendToCompilerUsed (M, Values);
73- }
74- #endif
75- }
76-
7734/* Julia GC Root Placement pass. For a general overview of the design of GC
7835 root lowering, see the devdocs. This file is the actual implementation.
7936
@@ -340,16 +297,6 @@ namespace llvm {
340297 void initializeLateLowerGCFramePass (PassRegistry &Registry);
341298}
342299
343- template <typename T>
344- static void addReturnAttr (T *f, Attribute::AttrKind Kind)
345- {
346- #if JL_LLVM_VERSION >= 50000
347- f->addAttribute (AttributeList::ReturnIndex, Kind);
348- #else
349- f->addAttribute (AttributeSet::ReturnIndex, Kind);
350- #endif
351- }
352-
353300extern std::pair<MDNode*,MDNode*> tbaa_make_child (const char *name, MDNode *parent=nullptr , bool isConstant=false );
354301struct LateLowerGCFrame : public FunctionPass {
355302 static char ID;
@@ -822,6 +769,8 @@ JL_USED_FUNC static void dumpLivenessState(Function &F, State &S) {
822769// jtbaa_immut.
823770static bool isLoadFromImmut (LoadInst *LI)
824771{
772+ if (LI->getMetadata (LLVMContext::MD_invariant_load))
773+ return true ;
825774 MDNode *TBAA = LI->getMetadata (LLVMContext::MD_tbaa);
826775 if (!TBAA)
827776 return false ;
@@ -1573,7 +1522,6 @@ Value *LateLowerGCFrame::EmitLoadTag(IRBuilder<> &builder, Value *V)
15731522// that's initialized by `addrspacecast`. Such a global variable is not supported by the backend.
15741523// This is not a problem on 4.0+ since that transformation (in loop-idiom) is disabled
15751524// for NI pointers.
1576- #if JL_LLVM_VERSION >= 40000
15771525static SmallVector<int , 1 > *FindRefinements (Value *V, State *S)
15781526{
15791527 if (!S)
@@ -1593,12 +1541,6 @@ static bool IsPermRooted(Value *V, State *S)
15931541 return RefinePtr->size () == 1 && (*RefinePtr)[0 ] == -2 ;
15941542 return false ;
15951543}
1596- #else
1597- static bool IsPermRooted (Value *V, State *S)
1598- {
1599- return false ;
1600- }
1601- #endif
16021544
16031545static inline void UpdatePtrNumbering (Value *From, Value *To, State *S)
16041546{
@@ -1625,11 +1567,8 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
16251567 AllocaInst *Frame = nullptr ;
16261568 if (T_prjlvalue) {
16271569 T_pprjlvalue = T_prjlvalue->getPointerTo ();
1628- Frame = new AllocaInst (T_prjlvalue,
1629- #if JL_LLVM_VERSION >= 50000
1630- 0 ,
1631- #endif
1632- ConstantInt::get (T_int32, maxframeargs), " " , StartOff);
1570+ Frame = new AllocaInst (T_prjlvalue, 0 ,
1571+ ConstantInt::get (T_int32, maxframeargs), " " , StartOff);
16331572 }
16341573 SmallVector<CallInst*, 16 > write_barriers;
16351574 for (BasicBlock &BB : F) {
@@ -1670,8 +1609,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
16701609 auto pool_osize = ConstantInt::get (T_int32, osize);
16711610 newI = builder.CreateCall (pool_alloc_func, {ptls, pool_offs, pool_osize});
16721611 }
1673- addReturnAttr (newI, Attribute::NoAlias);
1674- addReturnAttr (newI, Attribute::NonNull);
1612+ newI->setAttributes (newI->getCalledFunction ()->getAttributes ());
16751613 newI->takeName (CI);
16761614 auto store = builder.CreateStore (CI->getArgOperand (2 ),
16771615 EmitTagPtr (builder, T_prjlvalue, newI));
@@ -1726,26 +1664,10 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
17261664 CallInst *NewCall = CallInst::Create (newFptr, ReplacementArgs, " " , CI);
17271665 NewCall->setTailCallKind (CI->getTailCallKind ());
17281666 auto old_attrs = CI->getAttributes ();
1729- #if JL_LLVM_VERSION >= 50000
17301667 NewCall->setAttributes (AttributeList::get (CI->getContext (),
17311668 old_attrs.getFnAttributes (),
17321669 old_attrs.getRetAttributes (), {}));
1733- #else
1734- AttributeSet attr;
1735- attr = attr.addAttributes (CI->getContext (), AttributeSet::ReturnIndex,
1736- old_attrs.getRetAttributes ())
1737- .addAttributes (CI->getContext (), AttributeSet::FunctionIndex,
1738- old_attrs.getFnAttributes ());
1739- NewCall->setAttributes (attr);
1740- #endif
1741- #if JL_LLVM_VERSION >= 40000
17421670 NewCall->copyMetadata (*CI);
1743- #else
1744- SmallVector<std::pair<unsigned , MDNode *>, 1 > MDs;
1745- CI->getAllMetadata (MDs);
1746- for (auto MD : MDs)
1747- NewCall->setMetadata (MD.first , MD.second );
1748- #endif
17491671 CI->replaceAllUsesWith (NewCall);
17501672 UpdatePtrNumbering (CI, NewCall, S);
17511673 } else if (CI->getNumArgOperands () == CI->getNumOperands ()) {
@@ -1755,14 +1677,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
17551677 } else {
17561678 CallInst *NewCall = CallInst::Create (CI, None, CI);
17571679 NewCall->takeName (CI);
1758- #if JL_LLVM_VERSION >= 40000
17591680 NewCall->copyMetadata (*CI);
1760- #else
1761- SmallVector<std::pair<unsigned , MDNode *>, 1 > MDs;
1762- CI->getAllMetadata (MDs);
1763- for (auto MD : MDs)
1764- NewCall->setMetadata (MD.first , MD.second );
1765- #endif
17661681 CI->replaceAllUsesWith (NewCall);
17671682 UpdatePtrNumbering (CI, NewCall, S);
17681683 }
@@ -1910,11 +1825,8 @@ void LateLowerGCFrame::PlaceRootsAndUpdateCalls(std::vector<int> &Colors, State
19101825 if (MaxColor != -1 || S.Allocas .size () != 0 ) {
19111826 unsigned NRoots = MaxColor + 1 + S.Allocas .size ();
19121827 // Create GC Frame
1913- AllocaInst *gcframe = new AllocaInst (T_prjlvalue,
1914- #if JL_LLVM_VERSION >= 50000
1915- 0 ,
1916- #endif
1917- ConstantInt::get (T_int32, NRoots+2 ), " gcframe" );
1828+ AllocaInst *gcframe = new AllocaInst (T_prjlvalue, 0 ,
1829+ ConstantInt::get (T_int32, NRoots + 2 ), " gcframe" );
19181830 gcframe->insertBefore (&*F->getEntryBlock ().begin ());
19191831 // Zero out gcframe
19201832 BitCastInst *tempSlot_i8 = new BitCastInst (gcframe, Type::getInt8PtrTy (F->getContext ()), " " );
@@ -2009,17 +1921,21 @@ bool LateLowerGCFrame::doInitialization(Module &M) {
20091921 args.push_back (T_int32);
20101922 pool_alloc_func = Function::Create (FunctionType::get (T_prjlvalue, args, false ),
20111923 Function::ExternalLinkage, " jl_gc_pool_alloc" , &M);
2012- addReturnAttr (pool_alloc_func, Attribute::NoAlias);
2013- addReturnAttr (pool_alloc_func, Attribute::NonNull);
1924+ pool_alloc_func->setAttributes (AttributeList::get (M.getContext (),
1925+ alloc_obj_func->getAttributes ().getFnAttributes (),
1926+ alloc_obj_func->getAttributes ().getRetAttributes (),
1927+ None));
20141928 }
20151929 if (!(big_alloc_func = M.getFunction (" jl_gc_big_alloc" ))) {
20161930 std::vector<Type*> args (0 );
20171931 args.push_back (T_pint8);
20181932 args.push_back (T_size);
20191933 big_alloc_func = Function::Create (FunctionType::get (T_prjlvalue, args, false ),
20201934 Function::ExternalLinkage, " jl_gc_big_alloc" , &M);
2021- addReturnAttr (big_alloc_func, Attribute::NoAlias);
2022- addReturnAttr (big_alloc_func, Attribute::NonNull);
1935+ big_alloc_func->setAttributes (AttributeList::get (M.getContext (),
1936+ alloc_obj_func->getAttributes ().getFnAttributes (),
1937+ alloc_obj_func->getAttributes ().getRetAttributes (),
1938+ None));
20231939 }
20241940 auto T_jlvalue = cast<PointerType>(T_prjlvalue)->getElementType ();
20251941 T_pjlvalue = PointerType::get (T_jlvalue, 0 );
@@ -2053,7 +1969,7 @@ bool LateLowerGCFrame::doInitialization(Module &M) {
20531969 j++;
20541970 }
20551971 if (j != 0 )
2056- append_to_compiler_used (M, ArrayRef<GlobalValue*>(function_list, j));
1972+ appendToCompilerUsed (M, ArrayRef<GlobalValue*>(function_list, j));
20571973 return true ;
20581974}
20591975
0 commit comments