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: |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 27 | Optimizer(rr::Nucleus::OptimizerReport *report) |
| 28 | : report(report) |
| 29 | { |
| 30 | } |
| 31 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 32 | void run(Ice::Cfg *function); |
| 33 | |
| 34 | private: |
| 35 | void analyzeUses(Ice::Cfg *function); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 36 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 37 | void eliminateDeadCode(); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 38 | void propagateAlloca(); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 39 | void performScalarReplacementOfAggregates(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 40 | void eliminateUnitializedLoads(); |
| 41 | void eliminateLoadsFollowingSingleStore(); |
| 42 | void optimizeStoresInSingleBasicBlock(); |
| 43 | |
| 44 | void replace(Ice::Inst *instruction, Ice::Operand *newValue); |
| 45 | void deleteInstruction(Ice::Inst *instruction); |
| 46 | bool isDead(Ice::Inst *instruction); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 47 | bool isStaticallyIndexedArray(Ice::Operand *allocaAddress); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 48 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 49 | static const Ice::InstIntrinsic *asLoadSubVector(const Ice::Inst *instruction); |
| 50 | static const Ice::InstIntrinsic *asStoreSubVector(const Ice::Inst *instruction); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 51 | static bool isLoad(const Ice::Inst &instruction); |
| 52 | static bool isStore(const Ice::Inst &instruction); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 53 | static std::size_t storeSize(const Ice::Inst *instruction); |
| 54 | static bool loadTypeMatchesStore(const Ice::Inst *load, const Ice::Inst *store); |
| 55 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 56 | void collectDiagnostics(); |
| 57 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 58 | Ice::Cfg *function; |
| 59 | Ice::GlobalContext *context; |
| 60 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 61 | struct Uses : std::vector<Ice::Inst *> |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 62 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 63 | bool areOnlyLoadStore() const; |
| 64 | void insert(Ice::Operand *value, Ice::Inst *instruction); |
| 65 | void erase(Ice::Inst *instruction); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 66 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 67 | std::vector<Ice::Inst *> loads; |
| 68 | std::vector<Ice::Inst *> stores; |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 71 | struct LoadStoreInst |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 72 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 73 | LoadStoreInst(Ice::Inst *inst, bool isStore) |
| 74 | : inst(inst) |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 75 | , address(isStore ? inst->getStoreAddress() : inst->getLoadAddress()) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 76 | , isStore(isStore) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 77 | { |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 78 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 79 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 80 | Ice::Inst *inst; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 81 | Ice::Operand *address; |
| 82 | bool isStore; |
| 83 | }; |
| 84 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 85 | Optimizer::Uses *getUses(Ice::Operand *); |
| 86 | void setUses(Ice::Operand *, Optimizer::Uses *); |
| 87 | bool hasUses(Ice::Operand *) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 88 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 89 | Ice::CfgNode *getNode(Ice::Inst *); |
| 90 | void setNode(Ice::Inst *, Ice::CfgNode *); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 91 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 92 | Ice::Inst *getDefinition(Ice::Variable *); |
| 93 | void setDefinition(Ice::Variable *, Ice::Inst *); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 94 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 95 | const std::vector<LoadStoreInst> &getLoadStoreInsts(Ice::CfgNode *); |
| 96 | void setLoadStoreInsts(Ice::CfgNode *, std::vector<LoadStoreInst> *); |
| 97 | bool hasLoadStoreInsts(Ice::CfgNode *node) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 98 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 99 | std::vector<Ice::Operand *> operandsWithUses; |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 100 | |
| 101 | rr::Nucleus::OptimizerReport *report = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | void Optimizer::run(Ice::Cfg *function) |
| 105 | { |
| 106 | this->function = function; |
| 107 | this->context = function->getContext(); |
| 108 | |
| 109 | analyzeUses(function); |
| 110 | |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 111 | // Start by eliminating any dead code, to avoid redundant work for the |
| 112 | // subsequent optimization passes. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 113 | eliminateDeadCode(); |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 114 | |
| 115 | // Eliminate allocas which store the address of other allocas. |
| 116 | propagateAlloca(); |
| 117 | |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 118 | // Replace arrays with individual elements if only statically indexed. |
| 119 | performScalarReplacementOfAggregates(); |
| 120 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 121 | eliminateUnitializedLoads(); |
| 122 | eliminateLoadsFollowingSingleStore(); |
| 123 | optimizeStoresInSingleBasicBlock(); |
| 124 | eliminateDeadCode(); |
| 125 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 126 | for(auto operand : operandsWithUses) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 127 | { |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 128 | // Deletes the Uses instance on the operand |
| 129 | setUses(operand, nullptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 130 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 131 | operandsWithUses.clear(); |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 132 | |
| 133 | collectDiagnostics(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Nicolas Capens | 0b506e1 | 2021-02-04 17:00:08 -0500 | [diff] [blame] | 136 | // Eliminates allocas which store the address of other allocas. |
| 137 | void Optimizer::propagateAlloca() |
| 138 | { |
| 139 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 140 | Ice::InstList &instList = entryBlock->getInsts(); |
| 141 | |
| 142 | for(Ice::Inst &inst : instList) |
| 143 | { |
| 144 | if(inst.isDeleted()) |
| 145 | { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | auto *alloca = llvm::dyn_cast<Ice::InstAlloca>(&inst); |
| 150 | |
| 151 | if(!alloca) |
| 152 | { |
| 153 | break; // Allocas are all at the top |
| 154 | } |
| 155 | |
| 156 | // Look for stores of this alloca's address. |
| 157 | Ice::Operand *address = alloca->getDest(); |
| 158 | Uses uses = *getUses(address); // Hard copy |
| 159 | |
| 160 | for(auto *store : uses) |
| 161 | { |
| 162 | if(isStore(*store) && store->getData() == address) |
| 163 | { |
| 164 | Ice::Operand *dest = store->getStoreAddress(); |
| 165 | Ice::Variable *destVar = llvm::dyn_cast<Ice::Variable>(dest); |
| 166 | Ice::Inst *def = destVar ? getDefinition(destVar) : nullptr; |
| 167 | |
| 168 | // If the address is stored into another stack variable, eliminate the latter. |
| 169 | if(def && def->getKind() == Ice::Inst::Alloca) |
| 170 | { |
| 171 | Uses destUses = *getUses(dest); // Hard copy |
| 172 | |
| 173 | // Make sure the only store into the stack variable is this address, and that all of its other uses are loads. |
| 174 | // This prevents dynamic array loads/stores to be replaced by a scalar. |
| 175 | if((destUses.stores.size() == 1) && (destUses.loads.size() == destUses.size() - 1)) |
| 176 | { |
| 177 | for(auto *load : destUses.loads) |
| 178 | { |
| 179 | replace(load, address); |
| 180 | } |
| 181 | |
| 182 | // The address is now only stored, never loaded, so the store can be eliminated, together with its alloca. |
| 183 | assert(getUses(dest)->size() == 1); |
| 184 | deleteInstruction(store); |
| 185 | assert(def->isDeleted()); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 193 | Ice::Type pointerType() |
| 194 | { |
| 195 | if(sizeof(void *) == 8) |
| 196 | { |
| 197 | return Ice::IceType_i64; |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | return Ice::IceType_i32; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Replace arrays with individual elements if only statically indexed. |
| 206 | void Optimizer::performScalarReplacementOfAggregates() |
| 207 | { |
| 208 | std::vector<Ice::InstAlloca *> newAllocas; |
| 209 | |
| 210 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 211 | Ice::InstList &instList = entryBlock->getInsts(); |
| 212 | |
| 213 | for(Ice::Inst &inst : instList) |
| 214 | { |
| 215 | if(inst.isDeleted()) |
| 216 | { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | auto *alloca = llvm::dyn_cast<Ice::InstAlloca>(&inst); |
| 221 | |
| 222 | if(!alloca) |
| 223 | { |
| 224 | break; // Allocas are all at the top |
| 225 | } |
| 226 | |
| 227 | uint32_t sizeInBytes = llvm::cast<Ice::ConstantInteger32>(alloca->getSizeInBytes())->getValue(); |
| 228 | uint32_t alignInBytes = alloca->getAlignInBytes(); |
| 229 | |
| 230 | // This pass relies on array elements to be naturally aligned (i.e. matches the type size). |
| 231 | assert(sizeInBytes >= alignInBytes); |
| 232 | assert(sizeInBytes % alignInBytes == 0); |
| 233 | uint32_t elementCount = sizeInBytes / alignInBytes; |
| 234 | |
| 235 | Ice::Operand *address = alloca->getDest(); |
| 236 | |
| 237 | if(isStaticallyIndexedArray(address)) |
| 238 | { |
| 239 | // Delete the old array. |
| 240 | alloca->setDeleted(); |
| 241 | |
| 242 | // Allocate new stack slots for each element. |
| 243 | std::vector<Ice::Variable *> newAddress(elementCount); |
| 244 | auto *bytes = Ice::ConstantInteger32::create(context, Ice::IceType_i32, alignInBytes); |
| 245 | |
| 246 | for(uint32_t i = 0; i < elementCount; i++) |
| 247 | { |
| 248 | newAddress[i] = function->makeVariable(pointerType()); |
| 249 | auto *alloca = Ice::InstAlloca::create(function, newAddress[i], bytes, alignInBytes); |
| 250 | setDefinition(newAddress[i], alloca); |
| 251 | |
| 252 | newAllocas.push_back(alloca); |
| 253 | } |
| 254 | |
| 255 | Uses uses = *getUses(address); // Hard copy |
| 256 | |
| 257 | for(auto *use : uses) |
| 258 | { |
| 259 | assert(!use->isDeleted()); |
| 260 | |
| 261 | if(isLoad(*use)) // Direct use of base address |
| 262 | { |
| 263 | use->replaceSource(asLoadSubVector(use) ? 1 : 0, newAddress[0]); |
| 264 | getUses(newAddress[0])->insert(newAddress[0], use); |
| 265 | } |
| 266 | else if(isStore(*use)) // Direct use of base address |
| 267 | { |
| 268 | use->replaceSource(asStoreSubVector(use) ? 2 : 1, newAddress[0]); |
| 269 | getUses(newAddress[0])->insert(newAddress[0], use); |
| 270 | } |
| 271 | else // Statically indexed use |
| 272 | { |
| 273 | auto *arithmetic = llvm::cast<Ice::InstArithmetic>(use); |
| 274 | |
| 275 | if(arithmetic->getOp() == Ice::InstArithmetic::Add) |
| 276 | { |
| 277 | auto *rhs = arithmetic->getSrc(1); |
| 278 | int32_t offset = llvm::cast<Ice::ConstantInteger32>(rhs)->getValue(); |
| 279 | |
| 280 | assert(offset % alignInBytes == 0); |
| 281 | int32_t index = offset / alignInBytes; |
| 282 | assert(static_cast<uint32_t>(index) < elementCount); |
| 283 | |
| 284 | replace(arithmetic, newAddress[index]); |
| 285 | } |
| 286 | else |
| 287 | assert(false && "Mismatch between isStaticallyIndexedArray() and scalarReplacementOfAggregates()"); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // After looping over all the old allocas, add any new ones that replace them. |
| 294 | // They're added to the front in reverse order, to retain their original order. |
| 295 | for(size_t i = newAllocas.size(); i-- != 0;) |
| 296 | { |
| 297 | if(!isDead(newAllocas[i])) |
| 298 | { |
| 299 | instList.push_front(newAllocas[i]); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 304 | void Optimizer::eliminateDeadCode() |
| 305 | { |
| 306 | bool modified; |
| 307 | do |
| 308 | { |
| 309 | modified = false; |
| 310 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 311 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 312 | for(Ice::Inst &inst : Ice::reverse_range(basicBlock->getInsts())) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 313 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 314 | if(inst.isDeleted()) |
| 315 | { |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | if(isDead(&inst)) |
| 320 | { |
| 321 | deleteInstruction(&inst); |
| 322 | modified = true; |
| 323 | } |
| 324 | } |
| 325 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 326 | } while(modified); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void Optimizer::eliminateUnitializedLoads() |
| 330 | { |
| 331 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 332 | |
| 333 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 334 | { |
| 335 | if(alloca.isDeleted()) |
| 336 | { |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 341 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 342 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | Ice::Operand *address = alloca.getDest(); |
| 346 | |
| 347 | if(!hasUses(address)) |
| 348 | { |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | const auto &addressUses = *getUses(address); |
| 353 | |
| 354 | if(!addressUses.areOnlyLoadStore()) |
| 355 | { |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | if(addressUses.stores.empty()) |
| 360 | { |
| 361 | for(Ice::Inst *load : addressUses.loads) |
| 362 | { |
| 363 | Ice::Variable *loadData = load->getDest(); |
| 364 | |
| 365 | if(hasUses(loadData)) |
| 366 | { |
| 367 | for(Ice::Inst *use : *getUses(loadData)) |
| 368 | { |
| 369 | for(Ice::SizeT i = 0; i < use->getSrcSize(); i++) |
| 370 | { |
| 371 | if(use->getSrc(i) == loadData) |
| 372 | { |
| 373 | auto *undef = context->getConstantUndef(loadData->getType()); |
| 374 | |
| 375 | use->replaceSource(i, undef); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | setUses(loadData, nullptr); |
| 381 | } |
| 382 | |
| 383 | load->setDeleted(); |
| 384 | } |
| 385 | |
| 386 | alloca.setDeleted(); |
| 387 | setUses(address, nullptr); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void Optimizer::eliminateLoadsFollowingSingleStore() |
| 393 | { |
| 394 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 395 | |
| 396 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 397 | { |
| 398 | if(alloca.isDeleted()) |
| 399 | { |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 404 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 405 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | Ice::Operand *address = alloca.getDest(); |
| 409 | |
| 410 | if(!hasUses(address)) |
| 411 | { |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | auto &addressUses = *getUses(address); |
| 416 | |
| 417 | if(!addressUses.areOnlyLoadStore()) |
| 418 | { |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | if(addressUses.stores.size() == 1) |
| 423 | { |
| 424 | Ice::Inst *store = addressUses.stores[0]; |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 425 | Ice::Operand *storeValue = store->getData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 426 | |
Nicolas Capens | 3ab17bd | 2021-02-10 16:41:31 -0500 | [diff] [blame] | 427 | auto instIterator = store->getIterator(); |
| 428 | auto basicBlockEnd = getNode(store)->getInsts().end(); |
| 429 | |
| 430 | while(++instIterator != basicBlockEnd) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 431 | { |
Nicolas Capens | 3ab17bd | 2021-02-10 16:41:31 -0500 | [diff] [blame] | 432 | Ice::Inst *load = &*instIterator; |
| 433 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 434 | if(load->isDeleted() || !isLoad(*load)) |
| 435 | { |
| 436 | continue; |
| 437 | } |
| 438 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 439 | if(load->getLoadAddress() != address) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 440 | { |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | if(!loadTypeMatchesStore(load, store)) |
| 445 | { |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | replace(load, storeValue); |
| 450 | |
| 451 | for(size_t i = 0; i < addressUses.loads.size(); i++) |
| 452 | { |
| 453 | if(addressUses.loads[i] == load) |
| 454 | { |
| 455 | addressUses.loads[i] = addressUses.loads.back(); |
| 456 | addressUses.loads.pop_back(); |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | for(size_t i = 0; i < addressUses.size(); i++) |
| 462 | { |
| 463 | if(addressUses[i] == load) |
| 464 | { |
| 465 | addressUses[i] = addressUses.back(); |
| 466 | addressUses.pop_back(); |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if(addressUses.size() == 1) |
| 472 | { |
| 473 | assert(addressUses[0] == store); |
| 474 | |
| 475 | alloca.setDeleted(); |
| 476 | store->setDeleted(); |
| 477 | setUses(address, nullptr); |
| 478 | |
| 479 | if(hasUses(storeValue)) |
| 480 | { |
| 481 | auto &valueUses = *getUses(storeValue); |
| 482 | |
| 483 | for(size_t i = 0; i < valueUses.size(); i++) |
| 484 | { |
| 485 | if(valueUses[i] == store) |
| 486 | { |
| 487 | valueUses[i] = valueUses.back(); |
| 488 | valueUses.pop_back(); |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if(valueUses.empty()) |
| 494 | { |
| 495 | setUses(storeValue, nullptr); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void Optimizer::optimizeStoresInSingleBasicBlock() |
| 507 | { |
| 508 | Ice::CfgNode *entryBlock = function->getEntryNode(); |
| 509 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 510 | std::vector<std::vector<LoadStoreInst> *> allocatedVectors; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 511 | |
| 512 | for(Ice::Inst &alloca : entryBlock->getInsts()) |
| 513 | { |
| 514 | if(alloca.isDeleted()) |
| 515 | { |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | if(!llvm::isa<Ice::InstAlloca>(alloca)) |
| 520 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 521 | break; // Allocas are all at the top |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | Ice::Operand *address = alloca.getDest(); |
| 525 | |
| 526 | if(!hasUses(address)) |
| 527 | { |
| 528 | continue; |
| 529 | } |
| 530 | |
| 531 | const auto &addressUses = *getUses(address); |
| 532 | |
| 533 | if(!addressUses.areOnlyLoadStore()) |
| 534 | { |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | Ice::CfgNode *singleBasicBlock = getNode(addressUses.stores[0]); |
| 539 | |
| 540 | for(size_t i = 1; i < addressUses.stores.size(); i++) |
| 541 | { |
| 542 | Ice::Inst *store = addressUses.stores[i]; |
| 543 | if(getNode(store) != singleBasicBlock) |
| 544 | { |
| 545 | singleBasicBlock = nullptr; |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if(singleBasicBlock) |
| 551 | { |
| 552 | if(!hasLoadStoreInsts(singleBasicBlock)) |
| 553 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 554 | std::vector<LoadStoreInst> *loadStoreInstVector = new std::vector<LoadStoreInst>(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 555 | setLoadStoreInsts(singleBasicBlock, loadStoreInstVector); |
| 556 | allocatedVectors.push_back(loadStoreInstVector); |
| 557 | for(Ice::Inst &inst : singleBasicBlock->getInsts()) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 558 | { |
| 559 | if(inst.isDeleted()) |
| 560 | { |
| 561 | continue; |
| 562 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 563 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 564 | bool isStoreInst = isStore(inst); |
| 565 | bool isLoadInst = isLoad(inst); |
| 566 | |
| 567 | if(isStoreInst || isLoadInst) |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 568 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 569 | loadStoreInstVector->push_back(LoadStoreInst(&inst, isStoreInst)); |
Nicolas Capens | c9d70d5 | 2016-12-12 15:07:39 -0500 | [diff] [blame] | 570 | } |
| 571 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 572 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 573 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 574 | Ice::Inst *store = nullptr; |
| 575 | Ice::Operand *storeValue = nullptr; |
| 576 | bool unmatchedLoads = false; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 577 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 578 | for(auto &loadStoreInst : getLoadStoreInsts(singleBasicBlock)) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 579 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 580 | Ice::Inst *inst = loadStoreInst.inst; |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 581 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 582 | if((loadStoreInst.address != address) || inst->isDeleted()) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 583 | { |
| 584 | continue; |
| 585 | } |
| 586 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 587 | if(loadStoreInst.isStore) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 588 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 589 | // New store found. If we had a previous one, try to eliminate it. |
| 590 | if(store && !unmatchedLoads) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 591 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 592 | // If the previous store is wider than the new one, we can't eliminate it |
| 593 | // because there could be a wide load reading its non-overwritten data. |
| 594 | if(storeSize(inst) >= storeSize(store)) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 595 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 596 | deleteInstruction(store); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 600 | store = inst; |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 601 | storeValue = store->getData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 602 | unmatchedLoads = false; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | if(!loadTypeMatchesStore(inst, store)) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 607 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 608 | unmatchedLoads = true; |
| 609 | continue; |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 610 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 611 | |
| 612 | replace(inst, storeValue); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 617 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 618 | for(auto loadStoreInstVector : allocatedVectors) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 619 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 620 | delete loadStoreInstVector; |
| 621 | } |
| 622 | } |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 623 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 624 | void Optimizer::analyzeUses(Ice::Cfg *function) |
| 625 | { |
| 626 | for(Ice::CfgNode *basicBlock : function->getNodes()) |
| 627 | { |
| 628 | for(Ice::Inst &instruction : basicBlock->getInsts()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 629 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 630 | if(instruction.isDeleted()) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 631 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 632 | continue; |
| 633 | } |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 634 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 635 | setNode(&instruction, basicBlock); |
| 636 | if(instruction.getDest()) |
| 637 | { |
| 638 | setDefinition(instruction.getDest(), &instruction); |
| 639 | } |
| 640 | |
| 641 | for(Ice::SizeT i = 0; i < instruction.getSrcSize(); i++) |
| 642 | { |
| 643 | Ice::SizeT unique = 0; |
| 644 | for(; unique < i; unique++) |
Nicolas Capens | e205c3d | 2016-11-30 15:14:28 -0500 | [diff] [blame] | 645 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 646 | if(instruction.getSrc(i) == instruction.getSrc(unique)) |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 647 | { |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | } |
| 651 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 652 | if(i == unique) |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 653 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 654 | Ice::Operand *src = instruction.getSrc(i); |
| 655 | getUses(src)->insert(src, &instruction); |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 656 | } |
Nicolas Capens | f4452fc | 2016-12-12 13:08:06 -0500 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 662 | void Optimizer::replace(Ice::Inst *instruction, Ice::Operand *newValue) |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 663 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 664 | Ice::Variable *oldValue = instruction->getDest(); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 665 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 666 | if(!newValue) |
| 667 | { |
| 668 | newValue = context->getConstantUndef(oldValue->getType()); |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 669 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 670 | |
| 671 | if(hasUses(oldValue)) |
| 672 | { |
| 673 | for(Ice::Inst *use : *getUses(oldValue)) |
| 674 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 675 | assert(!use->isDeleted()); // Should have been removed from uses already |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 676 | |
| 677 | for(Ice::SizeT i = 0; i < use->getSrcSize(); i++) |
| 678 | { |
| 679 | if(use->getSrc(i) == oldValue) |
| 680 | { |
| 681 | use->replaceSource(i, newValue); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | getUses(newValue)->insert(newValue, use); |
| 686 | } |
| 687 | |
| 688 | setUses(oldValue, nullptr); |
| 689 | } |
| 690 | |
| 691 | deleteInstruction(instruction); |
| 692 | } |
| 693 | |
| 694 | void Optimizer::deleteInstruction(Ice::Inst *instruction) |
| 695 | { |
| 696 | if(!instruction || instruction->isDeleted()) |
| 697 | { |
| 698 | return; |
| 699 | } |
| 700 | |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 701 | assert(!instruction->getDest() || getUses(instruction->getDest())->empty()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 702 | instruction->setDeleted(); |
| 703 | |
| 704 | for(Ice::SizeT i = 0; i < instruction->getSrcSize(); i++) |
| 705 | { |
| 706 | Ice::Operand *src = instruction->getSrc(i); |
| 707 | |
| 708 | if(hasUses(src)) |
| 709 | { |
| 710 | auto &srcUses = *getUses(src); |
| 711 | |
| 712 | srcUses.erase(instruction); |
| 713 | |
| 714 | if(srcUses.empty()) |
| 715 | { |
| 716 | setUses(src, nullptr); |
| 717 | |
| 718 | if(Ice::Variable *var = llvm::dyn_cast<Ice::Variable>(src)) |
| 719 | { |
| 720 | deleteInstruction(getDefinition(var)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | bool Optimizer::isDead(Ice::Inst *instruction) |
| 728 | { |
| 729 | Ice::Variable *dest = instruction->getDest(); |
| 730 | |
| 731 | if(dest) |
| 732 | { |
| 733 | return (!hasUses(dest) || getUses(dest)->empty()) && !instruction->hasSideEffects(); |
| 734 | } |
| 735 | else if(isStore(*instruction)) |
| 736 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 737 | if(Ice::Variable *address = llvm::dyn_cast<Ice::Variable>(instruction->getStoreAddress())) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 738 | { |
| 739 | Ice::Inst *def = getDefinition(address); |
| 740 | |
| 741 | if(def && llvm::isa<Ice::InstAlloca>(def)) |
| 742 | { |
| 743 | if(hasUses(address)) |
| 744 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 745 | Optimizer::Uses *uses = getUses(address); |
| 746 | return uses->size() == uses->stores.size(); // Dead if all uses are stores |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 747 | } |
| 748 | else |
| 749 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 750 | return true; // No uses |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return false; |
| 757 | } |
| 758 | |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 759 | bool Optimizer::isStaticallyIndexedArray(Ice::Operand *allocaAddress) |
| 760 | { |
| 761 | auto &uses = *getUses(allocaAddress); |
| 762 | |
| 763 | for(auto *use : uses) |
| 764 | { |
| 765 | // Direct load from base address. |
| 766 | if(isLoad(*use) && use->getLoadAddress() == allocaAddress) |
| 767 | { |
| 768 | continue; // This is fine. |
| 769 | } |
| 770 | |
| 771 | if(isStore(*use)) |
| 772 | { |
| 773 | // Can either be the address we're storing to, or the data we're storing. |
| 774 | if(use->getStoreAddress() == allocaAddress) |
| 775 | { |
| 776 | continue; |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | // propagateAlloca() eliminates most of the stores of the address itself. |
| 781 | // For the cases it doesn't handle, assume SRoA is not feasible. |
| 782 | return false; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // Pointer arithmetic is fine if it only uses constants. |
| 787 | auto *arithmetic = llvm::dyn_cast<Ice::InstArithmetic>(use); |
| 788 | if(arithmetic && arithmetic->getOp() == Ice::InstArithmetic::Add) |
| 789 | { |
| 790 | auto *rhs = arithmetic->getSrc(1); |
| 791 | |
| 792 | if(llvm::isa<Ice::Constant>(rhs)) |
| 793 | { |
| 794 | continue; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // If there's any other type of use, bail out. |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | return true; |
| 803 | } |
| 804 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 805 | const Ice::InstIntrinsic *Optimizer::asLoadSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 806 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 807 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 808 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 809 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::LoadSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 810 | { |
| 811 | return instrinsic; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return nullptr; |
| 816 | } |
| 817 | |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 818 | const Ice::InstIntrinsic *Optimizer::asStoreSubVector(const Ice::Inst *instruction) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 819 | { |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 820 | if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsic>(instruction)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 821 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 822 | if(instrinsic->getIntrinsicID() == Ice::Intrinsics::StoreSubVector) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 823 | { |
| 824 | return instrinsic; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return nullptr; |
| 829 | } |
| 830 | |
| 831 | bool Optimizer::isLoad(const Ice::Inst &instruction) |
| 832 | { |
| 833 | if(llvm::isa<Ice::InstLoad>(&instruction)) |
| 834 | { |
| 835 | return true; |
| 836 | } |
| 837 | |
| 838 | return asLoadSubVector(&instruction) != nullptr; |
| 839 | } |
| 840 | |
| 841 | bool Optimizer::isStore(const Ice::Inst &instruction) |
| 842 | { |
| 843 | if(llvm::isa<Ice::InstStore>(&instruction)) |
| 844 | { |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | return asStoreSubVector(&instruction) != nullptr; |
| 849 | } |
| 850 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 851 | std::size_t Optimizer::storeSize(const Ice::Inst *store) |
| 852 | { |
| 853 | assert(isStore(*store)); |
| 854 | |
| 855 | if(auto *instStore = llvm::dyn_cast<Ice::InstStore>(store)) |
| 856 | { |
| 857 | return Ice::typeWidthInBytes(instStore->getData()->getType()); |
| 858 | } |
| 859 | |
| 860 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 861 | { |
| 862 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(3))->getValue(); |
| 863 | } |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | bool Optimizer::loadTypeMatchesStore(const Ice::Inst *load, const Ice::Inst *store) |
| 869 | { |
| 870 | if(!load || !store) |
| 871 | { |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | assert(isLoad(*load) && isStore(*store)); |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 876 | assert(load->getLoadAddress() == store->getStoreAddress()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 877 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 878 | if(store->getData()->getType() != load->getDest()->getType()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 879 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 880 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if(auto *storeSubVector = asStoreSubVector(store)) |
| 884 | { |
| 885 | if(auto *loadSubVector = asLoadSubVector(load)) |
| 886 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 887 | // Check for matching sub-vector width. |
| 888 | return llvm::cast<Ice::ConstantInteger32>(storeSubVector->getSrc(2))->getValue() == |
| 889 | llvm::cast<Ice::ConstantInteger32>(loadSubVector->getSrc(1))->getValue(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 893 | return true; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 894 | } |
| 895 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 896 | void Optimizer::collectDiagnostics() |
| 897 | { |
| 898 | if(report) |
| 899 | { |
| 900 | *report = {}; |
| 901 | |
| 902 | for(auto *basicBlock : function->getNodes()) |
| 903 | { |
| 904 | for(auto &inst : basicBlock->getInsts()) |
| 905 | { |
| 906 | if(inst.isDeleted()) |
| 907 | { |
| 908 | continue; |
| 909 | } |
| 910 | |
| 911 | if(llvm::isa<Ice::InstAlloca>(inst)) |
| 912 | { |
| 913 | report->allocas++; |
| 914 | } |
| 915 | else if(isLoad(inst)) |
| 916 | { |
| 917 | report->loads++; |
| 918 | } |
| 919 | else if(isStore(inst)) |
| 920 | { |
| 921 | report->stores++; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 928 | Optimizer::Uses *Optimizer::getUses(Ice::Operand *operand) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 929 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 930 | Optimizer::Uses *uses = (Optimizer::Uses *)operand->Ice::Operand::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 931 | if(!uses) |
| 932 | { |
| 933 | uses = new Optimizer::Uses; |
| 934 | setUses(operand, uses); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 935 | operandsWithUses.push_back(operand); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 936 | } |
| 937 | return uses; |
| 938 | } |
| 939 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 940 | void Optimizer::setUses(Ice::Operand *operand, Optimizer::Uses *uses) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 941 | { |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 942 | if(auto *oldUses = reinterpret_cast<Optimizer::Uses *>(operand->Ice::Operand::getExternalData())) |
| 943 | { |
| 944 | delete oldUses; |
| 945 | } |
| 946 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 947 | operand->Ice::Operand::setExternalData(uses); |
| 948 | } |
| 949 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 950 | bool Optimizer::hasUses(Ice::Operand *operand) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 951 | { |
| 952 | return operand->Ice::Operand::getExternalData() != nullptr; |
| 953 | } |
| 954 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 955 | Ice::CfgNode *Optimizer::getNode(Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 956 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 957 | return (Ice::CfgNode *)inst->Ice::Inst::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 958 | } |
| 959 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 960 | void Optimizer::setNode(Ice::Inst *inst, Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 961 | { |
| 962 | inst->Ice::Inst::setExternalData(node); |
| 963 | } |
| 964 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 965 | Ice::Inst *Optimizer::getDefinition(Ice::Variable *var) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 966 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 967 | return (Ice::Inst *)var->Ice::Variable::getExternalData(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 968 | } |
| 969 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 970 | void Optimizer::setDefinition(Ice::Variable *var, Ice::Inst *inst) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 971 | { |
| 972 | var->Ice::Variable::setExternalData(inst); |
| 973 | } |
| 974 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 975 | const std::vector<Optimizer::LoadStoreInst> &Optimizer::getLoadStoreInsts(Ice::CfgNode *node) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 976 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 977 | return *((const std::vector<LoadStoreInst> *)node->Ice::CfgNode::getExternalData()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 978 | } |
| 979 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 980 | void Optimizer::setLoadStoreInsts(Ice::CfgNode *node, std::vector<LoadStoreInst> *insts) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 981 | { |
| 982 | node->Ice::CfgNode::setExternalData(insts); |
| 983 | } |
| 984 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 985 | bool Optimizer::hasLoadStoreInsts(Ice::CfgNode *node) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 986 | { |
| 987 | return node->Ice::CfgNode::getExternalData() != nullptr; |
| 988 | } |
| 989 | |
| 990 | bool Optimizer::Uses::areOnlyLoadStore() const |
| 991 | { |
| 992 | return size() == (loads.size() + stores.size()); |
| 993 | } |
| 994 | |
| 995 | void Optimizer::Uses::insert(Ice::Operand *value, Ice::Inst *instruction) |
| 996 | { |
| 997 | push_back(instruction); |
| 998 | |
| 999 | if(isLoad(*instruction)) |
| 1000 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 1001 | if(value == instruction->getLoadAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1002 | { |
| 1003 | loads.push_back(instruction); |
| 1004 | } |
| 1005 | } |
| 1006 | else if(isStore(*instruction)) |
| 1007 | { |
Nicolas Capens | 673a7fe | 2021-02-05 15:03:22 -0500 | [diff] [blame] | 1008 | if(value == instruction->getStoreAddress()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1009 | { |
| 1010 | stores.push_back(instruction); |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | void Optimizer::Uses::erase(Ice::Inst *instruction) |
| 1016 | { |
| 1017 | auto &uses = *this; |
| 1018 | |
| 1019 | for(size_t i = 0; i < uses.size(); i++) |
| 1020 | { |
| 1021 | if(uses[i] == instruction) |
| 1022 | { |
| 1023 | uses[i] = back(); |
| 1024 | pop_back(); |
| 1025 | |
| 1026 | for(size_t i = 0; i < loads.size(); i++) |
| 1027 | { |
| 1028 | if(loads[i] == instruction) |
| 1029 | { |
| 1030 | loads[i] = loads.back(); |
| 1031 | loads.pop_back(); |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | for(size_t i = 0; i < stores.size(); i++) |
| 1037 | { |
| 1038 | if(stores[i] == instruction) |
| 1039 | { |
| 1040 | stores[i] = stores.back(); |
| 1041 | stores.pop_back(); |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | break; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1051 | } // anonymous namespace |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1052 | |
| 1053 | namespace rr { |
| 1054 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 1055 | void optimize(Ice::Cfg *function, Nucleus::OptimizerReport *report) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1056 | { |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 1057 | Optimizer optimizer(report); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1058 | |
| 1059 | optimizer.run(function); |
| 1060 | } |
| 1061 | |
Antonio Maiorano | 614a4d4 | 2020-01-29 13:33:02 -0500 | [diff] [blame] | 1062 | } // namespace rr |