[llvm] eadbc4b - [Constants] Use getGEPReturnType() (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 18 05:11:47 PDT 2023


Author: Nikita Popov
Date: 2023-07-18T14:11:38+02:00
New Revision: eadbc4b004b6f5bcd67f17b1d79c9955bd410fb8

URL: https://github.com/llvm/llvm-project/commit/eadbc4b004b6f5bcd67f17b1d79c9955bd410fb8
DIFF: https://github.com/llvm/llvm-project/commit/eadbc4b004b6f5bcd67f17b1d79c9955bd410fb8.diff

LOG: [Constants] Use getGEPReturnType() (NFC)

This reimplements essentially the same logic.

Added: 
    

Modified: 
    llvm/lib/IR/Constants.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 9c8d044e502902..c69c7c095f7856 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -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;


        


More information about the llvm-commits mailing list