Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Sep 14, 2020
1 parent 26848b6 commit d2e20ef
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/llvm-multiversioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ struct CloneCtx {
Constant *get_ptrdiff32(Constant *ptr, Constant *base) const;
template<typename T>
Constant *emit_offset_table(const std::vector<T*> &vars, StringRef name) const;
std::pair<Function *, GlobalVariable *> rewrite_alias(GlobalAlias *alias);
void rewrite_alias(GlobalAlias *alias);

LLVMContext &ctx;
Type *T_size;
Expand Down Expand Up @@ -697,7 +697,7 @@ Constant *CloneCtx::rewrite_gv_init(const Stack& stack)
}

// replace an alias to a function with a trampoline and (uninitialized) global variable slot
std::pair<Function *, GlobalVariable *> CloneCtx::rewrite_alias(GlobalAlias *alias)
void CloneCtx::rewrite_alias(GlobalAlias *alias)
{
Function *F = cast<Function>(alias->getAliasee());
assert(!is_vector(F->getFunctionType()));
Expand Down Expand Up @@ -725,28 +725,34 @@ std::pair<Function *, GlobalVariable *> CloneCtx::rewrite_alias(GlobalAlias *ali
Args.push_back(&arg);
auto call = irbuilder.CreateCall(ptr, makeArrayRef(Args));
call->setTailCallKind(CallInst::TCK_MustTail);
// musttail support is very bad on ARM, PPC, PPC64 (as of LLVM 3.9)
// Known failures includes vararg and sret.
#if (defined(_CPU_ARM_) || defined(_CPU_PPC_) || defined(_CPU_PPC64_))
if (F->isVarArg())
abort();
#endif

if (F->getReturnType() == T_void)
irbuilder.CreateRetVoid();
else
irbuilder.CreateRet(call);

return std::make_pair(trampoline, slot);
}

void CloneCtx::fix_gv_uses()
{
auto single_pass = [&] (Function *orig_f) {
bool changed = false;
for (auto uses = ConstantUses<GlobalValue>(orig_f, M); !uses.done(); uses.next()) {
changed = true;
auto &stack = uses.get_stack();
auto info = uses.get_info();
// We only support absolute pointer relocation.
assert(info.samebits);
GlobalVariable *val;
if (auto alias = dyn_cast<GlobalAlias>(info.val)) {
Function *trampoline;
std::tie(trampoline, val) = rewrite_alias(alias);
rewrite_alias(alias);
uint32_t id;
std::tie(id, val) = get_reloc_slot(orig_f);
}
else {
val = cast<GlobalVariable>(info.val);
Expand All @@ -759,7 +765,6 @@ void CloneCtx::fix_gv_uses()
addr = ConstantExpr::getAdd(addr, ConstantInt::get(T_size, info.offset));
gv_relocs.emplace_back(addr, fid);
val->setInitializer(rewrite_gv_init(stack));
changed = true;
}
return changed;
};
Expand Down

0 comments on commit d2e20ef

Please sign in to comment.