From 30d712103faa8c78e8b1dbc9cc6c9b831bb20e4c Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 28 Mar 2020 19:02:48 +0100 Subject: [PATCH] [InstCombine] Use replaceOperand() API in GEP transforms 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. --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 9 ++++----- llvm/test/Transforms/InstCombine/gepphigep.ll | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 49e39c3bc87a4..2e71f5a9ef481 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -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; } @@ -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(&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); @@ -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 diff --git a/llvm/test/Transforms/InstCombine/gepphigep.ll b/llvm/test/Transforms/InstCombine/gepphigep.ll index cc90d714be734..7d3fe949ede26 100644 --- a/llvm/test/Transforms/InstCombine/gepphigep.ll +++ b/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 }