diff --git a/lib/Target/X86/InstSelectSimple.cpp b/lib/Target/X86/InstSelectSimple.cpp index 5cabd485bd0..68a602b3143 100644 --- a/lib/Target/X86/InstSelectSimple.cpp +++ b/lib/Target/X86/InstSelectSimple.cpp @@ -3663,6 +3663,21 @@ void ISel::emitGEPOperation(MachineBasicBlock *MBB, if (ConstantPointerRef *CPR = dyn_cast(Src)) Src = CPR->getValue(); + // If this is a getelementptr null, with all constant integer indices, just + // replace it with TargetReg = 42. + if (isa(Src)) { + User::op_iterator I = IdxBegin; + for (; I != IdxEnd; ++I) + if (!isa(*I)) + break; + if (I == IdxEnd) { // All constant indices + unsigned Offset = TD.getIndexedOffset(Src->getType(), + std::vector(IdxBegin, IdxEnd)); + BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset); + return; + } + } + std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); GEPOps[0] = Src; diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp index 5cabd485bd0..68a602b3143 100644 --- a/lib/Target/X86/X86ISelSimple.cpp +++ b/lib/Target/X86/X86ISelSimple.cpp @@ -3663,6 +3663,21 @@ void ISel::emitGEPOperation(MachineBasicBlock *MBB, if (ConstantPointerRef *CPR = dyn_cast(Src)) Src = CPR->getValue(); + // If this is a getelementptr null, with all constant integer indices, just + // replace it with TargetReg = 42. + if (isa(Src)) { + User::op_iterator I = IdxBegin; + for (; I != IdxEnd; ++I) + if (!isa(*I)) + break; + if (I == IdxEnd) { // All constant indices + unsigned Offset = TD.getIndexedOffset(Src->getType(), + std::vector(IdxBegin, IdxEnd)); + BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset); + return; + } + } + std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); GEPOps[0] = Src;