OLD | NEW |
(Empty) | |
| 1 //===- RemoveAsmMemory.cpp - Remove ``asm("":::"memory")`` ----------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This pass removes all instances of ``asm("":::"memory")``. |
| 11 // |
| 12 //===----------------------------------------------------------------------===// |
| 13 |
| 14 #include "llvm/ADT/Twine.h" |
| 15 #include "llvm/IR/Function.h" |
| 16 #include "llvm/IR/InlineAsm.h" |
| 17 #include "llvm/IR/Instructions.h" |
| 18 #include "llvm/IR/Module.h" |
| 19 #include "llvm/InstVisitor.h" |
| 20 #include "llvm/Pass.h" |
| 21 #include <string> |
| 22 |
| 23 using namespace llvm; |
| 24 |
| 25 namespace { |
| 26 class RemoveAsmMemory : public FunctionPass { |
| 27 public: |
| 28 static char ID; // Pass identification, replacement for typeid |
| 29 RemoveAsmMemory() : FunctionPass(ID) { |
| 30 initializeRemoveAsmMemoryPass(*PassRegistry::getPassRegistry()); |
| 31 } |
| 32 |
| 33 virtual bool runOnFunction(Function &F); |
| 34 }; |
| 35 |
| 36 class AsmDirectivesVisitor : public InstVisitor<AsmDirectivesVisitor> { |
| 37 public: |
| 38 AsmDirectivesVisitor(Function &F) |
| 39 : F(F), C(F.getParent()->getContext()), ModifiedFunction(false) {} |
| 40 ~AsmDirectivesVisitor() {} |
| 41 bool modifiedFunction() const { return ModifiedFunction; } |
| 42 |
| 43 /// Only Call Instructions are ever inline assembly directives. |
| 44 void visitCallInst(CallInst &CI); |
| 45 |
| 46 private: |
| 47 Function &F; |
| 48 LLVMContext &C; |
| 49 bool ModifiedFunction; |
| 50 |
| 51 AsmDirectivesVisitor() LLVM_DELETED_FUNCTION; |
| 52 AsmDirectivesVisitor(const AsmDirectivesVisitor &) LLVM_DELETED_FUNCTION; |
| 53 AsmDirectivesVisitor &operator=( |
| 54 const AsmDirectivesVisitor &) LLVM_DELETED_FUNCTION; |
| 55 }; |
| 56 } |
| 57 |
| 58 char RemoveAsmMemory::ID = 0; |
| 59 INITIALIZE_PASS(RemoveAsmMemory, "remove-asm-memory", |
| 60 "remove all instances of ``asm(\"\":::\"memory\")``", false, |
| 61 false) |
| 62 |
| 63 bool RemoveAsmMemory::runOnFunction(Function &F) { |
| 64 AsmDirectivesVisitor AV(F); |
| 65 AV.visit(F); |
| 66 return AV.modifiedFunction(); |
| 67 } |
| 68 |
| 69 void AsmDirectivesVisitor::visitCallInst(CallInst &CI) { |
| 70 if (!CI.isInlineAsm() || |
| 71 !cast<InlineAsm>(CI.getCalledValue())->isAsmMemory()) |
| 72 return; |
| 73 |
| 74 // In NaCl ``asm("":::"memory")`` always comes in pairs, straddling a |
| 75 // sequentially consistent fence. Other passes rewrite this fence to |
| 76 // an equivalent stable NaCl intrinsic, meaning that this assembly can |
| 77 // be removed. |
| 78 CI.eraseFromParent(); |
| 79 ModifiedFunction = true; |
| 80 } |
| 81 |
| 82 namespace llvm { |
| 83 FunctionPass *createRemoveAsmMemoryPass() { return new RemoveAsmMemory(); } |
| 84 } |
OLD | NEW |