|
| 1 | +//===- SplitBlocksAfterCalls.cpp -===// |
| 2 | +// |
| 3 | +// Makes function calls effectively terminators by splitting blocks after each |
| 4 | +// call. This ensures that there can only be at most one call per block. This |
| 5 | +// is used in order to detect recursion and external function calls within a |
| 6 | +// trace. |
| 7 | + |
| 8 | +#include "llvm/Transforms/Yk/SplitBlocksAfterCalls.h" |
| 9 | +#include "llvm/IR/BasicBlock.h" |
| 10 | +#include "llvm/IR/Function.h" |
| 11 | +#include "llvm/IR/IRBuilder.h" |
| 12 | +#include "llvm/IR/Instructions.h" |
| 13 | +#include "llvm/IR/Module.h" |
| 14 | +#include "llvm/IR/Verifier.h" |
| 15 | +#include "llvm/InitializePasses.h" |
| 16 | +#include "llvm/Pass.h" |
| 17 | +#include "llvm/Transforms/Yk/LivenessAnalysis.h" |
| 18 | + |
| 19 | +#include <map> |
| 20 | + |
| 21 | +#define DEBUG_TYPE "yk-splitblocksaftercalls" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +namespace llvm { |
| 26 | +void initializeYkSplitBlocksAfterCallsPass(PassRegistry &); |
| 27 | +} // namespace llvm |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +class YkSplitBlocksAfterCalls : public ModulePass { |
| 32 | +public: |
| 33 | + static char ID; |
| 34 | + YkSplitBlocksAfterCalls() : ModulePass(ID) { |
| 35 | + initializeYkSplitBlocksAfterCallsPass(*PassRegistry::getPassRegistry()); |
| 36 | + } |
| 37 | + |
| 38 | + bool runOnModule(Module &M) override { |
| 39 | + LLVMContext &Context = M.getContext(); |
| 40 | + |
| 41 | + for (Function &F : M) { |
| 42 | + if (F.empty()) // skip declarations. |
| 43 | + continue; |
| 44 | + // As we will be modifying the blocks of this function inplace, we |
| 45 | + // require a work list to process all existing and newly inserted blocks |
| 46 | + // in order to not miss any. |
| 47 | + std::vector<BasicBlock *> Todo; |
| 48 | + std::set<BasicBlock *> Seen; |
| 49 | + BasicBlock &Entry = F.getEntryBlock(); |
| 50 | + |
| 51 | + // This pass requires the `NoCallsInEntryBlocksPass` to have run first, |
| 52 | + // which in turn needs to run before the shadowstack pass. Otherwise, |
| 53 | + // this pass would split the block after the shadowstack malloc, which |
| 54 | + // results in allocas outside of the entry block which breaks stackmaps. |
| 55 | + Instruction *T = Entry.getTerminator(); |
| 56 | + for (size_t I = 0; I < T->getNumSuccessors(); I++) { |
| 57 | + Todo.push_back(T->getSuccessor(I)); |
| 58 | + } |
| 59 | + Seen.insert(&Entry); |
| 60 | + while (!Todo.empty()) { |
| 61 | + BasicBlock *Next = Todo.back(); |
| 62 | + Todo.pop_back(); |
| 63 | + if (Seen.count(Next) > 0) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + Seen.insert(Next); |
| 67 | + |
| 68 | + for (Instruction &I : *Next) { |
| 69 | + if (I.isDebugOrPseudoInst()) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (isa<CallInst>(I)) { |
| 73 | + // YKFIXME: Can we determine at compile time if inline asm contains |
| 74 | + // calls or jumps, e.g. via `getAsmString`, and then not split the |
| 75 | + // block after them? |
| 76 | + CallInst *CI = cast<CallInst>(&I); |
| 77 | + Function *F = CI->getCalledFunction(); |
| 78 | + if (F && F->getName() == "llvm.frameaddress.p0") { |
| 79 | + // This call is always inlined so we don't need to split the |
| 80 | + // block here. |
| 81 | + continue; |
| 82 | + } |
| 83 | + // YKFIXME: If the next instruction is an unconditional branch, we |
| 84 | + // don't need to split the block here. |
| 85 | + |
| 86 | + // Since `splitBasicBlock` splits before the given instruction, |
| 87 | + // pass the instruction following this call instead. |
| 88 | + Next->splitBasicBlock(I.getNextNode()); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Add successors to todo list. |
| 94 | + Instruction *T = Next->getTerminator(); |
| 95 | + for (size_t I = 0; I < T->getNumSuccessors(); I++) { |
| 96 | + Todo.insert(Todo.begin(), T->getSuccessor(I)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +#ifndef NDEBUG |
| 102 | + // Our pass runs after LLVM normally does its verify pass. In debug builds |
| 103 | + // we run it again to check that our pass is generating valid IR. |
| 104 | + if (verifyModule(M, &errs())) { |
| 105 | + Context.emitError("Stackmap insertion pass generated invalid IR!"); |
| 106 | + return false; |
| 107 | + } |
| 108 | +#endif |
| 109 | + return true; |
| 110 | + } |
| 111 | +}; |
| 112 | +} // namespace |
| 113 | + |
| 114 | +char YkSplitBlocksAfterCalls::ID = 0; |
| 115 | +INITIALIZE_PASS(YkSplitBlocksAfterCalls, DEBUG_TYPE, "yk stackmaps", false, |
| 116 | + false) |
| 117 | + |
| 118 | +ModulePass *llvm::createYkSplitBlocksAfterCallsPass() { |
| 119 | + return new YkSplitBlocksAfterCalls(); |
| 120 | +} |
0 commit comments