mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-06 23:32:27 +00:00
Remap frame variables for native Windows exception handling.
Differential Revision: http://reviews.llvm.org/D7770 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230249 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
ee7b509aa3
commit
1d10231766
@ -153,6 +153,9 @@ public:
|
|||||||
virtual CloningAction handleInstruction(ValueToValueMapTy &VMap,
|
virtual CloningAction handleInstruction(ValueToValueMapTy &VMap,
|
||||||
const Instruction *Inst,
|
const Instruction *Inst,
|
||||||
BasicBlock *NewBB) = 0;
|
BasicBlock *NewBB) = 0;
|
||||||
|
|
||||||
|
virtual ValueMapTypeRemapper *getTypeRemapper() { return nullptr; }
|
||||||
|
virtual ValueMaterializer *getValueMaterializer() { return nullptr; }
|
||||||
};
|
};
|
||||||
|
|
||||||
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
|
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/CodeGen/Passes.h"
|
#include "llvm/CodeGen/Passes.h"
|
||||||
|
#include "llvm/ADT/MapVector.h"
|
||||||
|
#include "llvm/ADT/TinyPtrVector.h"
|
||||||
#include "llvm/Analysis/LibCallSemantics.h"
|
#include "llvm/Analysis/LibCallSemantics.h"
|
||||||
#include "llvm/IR/Function.h"
|
#include "llvm/IR/Function.h"
|
||||||
#include "llvm/IR/IRBuilder.h"
|
#include "llvm/IR/IRBuilder.h"
|
||||||
@ -33,6 +35,19 @@ using namespace llvm::PatternMatch;
|
|||||||
#define DEBUG_TYPE "winehprepare"
|
#define DEBUG_TYPE "winehprepare"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
struct HandlerAllocas {
|
||||||
|
TinyPtrVector<AllocaInst *> Allocas;
|
||||||
|
int ParentFrameAllocationIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
// This map is used to model frame variable usage during outlining, to
|
||||||
|
// construct a structure type to hold the frame variables in a frame
|
||||||
|
// allocation block, and to remap the frame variable allocas (including
|
||||||
|
// spill locations as needed) to GEPs that get the variable from the
|
||||||
|
// frame allocation structure.
|
||||||
|
typedef MapVector<AllocaInst *, HandlerAllocas> FrameVarInfoMap;
|
||||||
|
|
||||||
class WinEHPrepare : public FunctionPass {
|
class WinEHPrepare : public FunctionPass {
|
||||||
std::unique_ptr<FunctionPass> DwarfPrepare;
|
std::unique_ptr<FunctionPass> DwarfPrepare;
|
||||||
|
|
||||||
@ -55,13 +70,30 @@ private:
|
|||||||
bool prepareCPPEHHandlers(Function &F,
|
bool prepareCPPEHHandlers(Function &F,
|
||||||
SmallVectorImpl<LandingPadInst *> &LPads);
|
SmallVectorImpl<LandingPadInst *> &LPads);
|
||||||
bool outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
bool outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
||||||
LandingPadInst *LPad, StructType *EHDataStructTy);
|
LandingPadInst *LPad, CallInst *&EHAlloc,
|
||||||
|
AllocaInst *&EHObjPtr, FrameVarInfoMap &VarInfo);
|
||||||
|
};
|
||||||
|
|
||||||
|
class WinEHFrameVariableMaterializer : public ValueMaterializer {
|
||||||
|
public:
|
||||||
|
WinEHFrameVariableMaterializer(Function *OutlinedFn,
|
||||||
|
FrameVarInfoMap &FrameVarInfo);
|
||||||
|
~WinEHFrameVariableMaterializer() {}
|
||||||
|
|
||||||
|
virtual Value *materializeValueFor(Value *V) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Function *OutlinedFn;
|
||||||
|
FrameVarInfoMap &FrameVarInfo;
|
||||||
|
IRBuilder<> Builder;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WinEHCatchDirector : public CloningDirector {
|
class WinEHCatchDirector : public CloningDirector {
|
||||||
public:
|
public:
|
||||||
WinEHCatchDirector(LandingPadInst *LPI, Value *Selector, Value *EHObj)
|
WinEHCatchDirector(LandingPadInst *LPI, Function *CatchFn, Value *Selector,
|
||||||
|
Value *EHObj, FrameVarInfoMap &VarInfo)
|
||||||
: LPI(LPI), CurrentSelector(Selector->stripPointerCasts()), EHObj(EHObj),
|
: LPI(LPI), CurrentSelector(Selector->stripPointerCasts()), EHObj(EHObj),
|
||||||
|
Materializer(CatchFn, VarInfo),
|
||||||
SelectorIDType(Type::getInt32Ty(LPI->getContext())),
|
SelectorIDType(Type::getInt32Ty(LPI->getContext())),
|
||||||
Int8PtrType(Type::getInt8PtrTy(LPI->getContext())) {}
|
Int8PtrType(Type::getInt8PtrTy(LPI->getContext())) {}
|
||||||
|
|
||||||
@ -69,10 +101,13 @@ public:
|
|||||||
const Instruction *Inst,
|
const Instruction *Inst,
|
||||||
BasicBlock *NewBB) override;
|
BasicBlock *NewBB) override;
|
||||||
|
|
||||||
|
ValueMaterializer *getValueMaterializer() override { return &Materializer; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LandingPadInst *LPI;
|
LandingPadInst *LPI;
|
||||||
Value *CurrentSelector;
|
Value *CurrentSelector;
|
||||||
Value *EHObj;
|
Value *EHObj;
|
||||||
|
WinEHFrameVariableMaterializer Materializer;
|
||||||
Type *SelectorIDType;
|
Type *SelectorIDType;
|
||||||
Type *Int8PtrType;
|
Type *Int8PtrType;
|
||||||
|
|
||||||
@ -145,14 +180,13 @@ void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
|
|||||||
|
|
||||||
bool WinEHPrepare::prepareCPPEHHandlers(
|
bool WinEHPrepare::prepareCPPEHHandlers(
|
||||||
Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
|
Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
|
||||||
// FIXME: Find all frame variable references in the handlers
|
// These containers are used to re-map frame variables that are used in
|
||||||
// to populate the structure elements.
|
// outlined catch and cleanup handlers. They will be populated as the
|
||||||
SmallVector<Type *, 2> AllocStructTys;
|
// handlers are outlined.
|
||||||
AllocStructTys.push_back(Type::getInt32Ty(F.getContext())); // EH state
|
FrameVarInfoMap FrameVarInfo;
|
||||||
AllocStructTys.push_back(Type::getInt8PtrTy(F.getContext())); // EH object
|
SmallVector<CallInst *, 4> HandlerAllocs;
|
||||||
StructType *EHDataStructTy =
|
SmallVector<AllocaInst *, 4> HandlerEHObjPtrs;
|
||||||
StructType::create(F.getContext(), AllocStructTys,
|
|
||||||
"struct." + F.getName().str() + ".ehdata");
|
|
||||||
bool HandlersOutlined = false;
|
bool HandlersOutlined = false;
|
||||||
|
|
||||||
for (LandingPadInst *LPad : LPads) {
|
for (LandingPadInst *LPad : LPads) {
|
||||||
@ -175,18 +209,174 @@ bool WinEHPrepare::prepareCPPEHHandlers(
|
|||||||
|
|
||||||
for (unsigned Idx = 0, NumClauses = LPad->getNumClauses(); Idx < NumClauses;
|
for (unsigned Idx = 0, NumClauses = LPad->getNumClauses(); Idx < NumClauses;
|
||||||
++Idx) {
|
++Idx) {
|
||||||
if (LPad->isCatch(Idx))
|
if (LPad->isCatch(Idx)) {
|
||||||
HandlersOutlined =
|
// Create a new instance of the handler data structure in the
|
||||||
outlineCatchHandler(&F, LPad->getClause(Idx), LPad, EHDataStructTy);
|
// HandlerData vector.
|
||||||
} // End for each clause
|
CallInst *EHAlloc = nullptr;
|
||||||
} // End for each landingpad
|
AllocaInst *EHObjPtr = nullptr;
|
||||||
|
bool Outlined = outlineCatchHandler(&F, LPad->getClause(Idx), LPad,
|
||||||
|
EHAlloc, EHObjPtr, FrameVarInfo);
|
||||||
|
if (Outlined) {
|
||||||
|
HandlersOutlined = true;
|
||||||
|
// These values must be resolved after all handlers have been
|
||||||
|
// outlined.
|
||||||
|
if (EHAlloc)
|
||||||
|
HandlerAllocs.push_back(EHAlloc);
|
||||||
|
if (EHObjPtr)
|
||||||
|
HandlerEHObjPtrs.push_back(EHObjPtr);
|
||||||
|
}
|
||||||
|
} // End if (isCatch)
|
||||||
|
} // End for each clause
|
||||||
|
} // End for each landingpad
|
||||||
|
|
||||||
|
// If nothing got outlined, there is no more processing to be done.
|
||||||
|
if (!HandlersOutlined)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// FIXME: We will replace the landingpad bodies with llvm.eh.actions
|
||||||
|
// calls and indirect branches here and then delete blocks
|
||||||
|
// which are no longer reachable. That will get rid of the
|
||||||
|
// handlers that we have outlined. There is code below
|
||||||
|
// that looks for allocas with no uses in the parent function.
|
||||||
|
// That will only happen after the pruning is implemented.
|
||||||
|
|
||||||
|
// Remap the frame variables.
|
||||||
|
SmallVector<Type *, 2> StructTys;
|
||||||
|
StructTys.push_back(Type::getInt32Ty(F.getContext())); // EH state
|
||||||
|
StructTys.push_back(Type::getInt8PtrTy(F.getContext())); // EH object
|
||||||
|
|
||||||
|
// Start the index at two since we always have the above fields at 0 and 1.
|
||||||
|
int Idx = 2;
|
||||||
|
|
||||||
|
// FIXME: Sort the FrameVarInfo vector by the ParentAlloca size and alignment
|
||||||
|
// and add padding as necessary to provide the proper alignment.
|
||||||
|
|
||||||
|
// Map the alloca instructions to the corresponding index in the
|
||||||
|
// frame allocation structure. If any alloca is used only in a single
|
||||||
|
// handler and is not used in the parent frame after outlining, it will
|
||||||
|
// be assigned an index of -1, meaning the handler can keep its
|
||||||
|
// "temporary" alloca and the original alloca can be erased from the
|
||||||
|
// parent function. If we later encounter this alloca in a second
|
||||||
|
// handler, we will assign it a place in the frame allocation structure
|
||||||
|
// at that time. Since the instruction replacement doesn't happen until
|
||||||
|
// all the entries in the HandlerData have been processed this isn't a
|
||||||
|
// problem.
|
||||||
|
for (auto &VarInfoEntry : FrameVarInfo) {
|
||||||
|
AllocaInst *ParentAlloca = VarInfoEntry.first;
|
||||||
|
HandlerAllocas &AllocaInfo = VarInfoEntry.second;
|
||||||
|
|
||||||
|
// If the instruction still has uses in the parent function or if it is
|
||||||
|
// referenced by more than one handler, add it to the frame allocation
|
||||||
|
// structure.
|
||||||
|
if (ParentAlloca->getNumUses() != 0 || AllocaInfo.Allocas.size() > 1) {
|
||||||
|
Type *VarTy = ParentAlloca->getAllocatedType();
|
||||||
|
StructTys.push_back(VarTy);
|
||||||
|
AllocaInfo.ParentFrameAllocationIndex = Idx++;
|
||||||
|
} else {
|
||||||
|
// If the variable is not used in the parent frame and it is only used
|
||||||
|
// in one handler, the alloca can be removed from the parent frame
|
||||||
|
// and the handler will keep its "temporary" alloca to define the value.
|
||||||
|
// An element index of -1 is used to indicate this condition.
|
||||||
|
AllocaInfo.ParentFrameAllocationIndex = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Having filled the StructTys vector and assigned an index to each element,
|
||||||
|
// we can now create the structure.
|
||||||
|
StructType *EHDataStructTy = StructType::create(
|
||||||
|
F.getContext(), StructTys, "struct." + F.getName().str() + ".ehdata");
|
||||||
|
IRBuilder<> Builder(F.getParent()->getContext());
|
||||||
|
|
||||||
|
// Create a frame allocation.
|
||||||
|
Module *M = F.getParent();
|
||||||
|
LLVMContext &Context = M->getContext();
|
||||||
|
BasicBlock *Entry = &F.getEntryBlock();
|
||||||
|
Builder.SetInsertPoint(Entry->getFirstInsertionPt());
|
||||||
|
Function *FrameAllocFn =
|
||||||
|
Intrinsic::getDeclaration(M, Intrinsic::frameallocate);
|
||||||
|
uint64_t EHAllocSize = M->getDataLayout()->getTypeAllocSize(EHDataStructTy);
|
||||||
|
Value *FrameAllocArgs[] = {
|
||||||
|
ConstantInt::get(Type::getInt32Ty(Context), EHAllocSize)};
|
||||||
|
CallInst *FrameAlloc =
|
||||||
|
Builder.CreateCall(FrameAllocFn, FrameAllocArgs, "frame.alloc");
|
||||||
|
|
||||||
|
Value *FrameEHData = Builder.CreateBitCast(
|
||||||
|
FrameAlloc, EHDataStructTy->getPointerTo(), "eh.data");
|
||||||
|
|
||||||
|
// Now visit each handler that is using the structure and bitcast its EHAlloc
|
||||||
|
// value to be a pointer to the frame alloc structure.
|
||||||
|
DenseMap<Function *, Value *> EHDataMap;
|
||||||
|
for (CallInst *EHAlloc : HandlerAllocs) {
|
||||||
|
// The EHAlloc has no uses at this time, so we need to just insert the
|
||||||
|
// cast before the next instruction. There is always a next instruction.
|
||||||
|
BasicBlock::iterator II = EHAlloc;
|
||||||
|
++II;
|
||||||
|
Builder.SetInsertPoint(cast<Instruction>(II));
|
||||||
|
Value *EHData = Builder.CreateBitCast(
|
||||||
|
EHAlloc, EHDataStructTy->getPointerTo(), "eh.data");
|
||||||
|
EHDataMap[EHAlloc->getParent()->getParent()] = EHData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next, replace the place-holder EHObjPtr allocas with GEP instructions
|
||||||
|
// that pull the EHObjPtr from the frame alloc structure
|
||||||
|
for (AllocaInst *EHObjPtr : HandlerEHObjPtrs) {
|
||||||
|
Value *EHData = EHDataMap[EHObjPtr->getParent()->getParent()];
|
||||||
|
Value *ElementPtr = Builder.CreateConstInBoundsGEP2_32(EHData, 0, 1);
|
||||||
|
EHObjPtr->replaceAllUsesWith(ElementPtr);
|
||||||
|
EHObjPtr->removeFromParent();
|
||||||
|
ElementPtr->takeName(EHObjPtr);
|
||||||
|
delete EHObjPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, replace all of the temporary allocas for frame variables used in
|
||||||
|
// the outlined handlers and the original frame allocas with GEP instructions
|
||||||
|
// that get the equivalent pointer from the frame allocation struct.
|
||||||
|
for (auto &VarInfoEntry : FrameVarInfo) {
|
||||||
|
AllocaInst *ParentAlloca = VarInfoEntry.first;
|
||||||
|
HandlerAllocas &AllocaInfo = VarInfoEntry.second;
|
||||||
|
int Idx = AllocaInfo.ParentFrameAllocationIndex;
|
||||||
|
|
||||||
|
// If we have an index of -1 for this instruction, it means it isn't used
|
||||||
|
// outside of this handler. In that case, we just keep the "temporary"
|
||||||
|
// alloca in the handler and erase the original alloca from the parent.
|
||||||
|
if (Idx == -1) {
|
||||||
|
ParentAlloca->eraseFromParent();
|
||||||
|
} else {
|
||||||
|
// Otherwise, we replace the parent alloca and all outlined allocas
|
||||||
|
// which map to it with GEP instructions.
|
||||||
|
|
||||||
|
// First replace the original alloca.
|
||||||
|
Builder.SetInsertPoint(ParentAlloca);
|
||||||
|
Builder.SetCurrentDebugLocation(ParentAlloca->getDebugLoc());
|
||||||
|
Value *ElementPtr =
|
||||||
|
Builder.CreateConstInBoundsGEP2_32(FrameEHData, 0, Idx);
|
||||||
|
ParentAlloca->replaceAllUsesWith(ElementPtr);
|
||||||
|
ParentAlloca->removeFromParent();
|
||||||
|
ElementPtr->takeName(ParentAlloca);
|
||||||
|
delete ParentAlloca;
|
||||||
|
|
||||||
|
// Next replace all outlined allocas that are mapped to it.
|
||||||
|
for (AllocaInst *TempAlloca : AllocaInfo.Allocas) {
|
||||||
|
Value *EHData = EHDataMap[TempAlloca->getParent()->getParent()];
|
||||||
|
// FIXME: Sink this GEP into the blocks where it is used.
|
||||||
|
Builder.SetInsertPoint(TempAlloca);
|
||||||
|
Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
|
||||||
|
ElementPtr = Builder.CreateConstInBoundsGEP2_32(EHData, 0, Idx);
|
||||||
|
TempAlloca->replaceAllUsesWith(ElementPtr);
|
||||||
|
TempAlloca->removeFromParent();
|
||||||
|
ElementPtr->takeName(TempAlloca);
|
||||||
|
delete TempAlloca;
|
||||||
|
}
|
||||||
|
} // end else of if (Idx == -1)
|
||||||
|
} // End for each FrameVarInfo entry.
|
||||||
|
|
||||||
return HandlersOutlined;
|
return HandlersOutlined;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WinEHPrepare::outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
bool WinEHPrepare::outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
||||||
LandingPadInst *LPad,
|
LandingPadInst *LPad, CallInst *&EHAlloc,
|
||||||
StructType *EHDataStructTy) {
|
AllocaInst *&EHObjPtr,
|
||||||
|
FrameVarInfoMap &VarInfo) {
|
||||||
Module *M = SrcFn->getParent();
|
Module *M = SrcFn->getParent();
|
||||||
LLVMContext &Context = M->getContext();
|
LLVMContext &Context = M->getContext();
|
||||||
|
|
||||||
@ -221,12 +411,11 @@ bool WinEHPrepare::outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
|||||||
Intrinsic::getDeclaration(M, Intrinsic::framerecover);
|
Intrinsic::getDeclaration(M, Intrinsic::framerecover);
|
||||||
Value *RecoverArgs[] = {Builder.CreateBitCast(SrcFn, Int8PtrType, ""),
|
Value *RecoverArgs[] = {Builder.CreateBitCast(SrcFn, Int8PtrType, ""),
|
||||||
&(CatchHandler->getArgumentList().back())};
|
&(CatchHandler->getArgumentList().back())};
|
||||||
CallInst *EHAlloc =
|
EHAlloc = Builder.CreateCall(RecoverFrameFn, RecoverArgs, "eh.alloc");
|
||||||
Builder.CreateCall(RecoverFrameFn, RecoverArgs, "eh.alloc");
|
|
||||||
Value *EHData =
|
// This alloca is only temporary. We'll be replacing it once we know all the
|
||||||
Builder.CreateBitCast(EHAlloc, EHDataStructTy->getPointerTo(), "ehdata");
|
// frame variables that need to go in the frame allocation structure.
|
||||||
Value *EHObjPtr =
|
EHObjPtr = Builder.CreateAlloca(Int8PtrType, 0, "eh.obj.ptr");
|
||||||
Builder.CreateConstInBoundsGEP2_32(EHData, 0, 1, "eh.obj.ptr");
|
|
||||||
|
|
||||||
// This will give us a raw pointer to the exception object, which
|
// This will give us a raw pointer to the exception object, which
|
||||||
// corresponds to the formal parameter of the catch statement. If the
|
// corresponds to the formal parameter of the catch statement. If the
|
||||||
@ -240,7 +429,7 @@ bool WinEHPrepare::outlineCatchHandler(Function *SrcFn, Constant *SelectorType,
|
|||||||
|
|
||||||
// FIXME: Map other values referenced in the filter handler.
|
// FIXME: Map other values referenced in the filter handler.
|
||||||
|
|
||||||
WinEHCatchDirector Director(LPad, SelectorType, EHObj);
|
WinEHCatchDirector Director(LPad, CatchHandler, SelectorType, EHObj, VarInfo);
|
||||||
|
|
||||||
SmallVector<ReturnInst *, 8> Returns;
|
SmallVector<ReturnInst *, 8> Returns;
|
||||||
ClonedCodeInfo InlinedFunctionInfo;
|
ClonedCodeInfo InlinedFunctionInfo;
|
||||||
@ -271,9 +460,11 @@ CloningDirector::CloningAction WinEHCatchDirector::handleInstruction(
|
|||||||
"Unexpected operation: extracting an unknown landing pad element");
|
"Unexpected operation: extracting an unknown landing pad element");
|
||||||
|
|
||||||
if (*(Extract->idx_begin()) == 0) {
|
if (*(Extract->idx_begin()) == 0) {
|
||||||
// Element 0 doesn't directly corresponds to anything in the WinEH scheme.
|
// Element 0 doesn't directly corresponds to anything in the WinEH
|
||||||
|
// scheme.
|
||||||
// It will be stored to a memory location, then later loaded and finally
|
// It will be stored to a memory location, then later loaded and finally
|
||||||
// the loaded value will be used as the argument to an llvm.eh.begincatch
|
// the loaded value will be used as the argument to an
|
||||||
|
// llvm.eh.begincatch
|
||||||
// call. We're tracking it here so that we can skip the store and load.
|
// call. We're tracking it here so that we can skip the store and load.
|
||||||
ExtractedEHPtr = Inst;
|
ExtractedEHPtr = Inst;
|
||||||
} else {
|
} else {
|
||||||
@ -362,10 +553,10 @@ CloningDirector::CloningAction WinEHCatchDirector::handleInstruction(
|
|||||||
const BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
|
const BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
|
||||||
assert(Branch && Branch->isUnconditional());
|
assert(Branch && Branch->isUnconditional());
|
||||||
assert(std::next(BasicBlock::const_iterator(IntrinCall)) ==
|
assert(std::next(BasicBlock::const_iterator(IntrinCall)) ==
|
||||||
BasicBlock::const_iterator(Branch));
|
BasicBlock::const_iterator(Branch));
|
||||||
|
|
||||||
ReturnInst::Create(NewBB->getContext(),
|
ReturnInst::Create(NewBB->getContext(),
|
||||||
BlockAddress::get(Branch->getSuccessor(0)), NewBB);
|
BlockAddress::get(Branch->getSuccessor(0)), NewBB);
|
||||||
|
|
||||||
// We just added a terminator to the cloned block.
|
// We just added a terminator to the cloned block.
|
||||||
// Tell the caller to stop processing the current basic block so that
|
// Tell the caller to stop processing the current basic block so that
|
||||||
@ -388,3 +579,49 @@ CloningDirector::CloningAction WinEHCatchDirector::handleInstruction(
|
|||||||
// Continue with the default cloning behavior.
|
// Continue with the default cloning behavior.
|
||||||
return CloningDirector::CloneInstruction;
|
return CloningDirector::CloneInstruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
|
||||||
|
Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo)
|
||||||
|
: OutlinedFn(OutlinedFn), FrameVarInfo(FrameVarInfo),
|
||||||
|
Builder(OutlinedFn->getContext()) {
|
||||||
|
Builder.SetInsertPoint(&OutlinedFn->getEntryBlock());
|
||||||
|
// FIXME: Do something with the FrameVarMapped so that it is shared across the
|
||||||
|
// function.
|
||||||
|
}
|
||||||
|
|
||||||
|
Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
|
||||||
|
// If we're asked to materialize an alloca variable, we temporarily
|
||||||
|
// create a matching alloca in the outlined function. When all the
|
||||||
|
// outlining is complete, we'll collect these into a structure and
|
||||||
|
// replace these temporary allocas with GEPs referencing the frame
|
||||||
|
// allocation block.
|
||||||
|
if (auto *AV = dyn_cast<AllocaInst>(V)) {
|
||||||
|
AllocaInst *NewAlloca = Builder.CreateAlloca(
|
||||||
|
AV->getAllocatedType(), AV->getArraySize(), AV->getName());
|
||||||
|
FrameVarInfo[AV].Allocas.push_back(NewAlloca);
|
||||||
|
return NewAlloca;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Do PHI nodes need special handling?
|
||||||
|
|
||||||
|
// FIXME: Are there other cases we can handle better? GEP, ExtractValue, etc.
|
||||||
|
|
||||||
|
// FIXME: This doesn't work during cloning because it finds an instruction
|
||||||
|
// in the use list that isn't yet part of a basic block.
|
||||||
|
#if 0
|
||||||
|
// If we're asked to remap some other instruction, we'll need to
|
||||||
|
// spill it to an alloca variable in the parent function and add a
|
||||||
|
// temporary alloca in the outlined function to be processed as
|
||||||
|
// described above.
|
||||||
|
Instruction *Inst = dyn_cast<Instruction>(V);
|
||||||
|
if (Inst) {
|
||||||
|
AllocaInst *Spill = DemoteRegToStack(*Inst, true);
|
||||||
|
AllocaInst *NewAlloca = Builder.CreateAlloca(Spill->getAllocatedType(),
|
||||||
|
Spill->getArraySize());
|
||||||
|
FrameVarMap[AV] = NewAlloca;
|
||||||
|
return NewAlloca;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
@ -261,6 +261,8 @@ namespace {
|
|||||||
ClonedCodeInfo *CodeInfo;
|
ClonedCodeInfo *CodeInfo;
|
||||||
const DataLayout *DL;
|
const DataLayout *DL;
|
||||||
CloningDirector *Director;
|
CloningDirector *Director;
|
||||||
|
ValueMapTypeRemapper *TypeMapper;
|
||||||
|
ValueMaterializer *Materializer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
|
PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
|
||||||
@ -274,6 +276,14 @@ namespace {
|
|||||||
VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
|
VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
|
||||||
NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL),
|
NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL),
|
||||||
Director(Director) {
|
Director(Director) {
|
||||||
|
// These are optional components. The Director may return null.
|
||||||
|
if (Director) {
|
||||||
|
TypeMapper = Director->getTypeRemapper();
|
||||||
|
Materializer = Director->getValueMaterializer();
|
||||||
|
} else {
|
||||||
|
TypeMapper = nullptr;
|
||||||
|
Materializer = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CloneBlock - The specified block is found to be reachable, clone it and
|
/// CloneBlock - The specified block is found to be reachable, clone it and
|
||||||
@ -344,7 +354,8 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
|
|||||||
// nodes for which we defer processing until we update the CFG.
|
// nodes for which we defer processing until we update the CFG.
|
||||||
if (!isa<PHINode>(NewInst)) {
|
if (!isa<PHINode>(NewInst)) {
|
||||||
RemapInstruction(NewInst, VMap,
|
RemapInstruction(NewInst, VMap,
|
||||||
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
||||||
|
TypeMapper, Materializer);
|
||||||
|
|
||||||
// If we can simplify this instruction to some other value, simply add
|
// If we can simplify this instruction to some other value, simply add
|
||||||
// a mapping to that value rather than inserting a new instruction into
|
// a mapping to that value rather than inserting a new instruction into
|
||||||
@ -459,6 +470,14 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
|
|||||||
CloningDirector *Director) {
|
CloningDirector *Director) {
|
||||||
assert(NameSuffix && "NameSuffix cannot be null!");
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
||||||
|
|
||||||
|
ValueMapTypeRemapper *TypeMapper = nullptr;
|
||||||
|
ValueMaterializer *Materializer = nullptr;
|
||||||
|
|
||||||
|
if (Director) {
|
||||||
|
TypeMapper = Director->getTypeRemapper();
|
||||||
|
Materializer = Director->getValueMaterializer();
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
// If the cloning starts at the begining of the function, verify that
|
// If the cloning starts at the begining of the function, verify that
|
||||||
// the function arguments are mapped.
|
// the function arguments are mapped.
|
||||||
@ -513,7 +532,8 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
|
|||||||
// Finally, remap the terminator instructions, as those can't be remapped
|
// Finally, remap the terminator instructions, as those can't be remapped
|
||||||
// until all BBs are mapped.
|
// until all BBs are mapped.
|
||||||
RemapInstruction(NewBB->getTerminator(), VMap,
|
RemapInstruction(NewBB->getTerminator(), VMap,
|
||||||
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
||||||
|
TypeMapper, Materializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defer PHI resolution until rest of function is resolved, PHI resolution
|
// Defer PHI resolution until rest of function is resolved, PHI resolution
|
||||||
|
@ -55,8 +55,8 @@ try.cont: ; preds = %invoke.cont2, %invo
|
|||||||
; CHECK: define i8* @_Z4testv.catch(i8*, i8*) {
|
; CHECK: define i8* @_Z4testv.catch(i8*, i8*) {
|
||||||
; CHECK: catch.entry:
|
; CHECK: catch.entry:
|
||||||
; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
|
; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
|
||||||
; CHECK: %ehdata = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
|
; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
|
||||||
; CHECK: %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata* %ehdata, i32 0, i32 1
|
; CHECK: %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
|
||||||
; CHECK: %eh.obj = load i8** %eh.obj.ptr
|
; CHECK: %eh.obj = load i8** %eh.obj.ptr
|
||||||
; CHECK: call void @_Z16handle_exceptionv()
|
; CHECK: call void @_Z16handle_exceptionv()
|
||||||
; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
|
; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
; {
|
; {
|
||||||
; try {
|
; try {
|
||||||
; may_throw();
|
; may_throw();
|
||||||
; } catch (int) {
|
; } catch (int i) {
|
||||||
; handle_int();
|
; handle_int(i);
|
||||||
; }
|
; }
|
||||||
; }
|
; }
|
||||||
;
|
;
|
||||||
@ -18,13 +18,27 @@
|
|||||||
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
|
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
|
||||||
target triple = "x86_64-pc-windows-msvc"
|
target triple = "x86_64-pc-windows-msvc"
|
||||||
|
|
||||||
|
; This is the structure that will get created for the frame allocation.
|
||||||
|
; CHECK: %struct._Z4testv.ehdata = type { i32, i8*, i32 }
|
||||||
|
|
||||||
@_ZTIi = external constant i8*
|
@_ZTIi = external constant i8*
|
||||||
|
|
||||||
|
; The function entry will be rewritten like this.
|
||||||
|
; CHECK: define void @_Z4testv() #0 {
|
||||||
|
; CHECK: entry:
|
||||||
|
; CHECK: %frame.alloc = call i8* @llvm.frameallocate(i32 24)
|
||||||
|
; CHECK: %eh.data = bitcast i8* %frame.alloc to %struct._Z4testv.ehdata*
|
||||||
|
; CHECK: %exn.slot = alloca i8*
|
||||||
|
; CHECK: %ehselector.slot = alloca i32
|
||||||
|
; CHECK-NOT: %i = alloca i32, align 4
|
||||||
|
; CHECK: %i = getelementptr inbounds %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2
|
||||||
|
|
||||||
; Function Attrs: uwtable
|
; Function Attrs: uwtable
|
||||||
define void @_Z4testv() #0 {
|
define void @_Z4testv() #0 {
|
||||||
entry:
|
entry:
|
||||||
%exn.slot = alloca i8*
|
%exn.slot = alloca i8*
|
||||||
%ehselector.slot = alloca i32
|
%ehselector.slot = alloca i32
|
||||||
|
%i = alloca i32, align 4
|
||||||
invoke void @_Z9may_throwv()
|
invoke void @_Z9may_throwv()
|
||||||
to label %invoke.cont unwind label %lpad
|
to label %invoke.cont unwind label %lpad
|
||||||
|
|
||||||
@ -50,7 +64,10 @@ catch: ; preds = %catch.dispatch
|
|||||||
%exn11 = load i8** %exn.slot
|
%exn11 = load i8** %exn.slot
|
||||||
%4 = call i8* @llvm.eh.begincatch(i8* %exn11) #3
|
%4 = call i8* @llvm.eh.begincatch(i8* %exn11) #3
|
||||||
%5 = bitcast i8* %4 to i32*
|
%5 = bitcast i8* %4 to i32*
|
||||||
call void @_Z10handle_intv()
|
%6 = load i32* %5, align 4
|
||||||
|
store i32 %6, i32* %i, align 4
|
||||||
|
%7 = load i32* %i, align 4
|
||||||
|
call void @_Z10handle_inti(i32 %7)
|
||||||
br label %invoke.cont2
|
br label %invoke.cont2
|
||||||
|
|
||||||
invoke.cont2: ; preds = %catch
|
invoke.cont2: ; preds = %catch
|
||||||
@ -71,11 +88,15 @@ eh.resume: ; preds = %catch.dispatch
|
|||||||
; CHECK: define i8* @_Z4testv.catch(i8*, i8*) {
|
; CHECK: define i8* @_Z4testv.catch(i8*, i8*) {
|
||||||
; CHECK: catch.entry:
|
; CHECK: catch.entry:
|
||||||
; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
|
; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
|
||||||
; CHECK: %ehdata = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
|
; CHECK: %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
|
||||||
; CHECK: %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata* %ehdata, i32 0, i32 1
|
; CHECK: %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
|
||||||
; CHECK: %eh.obj = load i8** %eh.obj.ptr
|
; CHECK: %eh.obj = load i8** %eh.obj.ptr
|
||||||
|
; CHECK: %i = getelementptr inbounds %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2
|
||||||
; CHECK: %2 = bitcast i8* %eh.obj to i32*
|
; CHECK: %2 = bitcast i8* %eh.obj to i32*
|
||||||
; CHECK: call void @_Z10handle_intv()
|
; CHECK: %3 = load i32* %2, align 4
|
||||||
|
; CHECK: store i32 %3, i32* %i, align 4
|
||||||
|
; CHECK: %4 = load i32* %i, align 4
|
||||||
|
; CHECK: call void @_Z10handle_inti(i32 %4)
|
||||||
; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
|
; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
|
||||||
; CHECK: }
|
; CHECK: }
|
||||||
|
|
||||||
@ -90,7 +111,7 @@ declare i8* @llvm.eh.begincatch(i8*)
|
|||||||
|
|
||||||
declare void @llvm.eh.endcatch()
|
declare void @llvm.eh.endcatch()
|
||||||
|
|
||||||
declare void @_Z10handle_intv() #1
|
declare void @_Z10handle_inti(i32) #1
|
||||||
|
|
||||||
attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
||||||
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user