Skip to content

Commit

Permalink
Reapply "[NFC][RemoveDIs] Switch ConstantExpr::getAsInstruction to no…
Browse files Browse the repository at this point in the history
…t insert (#84737)"

Fixes a build error caused by an unupdated getAsInstruction callsite in clang.

This reverts commit ab851f7.
  • Loading branch information
SLTozer committed Mar 19, 2024
1 parent 25bb743 commit 9a96fb4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGCUDANV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ static void replaceManagedVar(llvm::GlobalVariable *Var,
// variable with instructions.
for (auto &&Op : WorkItem) {
auto *CE = cast<llvm::ConstantExpr>(Op);
auto *NewInst = CE->getAsInstruction(I);
auto *NewInst = CE->getAsInstruction();
NewInst->insertBefore(*I->getParent(), I->getIterator());
NewInst->replaceUsesOfWith(OldV, NewV);
OldV = CE;
NewV = NewInst;
Expand Down
5 changes: 2 additions & 3 deletions llvm/include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1289,14 +1289,13 @@ class ConstantExpr : public Constant {
Type *SrcTy = nullptr) const;

/// Returns an Instruction which implements the same operation as this
/// ConstantExpr. If \p InsertBefore is not null, the new instruction is
/// inserted before it, otherwise it is not inserted into any basic block.
/// ConstantExpr. It is not inserted into any basic block.
///
/// A better approach to this could be to have a constructor for Instruction
/// which would take a ConstantExpr parameter, but that would have spread
/// implementation details of ConstantExpr outside of Constants.cpp, which
/// would make it harder to remove ConstantExprs altogether.
Instruction *getAsInstruction(Instruction *InsertBefore = nullptr) const;
Instruction *getAsInstruction() const;

/// Whether creating a constant expression for this binary operator is
/// desirable.
Expand Down
13 changes: 9 additions & 4 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5073,10 +5073,15 @@ FunctionCallee OpenMPIRBuilder::createDispatchFiniFunction(unsigned IVSize,

static void replaceConstatExprUsesInFuncWithInstr(ConstantExpr *ConstExpr,
Function *Func) {
for (User *User : make_early_inc_range(ConstExpr->users()))
if (auto *Instr = dyn_cast<Instruction>(User))
if (Instr->getFunction() == Func)
Instr->replaceUsesOfWith(ConstExpr, ConstExpr->getAsInstruction(Instr));
for (User *User : make_early_inc_range(ConstExpr->users())) {
if (auto *Instr = dyn_cast<Instruction>(User)) {
if (Instr->getFunction() == Func) {
Instruction *ConstInst = ConstExpr->getAsInstruction();
ConstInst->insertBefore(*Instr->getParent(), Instr->getIterator());
Instr->replaceUsesOfWith(ConstExpr, ConstInst);
}
}
}
}

static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input,
Expand Down
21 changes: 10 additions & 11 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3303,7 +3303,7 @@ Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
NewOps, this, From, To, NumUpdated, OperandNo);
}

Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
Instruction *ConstantExpr::getAsInstruction() const {
SmallVector<Value *, 4> ValueOperands(operands());
ArrayRef<Value*> Ops(ValueOperands);

Expand All @@ -3322,32 +3322,31 @@ Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
case Instruction::BitCast:
case Instruction::AddrSpaceCast:
return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0],
getType(), "", InsertBefore);
getType(), "");
case Instruction::InsertElement:
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore);
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "");
case Instruction::ExtractElement:
return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore);
return ExtractElementInst::Create(Ops[0], Ops[1], "");
case Instruction::ShuffleVector:
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "",
InsertBefore);
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");

case Instruction::GetElementPtr: {
const auto *GO = cast<GEPOperator>(this);
if (GO->isInBounds())
return GetElementPtrInst::CreateInBounds(
GO->getSourceElementType(), Ops[0], Ops.slice(1), "", InsertBefore);
return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
Ops[0], Ops.slice(1), "");
return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
Ops.slice(1), "", InsertBefore);
Ops.slice(1), "");
}
case Instruction::ICmp:
case Instruction::FCmp:
return CmpInst::Create((Instruction::OtherOps)getOpcode(),
(CmpInst::Predicate)getPredicate(), Ops[0], Ops[1],
"", InsertBefore);
"");
default:
assert(getNumOperands() == 2 && "Must be binary operator?");
BinaryOperator *BO = BinaryOperator::Create(
(Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "", InsertBefore);
(Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "");
if (isa<OverflowingBinaryOperator>(BO)) {
BO->setHasNoUnsignedWrap(SubclassOptionalData &
OverflowingBinaryOperator::NoUnsignedWrap);
Expand Down
13 changes: 7 additions & 6 deletions llvm/lib/IR/ReplaceConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ static bool isExpandableUser(User *U) {
return isa<ConstantExpr>(U) || isa<ConstantAggregate>(U);
}

static SmallVector<Instruction *, 4> expandUser(Instruction *InsertPt,
static SmallVector<Instruction *, 4> expandUser(BasicBlock::iterator InsertPt,
Constant *C) {
SmallVector<Instruction *, 4> NewInsts;
if (auto *CE = dyn_cast<ConstantExpr>(C)) {
NewInsts.push_back(CE->getAsInstruction(InsertPt));
Instruction *ConstInst = CE->getAsInstruction();
ConstInst->insertBefore(*InsertPt->getParent(), InsertPt);
NewInsts.push_back(ConstInst);
} else if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
Value *V = PoisonValue::get(C->getType());
for (auto [Idx, Op] : enumerate(C->operands())) {
Expand Down Expand Up @@ -80,12 +82,11 @@ bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts) {
Instruction *I = InstructionWorklist.pop_back_val();
DebugLoc Loc = I->getDebugLoc();
for (Use &U : I->operands()) {
auto *BI = I;
BasicBlock::iterator BI = I->getIterator();
if (auto *Phi = dyn_cast<PHINode>(I)) {
BasicBlock *BB = Phi->getIncomingBlock(U);
BasicBlock::iterator It = BB->getFirstInsertionPt();
assert(It != BB->end() && "Unexpected empty basic block");
BI = &*It;
BI = BB->getFirstInsertionPt();
assert(BI != BB->end() && "Unexpected empty basic block");
}

if (auto *C = dyn_cast<Constant>(U.get())) {
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
BasicBlock *PredBB = PN->getIncomingBlock(I);
if (PredBB->getTerminator()->getNumSuccessors() > 1)
PredBB = SplitEdge(PredBB, PN->getParent());
Instruction *InsertPos = PredBB->getTerminator();
Instruction *NewInst = CE->getAsInstruction(InsertPos);
BasicBlock::iterator InsertPos =
PredBB->getTerminator()->getIterator();
Instruction *NewInst = CE->getAsInstruction();
NewInst->insertBefore(*PredBB, InsertPos);
PN->setOperand(I, NewInst);
}
} else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
Instruction *NewInst = CE->getAsInstruction(Instr);
Instruction *NewInst = CE->getAsInstruction();
NewInst->insertBefore(*Instr->getParent(), Instr->getIterator());
Instr->replaceUsesOfWith(CE, NewInst);
} else {
ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
Expand Down

0 comments on commit 9a96fb4

Please sign in to comment.