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

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

Issue 221693002: PNaCl: Add support for GCC/LLVM vector extensions (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Address jvoung's comments. Created 6 years, 8 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
« no previous file with comments | « lib/Transforms/NaCl/FlattenGlobals.cpp ('k') | lib/Transforms/NaCl/PNaClABISimplify.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===- GlobalizeConstantVectors.cpp - Globalize constant vector -----------===//
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 replaces all constant vector operands by loads of the same
11 // vector value from a constant global. After this pass functions don't
12 // rely on ConstantVector and ConstantDataVector.
13 //
14 // The FlattenGlobals pass can be used to further simplify the globals
15 // that this pass creates.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Transforms/NaCl.h"
26
27 using namespace llvm;
28
29 namespace {
30 // Must be a ModulePass since it adds globals.
31 class GlobalizeConstantVectors : public ModulePass {
32 public:
33 static char ID; // Pass identification, replacement for typeid
34 GlobalizeConstantVectors() : ModulePass(ID), DL(0) {
35 initializeGlobalizeConstantVectorsPass(*PassRegistry::getPassRegistry());
36 }
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesCFG();
39 AU.addRequired<DataLayout>();
40 }
41 virtual bool runOnModule(Module &M);
42
43 private:
44 typedef SmallVector<Value *, 128> Values;
45 const DataLayout *DL;
46
47 void findConstantVectors(const Function &F, Values &CVs) const;
48 void globalizeConstantVectors(Module &M, Function &F,
49 const Values &CVs) const;
50 };
51 } // anonymous namespace
52
53 char GlobalizeConstantVectors::ID = 0;
54 INITIALIZE_PASS(GlobalizeConstantVectors, "globalize-constant-vectors",
55 "Replace constant vector operands with equivalent loads", false,
56 false)
57
58 void GlobalizeConstantVectors::findConstantVectors(const Function &F,
59 Values &CVs) const {
60 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE;
61 ++II) {
62 for (User::const_op_iterator OI = II->op_begin(), OE = II->op_end();
63 OI != OE; ++OI) {
64 Value *V = OI->get();
65 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V))
66 CVs.push_back(V);
67 }
68 }
69 }
70
71 void
72 GlobalizeConstantVectors::globalizeConstantVectors(Module &M, Function &F,
73 const Values &CVs) const {
74 // The first instruction in a function dominates all others, it is
75 // therefore a safe insertion point.
76 // TODO(jfb) Sink values closer to their use?
77 Instruction *FirstInst = F.getEntryBlock().getFirstNonPHI();
78 for (Values::const_iterator VI = CVs.begin(), VE = CVs.end(); VI != VE;
79 ++VI) {
80 Value *V = *VI;
81 static const char Name[] = "constant_vector";
82
83 GlobalVariable *GV = new GlobalVariable(
84 M, V->getType(), /* isConstant= */ true, GlobalValue::InternalLinkage,
85 cast<Constant>(V), Name);
86 GV->setAlignment(DL->getPrefTypeAlignment(V->getType()));
87 LoadInst *MaterializedGV = new LoadInst(GV, Name, /* isVolatile= */ false,
88 GV->getAlignment(), FirstInst);
89
90 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
91 ++UI) {
92 User *U = *UI;
93 if (Instruction *I = dyn_cast<Instruction>(U))
94 if (I->getParent()->getParent() != &F)
95 // Skip uses of the constant vector in other functions: we
96 // need to materialize it in every function which has a use.
97 continue;
98 if (isa<GlobalVariable>(U))
99 // Don't replace global uses of the constant vector: we just
100 // created a new one. This avoid recursive references.
101 continue;
102 for (User::op_iterator OI = U->op_begin(), OE = U->op_end(); OI != OE;
103 ++OI)
104 if (dyn_cast<Value>(*OI) == V)
105 // The current operand is a use of the constant vector,
106 // replace it with the materialized one.
107 *OI = MaterializedGV;
108 }
109 }
110 }
111
112 bool GlobalizeConstantVectors::runOnModule(Module &M) {
113 bool Changed = false;
114 DL = &getAnalysis<DataLayout>();
115 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
116 Function &F = *FI;
117 Values ConstantVectors;
118 findConstantVectors(F, ConstantVectors);
119 if (!ConstantVectors.empty()) {
120 Changed = true;
121 globalizeConstantVectors(M, F, ConstantVectors);
122 }
123 }
124 return Changed;
125 }
126
127 ModulePass *llvm::createGlobalizeConstantVectorsPass() {
128 return new GlobalizeConstantVectors();
129 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/FlattenGlobals.cpp ('k') | lib/Transforms/NaCl/PNaClABISimplify.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698