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

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

Issue 282553003: PNaCl translator: combine insertelement / extractelement patterns generated by the toolchain into s… (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Fix typo on canonicalization range. Created 6 years, 7 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/CMakeLists.txt ('k') | lib/Transforms/NaCl/ExpandShuffleVector.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 //===- CombineVectorInstructions.cpp --------------------------------------===//
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 cleans up some of the toolchain-side PNaCl ABI
11 // simplification passes relating to vectors. These passes allow PNaCl
12 // to have a simple and stable ABI, but they sometimes lead to
13 // harder-to-optimize code.
14 //
15 // It currently:
16 // - Re-generates shufflevector (not part of the PNaCl ABI) from
17 // insertelement / extractelement combinations. This is done by
18 // duplicating some of instcombine's implementation, and ignoring
19 // optimizations that should already have taken place.
20 // - TODO(jfb) Re-combine load/store for vectors, which are transformed
21 // into load/store of the underlying elements.
22 // - TODO(jfb) Re-materialize constant arguments, which are currently
23 // loads from global constant vectors.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IRBuilder.h"
31 #include "llvm/IR/Operator.h"
32 #include "llvm/InstVisitor.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Transforms/NaCl.h"
35
36 using namespace llvm;
37
38 namespace {
39 // TODO(jfb) This function is as-is from instcombine.
Derek Schuff 2014/05/12 20:49:22 there's nothing to do specified in this TODO.
JF 2014/05/12 22:00:01 Done.
40 //
41 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
42 /// elements from either LHS or RHS, return the shuffle mask and true.
43 /// Otherwise, return false.
44 bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
45 SmallVectorImpl<Constant*> &Mask) {
46 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
47 "Invalid CollectSingleShuffleElements");
48 unsigned NumElts = V->getType()->getVectorNumElements();
49
50 if (isa<UndefValue>(V)) {
51 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
52 return true;
53 }
54
55 if (V == LHS) {
56 for (unsigned i = 0; i != NumElts; ++i)
57 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
58 return true;
59 }
60
61 if (V == RHS) {
62 for (unsigned i = 0; i != NumElts; ++i)
63 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
64 i+NumElts));
65 return true;
66 }
67
68 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
69 // If this is an insert of an extract from some other vector, include it.
70 Value *VecOp = IEI->getOperand(0);
71 Value *ScalarOp = IEI->getOperand(1);
72 Value *IdxOp = IEI->getOperand(2);
73
74 if (!isa<ConstantInt>(IdxOp))
75 return false;
76 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
77
78 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
79 // Okay, we can handle this if the vector we are insertinting into is
80 // transitively ok.
81 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
82 // If so, update the mask to reflect the inserted undef.
83 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
84 return true;
85 }
86 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
87 if (isa<ConstantInt>(EI->getOperand(1)) &&
88 EI->getOperand(0)->getType() == V->getType()) {
89 unsigned ExtractedIdx =
90 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
91
92 // This must be extracting from either LHS or RHS.
93 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
94 // Okay, we can handle this if the vector we are insertinting into is
95 // transitively ok.
96 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
97 // If so, update the mask to reflect the inserted value.
98 if (EI->getOperand(0) == LHS) {
99 Mask[InsertedIdx % NumElts] =
100 ConstantInt::get(Type::getInt32Ty(V->getContext()),
101 ExtractedIdx);
102 } else {
103 assert(EI->getOperand(0) == RHS);
104 Mask[InsertedIdx % NumElts] =
105 ConstantInt::get(Type::getInt32Ty(V->getContext()),
106 ExtractedIdx+NumElts);
107 }
108 return true;
109 }
110 }
111 }
112 }
113 }
114 // TODO: Handle shufflevector here!
115
116 return false;
117 }
118
119 // TODO(jfb) This function is as-is from instcombine.
120 //
121 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
122 /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
123 /// that computes V and the LHS value of the shuffle.
124 Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask,
125 Value *&RHS) {
126 assert(V->getType()->isVectorTy() &&
127 (RHS == 0 || V->getType() == RHS->getType()) &&
128 "Invalid shuffle!");
129 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
130
131 if (isa<UndefValue>(V)) {
132 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
133 return V;
134 }
135
136 if (isa<ConstantAggregateZero>(V)) {
137 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
138 return V;
139 }
140
141 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
142 // If this is an insert of an extract from some other vector, include it.
143 Value *VecOp = IEI->getOperand(0);
144 Value *ScalarOp = IEI->getOperand(1);
145 Value *IdxOp = IEI->getOperand(2);
146
147 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
148 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
149 EI->getOperand(0)->getType() == V->getType()) {
150 unsigned ExtractedIdx =
151 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
152 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
153
154 // Either the extracted from or inserted into vector must be RHSVec,
155 // otherwise we'd end up with a shuffle of three inputs.
156 if (EI->getOperand(0) == RHS || RHS == 0) {
157 RHS = EI->getOperand(0);
158 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
159 Mask[InsertedIdx % NumElts] =
160 ConstantInt::get(Type::getInt32Ty(V->getContext()),
161 NumElts+ExtractedIdx);
162 return V;
163 }
164
165 if (VecOp == RHS) {
166 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
167 // Update Mask to reflect that `ScalarOp' has been inserted at
168 // position `InsertedIdx' within the vector returned by IEI.
169 Mask[InsertedIdx % NumElts] = Mask[ExtractedIdx];
170
171 // Everything but the extracted element is replaced with the RHS.
172 for (unsigned i = 0; i != NumElts; ++i) {
173 if (i != InsertedIdx)
174 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
175 NumElts+i);
176 }
177 return V;
178 }
179
180 // If this insertelement is a chain that comes from exactly these two
181 // vectors, return the vector and the effective shuffle.
182 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
183 return EI->getOperand(0);
184 }
185 }
186 }
187 // TODO: Handle shufflevector here!
188
189 // Otherwise, can't do anything fancy. Return an identity vector.
190 for (unsigned i = 0; i != NumElts; ++i)
191 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
192 return V;
193 }
194
195 class CombineVectorInstructions
196 : public BasicBlockPass,
197 public InstVisitor<CombineVectorInstructions, bool> {
198 public:
199 static char ID; // Pass identification, replacement for typeid
200 CombineVectorInstructions() : BasicBlockPass(ID) {
201 initializeCombineVectorInstructionsPass(*PassRegistry::getPassRegistry());
202 }
203
204 virtual bool runOnBasicBlock(BasicBlock &B);
205
206 // InstVisitor implementation. Unhandled instructions stay as-is.
207 bool visitInstruction(Instruction &I) { return false; }
208 bool visitInsertElementInst(InsertElementInst &IE);
209 };
210
211 } // anonymous namespace
212
213 char CombineVectorInstructions::ID = 0;
214 INITIALIZE_PASS(CombineVectorInstructions, "combine-vector-instructions",
215 "Combine vector instructions", false, false)
216 bool CombineVectorInstructions::runOnBasicBlock(BasicBlock &B) {
217 bool Modified = false;
218 for (BasicBlock::iterator BI = B.begin(), BE = B.end(); BI != BE; ++BI)
219 Modified |= visit(&*BI);
220 return Modified;
221 }
222
223 // TODO(jfb) This function is *almost* as-is from instcombine, avoiding
224 // silly cases that should already have been optimized.
225 bool CombineVectorInstructions::visitInsertElementInst(InsertElementInst &IE) {
226 Value *ScalarOp = IE.getOperand(1);
227 Value *IdxOp = IE.getOperand(2);
228
229 // If the inserted element was extracted from some other vector, and if the
230 // indexes are constant, try to turn this into a shufflevector operation.
231 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
232 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
233 EI->getOperand(0)->getType() == IE.getType()) {
234 unsigned NumVectorElts = IE.getType()->getNumElements();
235 unsigned ExtractedIdx =
236 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
237 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
238
239 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
240 return false;
241
242 if (InsertedIdx >= NumVectorElts) // Out of range insert.
243 return false;
244
245 // If this insertelement isn't used by some other insertelement, turn it
246 // (and any insertelements it points to), into one big shuffle.
247 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
248 typedef SmallVector<Constant *, 16> MaskT;
249 MaskT Mask;
250 Value *RHS = 0;
251 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
252 if (RHS == 0)
253 RHS = UndefValue::get(LHS->getType());
254 // We now have a shuffle of LHS, RHS, Mask.
255 if (isa<UndefValue>(LHS) && !isa<UndefValue>(RHS)) {
256 // Canonicalize shufflevector to always have undef on the RHS,
257 // and adjust the mask.
258 std::swap(LHS, RHS);
259 for (MaskT::iterator I = Mask.begin(), E = Mask.end(); I != E; ++I) {
260 unsigned Idx = cast<ConstantInt>(*I)->getZExtValue();
261 unsigned NewIdx =
262 Idx >= NumVectorElts ? Idx - NumVectorElts : Idx + NumVectorElts ;
Derek Schuff 2014/05/12 20:49:22 80 cols
JF 2014/05/12 22:00:01 Done.
263 *I = ConstantInt::get(Type::getInt32Ty(RHS->getContext()), NewIdx);
264 }
265 }
266 IRBuilder<> IRB(&IE);
267 IE.replaceAllUsesWith(
268 IRB.CreateShuffleVector(LHS, RHS, ConstantVector::get(Mask)));
269 return true;
270 }
271 }
272 }
273
274 return false;
275 }
276
277 BasicBlockPass *llvm::createCombineVectorInstructionsPass() {
278 return new CombineVectorInstructions();
279 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/CMakeLists.txt ('k') | lib/Transforms/NaCl/ExpandShuffleVector.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698