blob: 5a39793bd23e7798275557c82552679ea5fbcf35 [file] [log] [blame]
David Netoab03f432017-11-03 17:00:44 -04001// Copyright 2017 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <string>
David Neto8e39bd12017-11-14 21:08:12 -050016#include <utility>
David Netoab03f432017-11-03 17:00:44 -040017
David Neto118188e2018-08-24 11:27:54 -040018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/UniqueVector.h"
21#include "llvm/IR/Attributes.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/DerivedTypes.h"
David Neto118188e2018-08-24 11:27:54 -040024#include "llvm/IR/Function.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/raw_ostream.h"
David Netoab03f432017-11-03 17:00:44 -040029
David Neto482550a2018-03-24 05:21:07 -070030#include "clspv/Option.h"
31
Diego Novilloa4c44fa2019-04-11 10:56:15 -040032#include "Passes.h"
33
David Netoab03f432017-11-03 17:00:44 -040034using namespace llvm;
35using std::string;
36
David Neto8e39bd12017-11-14 21:08:12 -050037#define DEBUG_TYPE "rewriteinserts"
David Netoab03f432017-11-03 17:00:44 -040038
David Netoab03f432017-11-03 17:00:44 -040039namespace {
40
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040041const char *kCompositeConstructFunctionPrefix = "clspv.composite_construct.";
David Netoab03f432017-11-03 17:00:44 -040042
43class RewriteInsertsPass : public ModulePass {
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040044public:
David Netoab03f432017-11-03 17:00:44 -040045 static char ID;
46 RewriteInsertsPass() : ModulePass(ID) {}
47
48 bool runOnModule(Module &M) override;
49
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040050private:
51 using InsertionVector = SmallVector<Instruction *, 4>;
David Netoab03f432017-11-03 17:00:44 -040052
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040053 // Replaces chains of insertions that cover the entire value.
54 // Such a change always reduces the number of instructions, so
55 // we always perform these. Returns true if the module was modified.
56 bool ReplaceCompleteInsertionChains(Module &M);
David Neto8e39bd12017-11-14 21:08:12 -050057
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040058 // Replaces all InsertValue instructions, even if they aren't part
59 // of a complete insetion chain. Returns true if the module was modified.
60 bool ReplacePartialInsertions(Module &M);
David Neto8e39bd12017-11-14 21:08:12 -050061
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040062 // Load |values| and |chain| with the members of the struct value produced
63 // by a chain of InsertValue instructions ending with |iv|, and following
64 // the aggregate operand. Return the start of the chain: the aggregate
65 // value which is not an InsertValue instruction, or an InsertValue
66 // instruction which inserts a component that is replaced later in the
67 // chain. The |values| vector will match the order of struct members and
68 // is initialized to all nullptr members. The |chain| vector will list
69 // the chain of InsertValue instructions, listed in the order we discover
70 // them, e.g. begining with |iv|.
71 Value *LoadValuesEndingWithInsertion(InsertValueInst *iv,
72 std::vector<Value *> *values,
73 InsertionVector *chain) {
74 auto *structTy = dyn_cast<StructType>(iv->getType());
75 assert(structTy);
76 const auto numElems = structTy->getNumElements();
David Neto8e39bd12017-11-14 21:08:12 -050077
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040078 // Walk backward from the tail to an instruction we don't want to
79 // replace.
80 Value *frontier = iv;
81 while (auto *insertion = dyn_cast<InsertValueInst>(frontier)) {
82 chain->push_back(insertion);
83 // Only handle single-index insertions.
84 if (insertion->getNumIndices() == 1) {
85 // Try to replace this one.
David Neto8e39bd12017-11-14 21:08:12 -050086
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040087 unsigned index = insertion->getIndices()[0];
88 assert(index < numElems);
89 if ((*values)[index] != nullptr) {
90 // We already have a value for this slot. Stop now.
91 break;
92 }
93 (*values)[index] = insertion->getInsertedValueOperand();
94 frontier = insertion->getAggregateOperand();
95 } else {
96 break;
97 }
98 }
99 return frontier;
100 }
David Neto8e39bd12017-11-14 21:08:12 -0500101
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400102 // Returns the number of elements in the struct or array.
103 unsigned GetNumElements(Type *type) {
104 // CompositeType doesn't implement getNumElements(), but its inheritors
105 // do.
106 if (auto *struct_ty = dyn_cast<StructType>(type)) {
107 return struct_ty->getNumElements();
108 } else if (auto *seq_ty = dyn_cast<SequentialType>(type)) {
109 return seq_ty->getNumElements();
110 }
111 return 0;
112 }
Alan Bakerabc935e2018-09-06 11:33:53 -0400113
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400114 // If this is the tail of a chain of InsertValueInst instructions
115 // that covers the entire composite, then return a small vector
116 // containing the insertion instructions, in member order.
117 // Otherwise returns nullptr.
118 InsertionVector *CompleteInsertionChain(InsertValueInst *iv) {
119 if (iv->getNumIndices() == 1) {
120 auto numElems = GetNumElements(iv->getType());
121 if (numElems != 0) {
122 // Only handle single-index insertions.
123 unsigned index = iv->getIndices()[0];
124 if (index + 1u != numElems) {
125 // Not the last in the chain.
126 return nullptr;
127 }
128 InsertionVector candidates(numElems, nullptr);
129 for (unsigned i = index;
130 iv->getNumIndices() == 1 && i == iv->getIndices()[0]; --i) {
131 // iv inserts the i'th member
132 candidates[i] = iv;
Alan Bakerabc935e2018-09-06 11:33:53 -0400133
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400134 if (i == 0) {
135 // We're done!
136 return new InsertionVector(candidates);
137 }
David Netoab03f432017-11-03 17:00:44 -0400138
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400139 if (InsertValueInst *agg =
140 dyn_cast<InsertValueInst>(iv->getAggregateOperand())) {
141 iv = agg;
142 } else {
143 // The chain is broken.
144 break;
145 }
146 }
147 }
148 }
149 return nullptr;
150 }
David Netoab03f432017-11-03 17:00:44 -0400151
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400152 // If this is the tail of a chain of InsertElementInst instructions
153 // that covers the entire vector, then return a small vector
154 // containing the insertion instructions, in member order.
155 // Otherwise returns nullptr. Only handle insertions into vectors.
156 InsertionVector *CompleteInsertionChain(InsertElementInst *ie) {
157 // Don't handle i8 vectors. Only <4 x i8> is supported and it is
158 // translated as i32. Only handle single-index insertions.
159 if (auto vec_ty = dyn_cast<VectorType>(ie->getType())) {
160 if (vec_ty->getVectorElementType() == Type::getInt8Ty(ie->getContext())) {
161 return nullptr;
162 }
163 }
David Netoab03f432017-11-03 17:00:44 -0400164
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400165 // Only handle single-index insertions.
166 if (ie->getNumOperands() == 3) {
167 auto numElems = GetNumElements(ie->getType());
168 if (numElems != 0) {
169 if (auto *const_value = dyn_cast<ConstantInt>(ie->getOperand(2))) {
170 uint64_t index = const_value->getZExtValue();
171 if (index + 1u != numElems) {
172 // Not the last in the chain.
173 return nullptr;
174 }
175 InsertionVector candidates(numElems, nullptr);
176 Value *value = ie;
177 uint64_t i = index;
178 while (auto *insert = dyn_cast<InsertElementInst>(value)) {
179 if (insert->getNumOperands() != 3)
180 break;
181 if (auto *index_const =
182 dyn_cast<ConstantInt>(insert->getOperand(2))) {
183 if (i != index_const->getZExtValue())
184 break;
Alan Bakerabc935e2018-09-06 11:33:53 -0400185
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400186 candidates[i] = insert;
187 if (i == 0) {
188 // We're done!
189 return new InsertionVector(candidates);
190 }
Alan Bakerabc935e2018-09-06 11:33:53 -0400191
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400192 value = insert->getOperand(0);
193 --i;
194 } else {
195 break;
196 }
197 }
198 } else {
199 return nullptr;
200 }
201 }
202 }
203 return nullptr;
204 }
Alan Bakerabc935e2018-09-06 11:33:53 -0400205
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400206 // Return the name for the wrap function for the given type.
207 string &WrapFunctionNameForType(Type *type) {
208 auto where = function_for_type_.find(type);
209 if (where == function_for_type_.end()) {
210 // Insert it.
211 auto &result = function_for_type_[type] =
212 string(kCompositeConstructFunctionPrefix) +
213 std::to_string(function_for_type_.size());
214 return result;
215 } else {
216 return where->second;
217 }
218 }
David Neto8e39bd12017-11-14 21:08:12 -0500219
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400220 // Get or create the composite construct function definition.
221 Function *GetConstructFunction(Module &M, CompositeType *constructed_type) {
222 // Get or create the composite construct function definition.
223 const string &fn_name = WrapFunctionNameForType(constructed_type);
224 Function *fn = M.getFunction(fn_name);
225 if (!fn) {
226 // Make the function.
227 SmallVector<Type *, 16> elements;
228 unsigned num_elements = GetNumElements(constructed_type);
229 for (unsigned i = 0; i != num_elements; ++i)
230 elements.push_back(constructed_type->getTypeAtIndex(i));
231 FunctionType *fnTy = FunctionType::get(constructed_type, elements, false);
232 auto fn_constant = M.getOrInsertFunction(fn_name, fnTy);
233 fn = cast<Function>(fn_constant.getCallee());
234 fn->addFnAttr(Attribute::ReadOnly);
235 }
236 return fn;
237 }
David Netoab03f432017-11-03 17:00:44 -0400238
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400239 // Maps a loaded type to the name of the wrap function for that type.
240 DenseMap<Type *, string> function_for_type_;
David Netoab03f432017-11-03 17:00:44 -0400241};
242} // namespace
243
244char RewriteInsertsPass::ID = 0;
Diego Novilloa4c44fa2019-04-11 10:56:15 -0400245INITIALIZE_PASS(RewriteInsertsPass, "RewriteInserts",
246 "Rewrite chains of insertvalue to as composite-construction",
247 false, false)
David Netoab03f432017-11-03 17:00:44 -0400248
249namespace clspv {
250llvm::ModulePass *createRewriteInsertsPass() {
251 return new RewriteInsertsPass();
252}
253} // namespace clspv
254
255bool RewriteInsertsPass::runOnModule(Module &M) {
David Neto8e39bd12017-11-14 21:08:12 -0500256 bool Changed = ReplaceCompleteInsertionChains(M);
257
David Neto482550a2018-03-24 05:21:07 -0700258 if (clspv::Option::HackInserts()) {
David Neto8e39bd12017-11-14 21:08:12 -0500259 Changed |= ReplacePartialInsertions(M);
260 }
261
262 return Changed;
263}
264
265bool RewriteInsertsPass::ReplaceCompleteInsertionChains(Module &M) {
David Netoab03f432017-11-03 17:00:44 -0400266 bool Changed = false;
267
268 SmallVector<InsertionVector *, 16> WorkList;
269 for (Function &F : M) {
270 for (BasicBlock &BB : F) {
271 for (Instruction &I : BB) {
272 if (InsertValueInst *iv = dyn_cast<InsertValueInst>(&I)) {
273 if (InsertionVector *insertions = CompleteInsertionChain(iv)) {
274 WorkList.push_back(insertions);
275 }
Alan Bakerabc935e2018-09-06 11:33:53 -0400276 } else if (InsertElementInst *ie = dyn_cast<InsertElementInst>(&I)) {
277 if (InsertionVector *insertions = CompleteInsertionChain(ie)) {
278 WorkList.push_back(insertions);
279 }
David Netoab03f432017-11-03 17:00:44 -0400280 }
281 }
282 }
283 }
284
285 if (WorkList.size() == 0) {
286 return Changed;
287 }
288
289 for (InsertionVector *insertions : WorkList) {
290 Changed = true;
291
292 // Gather the member values and types.
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400293 SmallVector<Value *, 4> values;
294 for (Instruction *inst : *insertions) {
Alan Bakerabc935e2018-09-06 11:33:53 -0400295 if (auto *insert_value = dyn_cast<InsertValueInst>(inst)) {
296 values.push_back(insert_value->getInsertedValueOperand());
297 } else if (auto *insert_element = dyn_cast<InsertElementInst>(inst)) {
298 values.push_back(insert_element->getOperand(1));
299 } else {
300 llvm_unreachable("Unhandled insertion instruction");
301 }
David Netoab03f432017-11-03 17:00:44 -0400302 }
303
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400304 CompositeType *resultTy =
305 cast<CompositeType>(insertions->back()->getType());
Alan Bakerabc935e2018-09-06 11:33:53 -0400306 Function *fn = GetConstructFunction(M, resultTy);
David Netoab03f432017-11-03 17:00:44 -0400307
308 // Replace the chain.
309 auto call = CallInst::Create(fn, values);
310 call->insertAfter(insertions->back());
311 insertions->back()->replaceAllUsesWith(call);
312
313 // Remove the insertions if we can. Go from the tail back to
314 // the head, since the tail uses the previous insertion, etc.
315 for (auto iter = insertions->rbegin(), end = insertions->rend();
316 iter != end; ++iter) {
Alan Bakerabc935e2018-09-06 11:33:53 -0400317 Instruction *insertion = *iter;
David Netoab03f432017-11-03 17:00:44 -0400318 if (!insertion->hasNUsesOrMore(1)) {
319 insertion->eraseFromParent();
320 }
321 }
322
323 delete insertions;
324 }
325
326 return Changed;
327}
David Neto8e39bd12017-11-14 21:08:12 -0500328
329bool RewriteInsertsPass::ReplacePartialInsertions(Module &M) {
330 bool Changed = false;
331
332 // First find candidates. Collect all InsertValue instructions
333 // into struct type, but track their interdependencies. To minimize
334 // the number of new instructions, generate a construction for each
335 // tail of an insertion chain.
336
337 UniqueVector<InsertValueInst *> insertions;
338 for (Function &F : M) {
339 for (BasicBlock &BB : F) {
340 for (Instruction &I : BB) {
341 if (InsertValueInst *iv = dyn_cast<InsertValueInst>(&I)) {
342 if (iv->getType()->isStructTy()) {
343 insertions.insert(iv);
344 }
345 }
346 }
347 }
348 }
349
350 // Now count how many times each InsertValue is used by another InsertValue.
351 // The |num_uses| vector is indexed by the unique id that |insertions|
352 // assigns to it.
353 std::vector<unsigned> num_uses(insertions.size() + 1);
354 // Count from the user's perspective.
355 for (InsertValueInst *insertion : insertions) {
356 if (auto *agg =
357 dyn_cast<InsertValueInst>(insertion->getAggregateOperand())) {
358 ++(num_uses[insertions.idFor(agg)]);
359 }
360 }
361
362 // Proceed in rounds. Each round rewrites any chains ending with an
363 // insertion that is not used by another insertion.
364
365 // Get the first list of insertion tails.
366 InsertionVector WorkList;
367 for (InsertValueInst *insertion : insertions) {
368 if (num_uses[insertions.idFor(insertion)] == 0) {
369 WorkList.push_back(insertion);
370 }
371 }
372
373 // This records insertions in the order they should be removed.
374 // In this list, an insertion preceds any insertions it uses.
375 // (This is post-dominance order.)
376 InsertionVector ordered_candidates_for_removal;
377
378 // Proceed in rounds.
379 while (WorkList.size()) {
380 Changed = true;
381
382 // Record the list of tails for the next round.
383 InsertionVector NextRoundWorkList;
384
Alan Bakerabc935e2018-09-06 11:33:53 -0400385 for (Instruction *inst : WorkList) {
386 InsertValueInst *insertion = cast<InsertValueInst>(inst);
David Neto8e39bd12017-11-14 21:08:12 -0500387 // Rewrite |insertion|.
388
389 StructType *resultTy = cast<StructType>(insertion->getType());
390
391 const unsigned num_members = resultTy->getNumElements();
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400392 std::vector<Value *> members(num_members, nullptr);
David Neto8e39bd12017-11-14 21:08:12 -0500393 InsertionVector chain;
394 // Gather the member values. Walk backward from the insertion.
395 Value *base = LoadValuesEndingWithInsertion(insertion, &members, &chain);
396
397 // Populate remaining entries in |values| by extracting elements
398 // from |base|. Only make a new extractvalue instruction if we can't
399 // make a constant or undef. New instructions are inserted before
400 // the insertion we plan to remove.
401 for (unsigned i = 0; i < num_members; ++i) {
402 if (!members[i]) {
403 Type *memberTy = resultTy->getTypeAtIndex(i);
404 if (isa<UndefValue>(base)) {
405 members[i] = UndefValue::get(memberTy);
406 } else if (const auto *caz = dyn_cast<ConstantAggregateZero>(base)) {
407 members[i] = caz->getElementValue(i);
408 } else if (const auto *ca = dyn_cast<ConstantAggregate>(base)) {
409 members[i] = ca->getOperand(i);
410 } else {
411 members[i] = ExtractValueInst::Create(base, {i}, "", insertion);
412 }
413 }
414 }
415
416 // Create the call. It's dominated by any extractions we've just
417 // created.
Alan Bakerabc935e2018-09-06 11:33:53 -0400418 Function *construct_fn = GetConstructFunction(M, resultTy);
David Neto8e39bd12017-11-14 21:08:12 -0500419 auto *call = CallInst::Create(construct_fn, members, "", insertion);
420
421 // Disconnect this insertion. We'll remove it later.
422 insertion->replaceAllUsesWith(call);
423
424 // Trace backward through the chain, removing uses and deleting where
425 // we can. Stop at the first element that has a remaining use.
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400426 for (auto *chainElem : chain) {
David Neto8e39bd12017-11-14 21:08:12 -0500427 if (chainElem->hasNUsesOrMore(1)) {
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400428 unsigned &use_count =
429 num_uses[insertions.idFor(cast<InsertValueInst>(chainElem))];
David Neto8e39bd12017-11-14 21:08:12 -0500430 assert(use_count > 0);
431 --use_count;
432 if (use_count == 0) {
433 NextRoundWorkList.push_back(chainElem);
434 }
435 break;
436 } else {
437 chainElem->eraseFromParent();
438 }
439 }
440 }
441 WorkList = std::move(NextRoundWorkList);
442 }
443
444 return Changed;
445}