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

Side by Side Diff: lib/Transforms/NaCl/SimplifyAllocas.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 //===- SimplifyAllocas.cpp - Simplify allocas to arrays of bytes --===//
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 // Simplify all allocas into allocas of byte arrays.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Transforms/NaCl.h"
20
21 using namespace llvm;
22 namespace {
23 class SimplifyAllocas : public BasicBlockPass {
24 public:
25 static char ID; // Pass identification, replacement for typeid
26 SimplifyAllocas()
27 : BasicBlockPass(ID), Initialized(false), M(nullptr), IntPtrType(nullptr),
28 Int8Type(nullptr), DL(nullptr) {
29 initializeSimplifyAllocasPass(*PassRegistry::getPassRegistry());
30 }
31
32 private:
33 bool Initialized;
34 const Module *M;
35 Type *IntPtrType;
36 Type *Int8Type;
37 const DataLayout *DL;
38
39 using llvm::Pass::doInitialization;
40 bool doInitialization(Function &F) override {
41 if (!Initialized) {
42 M = F.getParent();
43 DL = M->getDataLayout();
44 IntPtrType = DL->getIntPtrType(M->getContext());
45 Int8Type = Type::getInt8Ty(M->getContext());
46 Initialized = true;
47 return true;
48 }
49 return false;
50 }
51
52 bool runOnBasicBlock(BasicBlock &BB) override {
53 bool Changed = false;
54 for (BasicBlock::iterator I = BB.getFirstInsertionPt(), E = BB.end();
55 I != E;) {
56 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(I++)) {
57 Changed = true;
58 Type *ElementTy = Alloca->getType()->getPointerElementType();
59 Constant *ElementSize =
60 ConstantInt::get(IntPtrType, DL->getTypeAllocSize(ElementTy));
61 // Expand out alloca's built-in multiplication.
62 Value *MulSize;
63 if (ConstantInt *C = dyn_cast<ConstantInt>(Alloca->getArraySize())) {
64 const APInt Value =
65 C->getValue().zextOrTrunc(IntPtrType->getScalarSizeInBits());
66 MulSize = ConstantExpr::getMul(ElementSize,
67 ConstantInt::get(IntPtrType, Value));
68 } else {
69 Value *ArraySize = Alloca->getArraySize();
70 if (ArraySize->getType() != IntPtrType) {
71 // We assume ArraySize is always positive, and thus is unsigned.
72 assert(!isa<ConstantInt>(ArraySize) ||
73 !cast<ConstantInt>(ArraySize)->isNegative());
74 ArraySize =
75 CastInst::CreateIntegerCast(ArraySize, IntPtrType,
76 /* isSigned = */ false, "", Alloca);
77 }
78 MulSize = CopyDebug(
79 BinaryOperator::Create(Instruction::Mul, ElementSize, ArraySize,
80 Alloca->getName() + ".alloca_mul", Alloca),
81 Alloca);
82 }
83 unsigned Alignment = Alloca->getAlignment();
84 if (Alignment == 0)
85 Alignment = DL->getPrefTypeAlignment(ElementTy);
86 AllocaInst *Tmp =
87 new AllocaInst(Int8Type, MulSize, Alignment, "", Alloca);
88 CopyDebug(Tmp, Alloca);
89 Tmp->takeName(Alloca);
90 BitCastInst *BC = new BitCastInst(Tmp, Alloca->getType(),
91 Tmp->getName() + ".bc", Alloca);
92 CopyDebug(BC, Alloca);
93 Alloca->replaceAllUsesWith(BC);
94 Alloca->eraseFromParent();
95 }
96 }
97 return Changed;
98 }
99 };
100 }
101 char SimplifyAllocas::ID = 0;
102
103 INITIALIZE_PASS(SimplifyAllocas, "simplify-allocas",
104 "Simplify allocas to arrays of bytes", false, false)
105
106 BasicBlockPass *llvm::createSimplifyAllocasPass() {
107 return new SimplifyAllocas();
108 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/RewritePNaClLibraryCalls.cpp ('k') | lib/Transforms/NaCl/StripAttributes.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698