Skip to content

Commit 5548e80

Browse files
committed
[IR] Remove support for extractvalue constant expression
This removes the extractvalue constant expression, as part of https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179. extractvalue is already not supported in bitcode, so we do not need to worry about bitcode auto-upgrade. Uses of ConstantExpr::getExtractValue() should be replaced with IRBuilder::CreateExtractValue() (if the fact that the result is constant is not important) or ConstantFoldExtractValueInstruction() (if it is). Though for this particular case, it is also possible and usually preferable to use getAggregateElement() instead. The C API function LLVMConstExtractValue() is removed, as the underlying constant expression no longer exists. Instead, LLVMBuildExtractValue() should be used (which will constant fold or create an instruction). Depending on the use-case, LLVMGetAggregateElement() may also be used instead. Differential Revision: https://reviews.llvm.org/D125795
1 parent ab72182 commit 5548e80

File tree

26 files changed

+61
-179
lines changed

26 files changed

+61
-179
lines changed

clang/lib/CodeGen/ItaniumCXXABI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
955955
adj = llvm::ConstantInt::get(adj->getType(), offset);
956956
}
957957

958-
llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
958+
llvm::Constant *srcAdj = src->getAggregateElement(1);
959959
llvm::Constant *dstAdj;
960960
if (isDerivedToBase)
961961
dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);

llvm/bindings/go/llvm/ir.go

-14
Original file line numberDiff line numberDiff line change
@@ -993,20 +993,6 @@ func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
993993
return
994994
}
995995

996-
//TODO
997-
//LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
998-
// unsigned NumIdx);
999-
1000-
func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
1001-
n := len(indices)
1002-
if n == 0 {
1003-
panic("one or more indices are required")
1004-
}
1005-
ptr := (*C.unsigned)(&indices[0])
1006-
rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
1007-
return
1008-
}
1009-
1010996
func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
1011997
n := len(indices)
1012998
if n == 0 {

llvm/bindings/ocaml/llvm/llvm.ml

-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ external const_insertelement : llvalue -> llvalue -> llvalue -> llvalue
704704
= "LLVMConstInsertElement"
705705
external const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue
706706
= "LLVMConstShuffleVector"
707-
external const_extractvalue : llvalue -> int array -> llvalue
708-
= "llvm_const_extractvalue"
709707
external const_insertvalue : llvalue -> llvalue -> int array -> llvalue
710708
= "llvm_const_insertvalue"
711709
external const_inline_asm : lltype -> string -> string -> bool -> bool ->

llvm/bindings/ocaml/llvm/llvm.mli

-5
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,6 @@ val const_insertelement : llvalue -> llvalue -> llvalue -> llvalue
13471347
See the method [llvm::ConstantExpr::getShuffleVector]. *)
13481348
val const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue
13491349

1350-
(** [const_extractvalue agg idxs] returns the constant [idxs]th value of
1351-
constant aggregate [agg]. Each [idxs] must be less than the size of the
1352-
aggregate. See the method [llvm::ConstantExpr::getExtractValue]. *)
1353-
val const_extractvalue : llvalue -> int array -> llvalue
1354-
13551350
(** [const_insertvalue agg val idxs] inserts the value [val] in the specified
13561351
indexs [idxs] in the aggregate [agg]. Each [idxs] must be less than the size
13571352
of the aggregate. See the method [llvm::ConstantExpr::getInsertValue]. *)

llvm/bindings/ocaml/llvm/llvm_ocaml.c

-16
Original file line numberDiff line numberDiff line change
@@ -1013,22 +1013,6 @@ LLVMValueRef llvm_const_intcast(LLVMValueRef CV, LLVMTypeRef T,
10131013
return LLVMConstIntCast(CV, T, Bool_val(IsSigned));
10141014
}
10151015

