@@ -64,21 +64,27 @@ class CGBuilderTy : public CGBuilderBaseTy {
6464 Address createConstGEP2_32 (Address Addr, unsigned Idx0, unsigned Idx1,
6565 const llvm::Twine &Name) {
6666 const llvm::DataLayout &DL = BB->getDataLayout ();
67- llvm::GetElementPtrInst *GEP ;
67+ llvm::Value *V ;
6868 if (IsInBounds)
69- GEP = cast<llvm::GetElementPtrInst>( CreateConstInBoundsGEP2_32 (
70- Addr. getElementType (), emitRawPointerFromAddress (Addr), Idx0, Idx1 ,
71- Name) );
69+ V = CreateConstInBoundsGEP2_32 (Addr. getElementType (),
70+ emitRawPointerFromAddress (Addr), Idx0,
71+ Idx1, Name);
7272 else
73- GEP = cast<llvm::GetElementPtrInst>(CreateConstGEP2_32 (
74- Addr.getElementType (), emitRawPointerFromAddress (Addr), Idx0, Idx1,
75- Name));
73+ V = CreateConstGEP2_32 (Addr.getElementType (),
74+ emitRawPointerFromAddress (Addr), Idx0, Idx1, Name);
7675 llvm::APInt Offset (
7776 DL.getIndexSizeInBits (Addr.getType ()->getPointerAddressSpace ()), 0 ,
7877 /* isSigned=*/ true );
79- if (!GEP->accumulateConstantOffset (DL, Offset))
80- llvm_unreachable (" offset of GEP with constants is always computable" );
81- return Address (GEP, GEP->getResultElementType (),
78+ llvm::Type *ElementTy = nullptr ;
79+ if (auto *GEP = dyn_cast<llvm::GEPOperator>(V)) {
80+ if (!GEP->accumulateConstantOffset (DL, Offset))
81+ llvm_unreachable (" offset of GEP with constants is always computable" );
82+ ElementTy = GEP->getResultElementType ();
83+ } else {
84+ ElementTy = llvm::GetElementPtrInst::getIndexedType (Addr.getElementType (),
85+ {Idx0, Idx1});
86+ }
87+ return Address (V, ElementTy,
8288 Addr.getAlignment ().alignmentAtOffset (
8389 CharUnits::fromQuantity (Offset.getSExtValue ())),
8490 IsInBounds ? Addr.isKnownNonNull () : NotKnownNonNull);
@@ -219,21 +225,17 @@ class CGBuilderTy : public CGBuilderBaseTy {
219225 // / true.
220226 using CGBuilderBaseTy::CreateStructGEP;
221227 Address CreateStructGEP (Address Addr, unsigned Index,
222- const llvm::Twine &Name = " " ,
223- bool IsBaseConstantNull = false ) {
228+ const llvm::Twine &Name = " " ) {
224229 llvm::StructType *ElTy = cast<llvm::StructType>(Addr.getElementType ());
225230 const llvm::DataLayout &DL = BB->getDataLayout ();
226231 const llvm::StructLayout *Layout = DL.getStructLayout (ElTy);
227232 auto Offset = CharUnits::fromQuantity (Layout->getElementOffset (Index));
228233
229- llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::noUnsignedWrap ();
230- if (!IsBaseConstantNull)
231- NWFlags |= llvm::GEPNoWrapFlags::inBounds ();
232- return Address (
233- CreateConstGEP2_32 (Addr.getElementType (), Addr.getBasePointer (), 0 ,
234- Index, Name, NWFlags),
235- ElTy->getElementType (Index),
236- Addr.getAlignment ().alignmentAtOffset (Offset), Addr.isKnownNonNull ());
234+ return Address (CreateStructGEP (Addr.getElementType (), Addr.getBasePointer (),
235+ Index, Name),
236+ ElTy->getElementType (Index),
237+ Addr.getAlignment ().alignmentAtOffset (Offset),
238+ Addr.isKnownNonNull ());
237239 }
238240
239241 // / Given
0 commit comments