[Constants] Use getGEPReturnType() (NFC)
authorNikita Popov <npopov@redhat.com>
Tue, 18 Jul 2023 10:37:35 +0000 (12:37 +0200)
committerNikita Popov <npopov@redhat.com>
Tue, 18 Jul 2023 12:11:38 +0000 (14:11 +0200)
This reimplements essentially the same logic.

llvm/lib/IR/Constants.cpp

index 9c8d044..c69c7c0 100644 (file)
@@ -2380,7 +2380,6 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
                                          ArrayRef<Value *> Idxs, bool InBounds,
                                          std::optional<unsigned> InRangeIndex,
                                          Type *OnlyIfReducedTy) {
-  PointerType *OrigPtrTy = cast<PointerType>(C->getType()->getScalarType());
   assert(Ty && "Must specify element type");
   assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
 
@@ -2388,27 +2387,17 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
           ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
     return FC;          // Fold a few common cases.
 
+  assert(GetElementPtrInst::getIndexedType(Ty, Idxs) &&
+         "GEP indices invalid!");;
+
   // Get the result type of the getelementptr!
-  Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
-  assert(DestTy && "GEP indices invalid!");
-  unsigned AS = OrigPtrTy->getAddressSpace();
-  Type *ReqTy = OrigPtrTy->isOpaque()
-      ? PointerType::get(OrigPtrTy->getContext(), AS)
-      : DestTy->getPointerTo(AS);
+  Type *ReqTy = GetElementPtrInst::getGEPReturnType(C, Idxs);
+  if (OnlyIfReducedTy == ReqTy)
+    return nullptr;
 
   auto EltCount = ElementCount::getFixed(0);
-  if (VectorType *VecTy = dyn_cast<VectorType>(C->getType()))
+  if (VectorType *VecTy = dyn_cast<VectorType>(ReqTy))
     EltCount = VecTy->getElementCount();
-  else
-    for (auto *Idx : Idxs)
-      if (VectorType *VecTy = dyn_cast<VectorType>(Idx->getType()))
-        EltCount = VecTy->getElementCount();
-
-  if (EltCount.isNonZero())
-    ReqTy = VectorType::get(ReqTy, EltCount);
-
-  if (OnlyIfReducedTy == ReqTy)
-    return nullptr;
 
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> ArgVec;