Skip to content

Commit

Permalink
[InstCombine] Remove code duplication between InstCombiner.h and Inst…
Browse files Browse the repository at this point in the history
…CombineInternal.h.

The class in InstCombineInternal.h inherits from InstCombiner.h.
I think this split was created when target specific InstCombines
were moved to go through TTI.

I had to update some of the code in InstCombiner.h to match changes
that had been made to InstCombineInternal.h.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D140230
  • Loading branch information
topperc committed Dec 16, 2022
1 parent 8a86860 commit ad476fb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 126 deletions.
9 changes: 6 additions & 3 deletions llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
Expand Up @@ -398,7 +398,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
"New instruction already inserted into a basic block!");
BasicBlock *BB = Old.getParent();
New->insertInto(BB, Old.getIterator()); // Insert inst
Worklist.push(New);
Worklist.add(New);
return New;
}

Expand All @@ -417,8 +417,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
// If there are no uses to replace, then we return nullptr to indicate that
// no changes were made to the program.
if (I.use_empty())
return nullptr;
if (I.use_empty()) return nullptr;

Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.

Expand All @@ -430,6 +429,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
<< " with " << *V << '\n');

// If V is a new unnamed instruction, take the name from the old one.
if (V->use_empty() && isa<Instruction>(V) && !V->hasName() && I.hasName())
V->takeName(&I);

I.replaceAllUsesWith(V);
return &I;
}
Expand Down
123 changes: 0 additions & 123 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Expand Up @@ -383,68 +383,6 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
bool IsAnd);

public:
/// Inserts an instruction \p New before instruction \p Old
///
/// Also adds the new instruction to the worklist and returns \p New so that
/// it is suitable for use as the return from the visitation patterns.
Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
assert(New && !New->getParent() &&
"New instruction already inserted into a basic block!");
BasicBlock *BB = Old.getParent();
New->insertInto(BB, Old.getIterator()); // Insert inst
Worklist.add(New);
return New;
}

/// Same as InsertNewInstBefore, but also sets the debug loc.
Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
New->setDebugLoc(Old.getDebugLoc());
return InsertNewInstBefore(New, Old);
}

/// A combiner-aware RAUW-like routine.
///
/// This method is to be used when an instruction is found to be dead,
/// replaceable with another preexisting expression. Here we add all uses of
/// I to the worklist, replace all uses of I with the new value, then return
/// I, so that the inst combiner will know that I was modified.
Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
// If there are no uses to replace, then we return nullptr to indicate that
// no changes were made to the program.
if (I.use_empty()) return nullptr;

Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.

// If we are replacing the instruction with itself, this must be in a
// segment of unreachable code, so just clobber the instruction.
if (&I == V)
V = PoisonValue::get(I.getType());

LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
<< " with " << *V << '\n');

// If V is a new unnamed instruction, take the name from the old one.
if (V->use_empty() && isa<Instruction>(V) && !V->hasName() && I.hasName())
V->takeName(&I);

I.replaceAllUsesWith(V);
MadeIRChange = true;
return &I;
}

/// Replace operand of instruction and add old operand to the worklist.
Instruction *replaceOperand(Instruction &I, unsigned OpNum, Value *V) {
Worklist.addValue(I.getOperand(OpNum));
I.setOperand(OpNum, V);
return &I;
}

/// Replace use and add the previously used value to the worklist.
void replaceUse(Use &U, Value *NewValue) {
Worklist.addValue(U);
U = NewValue;
}

/// Create and insert the idiom we use to indicate a block is unreachable
/// without having to rewrite the CFG from within InstCombine.
void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
Expand Down Expand Up @@ -477,67 +415,6 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
return nullptr; // Don't do anything with FI
}

void computeKnownBits(const Value *V, KnownBits &Known,
unsigned Depth, const Instruction *CxtI) const {
llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
}

KnownBits computeKnownBits(const Value *V, unsigned Depth,
const Instruction *CxtI) const {
return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);
}

bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
unsigned Depth = 0,
const Instruction *CxtI = nullptr) {
return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
}

bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
const Instruction *CxtI = nullptr) const {
return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
}

unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
const Instruction *CxtI = nullptr) const {
return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForSignedMul(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForSignedAdd(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
const Instruction *CxtI) const {
return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
}

OverflowResult computeOverflow(
Instruction::BinaryOps BinaryOp, bool IsSigned,
Value *LHS, Value *RHS, Instruction *CxtI) const;
Expand Down

0 comments on commit ad476fb

Please sign in to comment.