2015-12-03 00:09:05 +01:00
|
|
|
//===- EHPersonalities.cpp - Compute EH-related information ---------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2015-12-03 00:09:05 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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 06:38:55 +01:00
|
|
|
#include "llvm/IR/CFG.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2015-12-03 00:09:05 +01: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 06:38:55 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-12-12 06:53:20 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-12-03 00:09:05 +01: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())
|
2020-10-28 07:18:26 +01: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_TableSEH)
|
|
|
|
.Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
|
|
|
|
.Case("ProcessCLRException", EHPersonality::CoreCLR)
|
|
|
|
.Case("rust_eh_personality", EHPersonality::Rust)
|
|
|
|
.Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
|
2020-12-02 15:48:52 +01:00
|
|
|
.Case("__xlcxx_personality_v1", EHPersonality::XL_CXX)
|
2020-10-28 07:18:26 +01:00
|
|
|
.Default(EHPersonality::Unknown);
|
2015-12-03 00:09:05 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 22:41:13 +01: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";
|
2020-10-28 07:18:26 +01:00
|
|
|
case EHPersonality::MSVC_TableSEH:
|
|
|
|
return "__C_specific_handler";
|
2016-11-14 22:41:13 +01:00
|
|
|
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-17 22:52:03 +02:00
|
|
|
case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
|
2020-12-02 15:48:52 +01:00
|
|
|
case EHPersonality::XL_CXX:
|
|
|
|
return "__xlcxx_personality_v1";
|
2016-11-14 22:41:13 +01: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 00:09:05 +01: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 06:38:55 +01: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-14 19:34:23 +01: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 06:38:55 +01: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 00:21:41 +02: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 06:38:55 +01: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 12:04:59 +02: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 06:38:55 +01:00
|
|
|
if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
|
2016-01-15 22:16:19 +01: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 06:38:55 +01: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;
|
|
|
|
}
|