Skip to content

Commit f7bc8e0

Browse files
committed
[InstCombine] Remove redundant evaluateGEPOffsetExpression() fold (NFCI)
If we go through the generic EmitGEPOffset code, the resulting expression can be (and is) reduced in the same way this code did manually. There are no changes in lit tests or llvm-test-suite. This fold predates the time where we started adding nsw to the adds created by EmitGEPOffset, so it was likely needed back then. This might not actually be NFC due to worklist order changes etc.
1 parent 51f2f59 commit f7bc8e0

File tree

1 file changed

+1
-110
lines changed

1 file changed

+1
-110
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -404,108 +404,6 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
404404
return nullptr;
405405
}
406406

407-
/// Return a value that can be used to compare the *offset* implied by a GEP to
408-
/// zero. For example, if we have &A[i], we want to return 'i' for
409-
/// "icmp ne i, 0". Note that, in general, indices can be complex, and scales
410-
/// are involved. The above expression would also be legal to codegen as
411-
/// "icmp ne (i*4), 0" (assuming A is a pointer to i32).
412-
/// This latter form is less amenable to optimization though, and we are allowed
413-
/// to generate the first by knowing that pointer arithmetic doesn't overflow.
414-
///
415-
/// If we can't emit an optimized form for this expression, this returns null.
416-
///
417-
static Value *evaluateGEPOffsetExpression(User *GEP, InstCombinerImpl &IC,
418-
const DataLayout &DL) {
419-
gep_type_iterator GTI = gep_type_begin(GEP);
420-
421-
// Check to see if this gep only has a single variable index. If so, and if
422-
// any constant indices are a multiple of its scale, then we can compute this
423-
// in terms of the scale of the variable index. For example, if the GEP
424-
// implies an offset of "12 + i*4", then we can codegen this as "3 + i",
425-
// because the expression will cross zero at the same point.
426-
unsigned i, e = GEP->getNumOperands();
427-
int64_t Offset = 0;
428-
for (i = 1; i != e; ++i, ++GTI) {
429-
if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
430-
// Compute the aggregate offset of constant indices.
431-
if (CI->isZero()) continue;
432-
433-
// Handle a struct index, which adds its field offset to the pointer.
434-
if (StructType *STy = GTI.getStructTypeOrNull()) {
435-
Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
436-
} else {
437-
uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
438-
Offset += Size*CI->getSExtValue();
439-
}
440-
} else {
441-
// Found our variable index.
442-
break;
443-
}
444-
}
445-
446-
// If there are no variable indices, we must have a constant offset, just
447-
// evaluate it the general way.
448-
if (i == e) return nullptr;
449-
450-
Value *VariableIdx = GEP->getOperand(i);
451-
// Determine the scale factor of the variable element. For example, this is
452-
// 4 if the variable index is into an array of i32.
453-
uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType());
454-
455-
// Verify that there are no other variable indices. If so, emit the hard way.
456-
for (++i, ++GTI; i != e; ++i, ++GTI) {
457-
ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
458-
if (!CI) return nullptr;
459-
460-
// Compute the aggregate offset of constant indices.
461-
if (CI->isZero()) continue;
462-
463-
// Handle a struct index, which adds its field offset to the pointer.
464-
if (StructType *STy = GTI.getStructTypeOrNull()) {
465-
Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
466-
} else {
467-
uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
468-
Offset += Size*CI->getSExtValue();
469-
}
470-
}
471-
472-
// Okay, we know we have a single variable index, which must be a
473-
// pointer/array/vector index. If there is no offset, life is simple, return
474-
// the index.
475-
Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType());
476-
unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth();
477-
if (Offset == 0) {
478-
// Cast to intptrty in case a truncation occurs. If an extension is needed,
479-
// we don't need to bother extending: the extension won't affect where the
480-
// computation crosses zero.
481-
if (VariableIdx->getType()->getPrimitiveSizeInBits().getFixedSize() >
482-
IntPtrWidth) {
483-
VariableIdx = IC.Builder.CreateTrunc(VariableIdx, IntPtrTy);
484-
}
485-
return VariableIdx;
486-
}
487-
488-
// Otherwise, there is an index. The computation we will do will be modulo
489-
// the pointer size.
490-
Offset = SignExtend64(Offset, IntPtrWidth);
491-
VariableScale = SignExtend64(VariableScale, IntPtrWidth);
492-
493-
// To do this transformation, any constant index must be a multiple of the
494-
// variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
495-
// but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
496-
// multiple of the variable scale.
497-
int64_t NewOffs = Offset / (int64_t)VariableScale;
498-
if (Offset != NewOffs*(int64_t)VariableScale)
499-
return nullptr;
500-
501-
// Okay, we can do this evaluation. Start by converting the index to intptr.
502-
if (VariableIdx->getType() != IntPtrTy)
503-
VariableIdx = IC.Builder.CreateIntCast(VariableIdx, IntPtrTy,
504-
true /*Signed*/);
505-
Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
506-
return IC.Builder.CreateAdd(VariableIdx, OffsetVal, "offset");
507-
}
508-
509407
/// Returns true if we can rewrite Start as a GEP with pointer Base
510408
/// and some integer offset. The nodes that need to be re-written
511409
/// for this transformation will be added to Explored.
@@ -846,14 +744,7 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
846744
if (PtrBase == RHS && GEPLHS->isInBounds() &&
847745
!GEPLHS->getType()->isVectorTy()) {
848746
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
849-
// This transformation (ignoring the base and scales) is valid because we
850-
// know pointers can't overflow since the gep is inbounds. See if we can
851-
// output an optimized form.
852-
Value *Offset = evaluateGEPOffsetExpression(GEPLHS, *this, DL);
853-
854-
// If not, synthesize the offset the hard way.
855-
if (!Offset)
856-
Offset = EmitGEPOffset(GEPLHS);
747+
Value *Offset = EmitGEPOffset(GEPLHS);
857748
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
858749
Constant::getNullValue(Offset->getType()));
859750
}

0 commit comments

Comments
 (0)