Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2433,24 +2433,23 @@ // analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. unsigned OffsetBits = DL.getIndexTypeSizeInBits(GEPType); APInt Offset(OffsetBits, 0); - if (!isa(SrcOp) && GEP.accumulateConstantOffset(DL, Offset)) { + + // Returns true if the Type is void* + auto isVoidPointerType = [](Type *Ty) -> bool { + return (Ty->isPointerTy() && Ty->getPointerElementType()->isIntegerTy(8)); + }; + + // If the bitcast argument is an allocation (or void* in general), The bitcast + // is for convertion to actual type of allocation. Removing such bitcasts, + // results in having GEPs with i8* base and pure byte offsets. That means GEP + // is not aware of Struct or array hierarchy. + // By avoiding such GEPs, phi translation and MemoryDependencyAnalysis have + // a better chance to succeed + if (!isa(SrcOp) && GEP.accumulateConstantOffset(DL, Offset) && + !isVoidPointerType(SrcType)) { // If this GEP instruction doesn't move the pointer, just replace the GEP // with a bitcast of the real input to the dest type. if (!Offset) { - // If the bitcast is of an allocation, and the allocation will be - // converted to match the type of the cast, don't touch this. - if (isa(SrcOp) || isAllocationFn(SrcOp, &TLI)) { - // See if the bitcast simplifies, if so, don't nuke this GEP yet. - if (Instruction *I = visitBitCast(*BCI)) { - if (I != BCI) { - I->takeName(BCI); - BCI->getParent()->getInstList().insert(BCI->getIterator(), I); - replaceInstUsesWith(*BCI, I); - } - return &GEP; - } - } - if (SrcType->getPointerAddressSpace() != GEP.getAddressSpace()) return new AddrSpaceCastInst(SrcOp, GEPType); return new BitCastInst(SrcOp, GEPType);