[Clang][CodeGen] Do not use the GEP result to infer offset and result type by dtcxzyw · Pull Request #134221 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Yingwei Zheng (dtcxzyw)

Changes

If CreateConstInBoundsGEP2_32 returns a constant null/gep, the cast to GetElementPtrInst will fail.
This patch uses two static helpers GEPOperator::accumulateConstantOffset/GetElementPtrInst::getIndexedType to infer offset and result type instead of depending on the GEP result.

This patch is extracted from #130734. It may be useful to fix #132449.


Full diff: https://github.com/llvm/llvm-project/pull/134221.diff

1 Files Affected:

diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h index b8036cf6e6a30..090f75d3b5d3c 100644 --- a/clang/lib/CodeGen/CGBuilder.h +++ b/clang/lib/CodeGen/CGBuilder.h @@ -64,21 +64,25 @@ class CGBuilderTy : public CGBuilderBaseTy { Address createConstGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::Twine &Name) { const llvm::DataLayout &DL = BB->getDataLayout();