Folds an OpcompositeExtract where input is a composite constant.
| 108 | |
| 109 | // Folds an OpcompositeExtract where input is a composite constant. |
| 110 | ConstantFoldingRule FoldExtractWithConstants() { |
| 111 | return [](IRContext* context, Instruction* inst, |
| 112 | const std::vector<const analysis::Constant*>& constants) |
| 113 | -> const analysis::Constant* { |
| 114 | const analysis::Constant* c = constants[kExtractCompositeIdInIdx]; |
| 115 | if (c == nullptr) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
| 119 | for (uint32_t i = 1; i < inst->NumInOperands(); ++i) { |
| 120 | uint32_t element_index = inst->GetSingleWordInOperand(i); |
| 121 | if (c->AsNullConstant()) { |
| 122 | // Return Null for the return type. |
| 123 | analysis::ConstantManager* const_mgr = context->get_constant_mgr(); |
| 124 | analysis::TypeManager* type_mgr = context->get_type_mgr(); |
| 125 | return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {}); |
| 126 | } |
| 127 | |
| 128 | auto cc = c->AsCompositeConstant(); |
| 129 | assert(cc != nullptr); |
| 130 | auto components = cc->GetComponents(); |
| 131 | // Protect against invalid IR. Refuse to fold if the index is out |
| 132 | // of bounds. |
| 133 | if (element_index >= components.size()) return nullptr; |
| 134 | c = components[element_index]; |
| 135 | } |
| 136 | return c; |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | // Folds an OpcompositeInsert where input is a composite constant. |
| 141 | ConstantFoldingRule FoldInsertWithConstants() { |
no test coverage detected