1016-
/* llvalue -> int array -> llvalue */
1017-
LLVMValueRef llvm_const_extractvalue(LLVMValueRef Aggregate, value Indices) {
1018-
int size = Wosize_val(Indices);
1019-
int i;
1020-
LLVMValueRef result;
1021-
1022-
unsigned *idxs = (unsigned *)malloc(size * sizeof(unsigned));
1023-
for (i = 0; i < size; i++) {
1024-
idxs[i] = Int_val(Field(Indices, i));
1025-
}
1026-
1027-
result = LLVMConstExtractValue(Aggregate, idxs, size);
1028-
free(idxs);
1029-
return result;
1030-
}
1031-
10321016
/* llvalue -> llvalue -> int array -> llvalue */
10331017
LLVMValueRef llvm_const_insertvalue(LLVMValueRef Aggregate, LLVMValueRef Val,
10341018
value Indices) {

llvm/docs/ReleaseNotes.rst

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Changes to the LLVM IR
6868

6969
* Renamed ``llvm.experimental.vector.extract`` intrinsic to ``llvm.vector.extract``.
7070
* Renamed ``llvm.experimental.vector.insert`` intrinsic to ``llvm.vector.insert``.
71+
* The constant expression variants of the following instructions have been
72+
removed:
73+
* ``extractvalue``
7174

7275
Changes to building LLVM
7376
------------------------
@@ -171,6 +174,12 @@ Changes to the C API
171174
favor of the new function, which works on all constant aggregates, rather than
172175
only instances of ``ConstantDataSequential``.
173176

177+
* The following functions for creating constant expressions have been removed,
178+
because the underlying constant expressions are no longer supported. Instead,
179+
an instruction should be created using the ``LLVMBuildXYZ`` APIs, which will
180+
constant fold the operands if possible and create an instruction otherwise:
181+
* ``LLVMConstExtractValue``
182+
174183
Changes to the Go bindings
175184
--------------------------
176185

llvm/include/llvm-c/Core.h

-2
Original file line numberDiff line numberDiff line change
@@ -2238,8 +2238,6 @@ LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
22382238
LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
22392239
LLVMValueRef VectorBConstant,
22402240
LLVMValueRef MaskConstant);
2241-
LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
2242-
unsigned NumIdx);
22432241
LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
22442242
LLVMValueRef ElementValueConstant,
22452243
unsigned *IdxList, unsigned NumIdx);

