Skip to content

Commit 7e88d51

Browse files
authored
[NFC][RemoveDIs] Have CreateNeg only accept iterators (#82999)
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.
1 parent 6c2eec5 commit 7e88d51

File tree

5 files changed

+18
-25
lines changed

5 files changed

+18
-25
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,7 @@ class BinaryOperator : public Instruction {
468468
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
469469
BasicBlock::iterator InsertBefore);
470470
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
471-
Instruction *InsertBefore = nullptr);
472-
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
473-
BasicBlock *InsertAtEnd);
471+
BasicBlock *InsertAtEnd = nullptr);
474472
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
475473
BasicBlock::iterator InsertBefore);
476474
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",

llvm/lib/IR/Instruction.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
4646

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

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

5656
Instruction::~Instruction() {
@@ -73,7 +73,6 @@ Instruction::~Instruction() {
7373
setMetadata(LLVMContext::MD_DIAssignID, nullptr);
7474
}
7575

76-
7776
void Instruction::setParent(BasicBlock *P) {
7877
Parent = P;
7978
}

llvm/lib/IR/Instructions.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,14 +3245,6 @@ BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
32453245
InsertBefore);
32463246
}
32473247

3248-
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
3249-
Instruction *InsertBefore) {
3250-
Value *Zero = ConstantInt::get(Op->getType(), 0);
3251-
return new BinaryOperator(Instruction::Sub,
3252-
Zero, Op,
3253-
Op->getType(), Name, InsertBefore);
3254-
}
3255-
32563248
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
32573249
BasicBlock *InsertAtEnd) {
32583250
Value *Zero = ConstantInt::get(Op->getType(), 0);

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,8 @@ static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
905905
for (Operand &Op : Ops) {
906906
if (Op.D == Domain::NonNegative)
907907
continue;
908-
auto *BO =
909-
BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
908+
auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
909+
SDI->getIterator());
910910
BO->setDebugLoc(SDI->getDebugLoc());
911911
Op.V = BO;
912912
}
@@ -919,7 +919,8 @@ static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
919919

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

@@ -966,8 +967,8 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
966967
for (Operand &Op : Ops) {
967968
if (Op.D == Domain::NonNegative)
968969
continue;
969-
auto *BO =
970-
BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
970+
auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
971+
SDI->getIterator());
971972
BO->setDebugLoc(SDI->getDebugLoc());
972973
Op.V = BO;
973974
}
@@ -981,7 +982,8 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
981982

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

llvm/lib/Transforms/Scalar/Reassociate.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ static BinaryOperator *CreateMul(Value *S1, Value *S2, const Twine &Name,
270270
}
271271

272272
static Instruction *CreateNeg(Value *S1, const Twine &Name,
273-
Instruction *InsertBefore, Value *FlagsOp) {
273+
BasicBlock::iterator InsertBefore,
274+
Value *FlagsOp) {
274275
if (S1->getType()->isIntOrIntVectorTy())
275276
return BinaryOperator::CreateNeg(S1, Name, InsertBefore);
276277

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

959960
// Insert a 'neg' instruction that subtracts the value from zero to get the
960961
// negation.
961-
Instruction *NewNeg = CreateNeg(V, V->getName() + ".neg", BI, BI);
962+
Instruction *NewNeg =
963+
CreateNeg(V, V->getName() + ".neg", BI->getIterator(), BI);
962964
ToRedo.insert(NewNeg);
963965
return NewNeg;
964966
}
@@ -1246,7 +1248,7 @@ Value *ReassociatePass::RemoveFactorFromExpression(Value *V, Value *Factor) {
12461248
}
12471249

12481250
if (NeedsNegate)
1249-
V = CreateNeg(V, "neg", &*InsertPt, BO);
1251+
V = CreateNeg(V, "neg", InsertPt, BO);
12501252

12511253
return V;
12521254
}

0 commit comments

Comments
 (0)