2016-11-15 05:41:13 +08:00
|
|
|
//===- EscapeEnumerator.cpp -----------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-15 05:41:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines a helper class that enumerates all possible exits from a function,
|
|
|
|
// including exception handling.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/EscapeEnumerator.h"
|
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
2018-06-05 05:23:21 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2016-11-15 05:41:13 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
static FunctionCallee getDefaultPersonalityFn(Module *M) {
|
2016-11-15 05:41:13 +08:00
|
|
|
LLVMContext &C = M->getContext();
|
|
|
|
Triple T(M->getTargetTriple());
|
|
|
|
EHPersonality Pers = getDefaultEHPersonality(T);
|
|
|
|
return M->getOrInsertFunction(getEHPersonalityName(Pers),
|
|
|
|
FunctionType::get(Type::getInt32Ty(C), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
IRBuilder<> *EscapeEnumerator::Next() {
|
|
|
|
if (Done)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find all 'return', 'resume', and 'unwind' instructions.
|
|
|
|
while (StateBB != StateE) {
|
|
|
|
BasicBlock *CurBB = &*StateBB++;
|
|
|
|
|
|
|
|
// Branches and invokes do not escape, only unwind, resume, and return
|
|
|
|
// do.
|
2018-10-15 18:04:59 +08:00
|
|
|
Instruction *TI = CurBB->getTerminator();
|
2016-11-15 05:41:13 +08:00
|
|
|
if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(TI);
|
|
|
|
return &Builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
Done = true;
|
|
|
|
|
|
|
|
if (!HandleExceptions)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (F.doesNotThrow())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find all 'call' instructions that may throw.
|
|
|
|
SmallVector<Instruction *, 16> Calls;
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
for (Instruction &II : BB)
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&II))
|
|
|
|
if (!CI->doesNotThrow())
|
|
|
|
Calls.push_back(CI);
|
|
|
|
|
|
|
|
if (Calls.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Create a cleanup block.
|
|
|
|
LLVMContext &C = F.getContext();
|
|
|
|
BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
|
2017-05-10 03:31:13 +08:00
|
|
|
Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
|
2016-11-15 05:41:13 +08:00
|
|
|
if (!F.hasPersonalityFn()) {
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
|
|
|
|
F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
|
2016-11-15 05:41:13 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] Add Wasm personality and isScopedEHPersonality()
Summary:
- Add wasm personality function
- Re-categorize the existing `isFuncletEHPersonality()` function into
two different functions: `isFuncletEHPersonality()` and
`isScopedEHPersonality(). This becomes necessary as wasm EH uses scoped
EH instructions (catchswitch, catchpad/ret, and cleanuppad/ret) but not
outlined funclets.
- Changed some callsites of `isFuncletEHPersonality()` to
`isScopedEHPersonality()` if they are related to scoped EH IR-level
stuff.
Reviewers: majnemer, dschuff, rnk
Subscribers: jfb, sbc100, jgravelle-google, eraman, JDevlieghere, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45559
llvm-svn: 332667
2018-05-18 04:52:03 +08:00
|
|
|
if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
|
|
|
|
report_fatal_error("Scoped EH not supported");
|
2016-11-15 05:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LandingPadInst *LPad =
|
|
|
|
LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
|
|
|
|
LPad->setCleanup(true);
|
|
|
|
ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
|
|
|
|
|
|
|
|
// Transform the 'call' instructions into 'invoke's branching to the
|
|
|
|
// cleanup block. Go in reverse order to make prettier BB names.
|
|
|
|
SmallVector<Value *, 16> Args;
|
|
|
|
for (unsigned I = Calls.size(); I != 0;) {
|
|
|
|
CallInst *CI = cast<CallInst>(Calls[--I]);
|
|
|
|
changeToInvokeAndSplitBasicBlock(CI, CleanupBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(RI);
|
|
|
|
return &Builder;
|
|
|
|
}
|