aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms
diff options
context:
space:
mode:
authorMark Seaborn <mseaborn@chromium.org>2013-06-29 17:56:31 -0700
committerMark Seaborn <mseaborn@chromium.org>2013-06-29 17:56:31 -0700
commit7e6b35b1f14efaa250dfcf9d90aba90fda227ba4 (patch)
treeaa2ffebf7b6df616a312d1d847be27103b910338 /lib/Transforms
parentf2518db25a8d0cacebb82d2f8872b00014a03bc8 (diff)
PNaCl ABI: Remove use of @llvm.memset.p0i8.i64 (64-bit intrinsic variant)
Convert calls to this intrinsic to use the 32-bit variant instead. Do the same for the memcpy and memmove intrinsics too. Change the PNaCl ABI verifier to check this argument. BUG=https://code.google.com/p/nativeclient/issues/detail?id=3530 TEST=*.ll tests + PNaCl toolchain trybots Review URL: https://codereview.chromium.org/18226003
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/NaCl/CMakeLists.txt1
-rw-r--r--lib/Transforms/NaCl/CanonicalizeMemIntrinsics.cpp95
-rw-r--r--lib/Transforms/NaCl/PNaClABISimplify.cpp5
3 files changed, 101 insertions, 0 deletions
diff --git a/lib/Transforms/NaCl/CMakeLists.txt b/lib/Transforms/NaCl/CMakeLists.txt
index 49c5153bf3..9cf164926b 100644
--- a/lib/Transforms/NaCl/CMakeLists.txt
+++ b/lib/Transforms/NaCl/CMakeLists.txt
@@ -1,5 +1,6 @@
add_llvm_library(LLVMNaClTransforms
AddPNaClExternalDecls.cpp
+ CanonicalizeMemIntrinsics.cpp
ExpandArithWithOverflow.cpp
ExpandByVal.cpp
ExpandConstantExpr.cpp
diff --git a/lib/Transforms/NaCl/CanonicalizeMemIntrinsics.cpp b/lib/Transforms/NaCl/CanonicalizeMemIntrinsics.cpp
new file mode 100644
index 0000000000..fd44c65434
--- /dev/null
+++ b/lib/Transforms/NaCl/CanonicalizeMemIntrinsics.cpp
@@ -0,0 +1,95 @@
+//===- CanonicalizeMemIntrinsics.cpp - Make memcpy's "len" arg consistent--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass canonicalizes uses of the llvm.memset, llvm.memcpy and
+// llvm.memmove intrinsics so that the variants with 64-bit "len"
+// arguments aren't used, and the 32-bit variants are used instead.
+//
+// This means the PNaCl translator won't need to handle two versions
+// of each of these intrinsics, and it won't need to do any implicit
+// truncations from 64-bit to 32-bit.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.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 {
+ // This is a ModulePass because that makes it easier to find all
+ // uses of intrinsics efficiently.
+ class CanonicalizeMemIntrinsics : public ModulePass {
+ public:
+ static char ID; // Pass identification, replacement for typeid
+ CanonicalizeMemIntrinsics() : ModulePass(ID) {
+ initializeCanonicalizeMemIntrinsicsPass(*PassRegistry::getPassRegistry());
+ }
+
+ virtual bool runOnModule(Module &M);
+ };
+}
+
+char CanonicalizeMemIntrinsics::ID = 0;
+INITIALIZE_PASS(CanonicalizeMemIntrinsics, "canonicalize-mem-intrinsics",
+ "Make memcpy() et al's \"len\" argument consistent",
+ false, false)
+
+static bool expandIntrinsic(Module *M, Intrinsic::ID ID) {
+ SmallVector<Type *, 3> Types;
+ Types.push_back(Type::getInt8PtrTy(M->getContext()));
+ if (ID != Intrinsic::memset)
+ Types.push_back(Type::getInt8PtrTy(M->getContext()));
+ unsigned LengthTypePos = Types.size();
+ Types.push_back(Type::getInt64Ty(M->getContext()));
+
+ std::string OldName = Intrinsic::getName(ID, Types);
+ Function *OldIntrinsic = M->getFunction(OldName);
+ if (!OldIntrinsic)
+ return false;
+
+ Types[LengthTypePos] = Type::getInt32Ty(M->getContext());
+ Function *NewIntrinsic = Intrinsic::getDeclaration(M, ID, Types);
+
+ for (Value::use_iterator CallIter = OldIntrinsic->use_begin(),
+ E = OldIntrinsic->use_end(); CallIter != E; ) {
+ CallInst *Call = dyn_cast<CallInst>(*CallIter++);
+ if (!Call) {
+ report_fatal_error("CanonicalizeMemIntrinsics: Taking the address of an "
+ "intrinsic is not allowed: " + OldName);
+ }
+ // This temporarily leaves Call non-well-typed.
+ Call->setCalledFunction(NewIntrinsic);
+ // Truncate the "len" argument. No overflow check.
+ IRBuilder<> Builder(Call);
+ Value *Length = Builder.CreateTrunc(Call->getArgOperand(2),
+ Type::getInt32Ty(M->getContext()),
+ "mem_len_truncate");
+ Call->setArgOperand(2, Length);
+ }
+ OldIntrinsic->eraseFromParent();
+ return true;
+}
+
+bool CanonicalizeMemIntrinsics::runOnModule(Module &M) {
+ bool Changed = false;
+ Changed |= expandIntrinsic(&M, Intrinsic::memset);
+ Changed |= expandIntrinsic(&M, Intrinsic::memcpy);
+ Changed |= expandIntrinsic(&M, Intrinsic::memmove);
+ return Changed;
+}
+
+ModulePass *llvm::createCanonicalizeMemIntrinsicsPass() {
+ return new CanonicalizeMemIntrinsics();
+}
diff --git a/lib/Transforms/NaCl/PNaClABISimplify.cpp b/lib/Transforms/NaCl/PNaClABISimplify.cpp
index 056b0ae651..81fce8ce13 100644
--- a/lib/Transforms/NaCl/PNaClABISimplify.cpp
+++ b/lib/Transforms/NaCl/PNaClABISimplify.cpp
@@ -67,6 +67,11 @@ void llvm::PNaClABISimplifyAddPostOptPasses(PassManager &PM) {
PM.add(createPromoteI1OpsPass());
+ // Optimization passes and ExpandByVal introduce
+ // memset/memcpy/memmove intrinsics with a 64-bit size argument.
+ // This pass converts those arguments to 32-bit.
+ PM.add(createCanonicalizeMemIntrinsicsPass());
+
// We place StripMetadata after optimization passes because
// optimizations depend on the metadata.
PM.add(createStripMetadataPass());