@@ -35,6 +35,32 @@ bool IsDebugDeclareOrValue(Instruction* di) {
35
35
dbg_opcode == CommonDebugInfoDebugValue;
36
36
}
37
37
38
+ // Returns the number of members in |type|. If |type| is not a composite type
39
+ // or the number of components is not known at compile time, the return value
40
+ // will be 0.
41
+ uint32_t GetNumberOfMembers (const analysis::Type* type, IRContext* context) {
42
+ if (const analysis::Struct* struct_type = type->AsStruct ()) {
43
+ return static_cast <uint32_t >(struct_type->element_types ().size ());
44
+ } else if (const analysis::Array* array_type = type->AsArray ()) {
45
+ const analysis::Constant* length_const =
46
+ context->get_constant_mgr ()->FindDeclaredConstant (
47
+ array_type->LengthId ());
48
+
49
+ if (length_const == nullptr ) {
50
+ // This can happen if the length is an OpSpecConstant.
51
+ return 0 ;
52
+ }
53
+ assert (length_const->type ()->AsInteger ());
54
+ return length_const->GetU32 ();
55
+ } else if (const analysis::Vector* vector_type = type->AsVector ()) {
56
+ return vector_type->element_count ();
57
+ } else if (const analysis::Matrix* matrix_type = type->AsMatrix ()) {
58
+ return matrix_type->element_count ();
59
+ } else {
60
+ return 0 ;
61
+ }
62
+ }
63
+
38
64
} // namespace
39
65
40
66
Pass::Status CopyPropagateArrays::Process () {
@@ -357,22 +383,9 @@ CopyPropagateArrays::BuildMemoryObjectFromInsert(Instruction* insert_inst) {
357
383
358
384
analysis::DefUseManager* def_use_mgr = context ()->get_def_use_mgr ();
359
385
analysis::TypeManager* type_mgr = context ()->get_type_mgr ();
360
- analysis::ConstantManager* const_mgr = context ()->get_constant_mgr ();
361
386
const analysis::Type* result_type = type_mgr->GetType (insert_inst->type_id ());
362
387
363
- uint32_t number_of_elements = 0 ;
364
- if (const analysis::Struct* struct_type = result_type->AsStruct ()) {
365
- number_of_elements =
366
- static_cast <uint32_t >(struct_type->element_types ().size ());
367
- } else if (const analysis::Array* array_type = result_type->AsArray ()) {
368
- const analysis::Constant* length_const =
369
- const_mgr->FindDeclaredConstant (array_type->LengthId ());
370
- number_of_elements = length_const->GetU32 ();
371
- } else if (const analysis::Vector* vector_type = result_type->AsVector ()) {
372
- number_of_elements = vector_type->element_count ();
373
- } else if (const analysis::Matrix* matrix_type = result_type->AsMatrix ()) {
374
- number_of_elements = matrix_type->element_count ();
375
- }
388
+ uint32_t number_of_elements = GetNumberOfMembers (result_type, context ());
376
389
377
390
if (number_of_elements == 0 ) {
378
391
return nullptr ;
@@ -800,23 +813,8 @@ uint32_t CopyPropagateArrays::MemoryObject::GetNumberOfMembers() {
800
813
std::vector<uint32_t > access_indices = GetAccessIds ();
801
814
type = type_mgr->GetMemberType (type, access_indices);
802
815
803
- if (const analysis::Struct* struct_type = type->AsStruct ()) {
804
- return static_cast <uint32_t >(struct_type->element_types ().size ());
805
- } else if (const analysis::Array* array_type = type->AsArray ()) {
806
- const analysis::Constant* length_const =
807
- context->get_constant_mgr ()->FindDeclaredConstant (
808
- array_type->LengthId ());
809
- assert (length_const->type ()->AsInteger ());
810
- return length_const->GetU32 ();
811
- } else if (const analysis::Vector* vector_type = type->AsVector ()) {
812
- return vector_type->element_count ();
813
- } else if (const analysis::Matrix* matrix_type = type->AsMatrix ()) {
814
- return matrix_type->element_count ();
815
- } else {
816
- return 0 ;
817
- }
816
+ return opt::GetNumberOfMembers (type, context);
818
817
}
819
-
820
818
template <class iterator >
821
819
CopyPropagateArrays::MemoryObject::MemoryObject (Instruction* var_inst,
822
820
iterator begin, iterator end)
0 commit comments