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 | |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | class Optimizer |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 25 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | public: |
| 27 | void run(Ice::Cfg *function); |
| 28 | |
| 29 | private: |
| 30 | void analyzeUses(Ice::Cfg *function); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 31 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 32 | void eliminateDeadCode(); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 33 | void propagateAlloca(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 34 | void eliminateUnitializedLoads(); |
| 35 | void eliminateLoadsFollowingSingleStore(); |
| 36 | void optimizeStoresInSingleBasicBlock(); |
| 37 | |
| 38 | void replace(Ice::Inst *instruction, Ice::Operand *newValue); |
| 39 | void deleteInstruction(Ice::Inst *instruction); |
| 40 | bool isDead(Ice::Inst *instruction); |
| 41 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 42 | static const Ice::InstIntrinsic *asLoadSubVector(const Ice::Inst *instruction); |
| 43 | static const Ice::InstIntrinsic *asStoreSubVector(const Ice::Inst *instruction); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 44 | static bool isLoad(const Ice::Inst &instruction); |
| 45 | static bool isStore(const Ice::Inst &instruction); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 46 | static std::size_t storeSize(const Ice::Inst *instruction); |
| 47 | static bool loadTypeMatchesStore(const Ice::Inst *load, const Ice::Inst *store); |
| 48 | |
| 49 | Ice::Cfg *function; |
| 50 | Ice::GlobalContext *context; |
| 51 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 52 | struct Uses : std::vector<Ice::Inst *> |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 53 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 54 | bool areOnlyLoadStore() const; |
| 55 | void insert(Ice::Operand *value, Ice::Inst *instruction); |
| 56 | void erase(Ice::Inst *instruction); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 57 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 58 | std::vector<Ice::Inst *> loads; |
| 59 | std::vector<Ice::Inst *> stores; |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 62 | struct LoadStoreInst |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 63 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 64 | LoadStoreInst(Ice::Inst *inst, bool isStore) |
| 65 | : inst(inst) |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 66 | , address(isStore ? inst->getStoreAddress() : inst->getLoadAddress()) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 67 | , isStore(isStore) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 68 | { |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 69 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 70 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 71 | Ice::Inst *inst; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 72 | Ice::Operand *address; |
| 73 | bool isStore; |
| 74 | }; |
| 75 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 76 | Optimizer::Uses *getUses(Ice::Operand *); |
| 77 | void setUses(Ice::Operand *, Optimizer::Uses *); |
| 78 | bool hasUses(Ice::Operand *) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 79 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 80 | Ice::CfgNode *getNode(Ice::Inst *); |
| 81 | void setNode(Ice::Inst *, Ice::CfgNode *); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 82 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 83 | Ice::Inst *getDefinition(Ice::Variable *); |
| 84 | void setDefinition(Ice::Variable *, Ice::Inst *); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 85 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 86 | const std::vector<LoadStoreInst> &getLoadStoreInsts(Ice::CfgNode *); |
| 87 | void setLoadStoreInsts(Ice::CfgNode *, std::vector<LoadStoreInst> *); |
| 88 | bool hasLoadStoreInsts(Ice::CfgNode *node) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 89 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 90 | std::vector<Ice::Operand *> operandsWithUses; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | void Optimizer::run(Ice::Cfg *function) |
| 94 | { |
| 95 | this->function = function; |
| 96 | this->context = function->getContext(); |
| 97 | |
| 98 | analyzeUses(function); |
| 99 | |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 100 | // Start by eliminating any dead code, to avoid redundant work for the |
| 101 | // subsequent optimization passes. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 102 | eliminateDeadCode(); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 103 | |
| 104 | // Eliminate allocas which store the address of other allocas. |
| 105 | propagateAlloca(); |
| 106 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 107 | eliminateUnitializedLoads(); |
| 108 | eliminateLoadsFollowingSingleStore(); |
| 109 | optimizeStoresInSingleBasicBlock(); |
| 110 | eliminateDeadCode(); |
| 111 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 112 | for(auto operand : operandsWithUses) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 113 | { |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 114 | // Deletes the Uses instance on the operand |
| 115 | setUses(operand, nullptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 116 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 117 | operandsWithUses.clear(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 120 | // Eliminates allocas which store the address of other allocas. |
| 121 | void Optimizer::propagateAlloca() |
| 122 | { |
| 123 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 124 | Ice::InstList &instList = entryBlock->getInsts(); |
| 125 | |
| 126 | for(Ice::Inst &inst : instList) |
| 127 | { |
| 128 | if(inst.isDeleted()) |
| 129 | { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | auto *alloca = llvm::dyn_cast<Ice::InstAlloca>(&inst); |
| 134 | |
| 135 | if(!alloca) |
| 136 | { |
| 137 | break; // Allocas are all at the top |
| 138 | } |
| 139 | |
| 140 | // Look for stores of this alloca's address. |
| 141 | Ice::Operand *address = alloca->getDest(); |
| 142 | Uses uses = *getUses(address); // Hard copy |
| 143 | |
| 144 | for(auto *store : uses) |
| 145 | { |
| 146 | if(isStore(*store) && store->getData() == address) |
| 147 | { |
| 148 | Ice::Operand *dest = store->getStoreAddress(); |
| 149 | Ice::Variable *destVar = llvm::dyn_cast<Ice::Variable>(dest); |
| 150 | Ice::Inst *def = destVar ? getDefinition(destVar) : nullptr; |
| 151 | |
| 152 | // If the address is stored into another stack variable, eliminate the latter. |
| 153 | if(def && def->getKind() == Ice::Inst::Alloca) |
| 154 | { |
| 155 | Uses destUses = *getUses(dest); // Hard copy |
| 156 | |
| 157 | // Make sure the only store into the stack variable is this address, and that all of its other uses are loads. |
| 158 | // This prevents dynamic array loads/stores to be replaced by a scalar. |
| 159 | if((destUses.stores.size() == 1) && (destUses.loads.size() == destUses.size() - 1)) |
| 160 | { |
| 161 | for(auto *load : destUses.loads) |
| 162 | { |
| 163 | replace(load, address); |
| 164 | } |
| 165 | |
| 166 | // The address is now only stored, never loaded, so the store can be eliminated, together with its alloca. |
| 167 | assert(getUses(dest)->size() == 1); |
| 168 | deleteInstruction(store); |
| 169 | assert(def->isDeleted()); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 177 | void Optimizer::eliminateDeadCode() |
| 178 | { |
| 179 | bool modified; |
| 180 | do |
| 181 | { |
| 182 | modified = false; |
| 183 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 184 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 185 | for(Ice::Inst &inst : Ice::reverse_range(basicBlock->getInsts())) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 186 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 187 | if(inst.isDeleted()) |
| 188 | { |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | if(isDead(&inst)) |
| 193 | { |
| 194 | deleteInstruction(&inst); |
| 195 | modified = true; |
| 196 | } |
| 197 | } |
| 198 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 199 | } while(modified); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void Optimizer::eliminateUnitializedLoads() |
| 203 | { |
| 204 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 205 | |
| 206 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 207 | { |
| 208 | if(alloca.isDeleted()) |
| 209 | { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 214 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 215 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Ice::Operand *address = alloca.getDest(); |
| 219 | |
| 220 | if(!hasUses(address)) |
| 221 | { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | const auto &addressUses = *getUses(address); |
| 226 | |
| 227 | if(!addressUses.areOnlyLoadStore()) |
| 228 | { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | if(addressUses.stores.empty()) |
| 233 | { |
| 234 | for(Ice::Inst *load : addressUses.loads) |
| 235 | { |
| 236 | Ice::Variable *loadData = load->getDest(); |
| 237 | |
| 238 | if(hasUses(loadData)) |
| 239 | { |
| 240 | for(Ice::Inst *use : *getUses(loadData)) |
| 241 | { |
| 242 | for(Ice::SizeT i = 0; i < use->getSrcSize(); i++) |
| 243 | { |
| 244 | if(use->getSrc(i) == loadData) |
| 245 | { |
| 246 | auto *undef = context->getConstantUndef(loadData->getType()); |
| 247 | |
| 248 | use->replaceSource(i, undef); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | setUses(loadData, nullptr); |
| 254 | } |
| 255 | |
| 256 | load->setDeleted(); |
| 257 | } |
| 258 | |
| 259 | alloca.setDeleted(); |
| 260 | setUses(address, nullptr); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void Optimizer::eliminateLoadsFollowingSingleStore() |
| 266 | { |
| 267 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 268 | |
| 269 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 270 | { |
| 271 | if(alloca.isDeleted()) |
| 272 | { |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 277 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 278 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | Ice::Operand *address = alloca.getDest(); |
| 282 | |
| 283 | if(!hasUses(address)) |
| 284 | { |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | auto &addressUses = *getUses(address); |
| 289 | |
| 290 | if(!addressUses.areOnlyLoadStore()) |
| 291 | { |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | if(addressUses.stores.size() == 1) |
| 296 | { |
| 297 | Ice::Inst *store = addressUses.stores[0]; |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 298 | Ice::Operand *storeValue = store->getData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 299 | |
Nicolas Capens | 3ab17bd | 2021-02-10 16:41:31 -0500 | [diff] [blame] | 300 | auto instIterator = store->getIterator(); |
| 301 | auto basicBlockEnd = getNode(store)->getInsts().end(); |
| 302 | |
| 303 | while(++instIterator != basicBlockEnd) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 304 | { |
Nicolas Capens | 3ab17bd | 2021-02-10 16:41:31 -0500 | [diff] [blame] | 305 | Ice::Inst *load = &*instIterator; |
| 306 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 307 | if(load->isDeleted() || !isLoad(*load)) |
| 308 | { |
| 309 | continue; |
| 310 | } |
| 311 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 312 | if(load->getLoadAddress() != address) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 313 | { |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | if(!loadTypeMatchesStore(load, store)) |
| 318 | { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | replace(load, storeValue); |
| 323 | |
| 324 | for(size_t i = 0; i < addressUses.loads.size(); i++) |
| 325 | { |
| 326 | if(addressUses.loads[i] == load) |
| 327 | { |
| 328 | addressUses.loads[i] = addressUses.loads.back(); |
| 329 | addressUses.loads.pop_back(); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | for(size_t i = 0; i < addressUses.size(); i++) |
| 335 | { |
| 336 | if(addressUses[i] == load) |
| 337 | { |
| 338 | addressUses[i] = addressUses.back(); |
| 339 | addressUses.pop_back(); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if(addressUses.size() == 1) |
| 345 | { |
| 346 | assert(addressUses[0] == store); |
| 347 | |
| 348 | alloca.setDeleted(); |
| 349 | store->setDeleted(); |
| 350 | setUses(address, nullptr); |
| 351 | |
| 352 | if(hasUses(storeValue)) |
| 353 | { |
| 354 | auto &valueUses = *getUses(storeValue); |
| 355 | |
| 356 | for(size_t i = 0; i < valueUses.size(); i++) |
| 357 | { |
| 358 | if(valueUses[i] == store) |
| 359 | { |
| 360 | valueUses[i] = valueUses.back(); |
| 361 | valueUses.pop_back(); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if(valueUses.empty()) |
| 367 | { |
| 368 | setUses(storeValue, nullptr); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void Optimizer::optimizeStoresInSingleBasicBlock() |
| 380 | { |
| 381 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 382 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 383 | std::vector<std::vector<LoadStoreInst> *> allocatedVectors; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 384 | |
| 385 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 386 | { |
| 387 | if(alloca.isDeleted()) |
| 388 | { |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 393 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 394 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | Ice::Operand *address = alloca.getDest(); |
| 398 | |
| 399 | if(!hasUses(address)) |
| 400 | { |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | const auto &addressUses = *getUses(address); |
| 405 | |
| 406 | if(!addressUses.areOnlyLoadStore()) |
| 407 | { |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | Ice::CfgNode *singleBasicBlock = getNode(addressUses.stores[0]); |
| 412 | |
| 413 | for(size_t i = 1; i < addressUses.stores.size(); i++) |
| 414 | { |
| 415 | Ice::Inst *store = addressUses.stores[i]; |
| 416 | if(getNode(store) != singleBasicBlock) |
| 417 | { |
| 418 | singleBasicBlock = nullptr; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if(singleBasicBlock) |
| 424 | { |
| 425 | if(!hasLoadStoreInsts(singleBasicBlock)) |
| 426 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 427 | std::vector<LoadStoreInst> *loadStoreInstVector = new std::vector<LoadStoreInst>(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 428 | setLoadStoreInsts(singleBasicBlock, loadStoreInstVector); |
| 429 | allocatedVectors.push_back(loadStoreInstVector); |
| 430 | for(Ice::Inst &inst : singleBasicBlock->getInsts()) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 431 | { |
| 432 | if(inst.isDeleted()) |
| 433 | { |
| 434 | continue; |
| 435 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 436 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 437 | bool isStoreInst = isStore(inst); |
| 438 | bool isLoadInst = isLoad(inst); |
| 439 | |
| 440 | if(isStoreInst || isLoadInst) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 441 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 442 | loadStoreInstVector->push_back(LoadStoreInst(&inst, isStoreInst)); |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 443 | } |
| 444 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 445 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 446 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 447 | Ice::Inst *store = nullptr; |
| 448 | Ice::Operand *storeValue = nullptr; |
| 449 | bool unmatchedLoads = false; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 450 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 451 | for(auto &loadStoreInst : getLoadStoreInsts(singleBasicBlock)) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 452 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 453 | Ice::Inst *inst = loadStoreInst.inst; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 454 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 455 | if((loadStoreInst.address != address) || inst->isDeleted()) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 456 | { |
| 457 | continue; |
| 458 | } |
| 459 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 460 | if(loadStoreInst.isStore) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 461 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 462 | // New store found. If we had a previous one, try to eliminate it. |
| 463 | if(store && !unmatchedLoads) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 464 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 465 | // If the previous store is wider than the new one, we can't eliminate it |
| 466 | // because there could be a wide load reading its non-overwritten data. |
| 467 | if(storeSize(inst) >= storeSize(store)) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 468 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 469 | deleteInstruction(store); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 473 | store = inst; |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 474 | storeValue = store->getData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 475 | unmatchedLoads = false; |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | if(!loadTypeMatchesStore(inst, store)) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 480 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 481 | unmatchedLoads = true; |
| 482 | continue; |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 483 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 484 | |
| 485 | replace(inst, storeValue); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 490 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 491 | for(auto loadStoreInstVector : allocatedVectors) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 492 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 493 | delete loadStoreInstVector; |
| 494 | } |
| 495 | } |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 496 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 497 | void Optimizer::analyzeUses(Ice::Cfg *function) |
| 498 | { |
| 499 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
| 500 | { |
| 501 | for(Ice::Inst &instruction : basicBlock->getInsts()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 502 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 503 | if(instruction.isDeleted()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 504 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 505 | continue; |
| 506 | } |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 507 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 508 | setNode(&instruction, basicBlock); |
| 509 | if(instruction.getDest()) |
| 510 | { |
| 511 | setDefinition(instruction.getDest(), &instruction); |
| 512 | } |
| 513 | |
| 514 | for(Ice::SizeT i = 0; i < instruction.getSrcSize(); i++) |
| 515 | { |
| 516 | Ice::SizeT unique = 0; |
| 517 | for(; unique < i; unique++) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 518 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 519 | if(instruction.getSrc(i) == instruction.getSrc(unique)) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 520 | { |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 525 | if(i == unique) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 526 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | Ice::Operand *src = instruction.getSrc(i); |
| 528 | getUses(src)->insert(src, &instruction); |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 529 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 535 | void Optimizer::replace(Ice::Inst *instruction, Ice::Operand *newValue) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 536 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 537 | Ice::Variable *oldValue = instruction->getDest(); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 538 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 539 | if(!newValue) |
| 540 | { |
| 541 | newValue = context->getConstantUndef(oldValue->getType()); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 542 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 543 | |
| 544 | if(hasUses(oldValue)) |
| 545 | { |
| 546 | for(Ice::Inst *use : *getUses(oldValue)) |
| 547 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 548 | assert(!use->isDeleted()); // Should have been removed from uses already |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 549 | |
| 550 | for(Ice::SizeT i = 0; i < use->getSrcSize(); i++) |
| 551 | { |
| 552 | if(use->getSrc(i) == oldValue) |
| 553 | { |
| 554 | use->replaceSource(i, newValue); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | getUses(newValue)->insert(newValue, use); |
| 559 | } |
| 560 | |
| 561 | setUses(oldValue, nullptr); |
| 562 | } |
| 563 | |
| 564 | deleteInstruction(instruction); |
| 565 | } |
| 566 | |
| 567 | void Optimizer::deleteInstruction(Ice::Inst *instruction) |
| 568 | { |
| 569 | if(!instruction || instruction->isDeleted()) |
| 570 | { |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | instruction->setDeleted(); |
| 575 | |
| 576 | for(Ice::SizeT i = 0; i < instruction->getSrcSize(); i++) |
| 577 | { |
| 578 | Ice::Operand *src = instruction->getSrc(i); |
| 579 | |
| 580 | if(hasUses(src)) |
| 581 | { |
| 582 | auto &srcUses = *getUses(src); |
| 583 | |
| 584 | srcUses.erase(instruction); |
| 585 | |
| 586 | if(srcUses.empty()) |
| 587 | { |
| 588 | setUses(src, nullptr); |
| 589 | |
| 590 | if(Ice::Variable *var = llvm::dyn_cast<Ice::Variable>(src)) |
| 591 | { |
| 592 | deleteInstruction(getDefinition(var)); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | bool Optimizer::isDead(Ice::Inst *instruction) |
| 600 | { |
| 601 | Ice::Variable *dest = instruction->getDest(); |
| 602 | |
| 603 | if(dest) |
| 604 | { |
| 605 | return (!hasUses(dest) || getUses(dest)->empty()) && !instruction->hasSideEffects(); |
| 606 | } |
| 607 | else if(isStore(*instruction)) |
| 608 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 609 | if(Ice::Variable *address = llvm::dyn_cast<Ice::Variable>(instruction->getStoreAddress())) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 610 | { |
| 611 | Ice::Inst *def = getDefinition(address); |
| 612 | |
| 613 | if(def && llvm::isa<Ice::InstAlloca>(def)) |
| 614 | { |
| 615 | if(hasUses(address)) |
| 616 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 617 | Optimizer::Uses *uses = getUses(address); |
| 618 | return uses->size() == uses->stores.size(); // Dead if all uses are stores |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 619 | } |
| 620 | else |
| 621 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 622 | return true; // No uses |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return false; |
| 629 | } |
| 630 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 631 | const Ice::InstIntrinsic *Optimizer::asLoadSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 632 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 633 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 634 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 635 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::LoadSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 636 | { |
| 637 | return instrinsic; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return nullptr; |
| 642 | } |
| 643 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 644 | const Ice::InstIntrinsic *Optimizer::asStoreSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 645 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 646 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 647 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 648 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::StoreSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 649 | { |
| 650 | return instrinsic; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return nullptr; |
| 655 | } |
| 656 | |
| 657 | bool Optimizer::isLoad(const Ice::Inst &instruction) |
| 658 | { |
| 659 | if(llvm::isa<Ice::InstLoad>(&instruction)) |
| 660 | { |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | return asLoadSubVector(&instruction) != nullptr; |
| 665 | } |
| 666 | |
| 667 | bool Optimizer::isStore(const Ice::Inst &instruction) |
| 668 | { |
| 669 | if(llvm::isa<Ice::InstStore>(&instruction)) |
| 670 | { |
| 671 | return true; |
| 672 | } |
| 673 | |
| 674 | return asStoreSubVector(&instruction) != nullptr; |
| 675 | } |
| 676 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 677 | std::size_t Optimizer::storeSize(const Ice::Inst *store) |
| 678 | { |
| 679 | assert(isStore(*store)); |
| 680 | |
| 681 | if(auto *instStore = llvm::dyn_cast<Ice::InstStore>(store)) |
| 682 | { |
| 683 | return Ice::typeWidthInBytes(instStore->getData()->getType()); |
| 684 | } |
| 685 | |
| 686 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 687 | { |
| 688 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(3))->getValue(); |
| 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | bool Optimizer::loadTypeMatchesStore(const Ice::Inst *load, const Ice::Inst *store) |
| 695 | { |
| 696 | if(!load || !store) |
| 697 | { |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | assert(isLoad(*load) && isStore(*store)); |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 702 | assert(load->getLoadAddress() == store->getStoreAddress()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 703 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 704 | if(store->getData()->getType() != load->getDest()->getType()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 705 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 706 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 710 | { |
| 711 | if(auto *loadSubVector = asLoadSubVector(load)) |
| 712 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 713 | // Check for matching sub-vector width. |
| 714 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(2))->getValue() == |
| 715 | llvm::cast<Ice::ConstantInteger32>(loadSubVector->getSrc(1))->getValue(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 719 | return true; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 720 | } |
| 721 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 722 | Optimizer::Uses *Optimizer::getUses(Ice::Operand *operand) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 723 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 724 | Optimizer::Uses *uses = (Optimizer::Uses *)operand->Ice::Operand::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 725 | if(!uses) |
| 726 | { |
| 727 | uses = new Optimizer::Uses; |
| 728 | setUses(operand, uses); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 729 | operandsWithUses.push_back(operand); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 730 | } |
| 731 | return uses; |
| 732 | } |
| 733 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 734 | void Optimizer::setUses(Ice::Operand *operand, Optimizer::Uses *uses) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 735 | { |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 736 | if(auto *oldUses = reinterpret_cast<Optimizer::Uses *>(operand->Ice::Operand::getExternalData())) |
| 737 | { |
| 738 | delete oldUses; |
| 739 | } |
| 740 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 741 | operand->Ice::Operand::setExternalData(uses); |
| 742 | } |
| 743 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 744 | bool Optimizer::hasUses(Ice::Operand *operand) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 745 | { |
| 746 | return operand->Ice::Operand::getExternalData() != nullptr; |
| 747 | } |
| 748 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 749 | Ice::CfgNode *Optimizer::getNode(Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 750 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 751 | return (Ice::CfgNode *)inst->Ice::Inst::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 754 | void Optimizer::setNode(Ice::Inst *inst, Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 755 | { |
| 756 | inst->Ice::Inst::setExternalData(node); |
| 757 | } |
| 758 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 759 | Ice::Inst *Optimizer::getDefinition(Ice::Variable *var) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 760 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 761 | return (Ice::Inst *)var->Ice::Variable::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 762 | } |
| 763 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 764 | void Optimizer::setDefinition(Ice::Variable *var, Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 765 | { |
| 766 | var->Ice::Variable::setExternalData(inst); |
| 767 | } |
| 768 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 769 | const std::vector<Optimizer::LoadStoreInst> &Optimizer::getLoadStoreInsts(Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 770 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 771 | return *((const std::vector<LoadStoreInst> *)node->Ice::CfgNode::getExternalData()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 772 | } |
| 773 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 774 | void Optimizer::setLoadStoreInsts(Ice::CfgNode *node, std::vector<LoadStoreInst> *insts) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 775 | { |
| 776 | node->Ice::CfgNode::setExternalData(insts); |
| 777 | } |
| 778 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 779 | bool Optimizer::hasLoadStoreInsts(Ice::CfgNode *node) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 780 | { |
| 781 | return node->Ice::CfgNode::getExternalData() != nullptr; |
| 782 | } |
| 783 | |
| 784 | bool Optimizer::Uses::areOnlyLoadStore() const |
| 785 | { |
| 786 | return size() == (loads.size() + stores.size()); |
| 787 | } |
| 788 | |
| 789 | void Optimizer::Uses::insert(Ice::Operand *value, Ice::Inst *instruction) |
| 790 | { |
| 791 | push_back(instruction); |
| 792 | |
| 793 | if(isLoad(*instruction)) |
| 794 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 795 | if(value == instruction->getLoadAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 796 | { |
| 797 | loads.push_back(instruction); |
| 798 | } |
| 799 | } |
| 800 | else if(isStore(*instruction)) |
| 801 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 802 | if(value == instruction->getStoreAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 803 | { |
| 804 | stores.push_back(instruction); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void Optimizer::Uses::erase(Ice::Inst *instruction) |
| 810 | { |
| 811 | auto &uses = *this; |
| 812 | |
| 813 | for(size_t i = 0; i < uses.size(); i++) |
| 814 | { |
| 815 | if(uses[i] == instruction) |
| 816 | { |
| 817 | uses[i] = back(); |
| 818 | pop_back(); |
| 819 | |
| 820 | for(size_t i = 0; i < loads.size(); i++) |
| 821 | { |
| 822 | if(loads[i] == instruction) |
| 823 | { |
| 824 | loads[i] = loads.back(); |
| 825 | loads.pop_back(); |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | for(size_t i = 0; i < stores.size(); i++) |
| 831 | { |
| 832 | if(stores[i] == instruction) |
| 833 | { |
| 834 | stores[i] = stores.back(); |
| 835 | stores.pop_back(); |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 845 | } // anonymous namespace |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 846 | |
| 847 | namespace rr { |
| 848 | |
| 849 | void optimize(Ice::Cfg *function) |
| 850 | { |
| 851 | Optimizer optimizer; |
| 852 | |
| 853 | optimizer.run(function); |
| 854 | } |
| 855 | |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 856 | } // namespace rr |