diff options
| author | Eli Bendersky <eliben@chromium.org> | 2013-05-28 11:23:28 -0700 |
|---|---|---|
| committer | Eli Bendersky <eliben@chromium.org> | 2013-05-28 11:23:28 -0700 |
| commit | ffc13bcb40d4257202295fc0ffe25b38bdf64263 (patch) | |
| tree | e3269675fec1127c195d6b3c248a6a47376b32f0 /lib/Transforms | |
| parent | a7b1caf4ea69d2cc6ff207fcef7085494bbc686a (diff) | |
Add two passes that implement conversions from PNaCl's specific intrinsics
to external function calls during the translation stage (llc).
One of the passes is a ModulePass that adds the appropriate function
declarations to the module. The other is a FunctionPass that performs the
actual call replacement. This split exists because of bitcode streaming.
Initially the passes handle the llvm.nacl.{set|long}jmp intrinsics. In the
future they may handle additional intrinsics that are part of the PNaCl
stable bitcode ABI.
This CL also removes the previous approach to handling this conversion
(in SelectionDAGBuilder.cpp). That ended up not working - more details in
issue 3429.
BUG=https://code.google.com/p/nativeclient/issues/detail?id=3429
R=mseaborn@chromium.org
Review URL: https://codereview.chromium.org/16047002
Diffstat (limited to 'lib/Transforms')
| -rw-r--r-- | lib/Transforms/NaCl/AddPNaClExternalDecls.cpp | 71 | ||||
| -rw-r--r-- | lib/Transforms/NaCl/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | lib/Transforms/NaCl/ResolvePNaClIntrinsics.cpp | 100 |
3 files changed, 175 insertions, 0 deletions
diff --git a/lib/Transforms/NaCl/AddPNaClExternalDecls.cpp b/lib/Transforms/NaCl/AddPNaClExternalDecls.cpp new file mode 100644 index 0000000000..f96db09b2f --- /dev/null +++ b/lib/Transforms/NaCl/AddPNaClExternalDecls.cpp @@ -0,0 +1,71 @@ +//===- AddPNaClExternalDecls.cpp - Add decls for PNaCl external functions -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This pass adds function declarations for external functions used by PNaCl. +// These externals are implemented in native libraries and calls to them are +// created as part of the translation process. +// +// Running this pass is a precondition for running ResolvePNaClIntrinsics. They +// are separate because one is a ModulePass and the other is a FunctionPass. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" +#include "llvm/Pass.h" +#include "llvm/Transforms/NaCl.h" + +using namespace llvm; + +namespace { + // This is a module pass because it adds declarations to the module. + class AddPNaClExternalDecls : public ModulePass { + public: + static char ID; + AddPNaClExternalDecls() : ModulePass(ID) { + initializeAddPNaClExternalDeclsPass(*PassRegistry::getPassRegistry()); + } + + virtual bool runOnModule(Module &M); + }; +} + +bool AddPNaClExternalDecls::runOnModule(Module &M) { + // Add declarations for a pre-defined set of external functions to the module. + // The function names must match the functions implemented in native code (in + // pnacl/support). The function types must match the types of the LLVM + // intrinsics. + // We expect these declarations not to exist in the module before this pass + // runs, but don't assert it; it will be handled by the ABI verifier. + LLVMContext &C = M.getContext(); + M.getOrInsertFunction("setjmp", + // return type + Type::getInt32Ty(C), + // arguments + Type::getInt8Ty(C)->getPointerTo(), + NULL); + M.getOrInsertFunction("longjmp", + // return type + Type::getVoidTy(C), + // arguments + Type::getInt8Ty(C)->getPointerTo(), + Type::getInt32Ty(C), + NULL); + return true; +} + +char AddPNaClExternalDecls::ID = 0; +INITIALIZE_PASS(AddPNaClExternalDecls, "add-pnacl-external-decls", + "Add declarations of external functions used by PNaCl", + false, false) + +ModulePass *llvm::createAddPNaClExternalDeclsPass() { + return new AddPNaClExternalDecls(); +} diff --git a/lib/Transforms/NaCl/CMakeLists.txt b/lib/Transforms/NaCl/CMakeLists.txt index a98d929f83..14225d79b8 100644 --- a/lib/Transforms/NaCl/CMakeLists.txt +++ b/lib/Transforms/NaCl/CMakeLists.txt @@ -1,4 +1,7 @@ +set(LLVM_LINK_COMPONENTS ipo) + add_llvm_library(LLVMNaClTransforms + AddPNaClExternalDecls.cpp ExpandByVal.cpp ExpandConstantExpr.cpp ExpandCtors.cpp @@ -14,6 +17,7 @@ add_llvm_library(LLVMNaClTransforms PNaClABISimplify.cpp PromoteIntegers.cpp ReplacePtrsWithInts.cpp + ResolvePNaClIntrinsics.cpp RewritePNaClLibraryCalls.cpp StripMetadata.cpp ) diff --git a/lib/Transforms/NaCl/ResolvePNaClIntrinsics.cpp b/lib/Transforms/NaCl/ResolvePNaClIntrinsics.cpp new file mode 100644 index 0000000000..e4efeb67c3 --- /dev/null +++ b/lib/Transforms/NaCl/ResolvePNaClIntrinsics.cpp @@ -0,0 +1,100 @@ +//===- ResolvePNaClIntrinsics.cpp - Resolve calls to PNaCl intrinsics ----====// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This pass resolves calls to PNaCl stable bitcode intrinsics. It is +// normally run in the PNaCl translator. +// +// Running AddPNaClExternalDeclsPass is a precondition for running this pass. +// They are separate because one is a ModulePass and the other is a +// FunctionPass. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/SmallVector.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/Intrinsics.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" +#include "llvm/Transforms/NaCl.h" + +using namespace llvm; + +namespace { + class ResolvePNaClIntrinsics : public FunctionPass { + public: + ResolvePNaClIntrinsics() : FunctionPass(ID) { + initializeResolvePNaClIntrinsicsPass(*PassRegistry::getPassRegistry()); + } + + static char ID; + virtual bool runOnFunction(Function &F); + private: + // Some intrinsic calls are resolved simply by replacing the call with a + // call to an alternative function with exactly the same type. + bool resolveSimpleCall(Function &F, Intrinsic::ID IntrinsicID, + const char *TargetFunctionName); + }; +} + +bool ResolvePNaClIntrinsics::resolveSimpleCall(Function &F, + Intrinsic::ID IntrinsicID, + const char *TargetFunctionName) { + Module *M = F.getParent(); + bool Changed = false; + Function *IntrinsicFunction = Intrinsic::getDeclaration(M, IntrinsicID); + + if (!IntrinsicFunction) { + return false; + } + + // Expect to find the target function for this intrinsic already declared + Function *TargetFunction = M->getFunction(TargetFunctionName); + if (!TargetFunction) { + report_fatal_error( + std::string("Expected to find external declaration of ") + + TargetFunctionName); + } + + for (Value::use_iterator UI = IntrinsicFunction->use_begin(), + UE = IntrinsicFunction->use_end(); UI != UE;) { + // At this point, the only uses of the intrinsic can be calls, since + // we assume this pass runs on bitcode that passed ABI verification. + CallInst *Call = dyn_cast<CallInst>(*UI++); + + if (!Call) { + report_fatal_error( + std::string("Expected use of intrinsic to be a call: ") + + Intrinsic::getName(IntrinsicID)); + } + + // To be a well-behaving FunctionPass, don't touch uses in other + // functions. These will be handled when the pass manager gets to those + // functions. + if (Call->getParent()->getParent() == &F) { + Call->setCalledFunction(TargetFunction); + Changed = true; + } + } + + return Changed; +} + +bool ResolvePNaClIntrinsics::runOnFunction(Function &F) { + bool Changed = resolveSimpleCall(F, Intrinsic::nacl_setjmp, "setjmp"); + Changed |= resolveSimpleCall(F, Intrinsic::nacl_longjmp, "longjmp"); + return Changed; +} + +char ResolvePNaClIntrinsics::ID = 0; +INITIALIZE_PASS(ResolvePNaClIntrinsics, "resolve-pnacl-intrinsics", + "Resolve PNaCl intrinsic calls", false, false) + +FunctionPass *llvm::createResolvePNaClIntrinsicsPass() { + return new ResolvePNaClIntrinsics(); +} |
