Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame^] | 1 | // Copyright 2016 The SwiftShader 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 "Optimizer.hpp" |
| 16 | |
| 17 | #include "src/IceCfg.h" |
| 18 | #include "src/IceCfgNode.h" |
| 19 | |
| 20 | #include <map> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | class Optimizer |
| 26 | { |
| 27 | public: |
| 28 | void run(Ice::Cfg *function); |
| 29 | |
| 30 | private: |
| 31 | void analyzeUses(Ice::Cfg *function); |
| 32 | void eliminateUnusedAllocas(); |
| 33 | |
| 34 | Ice::Cfg *function; |
| 35 | |
| 36 | std::map<Ice::Operand*, std::vector<Ice::Inst*>> uses; |
| 37 | }; |
| 38 | |
| 39 | void Optimizer::run(Ice::Cfg *function) |
| 40 | { |
| 41 | this->function = function; |
| 42 | |
| 43 | analyzeUses(function); |
| 44 | |
| 45 | eliminateUnusedAllocas(); |
| 46 | } |
| 47 | |
| 48 | void Optimizer::eliminateUnusedAllocas() |
| 49 | { |
| 50 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 51 | |
| 52 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 53 | { |
| 54 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 55 | { |
| 56 | return; // Allocas are all at the top |
| 57 | } |
| 58 | |
| 59 | Ice::Operand *address = alloca.getDest(); |
| 60 | |
| 61 | if(uses[address].empty()) |
| 62 | { |
| 63 | alloca.setDeleted(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void Optimizer::analyzeUses(Ice::Cfg *function) |
| 69 | { |
| 70 | uses.clear(); |
| 71 | |
| 72 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
| 73 | { |
| 74 | for(Ice::Inst &instruction : basicBlock->getInsts()) |
| 75 | { |
| 76 | if(instruction.isDeleted()) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | for(int i = 0; i < instruction.getSrcSize(); i++) |
| 82 | { |
| 83 | int unique = 0; |
| 84 | for(; unique < i; unique++) |
| 85 | { |
| 86 | if(instruction.getSrc(i) == instruction.getSrc(unique)) |
| 87 | { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if(i == unique) |
| 93 | { |
| 94 | uses[instruction.getSrc(i)].push_back(&instruction); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | namespace sw |
| 103 | { |
| 104 | void optimize(Ice::Cfg *function) |
| 105 | { |
| 106 | Optimizer optimizer; |
| 107 | |
| 108 | optimizer.run(function); |
| 109 | } |
| 110 | } |