2015-12-03 07:09:05 +08:00
|
|
|
//===- EHPersonalities.cpp - Compute EH-related information ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2015-12-03 07:09:05 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-12-12 13:53:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-12-03 07:09:05 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// See if the given exception handling personality function is one that we
|
|
|
|
/// understand. If so, return a description of it; otherwise return Unknown.
|
|
|
|
EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
|
|
|
|
const Function *F =
|
|
|
|
Pers ? dyn_cast<Function>(Pers->stripPointerCasts()) : nullptr;
|
|
|
|
if (!F)
|
|
|
|
return EHPersonality::Unknown;
|
|
|
|
return StringSwitch<EHPersonality>(F->getName())
|
[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
|
|
|
.Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
|
|
|
|
.Case("__gxx_personality_v0", EHPersonality::GNU_CXX)
|
|
|
|
.Case("__gxx_personality_seh0", EHPersonality::GNU_CXX)
|
|
|
|
.Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
|
|
|
|
.Case("__gcc_personality_v0", EHPersonality::GNU_C)
|
|
|
|
.Case("__gcc_personality_seh0", EHPersonality::GNU_C)
|
|
|
|
.Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
|
|
|
|
.Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
|
|
|
|
.Case("_except_handler3", EHPersonality::MSVC_X86SEH)
|
|
|
|
.Case("_except_handler4", EHPersonality::MSVC_X86SEH)
|
|
|
|
.Case("__C_specific_handler", EHPersonality::MSVC_Win64SEH)
|
|
|
|
.Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
|
|
|
|
.Case("ProcessCLRException", EHPersonality::CoreCLR)
|
|
|
|
.Case("rust_eh_personality", EHPersonality::Rust)
|
|
|
|
.Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
|
2015-12-03 07:09:05 +08:00
|
|
|
.Default(EHPersonality::Unknown);
|
|
|
|
}
|
|
|
|
|
2016-11-15 05:41:13 +08:00
|
|
|
StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
|
|
|
|
switch (Pers) {
|
|
|
|
case EHPersonality::GNU_Ada: return "__gnat_eh_personality";
|
|
|
|
case EHPersonality::GNU_CXX: return "__gxx_personality_v0";
|
|
|
|
case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0";
|
|
|
|
case EHPersonality::GNU_C: return "__gcc_personality_v0";
|
|
|
|
case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0";
|
|
|
|
case EHPersonality::GNU_ObjC: return "__objc_personality_v0";
|
|
|
|
case EHPersonality::MSVC_X86SEH: return "_except_handler3";
|
|
|
|
case EHPersonality::MSVC_Win64SEH: return "__C_specific_handler";
|
|
|
|
case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3";
|
|
|
|
case EHPersonality::CoreCLR: return "ProcessCLRException";
|
|
|
|
case EHPersonality::Rust: return "rust_eh_personality";
|
[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
|
|
|
case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
|
2016-11-15 05:41:13 +08:00
|
|
|
case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid EHPersonality!");
|
|
|
|
}
|
|
|
|
|
|
|
|
EHPersonality llvm::getDefaultEHPersonality(const Triple &T) {
|
|
|
|
return EHPersonality::GNU_C;
|
|
|
|
}
|
|
|
|
|
2015-12-03 07:09:05 +08:00
|
|
|
bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
|
|
|
|
EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
|
|
|
|
// We can't simplify any invokes to nounwind functions if the personality
|
|
|
|
// function wants to catch asynch exceptions. The nounwind attribute only
|
|
|
|
// implies that the function does not throw synchronous exceptions.
|
|
|
|
return !isAsynchronousEHPersonality(Personality);
|
|
|
|
}
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
|
|
|
|
DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
|
|
|
|
SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
|
|
|
|
BasicBlock *EntryBlock = &F.getEntryBlock();
|
|
|
|
DenseMap<BasicBlock *, ColorVector> BlockColors;
|
|
|
|
|
|
|
|
// Build up the color map, which maps each block to its set of 'colors'.
|
|
|
|
// For any block B the "colors" of B are the set of funclets F (possibly
|
|
|
|
// including a root "funclet" representing the main function) such that
|
|
|
|
// F will need to directly contain B or a copy of B (where the term "directly
|
|
|
|
// contain" is used to distinguish from being "transitively contained" in
|
|
|
|
// a nested funclet).
|
|
|
|
//
|
2015-12-15 02:34:23 +08:00
|
|
|
// Note: Despite not being a funclet in the truest sense, a catchswitch is
|
|
|
|
// considered to belong to its own funclet for the purposes of coloring.
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
|
|
|
|
DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
|
|
|
|
<< F.getName() << "\n");
|
|
|
|
|
|
|
|
Worklist.push_back({EntryBlock, EntryBlock});
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
BasicBlock *Visiting;
|
|
|
|
BasicBlock *Color;
|
|
|
|
std::tie(Visiting, Color) = Worklist.pop_back_val();
|
|
|
|
DEBUG_WITH_TYPE("winehprepare-coloring",
|
|
|
|
dbgs() << "Visiting " << Visiting->getName() << ", "
|
|
|
|
<< Color->getName() << "\n");
|
|
|
|
Instruction *VisitingHead = Visiting->getFirstNonPHI();
|
|
|
|
if (VisitingHead->isEHPad()) {
|
|
|
|
// Mark this funclet head as a member of itself.
|
|
|
|
Color = Visiting;
|
|
|
|
}
|
|
|
|
// Note that this is a member of the given color.
|
|
|
|
ColorVector &Colors = BlockColors[Visiting];
|
2016-08-12 06:21:41 +08:00
|
|
|
if (!is_contained(Colors, Color))
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
Colors.push_back(Color);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG_WITH_TYPE("winehprepare-coloring",
|
|
|
|
dbgs() << " Assigned color \'" << Color->getName()
|
|
|
|
<< "\' to block \'" << Visiting->getName()
|
|
|
|
<< "\'.\n");
|
|
|
|
|
|
|
|
BasicBlock *SuccColor = Color;
|
2018-10-15 18:04:59 +08:00
|
|
|
Instruction *Terminator = Visiting->getTerminator();
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
|
2016-01-16 05:16:19 +08:00
|
|
|
Value *ParentPad = CatchRet->getCatchSwitchParentPad();
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
if (isa<ConstantTokenNone>(ParentPad))
|
|
|
|
SuccColor = EntryBlock;
|
|
|
|
else
|
|
|
|
SuccColor = cast<Instruction>(ParentPad)->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (BasicBlock *Succ : successors(Visiting))
|
|
|
|
Worklist.push_back({Succ, SuccColor});
|
|
|
|
}
|
|
|
|
return BlockColors;
|
|
|
|
}
|