Skip to content

Commit

Permalink
[NFC][RemoveDIs] Have CreateNeg only accept iterators (#82999)
Browse files Browse the repository at this point in the history
Removing debug-intrinsics requires that we always insert with an
iterator, not with an instruction position. To enforce that, we need to
eliminate the `Instruction *` taking functions. It's safe to leave the
insert-at-end-of-block functions as the intention is clear for debug
info purposes (i.e., insert after both instructions and debug-info at
the end of the function).

This patch demonstrates how that needs to happen. At a variety of
call-sites to the `CreateNeg` constructor we need to consider:
* Has this instruction been selected because of the operation it
performs? In that case, just call `getIterator` and pass an iterator in.
* Has this instruction been selected because of it's position? If so, we
need to keep the iterator identifying that position (see the 3rd hunk
changing Reassociate.cpp, although it's coincidentally not debug-info
significant).

This also demonstrates what we'll try and do with the constructor
methods going forwards: have one fully explicit set of parameters
including iterator, and another with default-arguments where the
block-to-insert-into argument defaults to nullptr / no-position,
creating an instruction that hasn't been inserted yet.
  • Loading branch information
jmorse committed Feb 29, 2024
1 parent 6c2eec5 commit 7e88d51
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
4 changes: 1 addition & 3 deletions llvm/include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,7 @@ class BinaryOperator : public Instruction {
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
BasicBlock::iterator InsertBefore);
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
Instruction *InsertBefore = nullptr);
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd);
BasicBlock *InsertAtEnd = nullptr);
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
BasicBlock::iterator InsertBefore);
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,

Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
BasicBlock *InsertAtEnd)
: User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
: User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {

// append this instruction into the basic block
assert(InsertAtEnd && "Basic block to append to may not be NULL!");
insertInto(InsertAtEnd, InsertAtEnd->end());
// If requested, append this instruction into the basic block.
if (InsertAtEnd)
insertInto(InsertAtEnd, InsertAtEnd->end());
}

Instruction::~Instruction() {
Expand All @@ -73,7 +73,6 @@ Instruction::~Instruction() {
setMetadata(LLVMContext::MD_DIAssignID, nullptr);
}


void Instruction::setParent(BasicBlock *P) {
Parent = P;
}
Expand Down
8 changes: 0 additions & 8 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3245,14 +3245,6 @@ BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
InsertBefore);
}

BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Instruction *InsertBefore) {
Value *Zero = ConstantInt::get(Op->getType(), 0);
return new BinaryOperator(Instruction::Sub,
Zero, Op,
Op->getType(), Name, InsertBefore);
}

BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd) {
Value *Zero = ConstantInt::get(Op->getType(), 0);
Expand Down
14 changes: 8 additions & 6 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
for (Operand &Op : Ops) {
if (Op.D == Domain::NonNegative)
continue;
auto *BO =
BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
SDI->getIterator());
BO->setDebugLoc(SDI->getDebugLoc());
Op.V = BO;
}
Expand All @@ -919,7 +919,8 @@ static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,

// If the divident was non-positive, we need to negate the result.
if (Ops[0].D == Domain::NonPositive) {
Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg", SDI);
Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
SDI->getIterator());
Res->setDebugLoc(SDI->getDebugLoc());
}

Expand Down Expand Up @@ -966,8 +967,8 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
for (Operand &Op : Ops) {
if (Op.D == Domain::NonNegative)
continue;
auto *BO =
BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
SDI->getIterator());
BO->setDebugLoc(SDI->getDebugLoc());
Op.V = BO;
}
Expand All @@ -981,7 +982,8 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,

// If the operands had two different domains, we need to negate the result.
if (Ops[0].D != Ops[1].D) {
Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg", SDI);
Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
SDI->getIterator());
Res->setDebugLoc(SDI->getDebugLoc());
}

Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Transforms/Scalar/Reassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ static BinaryOperator *CreateMul(Value *S1, Value *S2, const Twine &Name,
}

static Instruction *CreateNeg(Value *S1, const Twine &Name,
Instruction *InsertBefore, Value *FlagsOp) {
BasicBlock::iterator InsertBefore,
Value *FlagsOp) {
if (S1->getType()->isIntOrIntVectorTy())
return BinaryOperator::CreateNeg(S1, Name, InsertBefore);

Expand Down Expand Up @@ -958,7 +959,8 @@ static Value *NegateValue(Value *V, Instruction *BI,

// Insert a 'neg' instruction that subtracts the value from zero to get the
// negation.
Instruction *NewNeg = CreateNeg(V, V->getName() + ".neg", BI, BI);
Instruction *NewNeg =
CreateNeg(V, V->getName() + ".neg", BI->getIterator(), BI);
ToRedo.insert(NewNeg);
return NewNeg;
}
Expand Down Expand Up @@ -1246,7 +1248,7 @@ Value *ReassociatePass::RemoveFactorFromExpression(Value *V, Value *Factor) {
}

if (NeedsNegate)
V = CreateNeg(V, "neg", &*InsertPt, BO);
V = CreateNeg(V, "neg", InsertPt, BO);

return V;
}
Expand Down

0 comments on commit 7e88d51

Please sign in to comment.