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 | |
| 300 | for(Ice::Inst *load = &*++store->getIterator(), *next = nullptr; load != next; next = load, load = &*++store->getIterator()) |
| 301 | { |
| 302 | if(load->isDeleted() || !isLoad(*load)) |
| 303 | { |
| 304 | continue; |
| 305 | } |
| 306 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 307 | if(load->getLoadAddress() != address) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 308 | { |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | if(!loadTypeMatchesStore(load, store)) |
| 313 | { |
| 314 | continue; |
| 315 | } |
| 316 | |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 317 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 318 | // absolute address. We need to make sure we don't replace a variable with a constant |
| 319 | // on this load. |
| 320 | if(llvm::isa<Ice::Constant>(storeValue)) |
| 321 | { |
| 322 | continue; |
| 323 | } |
| 324 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 325 | replace(load, storeValue); |
| 326 | |
| 327 | for(size_t i = 0; i < addressUses.loads.size(); i++) |
| 328 | { |
| 329 | if(addressUses.loads[i] == load) |
| 330 | { |
| 331 | addressUses.loads[i] = addressUses.loads.back(); |
| 332 | addressUses.loads.pop_back(); |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | for(size_t i = 0; i < addressUses.size(); i++) |
| 338 | { |
| 339 | if(addressUses[i] == load) |
| 340 | { |
| 341 | addressUses[i] = addressUses.back(); |
| 342 | addressUses.pop_back(); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if(addressUses.size() == 1) |
| 348 | { |
| 349 | assert(addressUses[0] == store); |
| 350 | |
| 351 | alloca.setDeleted(); |
| 352 | store->setDeleted(); |
| 353 | setUses(address, nullptr); |
| 354 | |
| 355 | if(hasUses(storeValue)) |
| 356 | { |
| 357 | auto &valueUses = *getUses(storeValue); |
| 358 | |
| 359 | for(size_t i = 0; i < valueUses.size(); i++) |
| 360 | { |
| 361 | if(valueUses[i] == store) |
| 362 | { |
| 363 | valueUses[i] = valueUses.back(); |
| 364 | valueUses.pop_back(); |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if(valueUses.empty()) |
| 370 | { |
| 371 | setUses(storeValue, nullptr); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void Optimizer::optimizeStoresInSingleBasicBlock() |
| 383 | { |
| 384 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 385 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 386 | std::vector<std::vector<LoadStoreInst> *> allocatedVectors; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 387 | |
| 388 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 389 | { |
| 390 | if(alloca.isDeleted()) |
| 391 | { |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 396 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 397 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | Ice::Operand *address = alloca.getDest(); |
| 401 | |
| 402 | if(!hasUses(address)) |
| 403 | { |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | const auto &addressUses = *getUses(address); |
| 408 | |
| 409 | if(!addressUses.areOnlyLoadStore()) |
| 410 | { |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | Ice::CfgNode *singleBasicBlock = getNode(addressUses.stores[0]); |
| 415 | |
| 416 | for(size_t i = 1; i < addressUses.stores.size(); i++) |
| 417 | { |
| 418 | Ice::Inst *store = addressUses.stores[i]; |
| 419 | if(getNode(store) != singleBasicBlock) |
| 420 | { |
| 421 | singleBasicBlock = nullptr; |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if(singleBasicBlock) |
| 427 | { |
| 428 | if(!hasLoadStoreInsts(singleBasicBlock)) |
| 429 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 430 | std::vector<LoadStoreInst> *loadStoreInstVector = new std::vector<LoadStoreInst>(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 431 | setLoadStoreInsts(singleBasicBlock, loadStoreInstVector); |
| 432 | allocatedVectors.push_back(loadStoreInstVector); |
| 433 | for(Ice::Inst &inst : singleBasicBlock->getInsts()) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 434 | { |
| 435 | if(inst.isDeleted()) |
| 436 | { |
| 437 | continue; |
| 438 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 439 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 440 | bool isStoreInst = isStore(inst); |
| 441 | bool isLoadInst = isLoad(inst); |
| 442 | |
| 443 | if(isStoreInst || isLoadInst) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 444 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 445 | loadStoreInstVector->push_back(LoadStoreInst(&inst, isStoreInst)); |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 448 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 449 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 450 | Ice::Inst *store = nullptr; |
| 451 | Ice::Operand *storeValue = nullptr; |
| 452 | bool unmatchedLoads = false; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 453 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 454 | for(auto &loadStoreInst : getLoadStoreInsts(singleBasicBlock)) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 455 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 456 | Ice::Inst *inst = loadStoreInst.inst; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 457 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 458 | if((loadStoreInst.address != address) || inst->isDeleted()) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 459 | { |
| 460 | continue; |
| 461 | } |
| 462 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 463 | if(loadStoreInst.isStore) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 464 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 465 | // New store found. If we had a previous one, try to eliminate it. |
| 466 | if(store && !unmatchedLoads) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 467 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 468 | // If the previous store is wider than the new one, we can't eliminate it |
| 469 | // because there could be a wide load reading its non-overwritten data. |
| 470 | if(storeSize(inst) >= storeSize(store)) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 471 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 472 | deleteInstruction(store); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 476 | store = inst; |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 477 | storeValue = store->getData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 478 | unmatchedLoads = false; |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | if(!loadTypeMatchesStore(inst, store)) |
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 | unmatchedLoads = true; |
| 485 | continue; |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 486 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 487 | |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 488 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 489 | // absolute address. We need to make sure we don't replace a variable with a constant |
| 490 | // on this load. |
| 491 | if(llvm::isa<Ice::Constant>(storeValue)) |
| 492 | { |
Antonio Maiorano | 5ff1de5 | 2020-03-03 19:53:13 -0500 | [diff] [blame] | 493 | unmatchedLoads = true; |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 494 | continue; |
| 495 | } |
| 496 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 497 | replace(inst, storeValue); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 502 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 503 | for(auto loadStoreInstVector : allocatedVectors) |
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 | delete loadStoreInstVector; |
| 506 | } |
| 507 | } |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 508 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 509 | void Optimizer::analyzeUses(Ice::Cfg *function) |
| 510 | { |
| 511 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
| 512 | { |
| 513 | for(Ice::Inst &instruction : basicBlock->getInsts()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 514 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 515 | if(instruction.isDeleted()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 516 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 517 | continue; |
| 518 | } |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 519 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | setNode(&instruction, basicBlock); |
| 521 | if(instruction.getDest()) |
| 522 | { |
| 523 | setDefinition(instruction.getDest(), &instruction); |
| 524 | } |
| 525 | |
| 526 | for(Ice::SizeT i = 0; i < instruction.getSrcSize(); i++) |
| 527 | { |
| 528 | Ice::SizeT unique = 0; |
| 529 | for(; unique < i; unique++) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 530 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 531 | if(instruction.getSrc(i) == instruction.getSrc(unique)) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 532 | { |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | } |
| 536 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 537 | if(i == unique) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 538 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 539 | Ice::Operand *src = instruction.getSrc(i); |
| 540 | getUses(src)->insert(src, &instruction); |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 541 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 547 | void Optimizer::replace(Ice::Inst *instruction, Ice::Operand *newValue) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 548 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 549 | Ice::Variable *oldValue = instruction->getDest(); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 550 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 551 | if(!newValue) |
| 552 | { |
| 553 | newValue = context->getConstantUndef(oldValue->getType()); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 554 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 555 | |
| 556 | if(hasUses(oldValue)) |
| 557 | { |
| 558 | for(Ice::Inst *use : *getUses(oldValue)) |
| 559 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 560 | assert(!use->isDeleted()); // Should have been removed from uses already |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 561 | |
| 562 | for(Ice::SizeT i = 0; i < use->getSrcSize(); i++) |
| 563 | { |
| 564 | if(use->getSrc(i) == oldValue) |
| 565 | { |
| 566 | use->replaceSource(i, newValue); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | getUses(newValue)->insert(newValue, use); |
| 571 | } |
| 572 | |
| 573 | setUses(oldValue, nullptr); |
| 574 | } |
| 575 | |
| 576 | deleteInstruction(instruction); |
| 577 | } |
| 578 | |
| 579 | void Optimizer::deleteInstruction(Ice::Inst *instruction) |
| 580 | { |
| 581 | if(!instruction || instruction->isDeleted()) |
| 582 | { |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | instruction->setDeleted(); |
| 587 | |
| 588 | for(Ice::SizeT i = 0; i < instruction->getSrcSize(); i++) |
| 589 | { |
| 590 | Ice::Operand *src = instruction->getSrc(i); |
| 591 | |
| 592 | if(hasUses(src)) |
| 593 | { |
| 594 | auto &srcUses = *getUses(src); |
| 595 | |
| 596 | srcUses.erase(instruction); |
| 597 | |
| 598 | if(srcUses.empty()) |
| 599 | { |
| 600 | setUses(src, nullptr); |
| 601 | |
| 602 | if(Ice::Variable *var = llvm::dyn_cast<Ice::Variable>(src)) |
| 603 | { |
| 604 | deleteInstruction(getDefinition(var)); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | bool Optimizer::isDead(Ice::Inst *instruction) |
| 612 | { |
| 613 | Ice::Variable *dest = instruction->getDest(); |
| 614 | |
| 615 | if(dest) |
| 616 | { |
| 617 | return (!hasUses(dest) || getUses(dest)->empty()) && !instruction->hasSideEffects(); |
| 618 | } |
| 619 | else if(isStore(*instruction)) |
| 620 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 621 | if(Ice::Variable *address = llvm::dyn_cast<Ice::Variable>(instruction->getStoreAddress())) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 622 | { |
| 623 | Ice::Inst *def = getDefinition(address); |
| 624 | |
| 625 | if(def && llvm::isa<Ice::InstAlloca>(def)) |
| 626 | { |
| 627 | if(hasUses(address)) |
| 628 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 629 | Optimizer::Uses *uses = getUses(address); |
| 630 | return uses->size() == uses->stores.size(); // Dead if all uses are stores |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 634 | return true; // No uses |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return false; |
| 641 | } |
| 642 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 643 | const Ice::InstIntrinsic *Optimizer::asLoadSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 644 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 645 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 646 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 647 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::LoadSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 648 | { |
| 649 | return instrinsic; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | return nullptr; |
| 654 | } |
| 655 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 656 | const Ice::InstIntrinsic *Optimizer::asStoreSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 657 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 658 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 659 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 660 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::StoreSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 661 | { |
| 662 | return instrinsic; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return nullptr; |
| 667 | } |
| 668 | |
| 669 | bool Optimizer::isLoad(const Ice::Inst &instruction) |
| 670 | { |
| 671 | if(llvm::isa<Ice::InstLoad>(&instruction)) |
| 672 | { |
| 673 | return true; |
| 674 | } |
| 675 | |
| 676 | return asLoadSubVector(&instruction) != nullptr; |
| 677 | } |
| 678 | |
| 679 | bool Optimizer::isStore(const Ice::Inst &instruction) |
| 680 | { |
| 681 | if(llvm::isa<Ice::InstStore>(&instruction)) |
| 682 | { |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | return asStoreSubVector(&instruction) != nullptr; |
| 687 | } |
| 688 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 689 | std::size_t Optimizer::storeSize(const Ice::Inst *store) |
| 690 | { |
| 691 | assert(isStore(*store)); |
| 692 | |
| 693 | if(auto *instStore = llvm::dyn_cast<Ice::InstStore>(store)) |
| 694 | { |
| 695 | return Ice::typeWidthInBytes(instStore->getData()->getType()); |
| 696 | } |
| 697 | |
| 698 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 699 | { |
| 700 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(3))->getValue(); |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | bool Optimizer::loadTypeMatchesStore(const Ice::Inst *load, const Ice::Inst *store) |
| 707 | { |
| 708 | if(!load || !store) |
| 709 | { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | assert(isLoad(*load) && isStore(*store)); |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 714 | assert(load->getLoadAddress() == store->getStoreAddress()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 715 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 716 | if(store->getData()->getType() != load->getDest()->getType()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 717 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 718 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 722 | { |
| 723 | if(auto *loadSubVector = asLoadSubVector(load)) |
| 724 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 725 | // Check for matching sub-vector width. |
| 726 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(2))->getValue() == |
| 727 | llvm::cast<Ice::ConstantInteger32>(loadSubVector->getSrc(1))->getValue(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 731 | return true; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 734 | Optimizer::Uses *Optimizer::getUses(Ice::Operand *operand) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 735 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 736 | Optimizer::Uses *uses = (Optimizer::Uses *)operand->Ice::Operand::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 737 | if(!uses) |
| 738 | { |
| 739 | uses = new Optimizer::Uses; |
| 740 | setUses(operand, uses); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 741 | operandsWithUses.push_back(operand); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 742 | } |
| 743 | return uses; |
| 744 | } |
| 745 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 746 | void Optimizer::setUses(Ice::Operand *operand, Optimizer::Uses *uses) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 747 | { |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 748 | if(auto *oldUses = reinterpret_cast<Optimizer::Uses *>(operand->Ice::Operand::getExternalData())) |
| 749 | { |
| 750 | delete oldUses; |
| 751 | } |
| 752 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 753 | operand->Ice::Operand::setExternalData(uses); |
| 754 | } |
| 755 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 756 | bool Optimizer::hasUses(Ice::Operand *operand) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 757 | { |
| 758 | return operand->Ice::Operand::getExternalData() != nullptr; |
| 759 | } |
| 760 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 761 | Ice::CfgNode *Optimizer::getNode(Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 762 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 763 | return (Ice::CfgNode *)inst->Ice::Inst::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 764 | } |
| 765 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 766 | void Optimizer::setNode(Ice::Inst *inst, Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 767 | { |
| 768 | inst->Ice::Inst::setExternalData(node); |
| 769 | } |
| 770 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 771 | Ice::Inst *Optimizer::getDefinition(Ice::Variable *var) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 772 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 773 | return (Ice::Inst *)var->Ice::Variable::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 774 | } |
| 775 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 776 | void Optimizer::setDefinition(Ice::Variable *var, Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 777 | { |
| 778 | var->Ice::Variable::setExternalData(inst); |
| 779 | } |
| 780 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 781 | const std::vector<Optimizer::LoadStoreInst> &Optimizer::getLoadStoreInsts(Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 782 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 783 | return *((const std::vector<LoadStoreInst> *)node->Ice::CfgNode::getExternalData()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 784 | } |
| 785 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 786 | void Optimizer::setLoadStoreInsts(Ice::CfgNode *node, std::vector<LoadStoreInst> *insts) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 787 | { |
| 788 | node->Ice::CfgNode::setExternalData(insts); |
| 789 | } |
| 790 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 791 | bool Optimizer::hasLoadStoreInsts(Ice::CfgNode *node) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 792 | { |
| 793 | return node->Ice::CfgNode::getExternalData() != nullptr; |
| 794 | } |
| 795 | |
| 796 | bool Optimizer::Uses::areOnlyLoadStore() const |
| 797 | { |
| 798 | return size() == (loads.size() + stores.size()); |
| 799 | } |
| 800 | |
| 801 | void Optimizer::Uses::insert(Ice::Operand *value, Ice::Inst *instruction) |
| 802 | { |
| 803 | push_back(instruction); |
| 804 | |
| 805 | if(isLoad(*instruction)) |
| 806 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 807 | if(value == instruction->getLoadAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 808 | { |
| 809 | loads.push_back(instruction); |
| 810 | } |
| 811 | } |
| 812 | else if(isStore(*instruction)) |
| 813 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 814 | if(value == instruction->getStoreAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 815 | { |
| 816 | stores.push_back(instruction); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | void Optimizer::Uses::erase(Ice::Inst *instruction) |
| 822 | { |
| 823 | auto &uses = *this; |
| 824 | |
| 825 | for(size_t i = 0; i < uses.size(); i++) |
| 826 | { |
| 827 | if(uses[i] == instruction) |
| 828 | { |
| 829 | uses[i] = back(); |
| 830 | pop_back(); |
| 831 | |
| 832 | for(size_t i = 0; i < loads.size(); i++) |
| 833 | { |
| 834 | if(loads[i] == instruction) |
| 835 | { |
| 836 | loads[i] = loads.back(); |
| 837 | loads.pop_back(); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | for(size_t i = 0; i < stores.size(); i++) |
| 843 | { |
| 844 | if(stores[i] == instruction) |
| 845 | { |
| 846 | stores[i] = stores.back(); |
| 847 | stores.pop_back(); |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 857 | } // anonymous namespace |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 858 | |
| 859 | namespace rr { |
| 860 | |
| 861 | void optimize(Ice::Cfg *function) |
| 862 | { |
| 863 | Optimizer optimizer; |
| 864 | |
| 865 | optimizer.run(function); |
| 866 | } |
| 867 | |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 868 | } // namespace rr |