llvm/include/llvm/Analysis/TargetFolder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class TargetFolder final : public IRBuilderFolder {
109109
Value *FoldExtractValue(Value *Agg,
110110
ArrayRef<unsigned> IdxList) const override {
111111
if (auto *CAgg = dyn_cast<Constant>(Agg))
112-
return Fold(ConstantExpr::getExtractValue(CAgg, IdxList));
112+
return ConstantFoldExtractValueInstruction(CAgg, IdxList);
113113
return nullptr;
114114
};
115115

llvm/include/llvm/IR/ConstantFolder.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/STLExtras.h"
2121
#include "llvm/IR/Constants.h"
22+
#include "llvm/IR/ConstantFold.h"
2223
#include "llvm/IR/IRBuilderFolder.h"
2324
#include "llvm/IR/Instruction.h"
2425

@@ -97,7 +98,7 @@ class ConstantFolder final : public IRBuilderFolder {
9798
Value *FoldExtractValue(Value *Agg,
9899
ArrayRef<unsigned> IdxList) const override {
99100
if (auto *CAgg = dyn_cast<Constant>(Agg))
100-
return ConstantExpr::getExtractValue(CAgg, IdxList);
101+
return ConstantFoldExtractValueInstruction(CAgg, IdxList);
101102
return nullptr;
102103
};
103104

llvm/include/llvm/IR/Constants.h

-2
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,6 @@ class ConstantExpr : public Constant {
12941294
static Constant *getShuffleVector(Constant *V1, Constant *V2,
12951295
ArrayRef<int> Mask,
12961296
Type *OnlyIfReducedTy = nullptr);
1297-
static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1298-
Type *OnlyIfReducedTy = nullptr);
12991297
static Constant *getInsertValue(Constant *Agg, Constant *Val,
13001298
ArrayRef<unsigned> Idxs,
13011299
Type *OnlyIfReducedTy = nullptr);

llvm/lib/Analysis/ConstantFolding.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
10821082
case Instruction::ExtractElement:
10831083
return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
10841084
case Instruction::ExtractValue:
1085-
return ConstantExpr::getExtractValue(
1085+
return ConstantFoldExtractValueInstruction(
10861086
Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
10871087
case Instruction::InsertElement:
10881088
return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
@@ -1198,7 +1198,7 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
11981198
return ConstantExpr::getInsertValue(Ops[0], Ops[1], IVI->getIndices());
11991199

12001200
if (auto *EVI = dyn_cast<ExtractValueInst>(I))
1201-
return ConstantExpr::getExtractValue(Ops[0], EVI->getIndices());
1201+
return ConstantFoldExtractValueInstruction(Ops[0], EVI->getIndices());
12021202

12031203
return ConstantFoldInstOperands(I, Ops, DL, TLI);
12041204
}

llvm/lib/Analysis/InlineCost.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ bool CallAnalyzer::visitStore(StoreInst &I) {
20782078
bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) {
20792079
// Constant folding for extract value is trivial.
20802080
if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
2081-
return ConstantExpr::getExtractValue(COps[0], I.getIndices());
2081+
return ConstantFoldExtractValueInstruction(COps[0], I.getIndices());
20822082
}))
20832083
return true;
20842084

llvm/lib/AsmParser/LLParser.cpp

+2-18
Original file line numberDiff line numberDiff line change
@@ -3472,24 +3472,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
34723472
ID.Kind = ValID::t_Constant;
34733473
return false;
34743474
}
3475-
case lltok::kw_extractvalue: {
3476-
Lex.Lex();
3477-
Constant *Val;
3478-
SmallVector<unsigned, 4> Indices;
3479-
if (parseToken(lltok::lparen,
3480-
"expected '(' in extractvalue constantexpr") ||
3481-
parseGlobalTypeAndValue(Val) || parseIndexList(Indices) ||
3482-
parseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
3483-
return true;
3484-
3485-
if (!Val->getType()->isAggregateType())
3486-
return error(ID.Loc, "extractvalue operand must be aggregate type");
3487-
if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
3488-
return error(ID.Loc, "invalid indices for extractvalue");
3489-
ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
3490-
ID.Kind = ValID::t_Constant;
3491-
return false;
3492-
}
3475+
case lltok::kw_extractvalue:
3476+
return error(ID.Loc, "extractvalue constexprs are no longer supported");
34933477
case lltok::kw_insertvalue: {
34943478
Lex.Lex();
34953479
Constant *Val0, *Val1;

llvm/lib/IR/Constants.cpp

+1-36
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,6 @@ void llvm::deleteConstant(Constant *C) {
547547
delete static_cast<InsertElementConstantExpr *>(C);
548548
else if (isa<ShuffleVectorConstantExpr>(C))
549549
delete static_cast<ShuffleVectorConstantExpr *>(C);
550-
else if (isa<ExtractValueConstantExpr>(C))
551-
delete static_cast<ExtractValueConstantExpr *>(C);
552550
else if (isa<InsertValueConstantExpr>(C))
553551
delete static_cast<InsertValueConstantExpr *>(C);
554552
else if (isa<GetElementPtrConstantExpr>(C))
@@ -1491,15 +1489,10 @@ bool ConstantExpr::isCompare() const {
14911489
}
14921490

14931491
bool ConstantExpr::hasIndices() const {
1494-
return getOpcode() == Instruction::ExtractValue ||
1495-
getOpcode() == Instruction::InsertValue;
1492+
return getOpcode() == Instruction::InsertValue;
14961493
}
14971494

14981495
ArrayRef<unsigned> ConstantExpr::getIndices() const {
1499-
if (const ExtractValueConstantExpr *EVCE =
1500-
dyn_cast<ExtractValueConstantExpr>(this))
1501-
return EVCE->Indices;
1502-
15031496
return cast<InsertValueConstantExpr>(this)->Indices;
15041497
}
15051498

@@ -1549,8 +1542,6 @@ Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
15491542
case Instruction::InsertValue:
15501543
return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
15511544
OnlyIfReducedTy);
1552-
case Instruction::ExtractValue:
1553-
return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
15541545
case Instruction::FNeg:
15551546
return ConstantExpr::getFNeg(Ops[0]);
15561547
case Instruction::ShuffleVector:
@@ -2677,30 +2668,6 @@ Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
26772668
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
26782669
}
26792670

2680-
Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2681-
Type *OnlyIfReducedTy) {
2682-
assert(Agg->getType()->isFirstClassType() &&
2683-
"Tried to create extractelement operation on non-first-class type!");
2684-
2685-
Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
2686-
(void)ReqTy;
2687-
assert(ReqTy && "extractvalue indices invalid!");
2688-
2689-
assert(Agg->getType()->isFirstClassType() &&
2690-
"Non-first-class type for constant extractvalue expression");
2691-
if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2692-
return FC;
2693-
2694-
if (OnlyIfReducedTy == ReqTy)
2695-
return nullptr;
2696-
2697-
Constant *ArgVec[] = { Agg };
2698-
const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2699-
2700-
LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2701-
return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2702-
}
2703-
27042671
Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
27052672
assert(C->getType()->isIntOrIntVectorTy() &&
27062673
"Cannot NEG a nonintegral value!");
@@ -3553,8 +3520,6 @@ Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
35533520
case Instruction::InsertValue:
35543521
return InsertValueInst::Create(Ops[0], Ops[1], getIndices(), "",
35553522
InsertBefore);
3556-
case Instruction::ExtractValue:
3557-
return ExtractValueInst::Create(Ops[0], getIndices(), "", InsertBefore);
35583523
case Instruction::ShuffleVector:
35593524
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "",
35603525
InsertBefore);

llvm/lib/IR/ConstantsContext.h

-37
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,6 @@ class ShuffleVectorConstantExpr final : public ConstantExpr {
209209
}
210210
};
211211

