Repository: llvm/llvm-project
File: clang/lib/CodeGen/CGExprScalar.cpp (C++)

Code before:
    // merging subsequent shuffles into this one.
    if (CurIdx == 0)
      std::swap(V, Init);
    V = Builder.CreateShuffleVector(V, Init, Args, "vecinit");
    VIsPoisonShuffle = isa<llvm::PoisonValue>(Init);
    CurIdx += InitElts;
  }

  // FIXME: evaluate codegen vs. shuffling against constant null vector.
  // Emit remaining default initializers.
  llvm::Type *EltTy = VType->getElementType();

  // Emit remaining default initializers
  for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
    Value *Idx = Builder.getInt32(CurIdx);
    llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
    V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
  }

  // Matrix initializer lists are in row-major order but the memory layout for
  // codegen is determined by the -fmatrix-memory-layout flag (default:
  // column-major). When the memory layout is column-major, we need to shuffle
  // the elements from row-major to column-major order.
  if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
    if (CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
        LangOptions::MatrixMemoryLayout::MatrixColMajor) {
      llvm::MatrixBuilder MB(Builder);
      V = MB.CreateRowMajorToColumnMajorShuffle(
          V, MT->getNumRows(), MT->getNumColumns(), "matrix.rowmajor2colmajor");
    }
  }

  return V;
}

static bool isDeclRefKnownNonNull(CodeGenFunction &CGF, const ValueDecl *D) {
  return !D->isWeak();
}

static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E) {
  E = E->IgnoreParens();

  if (const auto *UO = dyn_cast<UnaryOperator>(E))
    if (UO->getOpcode() == UO_Deref)
      return CGF.isPointerKnownNonNull(UO->getSubExpr());

  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
    return isDeclRefKnownNonNull(CGF, DRE->getDecl());

  if (const auto *ME = dyn_cast<MemberExpr>(E)) {
    if (isa<FieldDecl>(ME->getMemberDecl()))

Review comment (suggestion):
nit:
```suggestion
  if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()
      && CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
             LangOptions::MatrixMemoryLayout::MatrixColMajor) {
```

Do we have a preference on this styling? I'd prefer a single if.

Code after:
    // merging subsequent shuffles into this one.
    if (CurIdx == 0)
      std::swap(V, Init);
    V = Builder.CreateShuffleVector(V, Init, Args, "vecinit");
    VIsPoisonShuffle = isa<llvm::PoisonValue>(Init);
    CurIdx += InitElts;
  }

  // FIXME: evaluate codegen vs. shuffling against constant null vector.
  // Emit remaining default initializers.
  llvm::Type *EltTy = VType->getElementType();

  // Emit remaining default initializers
  for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
    Value *Idx = Builder.getInt32(CurIdx);
    llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
    V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
  }

  // Matrix initializer lists are in row-major order but the memory layout for
  // codegen is determined by the -fmatrix-memory-layout flag (default:
  // column-major). When the memory layout is column-major, we need to shuffle
  // the elements from row-major to column-major order.
  if (const auto *MT = E->getType()->getAs<ConstantMatrixType>();
      MT && CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
                LangOptions::MatrixMemoryLayout::MatrixColMajor) {
    SmallVector<int, 16> Mask;
    for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I)
      Mask.push_back(MT->mapColumnMajorToRowMajorFlattenedIndex(I));
    V = Builder.CreateShuffleVector(V, Mask, "matrix.rowmajor2colmajor");
  }

  return V;
}

static bool isDeclRefKnownNonNull(CodeGenFunction &CGF, const ValueDecl *D) {
  return !D->isWeak();
}

static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E) {
  E = E->IgnoreParens();

  if (const auto *UO = dyn_cast<UnaryOperator>(E))
    if (UO->getOpcode() == UO_Deref)
      return CGF.isPointerKnownNonNull(UO->getSubExpr());

  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
    return isDeclRefKnownNonNull(CGF, DRE->getDecl());

  if (const auto *ME = dyn_cast<MemberExpr>(E)) {
    if (isa<FieldDecl>(ME->getMemberDecl()))