Skip to content

Commit

Permalink
[IR] Remove support for insertvalue constant expression
Browse files Browse the repository at this point in the history
This removes the insertvalue constant expression, as part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
This is very similar to the extractvalue removal from D125795.
insertvalue is also not supported in bitcode, so no auto-ugprade
is necessary.

ConstantExpr::getInsertValue() can be replaced with
IRBuilder::CreateInsertValue() or ConstantFoldInsertValueInstruction(),
depending on whether a constant result is required (with the latter
being fallible).

The ConstantExpr::hasIndices() and ConstantExpr::getIndices()
methods also go away here, because there are no longer any constant
expressions with indices.

Differential Revision: https://reviews.llvm.org/D128719
  • Loading branch information
nikic committed Jul 4, 2022
1 parent 1063dfc commit 7283f48
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 249 deletions.
10 changes: 0 additions & 10 deletions llvm/bindings/go/llvm/ir.go
Expand Up @@ -993,16 +993,6 @@ func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
return
}

func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
n := len(indices)
if n == 0 {
panic("one or more indices are required")
}
ptr := (*C.unsigned)(&indices[0])
rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
return
}

func BlockAddress(f Value, bb BasicBlock) (v Value) {
v.C = C.LLVMBlockAddress(f.C, bb.C)
return
Expand Down
2 changes: 0 additions & 2 deletions llvm/bindings/ocaml/llvm/llvm.ml
Expand Up @@ -704,8 +704,6 @@ external const_insertelement : llvalue -> llvalue -> llvalue -> llvalue
= "LLVMConstInsertElement"
external const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue
= "LLVMConstShuffleVector"
external const_insertvalue : llvalue -> llvalue -> int array -> llvalue
= "llvm_const_insertvalue"
external const_inline_asm : lltype -> string -> string -> bool -> bool ->
llvalue
= "llvm_const_inline_asm"
Expand Down
5 changes: 0 additions & 5 deletions llvm/bindings/ocaml/llvm/llvm.mli
Expand Up @@ -1347,11 +1347,6 @@ val const_insertelement : llvalue -> llvalue -> llvalue -> llvalue
See the method [llvm::ConstantExpr::getShuffleVector]. *)
val const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue

(** [const_insertvalue agg val idxs] inserts the value [val] in the specified
indexs [idxs] in the aggregate [agg]. Each [idxs] must be less than the size
of the aggregate. See the method [llvm::ConstantExpr::getInsertValue]. *)
val const_insertvalue : llvalue -> llvalue -> int array -> llvalue

(** [const_inline_asm ty asm con side align] inserts a inline assembly string.
See the method [llvm::InlineAsm::get]. *)
val const_inline_asm : lltype -> string -> string -> bool -> bool -> llvalue
Expand Down
17 changes: 0 additions & 17 deletions llvm/bindings/ocaml/llvm/llvm_ocaml.c
Expand Up @@ -1013,23 +1013,6 @@ LLVMValueRef llvm_const_intcast(LLVMValueRef CV, LLVMTypeRef T,
return LLVMConstIntCast(CV, T, Bool_val(IsSigned));
}

/* llvalue -> llvalue -> int array -> llvalue */
LLVMValueRef llvm_const_insertvalue(LLVMValueRef Aggregate, LLVMValueRef Val,
value Indices) {
int size = Wosize_val(Indices);
int i;
LLVMValueRef result;

unsigned *idxs = (unsigned *)malloc(size * sizeof(unsigned));
for (i = 0; i < size; i++) {
idxs[i] = Int_val(Field(Indices, i));
}

result = LLVMConstInsertValue(Aggregate, Val, idxs, size);
free(idxs);
return result;
}

/* lltype -> string -> string -> bool -> bool -> llvalue */
LLVMValueRef llvm_const_inline_asm(LLVMTypeRef Ty, value Asm, value Constraints,
value HasSideEffects, value IsAlignStack) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Expand Up @@ -71,6 +71,7 @@ Changes to the LLVM IR
* The constant expression variants of the following instructions have been
removed:
* ``extractvalue``
* ``insertvalue``

Changes to building LLVM
------------------------
Expand Down Expand Up @@ -179,6 +180,7 @@ Changes to the C API
an instruction should be created using the ``LLVMBuildXYZ`` APIs, which will
constant fold the operands if possible and create an instruction otherwise:
* ``LLVMConstExtractValue``
* ``LLVMConstInsertValue``

