10b57cec5SDimitry Andric //===- EscapeEnumerator.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Defines a helper class that enumerates all possible exits from a function,
100b57cec5SDimitry Andric // including exception handling.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Transforms/Utils/EscapeEnumerator.h"
1506c3fb27SDimitry Andric #include "llvm/IR/EHPersonalities.h"
160b57cec5SDimitry Andric #include "llvm/IR/Module.h"
1706c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
185ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/Local.h"
195ffd83dbSDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
getDefaultPersonalityFn(Module * M)220b57cec5SDimitry Andric static FunctionCallee getDefaultPersonalityFn(Module *M) {
230b57cec5SDimitry Andric   LLVMContext &C = M->getContext();
240b57cec5SDimitry Andric   Triple T(M->getTargetTriple());
250b57cec5SDimitry Andric   EHPersonality Pers = getDefaultEHPersonality(T);
260b57cec5SDimitry Andric   return M->getOrInsertFunction(getEHPersonalityName(Pers),
270b57cec5SDimitry Andric                                 FunctionType::get(Type::getInt32Ty(C), true));
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric 
Next()300b57cec5SDimitry Andric IRBuilder<> *EscapeEnumerator::Next() {
310b57cec5SDimitry Andric   if (Done)
320b57cec5SDimitry Andric     return nullptr;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   // Find all 'return', 'resume', and 'unwind' instructions.
350b57cec5SDimitry Andric   while (StateBB != StateE) {
360b57cec5SDimitry Andric     BasicBlock *CurBB = &*StateBB++;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric     // Branches and invokes do not escape, only unwind, resume, and return
390b57cec5SDimitry Andric     // do.
400b57cec5SDimitry Andric     Instruction *TI = CurBB->getTerminator();
410b57cec5SDimitry Andric     if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
420b57cec5SDimitry Andric       continue;
430b57cec5SDimitry Andric 
44e8d8bef9SDimitry Andric     if (CallInst *CI = CurBB->getTerminatingMustTailCall())
45e8d8bef9SDimitry Andric       TI = CI;
460b57cec5SDimitry Andric     Builder.SetInsertPoint(TI);
470b57cec5SDimitry Andric     return &Builder;
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   Done = true;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   if (!HandleExceptions)
530b57cec5SDimitry Andric     return nullptr;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   if (F.doesNotThrow())
560b57cec5SDimitry Andric     return nullptr;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // Find all 'call' instructions that may throw.
59e8d8bef9SDimitry Andric   // We cannot tranform calls with musttail tag.
600b57cec5SDimitry Andric   SmallVector<Instruction *, 16> Calls;
610b57cec5SDimitry Andric   for (BasicBlock &BB : F)
620b57cec5SDimitry Andric     for (Instruction &II : BB)
630b57cec5SDimitry Andric       if (CallInst *CI = dyn_cast<CallInst>(&II))
64e8d8bef9SDimitry Andric         if (!CI->doesNotThrow() && !CI->isMustTailCall())
650b57cec5SDimitry Andric           Calls.push_back(CI);
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   if (Calls.empty())
680b57cec5SDimitry Andric     return nullptr;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // Create a cleanup block.
710b57cec5SDimitry Andric   LLVMContext &C = F.getContext();
720b57cec5SDimitry Andric   BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
735f757f3fSDimitry Andric   Type *ExnTy = StructType::get(PointerType::getUnqual(C), Type::getInt32Ty(C));
740b57cec5SDimitry Andric   if (!F.hasPersonalityFn()) {
750b57cec5SDimitry Andric     FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
760b57cec5SDimitry Andric     F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
800b57cec5SDimitry Andric     report_fatal_error("Scoped EH not supported");
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   LandingPadInst *LPad =
840b57cec5SDimitry Andric       LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
850b57cec5SDimitry Andric   LPad->setCleanup(true);
860b57cec5SDimitry Andric   ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   // Transform the 'call' instructions into 'invoke's branching to the
890b57cec5SDimitry Andric   // cleanup block. Go in reverse order to make prettier BB names.
900b57cec5SDimitry Andric   SmallVector<Value *, 16> Args;
910b57cec5SDimitry Andric   for (unsigned I = Calls.size(); I != 0;) {
920b57cec5SDimitry Andric     CallInst *CI = cast<CallInst>(Calls[--I]);
93fe6060f1SDimitry Andric     changeToInvokeAndSplitBasicBlock(CI, CleanupBB, DTU);
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   Builder.SetInsertPoint(RI);
970b57cec5SDimitry Andric   return &Builder;
980b57cec5SDimitry Andric }
99