Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1600)

Side by Side Diff: lib/Transforms/NaCl/CanonicalizeMemIntrinsics.cpp

Issue 939073008: Rebased PNaCl localmods in LLVM to 223109 (Closed)
Patch Set: undo localmod Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- CanonicalizeMemIntrinsics.cpp - Make memcpy's "len" arg consistent--===//
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 canonicalizes uses of the llvm.memset, llvm.memcpy and
11 // llvm.memmove intrinsics so that the variants with 64-bit "len"
12 // arguments aren't used, and the 32-bit variants are used instead.
13 //
14 // This means the PNaCl translator won't need to handle two versions
15 // of each of these intrinsics, and it won't need to do any implicit
16 // truncations from 64-bit to 32-bit.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Transforms/NaCl.h"
27
28 using namespace llvm;
29
30 namespace {
31 // This is a ModulePass because that makes it easier to find all
32 // uses of intrinsics efficiently.
33 class CanonicalizeMemIntrinsics : public ModulePass {
34 public:
35 static char ID; // Pass identification, replacement for typeid
36 CanonicalizeMemIntrinsics() : ModulePass(ID) {
37 initializeCanonicalizeMemIntrinsicsPass(*PassRegistry::getPassRegistry());
38 }
39
40 virtual bool runOnModule(Module &M);
41 };
42 }
43
44 char CanonicalizeMemIntrinsics::ID = 0;
45 INITIALIZE_PASS(CanonicalizeMemIntrinsics, "canonicalize-mem-intrinsics",
46 "Make memcpy() et al's \"len\" argument consistent",
47 false, false)
48
49 static bool expandIntrinsic(Module *M, Intrinsic::ID ID) {
50 SmallVector<Type *, 3> Types;
51 Types.push_back(Type::getInt8PtrTy(M->getContext()));
52 if (ID != Intrinsic::memset)
53 Types.push_back(Type::getInt8PtrTy(M->getContext()));
54 unsigned LengthTypePos = Types.size();
55 Types.push_back(Type::getInt64Ty(M->getContext()));
56
57 std::string OldName = Intrinsic::getName(ID, Types);
58 Function *OldIntrinsic = M->getFunction(OldName);
59 if (!OldIntrinsic)
60 return false;
61
62 Types[LengthTypePos] = Type::getInt32Ty(M->getContext());
63 Function *NewIntrinsic = Intrinsic::getDeclaration(M, ID, Types);
64
65 SmallVector<CallInst *, 64> Calls;
66 for (User *U : OldIntrinsic->users()) {
67 if (CallInst *Call = dyn_cast<CallInst>(U))
68 Calls.push_back(Call);
69 else
70 report_fatal_error("CanonicalizeMemIntrinsics: Taking the address of an "
71 "intrinsic is not allowed: " +
72 OldName);
73 }
74
75 for (CallInst *Call : Calls) {
76 // This temporarily leaves Call non-well-typed.
77 Call->setCalledFunction(NewIntrinsic);
78 // Truncate the "len" argument. No overflow check.
79 IRBuilder<> Builder(Call);
80 Value *Length = Builder.CreateTrunc(Call->getArgOperand(2),
81 Type::getInt32Ty(M->getContext()),
82 "mem_len_truncate");
83 Call->setArgOperand(2, Length);
84 }
85
86 OldIntrinsic->eraseFromParent();
87 return true;
88 }
89
90 bool CanonicalizeMemIntrinsics::runOnModule(Module &M) {
91 bool Changed = false;
92 Changed |= expandIntrinsic(&M, Intrinsic::memset);
93 Changed |= expandIntrinsic(&M, Intrinsic::memcpy);
94 Changed |= expandIntrinsic(&M, Intrinsic::memmove);
95 return Changed;
96 }
97
98 ModulePass *llvm::createCanonicalizeMemIntrinsicsPass() {
99 return new CanonicalizeMemIntrinsics();
100 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/CMakeLists.txt ('k') | lib/Transforms/NaCl/ConstantInsertExtractElementIndex.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698