Changes to the Go bindings
--------------------------
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm-c/Core.h
Expand Up @@ -2238,9 +2238,6 @@ LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
LLVMValueRef VectorBConstant,
LLVMValueRef MaskConstant);
LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
LLVMValueRef ElementValueConstant,
unsigned *IdxList, unsigned NumIdx);
LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB);

/** Deprecated: Use LLVMGetInlineAsm instead. */
Expand Down
11 changes: 0 additions & 11 deletions llvm/include/llvm/IR/Constants.h
Expand Up @@ -1201,10 +1201,6 @@ class ConstantExpr : public Constant {
/// Return true if this is a compare constant expression
bool isCompare() const;

/// Return true if this is an insertvalue or extractvalue expression,
/// and the getIndices() method may be used.
bool hasIndices() const;

/// Select constant expr
///
/// \param OnlyIfReducedTy see \a getWithOperands() docs.
Expand Down Expand Up @@ -1294,9 +1290,6 @@ class ConstantExpr : public Constant {
static Constant *getShuffleVector(Constant *V1, Constant *V2,
ArrayRef<int> Mask,
Type *OnlyIfReducedTy = nullptr);
static Constant *getInsertValue(Constant *Agg, Constant *Val,
ArrayRef<unsigned> Idxs,
Type *OnlyIfReducedTy = nullptr);

/// Return the opcode at the root of this constant expression
unsigned getOpcode() const { return getSubclassDataFromValue(); }
Expand All @@ -1305,10 +1298,6 @@ class ConstantExpr : public Constant {
/// FCMP constant expression.
unsigned getPredicate() const;

/// Assert that this is an insertvalue or exactvalue
/// expression and return the list of indices.
ArrayRef<unsigned> getIndices() const;

/// Assert that this is a shufflevector and return the mask. See class
/// ShuffleVectorInst for a description of the mask representation.
ArrayRef<int> getShuffleMask() const;
Expand Down
28 changes: 2 additions & 26 deletions llvm/lib/AsmParser/LLParser.cpp
Expand Up @@ -3474,32 +3474,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
}
case lltok::kw_extractvalue:
return error(ID.Loc, "extractvalue constexprs are no longer supported");
case lltok::kw_insertvalue: {
Lex.Lex();
Constant *Val0, *Val1;
SmallVector<unsigned, 4> Indices;
if (parseToken(lltok::lparen, "expected '(' in insertvalue constantexpr") ||
parseGlobalTypeAndValue(Val0) ||
parseToken(lltok::comma,
"expected comma in insertvalue constantexpr") ||
parseGlobalTypeAndValue(Val1) || parseIndexList(Indices) ||
parseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
return true;
if (!Val0->getType()->isAggregateType())
return error(ID.Loc, "insertvalue operand must be aggregate type");
Type *IndexedType =
ExtractValueInst::getIndexedType(Val0->getType(), Indices);
if (!IndexedType)
return error(ID.Loc, "invalid indices for insertvalue");
if (IndexedType != Val1->getType())
return error(ID.Loc, "insertvalue operand and field disagree in type: '" +
getTypeString(Val1->getType()) +
"' instead of '" + getTypeString(IndexedType) +
"'");
ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
ID.Kind = ValID::t_Constant;
return false;
}
case lltok::kw_insertvalue:
return error(ID.Loc, "insertvalue constexprs are no longer supported");
case lltok::kw_icmp:
case lltok::kw_fcmp: {
unsigned PredVal, Opc = Lex.getUIntVal();
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Expand Up @@ -2674,9 +2674,6 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
Record.push_back(VE.getValueID(C->getOperand(1)));
Record.push_back(CE->getPredicate());
break;
case Instruction::InsertValue:
report_fatal_error("insertvalue constexprs not supported");
break;
}
} else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Code = bitc::CST_CODE_BLOCKADDRESS;
Expand Down
9 changes: 2 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Expand Up @@ -3747,13 +3747,8 @@ void SelectionDAGBuilder::visitShuffleVector(const User &I) {
setValue(&I, DAG.getBuildVector(VT, DL, Ops));
}

void SelectionDAGBuilder::visitInsertValue(const User &I) {
ArrayRef<unsigned> Indices;
if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(&I))
Indices = IV->getIndices();
else
Indices = cast<ConstantExpr>(&I)->getIndices();

void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
ArrayRef<unsigned> Indices = I.getIndices();
const Value *Op0 = I.getOperand(0);
const Value *Op1 = I.getOperand(1);
Type *AggTy = I.getType();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
Expand Up @@ -529,7 +529,7 @@ class SelectionDAGBuilder {
void visitShuffleVector(const User &I);

void visitExtractValue(const ExtractValueInst &I);
void visitInsertValue(const User &I);
void visitInsertValue(const InsertValueInst &I);
void visitLandingPad(const LandingPadInst &LP);

void visitGetElementPtr(const User &I);
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/IR/AsmWriter.cpp
Expand Up @@ -1590,10 +1590,6 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
Out << ", ";
}

if (CE->hasIndices())
for (unsigned I : CE->getIndices())
Out << ", " << I;

if (CE->isCast()) {
Out << " to ";
WriterCtx.TypePrinter->print(CE->getType(), Out);
Expand Down
44 changes: 2 additions & 42 deletions llvm/lib/IR/Constants.cpp
Expand Up @@ -547,8 +547,6 @@ void llvm::deleteConstant(Constant *C) {
delete static_cast<InsertElementConstantExpr *>(C);
else if (isa<ShuffleVectorConstantExpr>(C))
delete static_cast<ShuffleVectorConstantExpr *>(C);
else if (isa<InsertValueConstantExpr>(C))
delete static_cast<InsertValueConstantExpr *>(C);
else if (isa<GetElementPtrConstantExpr>(C))
delete static_cast<GetElementPtrConstantExpr *>(C);
else if (isa<CompareConstantExpr>(C))
Expand Down Expand Up @@ -1488,14 +1486,6 @@ bool ConstantExpr::isCompare() const {
return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
}

bool ConstantExpr::hasIndices() const {
return getOpcode() == Instruction::InsertValue;
}

ArrayRef<unsigned> ConstantExpr::getIndices() const {
return cast<InsertValueConstantExpr>(this)->Indices;
}

unsigned ConstantExpr::getPredicate() const {
return cast<CompareConstantExpr>(this)->predicate;
}
Expand Down Expand Up @@ -1539,9 +1529,6 @@ Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
OnlyIfReducedTy);
case Instruction::ExtractElement:
return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
case Instruction::InsertValue:
return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
OnlyIfReducedTy);
case Instruction::FNeg:
return ConstantExpr::getFNeg(Ops[0]);
case Instruction::ShuffleVector:
Expand Down Expand Up @@ -2517,7 +2504,7 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
if (InRangeIndex && *InRangeIndex < 63)
SubClassOptionalData |= (*InRangeIndex + 1) << 1;
const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
SubClassOptionalData, None, None, Ty);
SubClassOptionalData, None, Ty);