212-
/// ExtractValueConstantExpr - This class is private to
213-
/// Constants.cpp, and is used behind the scenes to implement
214-
/// extractvalue constant exprs.
215-
class ExtractValueConstantExpr final : public ConstantExpr {
216-
public:
217-
ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
218-
Type *DestTy)
219-
: ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
220-
Indices(IdxList.begin(), IdxList.end()) {
221-
Op<0>() = Agg;
222-
}
223-
224-
// allocate space for exactly one operand
225-
void *operator new(size_t S) { return User::operator new(S, 1); }
226-
void operator delete(void *Ptr) { User::operator delete(Ptr); }
227-
228-
/// Indices - These identify which value to extract.
229-
const SmallVector<unsigned, 4> Indices;
230-
231-
/// Transparently provide more efficient getOperand methods.
232-
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
233-
234-
static bool classof(const ConstantExpr *CE) {
235-
return CE->getOpcode() == Instruction::ExtractValue;
236-
}
237-
static bool classof(const Value *V) {
238-
return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
239-
}
240-
};
241-
242212
/// InsertValueConstantExpr - This class is private to
243213
/// Constants.cpp, and is used behind the scenes to implement
244214
/// insertvalue constant exprs.
@@ -362,11 +332,6 @@ struct OperandTraits<ShuffleVectorConstantExpr>
362332
: public FixedNumOperandTraits<ShuffleVectorConstantExpr, 2> {};
363333
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
364334

365-
template <>
366-
struct OperandTraits<ExtractValueConstantExpr>
367-
: public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
368-
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
369-
370335
template <>
371336
struct OperandTraits<InsertValueConstantExpr>
372337
: public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
@@ -620,8 +585,6 @@ struct ConstantExprKeyType {
620585
return new ShuffleVectorConstantExpr(Ops[0], Ops[1], ShuffleMask);
621586
case Instruction::InsertValue:
622587
return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
623-
case Instruction::ExtractValue:
624-
return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
625588
case Instruction::GetElementPtr:
626589
return GetElementPtrConstantExpr::Create(ExplicitTy, Ops[0], Ops.slice(1),
627590
Ty, SubclassOptionalData);

llvm/lib/IR/Core.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -1875,12 +1875,6 @@ LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
18751875
IntMask));
18761876
}
18771877

1878-
LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1879-
unsigned NumIdx) {
1880-
return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1881-
makeArrayRef(IdxList, NumIdx)));
1882-
}
1883-
18841878
LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
18851879
LLVMValueRef ElementValueConstant,
18861880
unsigned *IdxList, unsigned NumIdx) {

llvm/lib/Transforms/Coroutines/CoroElide.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,13 @@ bool Lowerer::processCoroId(CoroIdInst *CoroId, AAResults &AA,
336336
assert(Resumers && "PostSplit coro.id Info argument must refer to an array"
337337
"of coroutine subfunctions");
338338
auto *ResumeAddrConstant =
339-
ConstantExpr::getExtractValue(Resumers, CoroSubFnInst::ResumeIndex);
339+
Resumers->getAggregateElement(CoroSubFnInst::ResumeIndex);
340340

341341
replaceWithConstant(ResumeAddrConstant, ResumeAddr);
342342

343343
bool ShouldElide = shouldElide(CoroId->getFunction(), DT);
344344

345-
auto *DestroyAddrConstant = ConstantExpr::getExtractValue(
346-
Resumers,
345+
auto *DestroyAddrConstant = Resumers->getAggregateElement(
347346
ShouldElide ? CoroSubFnInst::CleanupIndex : CoroSubFnInst::DestroyIndex);
348347

349348
for (auto &It : DestroyAddr)

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
190190
if (!Elt) return nullptr;
191191

192192
// If this is indexing an array of structures, get the structure element.
193-
if (!LaterIndices.empty())
194-
Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
193+
if (!LaterIndices.empty()) {
194+
Elt = ConstantFoldExtractValueInstruction(Elt, LaterIndices);
195+
if (!Elt)
196+
return nullptr;
197+
}
195198

196199
// If the element is masked, handle it.
197200
if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);

llvm/lib/Transforms/Utils/Evaluator.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
361361
LLVM_DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
362362
<< "\n");
363363
} else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
364-
InstResult = ConstantExpr::getExtractValue(
364+
InstResult = ConstantFoldExtractValueInstruction(
365365
getVal(EVI->getAggregateOperand()), EVI->getIndices());
366+
if (!InstResult)
367+
return false;
366368
LLVM_DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: "
367369
<< *InstResult << "\n");
368370
} else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {

0 commit comments

Comments
 (0)