@@ -298,30 +298,35 @@ void collectCompositeElementsDefaultValuesRecursive(
298
298
size_t NumBytes = M.getDataLayout ().getTypeStoreSize (C->getType ());
299
299
std::fill_n (std::back_inserter (DefaultValues), NumBytes, 0 );
300
300
Offset += NumBytes;
301
- } else if (auto *DataSeqC = dyn_cast<ConstantDataSequential>(C)) {
301
+ return ;
302
+ }
303
+
304
+ if (auto *DataSeqC = dyn_cast<ConstantDataSequential>(C)) {
302
305
// This code is generic for both vectors and arrays of scalars
303
306
for (size_t I = 0 ; I < DataSeqC->getNumElements (); ++I) {
304
307
Constant *El = cast<Constant>(DataSeqC->getElementAsConstant (I));
305
308
collectCompositeElementsDefaultValuesRecursive (M, El, Offset,
306
309
DefaultValues);
307
310
}
308
- } else if (auto *ArrayC = dyn_cast<ConstantArray>(C)) {
311
+ return ;
312
+ }
313
+
314
+ if (auto *ArrayC = dyn_cast<ConstantArray>(C)) {
309
315
// This branch handles arrays of composite types (structs, arrays, etc.)
310
- auto *ArrTy = ArrayC->getType ();
311
- for (size_t I = 0 ; I < ArrTy->getNumElements (); ++I) {
312
- // We are shure the array is not a zeroinitializer.
313
- Constant *El = cast<Constant>(C->getOperand (I));
314
- collectCompositeElementsDefaultValuesRecursive (M, El, Offset,
315
- DefaultValues);
316
+ assert (!C->isZeroValue () && " C must not be a zeroinitializer" );
317
+ for (size_t I = 0 ; I < ArrayC->getType ()->getNumElements (); ++I) {
318
+ collectCompositeElementsDefaultValuesRecursive (M, ArrayC->getOperand (I),
319
+ Offset, DefaultValues);
316
320
}
317
- } else if (auto *StructC = dyn_cast<ConstantStruct>(C)) {
321
+ return ;
322
+ }
323
+
324
+ if (auto *StructC = dyn_cast<ConstantStruct>(C)) {
325
+ assert (!C->isZeroValue () && " C must not be a zeroinitializer" );
318
326
auto *StructTy = StructC->getType ();
319
327
const StructLayout *SL = M.getDataLayout ().getStructLayout (StructTy);
320
328
const size_t BaseDefaultValueOffset = DefaultValues.size ();
321
329
for (size_t I = 0 , E = StructTy->getNumElements (); I < E; ++I) {
322
- // We are shure the structure is not a zeroinitializer.
323
- Constant *El = cast<Constant>(C->getOperand (I));
324
-
325
330
// When handling elements of a structure, we do not use manually
326
331
// calculated offsets (which are sum of sizes of all previously
327
332
// encountered elements), but instead rely on data provided for us by
@@ -333,7 +338,8 @@ void collectCompositeElementsDefaultValuesRecursive(
333
338
while (LocalOffset != DefaultValues.size ())
334
339
DefaultValues.push_back (0 );
335
340
336
- collectCompositeElementsDefaultValuesRecursive (M, El, LocalOffset,
341
+ collectCompositeElementsDefaultValuesRecursive (M, StructC->getOperand (I),
342
+ LocalOffset,
337
343
DefaultValues);
338
344
}
339
345
const size_t SLSize = SL->getSizeInBytes ();
@@ -346,37 +352,39 @@ void collectCompositeElementsDefaultValuesRecursive(
346
352
// Update "global" offset according to the total size of a handled struct
347
353
// type.
348
354
Offset += SLSize;
349
- } else { // Assume that we encountered some scalar element
350
- size_t NumBytes = M.getDataLayout ().getTypeStoreSize (C->getType ());
351
- if (auto IntConst = dyn_cast<ConstantInt>(C)) {
352
- auto Val = IntConst->getValue ().getZExtValue ();
353
- std::copy_n (reinterpret_cast <char *>(&Val), NumBytes,
355
+ return ;
356
+ }
357
+
358
+ // Assume that we encountered some scalar element
359
+ size_t NumBytes = M.getDataLayout ().getTypeStoreSize (C->getType ());
360
+ if (auto IntConst = dyn_cast<ConstantInt>(C)) {
361
+ auto Val = IntConst->getValue ().getZExtValue ();
362
+ std::copy_n (reinterpret_cast <char *>(&Val), NumBytes,
363
+ std::back_inserter (DefaultValues));
364
+ } else if (auto FPConst = dyn_cast<ConstantFP>(C)) {
365
+ auto Val = FPConst->getValue ();
366
+
367
+ if (NumBytes == 2 ) {
368
+ auto IVal = Val.bitcastToAPInt ();
369
+ assert (IVal.getBitWidth () == 16 );
370
+ auto Storage = static_cast <uint16_t >(IVal.getZExtValue ());
371
+ std::copy_n (reinterpret_cast <char *>(&Storage), NumBytes,
372
+ std::back_inserter (DefaultValues));
373
+ } else if (NumBytes == 4 ) {
374
+ float v = Val.convertToFloat ();
375
+ std::copy_n (reinterpret_cast <char *>(&v), NumBytes,
376
+ std::back_inserter (DefaultValues));
377
+ } else if (NumBytes == 8 ) {
378
+ double v = Val.convertToDouble ();
379
+ std::copy_n (reinterpret_cast <char *>(&v), NumBytes,
354
380
std::back_inserter (DefaultValues));
355
- } else if (auto FPConst = dyn_cast<ConstantFP>(C)) {
356
- auto Val = FPConst->getValue ();
357
-
358
- if (NumBytes == 2 ) {
359
- auto IVal = Val.bitcastToAPInt ();
360
- assert (IVal.getBitWidth () == 16 );
361
- auto Storage = static_cast <uint16_t >(IVal.getZExtValue ());
362
- std::copy_n (reinterpret_cast <char *>(&Storage), NumBytes,
363
- std::back_inserter (DefaultValues));
364
- } else if (NumBytes == 4 ) {
365
- float v = Val.convertToFloat ();
366
- std::copy_n (reinterpret_cast <char *>(&v), NumBytes,
367
- std::back_inserter (DefaultValues));
368
- } else if (NumBytes == 8 ) {
369
- double v = Val.convertToDouble ();
370
- std::copy_n (reinterpret_cast <char *>(&v), NumBytes,
371
- std::back_inserter (DefaultValues));
372
- } else {
373
- llvm_unreachable (" Unexpected constant floating point type" );
374
- }
375
381
} else {
376
- llvm_unreachable (" Unexpected constant scalar type" );
382
+ llvm_unreachable (" Unexpected constant floating point type" );
377
383
}
378
- Offset += NumBytes;
384
+ } else {
385
+ llvm_unreachable (" Unexpected constant scalar type" );
379
386
}
387
+ Offset += NumBytes;
380
388
}
381
389
382
390
MDNode *generateSpecConstantMetadata (const Module &M, StringRef SymbolicID,
0 commit comments