Skip to content

Commit

Permalink
[InstCombine] Use replaceOperand() API in GEP transforms
Browse files Browse the repository at this point in the history
To make sure that replaced operands get DCEd. This drops one
iteration from gepphigep.ll, which is still not optimal.

This was the last test case performing more than 3 iterations.

NFC-ish, only worklist order should change.
  • Loading branch information
nikic committed Mar 28, 2020
1 parent b1f78ba commit 30d7121
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -1996,7 +1996,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {

GEP.getParent()->getInstList().insert(
GEP.getParent()->getFirstInsertionPt(), NewGEP);
GEP.setOperand(0, NewGEP);
replaceOperand(GEP, 0, NewGEP);
PtrOp = NewGEP;
}

Expand Down Expand Up @@ -2096,8 +2096,8 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// Update the GEP in place if possible.
if (Src->getNumOperands() == 2) {
GEP.setIsInBounds(isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP)));
GEP.setOperand(0, Src->getOperand(0));
GEP.setOperand(1, Sum);
replaceOperand(GEP, 0, Src->getOperand(0));
replaceOperand(GEP, 1, Sum);
return &GEP;
}
Indices.append(Src->op_begin()+1, Src->op_end()-1);
Expand Down Expand Up @@ -2215,9 +2215,8 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// array. Because the array type is never stepped over (there
// is a leading zero) we can fold the cast into this GEP.
if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace()) {
GEP.setOperand(0, StrippedPtr);
GEP.setSourceElementType(XATy);
return &GEP;
return replaceOperand(GEP, 0, StrippedPtr);
}
// Cannot replace the base pointer directly because StrippedPtr's
// address space is different. Instead, create a new GEP followed by
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/gepphigep.ll
@@ -1,4 +1,4 @@
; RUN: opt -instcombine -S < %s | FileCheck %s
; RUN: opt -instcombine -instcombine-infinite-loop-threshold=3 -S < %s | FileCheck %s

%struct1 = type { %struct2*, i32, i32, i32 }
%struct2 = type { i32, i32 }
Expand Down

0 comments on commit 30d7121

Please sign in to comment.