55#include " support/dtypes.h"
66#include " passes.h"
77
8- #include " llvm/IR/BasicBlock.h"
9- #include " llvm/IR/Instruction.h"
10- #include " llvm/Support/Debug.h"
118#include < llvm-c/Core.h>
129#include < llvm-c/Types.h>
1310
@@ -146,17 +143,7 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack, Function *pgcstack_getter,
146143 return ;
147144 }
148145 if (or_new) {
149- // pgcstack = pgstack_intrinsic()
150-
151-
152- // if (offset != 0)
153- // pgcstack = tp + offset; // fast
154- // else
155- // pgcstack_getter = load pgcstack_func_slot
156- // if pgcstack_getter == nullptr // Runtime not initialized
157- // pgcstack = nullptr
158- // else
159- // pgcstack = pgcstack_getter();
146+ // pgcstack();
160147 // if (pgcstack != nullptr)
161148 // last_gc_state = emit_gc_unsafe_enter(ctx);
162149 // phi = pgcstack; // fast
@@ -182,29 +169,17 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack, Function *pgcstack_getter,
182169 if (CFGModified)
183170 *CFGModified = true ;
184171 // emit slow branch code
185- Function *adoptFunc = M->getFunction (XSTR (jl_adopt_thread));
172+ CallInst *adopt = cast<CallInst>(pgcstack->clone ());
173+ Function *adoptFunc = M->getFunction (XSTR (jl_autoinit_and_adopt_thread));
186174 if (adoptFunc == NULL ) {
187- adoptFunc = Function::Create (FunctionType::get (builder. getPtrTy (), { builder. getPtrTy ()}, false ),
175+ adoptFunc = Function::Create (pgcstack_getter-> getFunctionType ( ),
188176 pgcstack_getter->getLinkage (), pgcstack_getter->getAddressSpace (),
189- XSTR (jl_adopt_thread ), M);
177+ XSTR (jl_autoinit_and_adopt_thread ), M);
190178 adoptFunc->copyAttributesFrom (pgcstack_getter);
191179 adoptFunc->copyMetadata (pgcstack_getter, 0 );
192180 }
193- builder.SetInsertPoint (slowTerm);
194- Value* handle = Constant::getNullValue (builder.getPtrTy ());
195- if (imaging_mode) {
196- // Adopt thread takes in a handle to the sysimage and this is the easiest way to get it.
197- Function *dladdr = M->getFunction (XSTR (jl_find_dynamic_library_by_addr)); // gets handle to sysimage
198- if (dladdr == NULL ) {
199- dladdr = Function::Create (FunctionType::get (builder.getPtrTy (), { builder.getPtrTy ()}, false ),
200- pgcstack_getter->getLinkage (), pgcstack_getter->getAddressSpace (),
201- XSTR (jl_find_dynamic_library_by_addr), M);
202- }
203- auto this_func = builder.GetInsertBlock ()->getParent ();
204- handle = builder.CreateCall (dladdr, {ConstantExpr::getBitCast (this_func, builder.getPtrTy ())});
205- }
206-
207- auto adopt = builder.CreateCall (adoptFunc, {handle});
181+ adopt->setCalledFunction (adoptFunc);
182+ adopt->insertBefore (slowTerm);
208183 phi->addIncoming (adopt, slowTerm->getParent ());
209184 // emit fast branch code
210185 builder.SetInsertPoint (fastTerm->getParent ());
@@ -235,20 +210,17 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack, Function *pgcstack_getter,
235210
236211 if (imaging_mode) {
237212 IRBuilder<> builder (pgcstack);
238- SmallVector<uint32_t , 2 > Weights{9 , 1 };
239- MDBuilder MDB (pgcstack->getContext ());
240213 if (jl_tls_elf_support) {
241214 // if (offset != 0)
242215 // pgcstack = tp + offset; // fast
243216 // else
244- // if pgcstack_getter == null
245- // pgcstack = null; // slow
246- // else
247- // pgcstack = pgcstack_getter(); // slow
217+ // pgcstack = getter(); // slow
248218 auto offset = builder.CreateLoad (T_size, pgcstack_offset);
249219 offset->setMetadata (llvm::LLVMContext::MD_tbaa, tbaa_const);
250220 offset->setMetadata (llvm::LLVMContext::MD_invariant_load, MDNode::get (pgcstack->getContext (), None));
251221 auto cmp = builder.CreateICmpNE (offset, Constant::getNullValue (offset->getType ()));
222+ MDBuilder MDB (pgcstack->getContext ());
223+ SmallVector<uint32_t , 2 > Weights{9 , 1 };
252224 TerminatorInst *fastTerm;
253225 TerminatorInst *slowTerm;
254226 SplitBlockAndInsertIfThenElse (cmp, pgcstack, &fastTerm, &slowTerm,
@@ -265,52 +237,21 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack, Function *pgcstack_getter,
265237 // refresh the basic block in the builder
266238 builder.SetInsertPoint (pgcstack);
267239 auto getter = builder.CreateLoad (T_pgcstack_getter, pgcstack_func_slot);
268- auto phi_value = cast<Instruction>(pgcstack);
269- if (or_new) {
270- // if pgcstack_func_slot is not initialized we set pgcstack to null to trigger the slow path
271- TerminatorInst *nonNullTerm;
272- TerminatorInst *nullTerm;
273- auto is_null = builder.CreateICmpEQ (getter, Constant::getNullValue (builder.getPtrTy ()));
274- SplitBlockAndInsertIfThenElse (is_null, pgcstack, &nullTerm, &nonNullTerm,
275- MDB.createBranchWeights (Weights));
276- builder.SetInsertPoint (pgcstack);
277- auto phi2 = builder.CreatePHI (T_pppjlvalue, 2 , " pgcstack" );
278- pgcstack->moveBefore (nonNullTerm);
279- phi2->addIncoming (pgcstack, nonNullTerm->getParent ());
280- phi2->addIncoming (Constant::getNullValue (T_pppjlvalue), nullTerm->getParent ());
281- phi_value = phi2;
282- builder.SetInsertPoint (pgcstack);
283- // Check if pgcstack_func_slot is initialized
284- }
285240 getter->setMetadata (llvm::LLVMContext::MD_tbaa, tbaa_const);
286241 getter->setMetadata (llvm::LLVMContext::MD_invariant_load, MDNode::get (pgcstack->getContext (), None));
287242 pgcstack->setCalledFunction (pgcstack->getFunctionType (), getter);
288243 set_pgcstack_attrs (pgcstack);
289244
290245 phi->addIncoming (fastTLS, fastTLS->getParent ());
291- phi->addIncoming (phi_value, phi_value->getParent ());
246+ phi->addIncoming (pgcstack, pgcstack->getParent ());
247+
292248 return ;
293249 }
294250 // In imaging mode, we emit the function address as a load of a static
295251 // variable to be filled (in `staticdata.c`) at initialization time of the sysimg.
296252 // This way we can bypass the extra indirection in `jl_get_pgcstack`
297253 // since we may not know which getter function to use ahead of time.
298254 auto getter = builder.CreateLoad (T_pgcstack_getter, pgcstack_func_slot);
299- if (or_new) {
300- // if pgcstack_func_slot is not initialized we set pgcstack to null to trigger the slow path
301- TerminatorInst *nonNullTerm;
302- TerminatorInst *nullTerm;
303- auto is_null = builder.CreateICmpEQ (getter, Constant::getNullValue (builder.getPtrTy ()));
304- SplitBlockAndInsertIfThenElse (is_null, pgcstack, &nullTerm, &nonNullTerm,
305- MDB.createBranchWeights (Weights));
306- builder.SetInsertPoint (pgcstack);
307- auto phi2 = builder.CreatePHI (T_pppjlvalue, 2 , " pgcstack" );
308- pgcstack->moveBefore (nonNullTerm);
309- pgcstack->replaceAllUsesWith (phi2);
310- phi2->addIncoming (pgcstack, nonNullTerm->getParent ());
311- phi2->addIncoming (Constant::getNullValue (T_pppjlvalue), nullTerm->getParent ());
312- builder.SetInsertPoint (pgcstack);
313- }
314255 getter->setMetadata (llvm::LLVMContext::MD_tbaa, tbaa_const);
315256 getter->setMetadata (llvm::LLVMContext::MD_invariant_load, MDNode::get (pgcstack->getContext (), None));
316257 if (TargetTriple.isOSDarwin ()) {
0 commit comments