LLVMContextImpl *pImpl = C->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Expand Down Expand Up @@ -2638,36 +2625,12 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,

// Look up the constant in the table first to ensure uniqueness
Constant *ArgVec[] = {V1, V2};
ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, 0, None, Mask);
ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, 0, Mask);

LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
}

Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
ArrayRef<unsigned> Idxs,
Type *OnlyIfReducedTy) {
assert(Agg->getType()->isFirstClassType() &&
"Non-first-class type for constant insertvalue expression");

assert(ExtractValueInst::getIndexedType(Agg->getType(),
Idxs) == Val->getType() &&
"insertvalue indices invalid!");
Type *ReqTy = Val->getType();

if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
return FC;

if (OnlyIfReducedTy == ReqTy)
return nullptr;

Constant *ArgVec[] = { Agg, Val };
const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);

LLVMContextImpl *pImpl = Agg->getContext().pImpl;
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
}

Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
assert(C->getType()->isIntOrIntVectorTy() &&
"Cannot NEG a nonintegral value!");
Expand Down Expand Up @@ -3517,9 +3480,6 @@ Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore);
case Instruction::ExtractElement:
return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore);
case Instruction::InsertValue:
return InsertValueInst::Create(Ops[0], Ops[1], getIndices(), "",
InsertBefore);
case Instruction::ShuffleVector:
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "",
InsertBefore);
Expand Down

0 comments on commit 7283f48

Please sign in to comment.