|
| 1 | +#include "slang-ir-address-analysis.h" |
| 2 | +#include "slang-ir-insts.h" |
| 3 | +#include "slang-ir-util.h" |
| 4 | + |
| 5 | +namespace Slang |
| 6 | +{ |
| 7 | + void moveInstToEarliestPoint(IRDominatorTree* domTree, IRGlobalValueWithCode* func, IRInst* inst) |
| 8 | + { |
| 9 | + if (!as<IRBlock>(inst->getParent())) |
| 10 | + return; |
| 11 | + if (domTree->isUnreachable(as<IRBlock>(inst->getParent()))) |
| 12 | + return; |
| 13 | + |
| 14 | + List<IRBlock*> blocks; |
| 15 | + HashSet<IRInst*> operandInsts; |
| 16 | + for (UInt i = 0; i < inst->getOperandCount(); i++) |
| 17 | + { |
| 18 | + operandInsts.Add(inst->getOperand(i)); |
| 19 | + auto parentBlock = as<IRBlock>(inst->getOperand(i)->getParent()); |
| 20 | + if (parentBlock) |
| 21 | + { |
| 22 | + if (!domTree->isUnreachable(parentBlock)) |
| 23 | + blocks.add(parentBlock); |
| 24 | + } |
| 25 | + } |
| 26 | + { |
| 27 | + operandInsts.Add(inst->getFullType()); |
| 28 | + auto parentBlock = as<IRBlock>(inst->getFullType()->getParent()); |
| 29 | + if (parentBlock) |
| 30 | + { |
| 31 | + if (!domTree->isUnreachable(parentBlock)) |
| 32 | + blocks.add(parentBlock); |
| 33 | + } |
| 34 | + } |
| 35 | + // Find earliest block that is dominated by all operand blocks. |
| 36 | + IRBlock* earliestBlock = as<IRBlock>(inst->getParent()); |
| 37 | + for (auto block : func->getBlocks()) |
| 38 | + { |
| 39 | + bool dominated = true; |
| 40 | + for (auto opBlock : blocks) |
| 41 | + { |
| 42 | + if (!domTree->dominates(opBlock, block)) |
| 43 | + { |
| 44 | + dominated = false; |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + if (dominated) |
| 49 | + { |
| 50 | + earliestBlock = block; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (!earliestBlock) |
| 56 | + return; |
| 57 | + |
| 58 | + IRInst* latestOperand = nullptr; |
| 59 | + for (auto childInst : earliestBlock->getChildren()) |
| 60 | + { |
| 61 | + if (operandInsts.Contains(childInst)) |
| 62 | + { |
| 63 | + latestOperand = childInst; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (!latestOperand || as<IRParam>(latestOperand)) |
| 68 | + inst->insertBefore(earliestBlock->getFirstOrdinaryInst()); |
| 69 | + else |
| 70 | + inst->insertAfter(latestOperand); |
| 71 | + } |
| 72 | + |
| 73 | + AddressAccessInfo analyzeAddressUse(IRDominatorTree* dom, IRGlobalValueWithCode* func) |
| 74 | + { |
| 75 | + DeduplicateContext deduplicateContext; |
| 76 | + |
| 77 | + AddressAccessInfo info; |
| 78 | + |
| 79 | + // Deduplicate and move known address insts. |
| 80 | + for (auto block : func->getBlocks()) |
| 81 | + { |
| 82 | + for (auto inst = block->getFirstChild(); inst;) |
| 83 | + { |
| 84 | + auto next = inst->getNextInst(); |
| 85 | + switch (inst->getOp()) |
| 86 | + { |
| 87 | + case kIROp_Var: |
| 88 | + { |
| 89 | + RefPtr<AddressInfo> addrInfo = new AddressInfo(); |
| 90 | + addrInfo->addrInst = inst; |
| 91 | + addrInfo->isConstant = true; |
| 92 | + addrInfo->parentAddress = nullptr; |
| 93 | + info.addressInfos[inst] = addrInfo; |
| 94 | + } |
| 95 | + break; |
| 96 | + case kIROp_Param: |
| 97 | + if (as<IRPtrTypeBase>(inst->getFullType())) |
| 98 | + { |
| 99 | + RefPtr<AddressInfo> addrInfo = new AddressInfo(); |
| 100 | + addrInfo->addrInst = inst; |
| 101 | + addrInfo->isConstant = (block == func->getFirstBlock() ? true : false); |
| 102 | + addrInfo->parentAddress = nullptr; |
| 103 | + info.addressInfos[inst] = addrInfo; |
| 104 | + } |
| 105 | + break; |
| 106 | + case kIROp_GetElementPtr: |
| 107 | + case kIROp_FieldAddress: |
| 108 | + { |
| 109 | + moveInstToEarliestPoint(dom, func, inst->getFullType()); |
| 110 | + moveInstToEarliestPoint(dom, func, inst); |
| 111 | + auto deduplicated = deduplicateContext.deduplicate(inst, [func](IRInst* inst) |
| 112 | + { |
| 113 | + if (!inst->getParent()) |
| 114 | + return false; |
| 115 | + if (inst->getParent()->getParent() != func) |
| 116 | + return false; |
| 117 | + switch (inst->getOp()) |
| 118 | + { |
| 119 | + case kIROp_GetElementPtr: |
| 120 | + case kIROp_FieldAddress: |
| 121 | + return true; |
| 122 | + default: |
| 123 | + return false; |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + if (deduplicated != inst) |
| 128 | + { |
| 129 | + SLANG_RELEASE_ASSERT(dom->dominates( |
| 130 | + as<IRBlock>(deduplicated->getParent()), |
| 131 | + as<IRBlock>(inst->getParent()))); |
| 132 | + |
| 133 | + inst->replaceUsesWith(deduplicated); |
| 134 | + inst->removeAndDeallocate(); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + RefPtr<AddressInfo> addrInfo = new AddressInfo(); |
| 139 | + addrInfo->addrInst = inst; |
| 140 | + if (inst->getOp() == kIROp_FieldAddress) |
| 141 | + { |
| 142 | + addrInfo->isConstant = true; |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + addrInfo->isConstant = |
| 147 | + as<IRConstant>(inst->getOperand(1)) ? true : false; |
| 148 | + } |
| 149 | + info.addressInfos[inst] = addrInfo; |
| 150 | + } |
| 151 | + } |
| 152 | + break; |
| 153 | + } |
| 154 | + inst = next; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Construct address info tree. |
| 159 | + for (auto& addr : info.addressInfos) |
| 160 | + { |
| 161 | + RefPtr<AddressInfo> parentInfo; |
| 162 | + if (addr.Value->addrInst->getOperandCount() > 1 && |
| 163 | + info.addressInfos.TryGetValue(addr.Value->addrInst->getOperand(0), parentInfo)) |
| 164 | + { |
| 165 | + addr.Value->parentAddress = parentInfo; |
| 166 | + parentInfo->children.add(addr.Value); |
| 167 | + if (!parentInfo->isConstant) |
| 168 | + addr.Value->isConstant = false; |
| 169 | + } |
| 170 | + } |
| 171 | + return info; |
| 172 | + } |
| 173 | +} |
0 commit comments