llvm.org GIT mirror llvm / fc6d3a4
Convert InsertValueInst and ExtractValueInst APIs to use ArrayRef. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135040 91177308-0d34-0410-b5e6-96231b3b80d8 Jay Foad 8 years ago
25 changed file(s) with 195 addition(s) and 432 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef LLVM_ANALYSIS_VALUETRACKING_H
1515 #define LLVM_ANALYSIS_VALUETRACKING_H
1616
17 #include "llvm/ADT/ArrayRef.h"
1718 #include "llvm/Support/DataTypes.h"
1819 #include
1920
107108 /// If InsertBefore is not null, this function will duplicate (modified)
108109 /// insertvalues when a part of a nested struct is extracted.
109110 Value *FindInsertedValue(Value *V,
110 const unsigned *idx_begin,
111 const unsigned *idx_end,
111 ArrayRef idx_range,
112112 Instruction *InsertBefore = 0);
113113
114 /// This is a convenience wrapper for finding values indexed by a single index
115 /// only.
116 inline Value *FindInsertedValue(Value *V, const unsigned Idx,
117 Instruction *InsertBefore = 0) {
118 const unsigned Idxs[1] = { Idx };
119 return FindInsertedValue(V, &Idxs[0], &Idxs[1], InsertBefore);
120 }
121
122114 /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
123115 /// it can be expressed as a base pointer plus a constant offset. Return the
124116 /// base and offset to the caller.
1515
1616 #include "llvm/Instructions.h"
1717 #include "llvm/InlineAsm.h"
18 #include "llvm/ADT/ArrayRef.h"
1819 #include "llvm/ADT/SmallVector.h"
1920 #include "llvm/CodeGen/ValueTypes.h"
2021 #include "llvm/CodeGen/ISDOpcodes.h"
3536 const unsigned *Indices,
3637 const unsigned *IndicesEnd,
3738 unsigned CurIndex = 0);
39
40 inline unsigned ComputeLinearIndex(const Type *Ty,
41 ArrayRef Indices,
42 unsigned CurIndex = 0) {
43 return ComputeLinearIndex(Ty, Indices.begin(), Indices.end(), CurIndex);
44 }
3845
3946 /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
4047 /// EVTs that represent all the individual underlying
854854 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
855855 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
856856 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
857 static Constant *getExtractValue(Constant *Agg,
858 const unsigned *IdxList, unsigned NumIdx);
857 static Constant *getExtractValue(Constant *Agg, ArrayRef Idxs);
859858 static Constant *getInsertValue(Constant *Agg, Constant *Val,
860 const unsigned *IdxList, unsigned NumIdx);
859 ArrayRef Idxs);
861860
862861 /// isNullValue - Return true if this is the value that would be returned by
863862 /// getNullValue.
1919 #include "llvm/DerivedTypes.h"
2020 #include "llvm/Attributes.h"
2121 #include "llvm/CallingConv.h"
22 #include "llvm/ADT/ArrayRef.h"
2223 #include "llvm/ADT/SmallVector.h"
2324 #include
2425
14271428 SmallVector Indices;
14281429
14291430 ExtractValueInst(const ExtractValueInst &EVI);
1430 void init(const unsigned *Idx, unsigned NumIdx,
1431 const Twine &NameStr);
1432 void init(unsigned Idx, const Twine &NameStr);
1433
1434 template
1435 void init(RandomAccessIterator IdxBegin,
1436 RandomAccessIterator IdxEnd,
1437 const Twine &NameStr,
1438 // This argument ensures that we have an iterator we can
1439 // do arithmetic on in constant time
1440 std::random_access_iterator_tag) {
1441 unsigned NumIdx = static_cast(std::distance(IdxBegin, IdxEnd));
1442
1443 // There's no fundamental reason why we require at least one index
1444 // (other than weirdness with &*IdxBegin being invalid; see
1445 // getelementptr's init routine for example). But there's no
1446 // present need to support it.
1447 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1448
1449 // This requires that the iterator points to contiguous memory.
1450 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1451 // we have to build an array here
1452 }
1453
1454 /// getIndexedType - Returns the type of the element that would be extracted
1455 /// with an extractvalue instruction with the specified parameters.
1456 ///
1457 /// Null is returned if the indices are invalid for the specified type.
1458 ///
1459 /// FIXME: Use ArrayRef
1460 static Type *getIndexedType(const Type *Agg,
1461 const unsigned *Idx, unsigned NumIdx);
1462
1463 template
1464 static Type *getIndexedType(const Type *Ptr,
1465 RandomAccessIterator IdxBegin,
1466 RandomAccessIterator IdxEnd,
1467 // This argument ensures that we
1468 // have an iterator we can do
1469 // arithmetic on in constant time
1470 std::random_access_iterator_tag) {
1471 unsigned NumIdx = static_cast(std::distance(IdxBegin, IdxEnd));
1472
1473 if (NumIdx > 0)
1474 // This requires that the iterator points to contiguous memory.
1475 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1476 else
1477 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1478 }
1431 void init(ArrayRef Idxs, const Twine &NameStr);
14791432
14801433 /// Constructors - Create a extractvalue instruction with a base aggregate
14811434 /// value and a list of indices. The first ctor can optionally insert before
14821435 /// an existing instruction, the second appends the new instruction to the
14831436 /// specified BasicBlock.
1484 template
14851437 inline ExtractValueInst(Value *Agg,
1486 RandomAccessIterator IdxBegin,
1487 RandomAccessIterator IdxEnd,
1438 ArrayRef Idxs,
14881439 const Twine &NameStr,
14891440 Instruction *InsertBefore);
1490 template
14911441 inline ExtractValueInst(Value *Agg,
1492 RandomAccessIterator IdxBegin,
1493 RandomAccessIterator IdxEnd,
1442 ArrayRef Idxs,
14941443 const Twine &NameStr, BasicBlock *InsertAtEnd);
14951444
14961445 // allocate space for exactly one operand
15011450 virtual ExtractValueInst *clone_impl() const;
15021451
15031452 public:
1504 template
15051453 static ExtractValueInst *Create(Value *Agg,
1506 RandomAccessIterator IdxBegin,
1507 RandomAccessIterator IdxEnd,
1454 ArrayRef Idxs,
15081455 const Twine &NameStr = "",
15091456 Instruction *InsertBefore = 0) {
15101457 return new
1511 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1512 }
1513 template
1458 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1459 }
15141460 static ExtractValueInst *Create(Value *Agg,
1515 RandomAccessIterator IdxBegin,
1516 RandomAccessIterator IdxEnd,
1461 ArrayRef Idxs,
15171462 const Twine &NameStr,
15181463 BasicBlock *InsertAtEnd) {
1519 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1520 }
1521
1522 /// Constructors - These two creators are convenience methods because one
1523 /// index extractvalue instructions are much more common than those with
1524 /// more than one.
1525 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1526 const Twine &NameStr = "",
1527 Instruction *InsertBefore = 0) {
1528 unsigned Idxs[1] = { Idx };
1529 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1530 }
1531 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532 const Twine &NameStr,
1533 BasicBlock *InsertAtEnd) {
1534 unsigned Idxs[1] = { Idx };
1535 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1464 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
15361465 }
15371466
15381467 /// getIndexedType - Returns the type of the element that would be extracted
15391468 /// with an extractvalue instruction with the specified parameters.
15401469 ///
15411470 /// Null is returned if the indices are invalid for the specified type.
1542 ///
1543 /// FIXME: Remove the templates and just use ArrayRef.
1544 template
1545 static Type *getIndexedType(const Type *Ptr,
1546 RandomAccessIterator IdxBegin,
1547 RandomAccessIterator IdxEnd) {
1548 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1549 typename std::iterator_traits::
1550 iterator_category());
1551 }
1552 static Type *getIndexedType(const Type *Ptr, unsigned Idx);
1471 static Type *getIndexedType(const Type *Agg, ArrayRef Idxs);
15531472
15541473 typedef const unsigned* idx_iterator;
15551474 inline idx_iterator idx_begin() const { return Indices.begin(); }
15651484 return 0U; // get index for modifying correct operand
15661485 }
15671486
1568 unsigned getNumIndices() const { // Note: always non-negative
1487 ArrayRef getIndices() const {
1488 return Indices;
1489 }
1490
1491 unsigned getNumIndices() const {
15691492 return (unsigned)Indices.size();
15701493 }
15711494
15831506 }
15841507 };
15851508
1586 template
15871509 ExtractValueInst::ExtractValueInst(Value *Agg,
1588 RandomAccessIterator IdxBegin,
1589 RandomAccessIterator IdxEnd,
1510 ArrayRef Idxs,
15901511 const Twine &NameStr,
15911512 Instruction *InsertBefore)
1592 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(),
1593 IdxBegin, IdxEnd)),
1513 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
15941514 ExtractValue, Agg, InsertBefore) {
1595 init(IdxBegin, IdxEnd, NameStr,
1596 typename std::iterator_traits
1597 ::iterator_category());
1515 init(Idxs, NameStr);
15981516 }
1599 template
16001517 ExtractValueInst::ExtractValueInst(Value *Agg,
1601 RandomAccessIterator IdxBegin,
1602 RandomAccessIterator IdxEnd,
1518 ArrayRef Idxs,
16031519 const Twine &NameStr,
16041520 BasicBlock *InsertAtEnd)
1605 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(),
1606 IdxBegin, IdxEnd)),
1521 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
16071522 ExtractValue, Agg, InsertAtEnd) {
1608 init(IdxBegin, IdxEnd, NameStr,
1609 typename std::iterator_traits
1610 ::iterator_category());
1523 init(Idxs, NameStr);
16111524 }
16121525
16131526
16231536
16241537 void *operator new(size_t, unsigned); // Do not implement
16251538 InsertValueInst(const InsertValueInst &IVI);
1626 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1539 void init(Value *Agg, Value *Val, ArrayRef Idxs,
16271540 const Twine &NameStr);
1628 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1629
1630 template
1631 void init(Value *Agg, Value *Val,
1632 RandomAccessIterator IdxBegin, RandomAccessIterator IdxEnd,
1633 const Twine &NameStr,
1634 // This argument ensures that we have an iterator we can
1635 // do arithmetic on in constant time
1636 std::random_access_iterator_tag) {
1637 unsigned NumIdx = static_cast(std::distance(IdxBegin, IdxEnd));
1638
1639 // There's no fundamental reason why we require at least one index
1640 // (other than weirdness with &*IdxBegin being invalid; see
1641 // getelementptr's init routine for example). But there's no
1642 // present need to support it.
1643 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1644
1645 // This requires that the iterator points to contiguous memory.
1646 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1647 // we have to build an array here
1648 }
16491541
16501542 /// Constructors - Create a insertvalue instruction with a base aggregate
16511543 /// value, a value to insert, and a list of indices. The first ctor can
16521544 /// optionally insert before an existing instruction, the second appends
16531545 /// the new instruction to the specified BasicBlock.
1654 template
16551546 inline InsertValueInst(Value *Agg, Value *Val,
1656 RandomAccessIterator IdxBegin,
1657 RandomAccessIterator IdxEnd,
1547 ArrayRef Idxs,
16581548 const Twine &NameStr,
16591549 Instruction *InsertBefore);
1660 template
16611550 inline InsertValueInst(Value *Agg, Value *Val,
1662 RandomAccessIterator IdxBegin,
1663 RandomAccessIterator IdxEnd,
1551 ArrayRef Idxs,
16641552 const Twine &NameStr, BasicBlock *InsertAtEnd);
16651553
16661554 /// Constructors - These two constructors are convenience methods because one
16781566 return User::operator new(s, 2);
16791567 }
16801568
1681 template
16821569 static InsertValueInst *Create(Value *Agg, Value *Val,
1683 RandomAccessIterator IdxBegin,
1684 RandomAccessIterator IdxEnd,
1570 ArrayRef Idxs,
16851571 const Twine &NameStr = "",
16861572 Instruction *InsertBefore = 0) {
1687 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1688 NameStr, InsertBefore);
1689 }
1690 template
1573 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1574 }
16911575 static InsertValueInst *Create(Value *Agg, Value *Val,
1692 RandomAccessIterator IdxBegin,
1693 RandomAccessIterator IdxEnd,
1576 ArrayRef Idxs,
16941577 const Twine &NameStr,
16951578 BasicBlock *InsertAtEnd) {
1696 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1697 NameStr, InsertAtEnd);
1698 }
1699
1700 /// Constructors - These two creators are convenience methods because one
1701 /// index insertvalue instructions are much more common than those with
1702 /// more than one.
1703 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1704 const Twine &NameStr = "",
1705 Instruction *InsertBefore = 0) {
1706 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1707 }
1708 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1709 const Twine &NameStr,
1710 BasicBlock *InsertAtEnd) {
1711 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1579 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
17121580 }
17131581
17141582 /// Transparently provide more efficient getOperand methods.
17381606 return 1U; // get index for modifying correct operand
17391607 }
17401608
1741 unsigned getNumIndices() const { // Note: always non-negative
1609 ArrayRef getIndices() const {
1610 return Indices;
1611 }
1612
1613 unsigned getNumIndices() const {
17421614 return (unsigned)Indices.size();
17431615 }
17441616
17611633 public FixedNumOperandTraits {
17621634 };
17631635
1764 template
17651636 InsertValueInst::InsertValueInst(Value *Agg,
17661637 Value *Val,
1767 RandomAccessIterator IdxBegin,
1768 RandomAccessIterator IdxEnd,
1638 ArrayRef Idxs,
17691639 const Twine &NameStr,
17701640 Instruction *InsertBefore)
17711641 : Instruction(Agg->getType(), InsertValue,
17721642 OperandTraits::op_begin(this),
17731643 2, InsertBefore) {
1774 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1775 typename std::iterator_traits
1776 ::iterator_category());
1644 init(Agg, Val, Idxs, NameStr);
17771645 }
1778 template
17791646 InsertValueInst::InsertValueInst(Value *Agg,
17801647 Value *Val,
1781 RandomAccessIterator IdxBegin,
1782 RandomAccessIterator IdxEnd,
1648 ArrayRef Idxs,
17831649 const Twine &NameStr,
17841650 BasicBlock *InsertAtEnd)
17851651 : Instruction(Agg->getType(), InsertValue,
17861652 OperandTraits::op_begin(this),
17871653 2, InsertAtEnd) {
1788 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1789 typename std::iterator_traits
1790 ::iterator_category());
1654 init(Agg, Val, Idxs, NameStr);
17911655 }
17921656
17931657 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
209209 return ConstantExpr::getShuffleVector(V1, V2, Mask);
210210 }
211211
212 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
213 unsigned NumIdx) const {
214 return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
212 Constant *CreateExtractValue(Constant *Agg,
213 ArrayRef IdxList) const {
214 return ConstantExpr::getExtractValue(Agg, IdxList);
215215 }
216216
217217 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
218 const unsigned *IdxList, unsigned NumIdx) const {
219 return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
218 ArrayRef IdxList) const {
219 return ConstantExpr::getInsertValue(Agg, Val, IdxList);
220220 }
221221 };
222222
11931193 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
11941194 }
11951195
1196 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1196 Value *CreateExtractValue(Value *Agg,
1197 ArrayRef Idxs,
11971198 const Twine &Name = "") {
11981199 if (Constant *AggC = dyn_cast(Agg))
1199 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1200 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1201 }
1202
1203 template
1204 Value *CreateExtractValue(Value *Agg,
1205 RandomAccessIterator IdxBegin,
1206 RandomAccessIterator IdxEnd,
1207 const Twine &Name = "") {
1208 if (Constant *AggC = dyn_cast(Agg))
1209 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1210 Name);
1211 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1212 }
1213
1214 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1200 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1201 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1202 }
1203
1204 Value *CreateInsertValue(Value *Agg, Value *Val,
1205 ArrayRef Idxs,
12151206 const Twine &Name = "") {
12161207 if (Constant *AggC = dyn_cast(Agg))
12171208 if (Constant *ValC = dyn_cast(Val))
1218 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1219 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1220 }
1221
1222 template
1223 Value *CreateInsertValue(Value *Agg, Value *Val,
1224 RandomAccessIterator IdxBegin,
1225 RandomAccessIterator IdxEnd,
1226 const Twine &Name = "") {
1227 if (Constant *AggC = dyn_cast(Agg))
1228 if (Constant *ValC = dyn_cast(Val))
1229 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1230 IdxEnd - IdxBegin),
1231 Name);
1232 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1209 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1210 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
12331211 }
12341212
12351213 //===--------------------------------------------------------------------===//
2121 #ifndef LLVM_SUPPORT_NOFOLDER_H
2222 #define LLVM_SUPPORT_NOFOLDER_H
2323
24 #include "llvm/ADT/ArrayRef.h"
2425 #include "llvm/Constants.h"
2526 #include "llvm/Instructions.h"
2627
268269 return new ShuffleVectorInst(V1, V2, Mask);
269270 }
270271
271 Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
272 unsigned NumIdx) const {
273 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
272 Instruction *CreateExtractValue(Constant *Agg,
273 ArrayRef IdxList) const {
274 return ExtractValueInst::Create(Agg, IdxList);
274275 }
275276
276277 Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
277 const unsigned *IdxList,
278 unsigned NumIdx) const {
279 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
278 ArrayRef IdxList) const {
279 return InsertValueInst::Create(Agg, Val, IdxList);
280280 }
281281 };
282282
2020
2121 #include "llvm/Constants.h"
2222 #include "llvm/InstrTypes.h"
23 #include "llvm/ADT/ArrayRef.h"
2324 #include "llvm/Analysis/ConstantFolding.h"
2425
2526 namespace llvm {
225226 return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
226227 }
227228
228 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
229 unsigned NumIdx) const {
230 return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
229 Constant *CreateExtractValue(Constant *Agg,
230 ArrayRef IdxList) const {
231 return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
231232 }
232233
233234 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
234 const unsigned *IdxList, unsigned NumIdx) const {
235 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));
235 ArrayRef IdxList) const {
236 return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
236237 }
237238 };
238239
770770 return ConstantExpr::getInsertValue(
771771 cast(IVI->getAggregateOperand()),
772772 cast(IVI->getInsertedValueOperand()),
773 IVI->idx_begin(), IVI->getNumIndices());
773 IVI->getIndices());
774774
775775 if (ExtractValueInst *EVI = dyn_cast(I))
776776 return ConstantExpr::getExtractValue(
777777 cast(EVI->getAggregateOperand()),
778 EVI->idx_begin(), EVI->getNumIndices());
778 EVI->getIndices());
779779
780780 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
781781 Ops.data(), Ops.size(), TD);
591591 return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
592592 } else if (ExtractValueInst *Ex = dyn_cast(V)) {
593593 if (Value *W = FindInsertedValue(Ex->getAggregateOperand(),
594 Ex->idx_begin(),
595 Ex->idx_end()))
594 Ex->getIndices()))
596595 if (W != V)
597596 return findValueImpl(W, OffsetOk, Visited);
598597 } else if (ConstantExpr *CE = dyn_cast(V)) {
606605 return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
607606 } else if (CE->getOpcode() == Instruction::ExtractValue) {
608607 ArrayRef Indices = CE->getIndices();
609 if (Value *W = FindInsertedValue(CE->getOperand(0),
610 Indices.begin(),
611 Indices.end()))
608 if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
612609 if (W != V)
613610 return findValueImpl(W, OffsetOk, Visited);
614611 }
13511351 // we might be able to find the complete struct somewhere.
13521352
13531353 // Find the value that is at that particular spot
1354 Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end());
1354 Value *V = FindInsertedValue(From, Idxs);
13551355
13561356 if (!V)
13571357 return NULL;
13581358
13591359 // Insert the value in the new (sub) aggregrate
1360 return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip,
1361 Idxs.end(), "tmp", InsertBefore);
1360 return llvm::InsertValueInst::Create(To, V,
1361 ArrayRef(Idxs).slice(IdxSkip),
1362 "tmp", InsertBefore);
13621363 }
13631364
13641365 // This helper takes a nested struct and extracts a part of it (which is again a
13731374 // insertvalue instruction somewhere).
13741375 //
13751376 // All inserted insertvalue instructions are inserted before InsertBefore
1376 static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
1377 const unsigned *idx_end,
1377 static Value *BuildSubAggregate(Value *From, ArrayRef idx_range,
13781378 Instruction *InsertBefore) {
13791379 assert(InsertBefore && "Must have someplace to insert!");
13801380 const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
1381 idx_begin,
1382 idx_end);
1381 idx_range);
13831382 Value *To = UndefValue::get(IndexedType);
1384 SmallVector Idxs(idx_begin, idx_end);
1383 SmallVector Idxs(idx_range.begin(), idx_range.end());
13851384 unsigned IdxSkip = Idxs.size();
13861385
13871386 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
13931392 ///
13941393 /// If InsertBefore is not null, this function will duplicate (modified)
13951394 /// insertvalues when a part of a nested struct is extracted.
1396 Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
1397 const unsigned *idx_end, Instruction *InsertBefore) {
1395 Value *llvm::FindInsertedValue(Value *V, ArrayRef idx_range,
1396 Instruction *InsertBefore) {
13981397 // Nothing to index? Just return V then (this is useful at the end of our
13991398 // recursion)
1400 if (idx_begin == idx_end)
1399 if (idx_range.empty())
14011400 return V;
14021401 // We have indices, so V should have an indexable type
14031402 assert((V->getType()->isStructTy() || V->getType()->isArrayTy())
14041403 && "Not looking at a struct or array?");
1405 assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end)
1404 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range)
14061405 && "Invalid indices for type?");
14071406 const CompositeType *PTy = cast(V->getType());
14081407
14091408 if (isa(V))
14101409 return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
1411 idx_begin,
1412 idx_end));
1410 idx_range));
14131411 else if (isa(V))
14141412 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
1415 idx_begin,
1416 idx_end));
1413 idx_range));
14171414 else if (Constant *C = dyn_cast(V)) {
14181415 if (isa(C) || isa(C))
14191416 // Recursively process this constant
1420 return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1,
1421 idx_end, InsertBefore);
1417 return FindInsertedValue(C->getOperand(idx_range[0]), idx_range.slice(1),
1418 InsertBefore);
14221419 } else if (InsertValueInst *I = dyn_cast(V)) {
14231420 // Loop the indices for the insertvalue instruction in parallel with the
14241421 // requested indices
1425 const unsigned *req_idx = idx_begin;
1422 const unsigned *req_idx = idx_range.begin();
14261423 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
14271424 i != e; ++i, ++req_idx) {
1428 if (req_idx == idx_end) {
1425 if (req_idx == idx_range.end()) {
14291426 if (InsertBefore)
14301427 // The requested index identifies a part of a nested aggregate. Handle
14311428 // this specially. For example,
14371434 // %C = insertvalue {i32, i32 } %A, i32 11, 1
14381435 // which allows the unused 0,0 element from the nested struct to be
14391436 // removed.
1440 return BuildSubAggregate(V, idx_begin, req_idx, InsertBefore);
1437 return BuildSubAggregate(V,
1438 ArrayRef(idx_range.begin(),
1439 req_idx),
1440 InsertBefore);
14411441 else
14421442 // We can't handle this without inserting insertvalues
14431443 return 0;
14471447 // See if the (aggregrate) value inserted into has the value we are
14481448 // looking for, then.
14491449 if (*req_idx != *i)
1450 return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end,
1450 return FindInsertedValue(I->getAggregateOperand(), idx_range,
14511451 InsertBefore);
14521452 }
14531453 // If we end up here, the indices of the insertvalue match with those
14541454 // requested (though possibly only partially). Now we recursively look at
14551455 // the inserted value, passing any remaining indices.
1456 return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end,
1456 return FindInsertedValue(I->getInsertedValueOperand(),
1457 ArrayRef(req_idx, idx_range.end()),
14571458 InsertBefore);
14581459 } else if (ExtractValueInst *I = dyn_cast(V)) {
14591460 // If we're extracting a value from an aggregrate that was extracted from
14611462 // However, we will need to chain I's indices with the requested indices.
14621463
14631464 // Calculate the number of indices required
1464 unsigned size = I->getNumIndices() + (idx_end - idx_begin);
1465 unsigned size = I->getNumIndices() + idx_range.size();
14651466 // Allocate some space to put the new indices in
14661467 SmallVector Idxs;
14671468 Idxs.reserve(size);
14681469 // Add indices from the extract value instruction
1469 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
1470 i != e; ++i)
1471 Idxs.push_back(*i);
1470 Idxs.append(I->idx_begin(), I->idx_end());
14721471
14731472 // Add requested indices
1474 for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i)
1475 Idxs.push_back(*i);
1473 Idxs.append(idx_range.begin(), idx_range.end());
14761474
14771475 assert(Idxs.size() == size
14781476 && "Number of indices added not correct?");
14791477
1480 return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(),
1481 InsertBefore);
1478 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
14821479 }
14831480 // Otherwise, we don't know (such as, extracting from a function return value
14841481 // or load instruction)
20852085
20862086 if (!Val->getType()->isAggregateType())
20872087 return Error(ID.Loc, "extractvalue operand must be aggregate type");
2088 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2089 Indices.end()))
2088 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
20902089 return Error(ID.Loc, "invalid indices for extractvalue");
2091 ID.ConstantVal =
2092 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());
2090 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
20932091 ID.Kind = ValID::t_Constant;
20942092 return false;
20952093 }
21062104 return true;
21072105 if (!Val0->getType()->isAggregateType())
21082106 return Error(ID.Loc, "insertvalue operand must be aggregate type");
2109 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2110 Indices.end()))
2107 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
21112108 return Error(ID.Loc, "invalid indices for insertvalue");
2112 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
2113 Indices.data(), Indices.size());
2109 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
21142110 ID.Kind = ValID::t_Constant;
21152111 return false;
21162112 }
36893685 if (!Val->getType()->isAggregateType())
36903686 return Error(Loc, "extractvalue operand must be aggregate type");
36913687
3692 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3693 Indices.end()))
3688 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
36943689 return Error(Loc, "invalid indices for extractvalue");
3695 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
3690 Inst = ExtractValueInst::Create(Val, Indices);
36963691 return AteExtraComma ? InstExtraComma : InstNormal;
36973692 }
36983693
37113706 if (!Val0->getType()->isAggregateType())
37123707 return Error(Loc0, "insertvalue operand must be aggregate type");
37133708
3714 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3715 Indices.end()))
3709 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
37163710 return Error(Loc0, "invalid indices for insertvalue");
3717 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
3711 Inst = InsertValueInst::Create(Val0, Val1, Indices);
37183712 return AteExtraComma ? InstExtraComma : InstNormal;
37193713 }
37203714
22052205 EXTRACTVALIdx.push_back((unsigned)Index);
22062206 }
22072207
2208 I = ExtractValueInst::Create(Agg,
2209 EXTRACTVALIdx.begin(), EXTRACTVALIdx.end());
2208 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
22102209 InstructionList.push_back(I);
22112210 break;
22122211 }
22302229 INSERTVALIdx.push_back((unsigned)Index);
22312230 }
22322231
2233 I = InsertValueInst::Create(Agg, Val,
2234 INSERTVALIdx.begin(), INSERTVALIdx.end());
2232 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
22352233 InstructionList.push_back(I);
22362234 break;
22372235 }
851851 return false; // fast-isel can't handle aggregate constants at the moment
852852
853853 // Get the actual result register, which is an offset from the base register.
854 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->idx_begin(), EVI->idx_end());
854 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
855855
856856 SmallVector AggValueVTs;
857857 ComputeValueVTs(TLI, AggTy, AggValueVTs);
28822882 bool IntoUndef = isa(Op0);
28832883 bool FromUndef = isa(Op1);
28842884
2885 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
2885 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
28862886
28872887 SmallVector AggValueVTs;
28882888 ComputeValueVTs(TLI, AggTy, AggValueVTs);
29222922 const Type *ValTy = I.getType();
29232923 bool OutOfUndef = isa(Op0);
29242924
2925 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.idx_begin(), I.idx_end());
2925 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
29262926
29272927 SmallVector ValValueVTs;
29282928 ComputeValueVTs(TLI, ValTy, ValValueVTs);
35593559 for (const unsigned *b = IVI.idx_begin(), *i = b, *e = IVI.idx_end();
35603560 i != e; ++i) {
35613561 const Type *IndexedTy =
3562 ExtractValueInst::getIndexedType(IVI.getOperand(0)->getType(), b, i+1);
3562 ExtractValueInst::getIndexedType(IVI.getOperand(0)->getType(),
3563 ArrayRef(b, i+1));
35633564 if (IndexedTy->isArrayTy())
35643565 Out << ".array[" << *i << "]";
35653566 else
35803581 for (const unsigned *b = EVI.idx_begin(), *i = b, *e = EVI.idx_end();
35813582 i != e; ++i) {
35823583 const Type *IndexedTy =
3583 ExtractValueInst::getIndexedType(EVI.getOperand(0)->getType(), b, i+1);
3584 ExtractValueInst::getIndexedType(EVI.getOperand(0)->getType(),
3585 ArrayRef(b, i+1));
35843586 if (IndexedTy->isArrayTy())
35853587 Out << ".array[" << *i << "]";
35863588 else
277277
278278 // If this is indexing an array of structures, get the structure element.
279279 if (!LaterIndices.empty())
280 Elt = ConstantExpr::getExtractValue(Elt, LaterIndices.data(),
281 LaterIndices.size());
280 Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
282281
283282 // If the element is masked, handle it.
284283 if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
11981198 if (EV.getNumIndices() > 1)
11991199 // Extract the remaining indices out of the constant indexed by the
12001200 // first index
1201 return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end());
1201 return ExtractValueInst::Create(V, EV.getIndices().slice(1));
12021202 else
12031203 return ReplaceInstUsesWith(EV, V);
12041204 }
12211221 // with
12221222 // %E = extractvalue { i32, { i32 } } %A, 0
12231223 return ExtractValueInst::Create(IV->getAggregateOperand(),
1224 EV.idx_begin(), EV.idx_end());
1224 EV.getIndices());
12251225 }
12261226 if (exti == exte && insi == inse)
12271227 // Both iterators are at the end: Index lists are identical. Replace
12391239 // by switching the order of the insert and extract (though the
12401240 // insertvalue should be left in, since it may have other uses).
12411241 Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
1242 EV.idx_begin(), EV.idx_end());
1242 EV.getIndices());
12431243 return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
1244 insi, inse);
1244 ArrayRef(insi, inse));
12451245 }
12461246 if (insi == inse)
12471247 // The insert list is a prefix of the extract list
12531253 // with
12541254 // %E extractvalue { i32 } { i32 42 }, 0
12551255 return ExtractValueInst::Create(IV->getInsertedValueOperand(),
1256 exti, exte);
1256 ArrayRef(exti, exte));
12571257 }
12581258 if (IntrinsicInst *II = dyn_cast(Agg)) {
12591259 // We're extracting from an intrinsic, see if we're the only user, which
879879 }
880880
881881 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
882 const unsigned *Idxs,
883 unsigned NumIdx) {
882 ArrayRef Idxs) {
884883 // Base case: no indices, so return the entire value.
885 if (NumIdx == 0)
884 if (Idxs.empty())
886885 return Agg;
887886
888887 if (isa(Agg)) // ev(undef, x) -> undef
889888 return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
890 Idxs,
891 Idxs + NumIdx));
889 Idxs));
892890
893891 if (isa(Agg)) // ev(0, x) -> 0
894892 return
895893 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
896 Idxs,
897 Idxs + NumIdx));
894 Idxs));
898895
899896 // Otherwise recurse.
900897 if (ConstantStruct *CS = dyn_cast(Agg))
901 return ConstantFoldExtractValueInstruction(CS->getOperand(*Idxs),
902 Idxs+1, NumIdx-1);
898 return ConstantFoldExtractValueInstruction(CS->getOperand(Idxs[0]),
899 Idxs.slice(1));
903900
904901 if (ConstantArray *CA = dyn_cast(Agg))
905 return ConstantFoldExtractValueInstruction(CA->getOperand(*Idxs),
906 Idxs+1, NumIdx-1);
902 return ConstantFoldExtractValueInstruction(CA->getOperand(Idxs[0]),
903 Idxs.slice(1));
907904 ConstantVector *CV = cast(Agg);
908 return ConstantFoldExtractValueInstruction(CV->getOperand(*Idxs),
909 Idxs+1, NumIdx-1);
905 return ConstantFoldExtractValueInstruction(CV->getOperand(Idxs[0]),
906 Idxs.slice(1));
910907 }
911908
912909 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
913910 Constant *Val,
914 const unsigned *Idxs,
915 unsigned NumIdx) {
911 ArrayRef Idxs) {
916912 // Base case: no indices, so replace the entire value.
917 if (NumIdx == 0)
913 if (Idxs.empty())
918914 return Val;
919915
920916 if (isa(Agg)) {
936932 for (unsigned i = 0; i < numOps; ++i) {
937933 const Type *MemberTy = AggTy->getTypeAtIndex(i);
938934 Constant *Op =
939 (*Idxs == i) ?
935 (Idxs[0] == i) ?
940936 ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
941 Val, Idxs+1, NumIdx-1) :
937 Val, Idxs.slice(1)) :
942938 UndefValue::get(MemberTy);
943939 Ops[i] = Op;
944940 }
967963 for (unsigned i = 0; i < numOps; ++i) {
968964 const Type *MemberTy = AggTy->getTypeAtIndex(i);
969965 Constant *Op =
970 (*Idxs == i) ?
966 (Idxs[0] == i) ?
971967 ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
972 Val, Idxs+1, NumIdx-1) :
968 Val, Idxs.slice(1)) :
973969 Constant::getNullValue(MemberTy);
974970 Ops[i] = Op;
975971 }
984980 std::vector Ops(Agg->getNumOperands());
985981 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
986982 Constant *Op = cast(Agg->getOperand(i));
987 if (*Idxs == i)
988 Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs+1, NumIdx-1);
983 if (Idxs[0] == i)
984 Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs.slice(1));
989985 Ops[i] = Op;
990986 }
991987
1818 #ifndef CONSTANTFOLDING_H
1919 #define CONSTANTFOLDING_H
2020
21 #include "llvm/ADT/ArrayRef.h"
22
2123 namespace llvm {
2224 class Value;
2325 class Constant;
3739 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
3840 Constant *Mask);
3941 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
40 const unsigned *Idxs,
41 unsigned NumIdx);
42 ArrayRef Idxs);
4243 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
43 const unsigned *Idxs,
44 unsigned NumIdx);
44 ArrayRef Idxs);
4545 Constant *ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1,
4646 Constant *V2);
4747 Constant *ConstantFoldCompareInstruction(unsigned short predicate,
17141714 }
17151715
17161716 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
1717 const unsigned *Idxs, unsigned NumIdx) {
1718 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1719 Idxs+NumIdx) == Val->getType() &&
1717 ArrayRef Idxs) {
1718 assert(ExtractValueInst::getIndexedType(Agg->getType(),
1719 Idxs) == Val->getType() &&
17201720 "insertvalue indices invalid!");
17211721 assert(Agg->getType()->isFirstClassType() &&
17221722 "Non-first-class type for constant insertvalue expression");
1723 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
1723 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs);
17241724 assert(FC && "insertvalue constant expr couldn't be folded!");
17251725 return FC;
17261726 }
17271727
17281728 Constant *ConstantExpr::getExtractValue(Constant *Agg,
1729 const unsigned *Idxs, unsigned NumIdx) {
1729 ArrayRef Idxs) {
17301730 assert(Agg->getType()->isFirstClassType() &&
17311731 "Tried to create extractelement operation on non-first-class type!");
17321732
1733 const Type *ReqTy =
1734 ExtractValueInst::getIndexedType(Agg->getType(), Idxs, Idxs+NumIdx);
1733 const Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
17351734 (void)ReqTy;
17361735 assert(ReqTy && "extractvalue indices invalid!");
17371736
17381737 assert(Agg->getType()->isFirstClassType() &&
17391738 "Non-first-class type for constant extractvalue expression");
1740 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
1739 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs);
17411740 assert(FC && "ExtractValue constant expr couldn't be folded!");
17421741 return FC;
17431742 }
20852084 if (Agg == From) Agg = To;
20862085
20872086 ArrayRef Indices = getIndices();
2088 Replacement = ConstantExpr::getExtractValue(Agg,
2089 &Indices[0], Indices.size());
2087 Replacement = ConstantExpr::getExtractValue(Agg, Indices);
20902088 } else if (getOpcode() == Instruction::InsertValue) {
20912089 Constant *Agg = getOperand(0);
20922090 Constant *Val = getOperand(1);
20942092 if (Val == From) Val = To;
20952093
20962094 ArrayRef Indices = getIndices();
2097 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2098 &Indices[0], Indices.size());
2095 Replacement = ConstantExpr::getInsertValue(Agg, Val, Indices);
20992096 } else if (isCast()) {
21002097 assert(getOperand(0) == From && "Cast only has one use!");
21012098 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
912912 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
913913 unsigned NumIdx) {
914914 return wrap(ConstantExpr::getExtractValue(unwrap(AggConstant),
915 IdxList, NumIdx));
915 ArrayRef(IdxList,
916 NumIdx)));
916917 }
917918
918919 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
920921 unsigned *IdxList, unsigned NumIdx) {
921922 return wrap(ConstantExpr::getInsertValue(unwrap(AggConstant),
922923 unwrap(ElementValueConstant),
923 IdxList, NumIdx));
924 ArrayRef(IdxList,
925 NumIdx)));
924926 }
925927
926928 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
203203 if (const InvokeInst *CI = dyn_cast(this))
204204 return CI->getCallingConv() == cast(I)->getCallingConv() &&
205205 CI->getAttributes() == cast(I)->getAttributes();
206 if (const InsertValueInst *IVI = dyn_cast(this)) {
207 if (IVI->getNumIndices() != cast(I)->getNumIndices())
208 return false;
209 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
210 if (IVI->idx_begin()[i] != cast(I)->idx_begin()[i])
211 return false;
212 return true;
213 }
214 if (const ExtractValueInst *EVI = dyn_cast(this)) {
215 if (EVI->getNumIndices() != cast(I)->getNumIndices())
216 return false;
217 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
218 if (EVI->idx_begin()[i] != cast(I)->idx_begin()[i])
219 return false;
220 return true;
221 }
206 if (const InsertValueInst *IVI = dyn_cast(this))
207 return IVI->getIndices() == cast(I)->getIndices();
208 if (const ExtractValueInst *EVI = dyn_cast(this))
209 return EVI->getIndices() == cast(I)->getIndices();
222210
223211 return true;
224212 }
255243 return CI->getCallingConv() == cast(I)->getCallingConv() &&
256244 CI->getAttributes() ==
257245 cast(I)->getAttributes();
258 if (const InsertValueInst *IVI = dyn_cast(this)) {
259 if (IVI->getNumIndices() != cast(I)->getNumIndices())
260 return false;
261 for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
262 if (IVI->idx_begin()[i] != cast(I)->idx_begin()[i])
263 return false;
264 return true;
265 }
266 if (const ExtractValueInst *EVI = dyn_cast(this)) {
267 if (EVI->getNumIndices() != cast(I)->getNumIndices())
268 return false;
269 for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
270 if (EVI->idx_begin()[i] != cast(I)->idx_begin()[i])
271 return false;
272 return true;
273 }
246 if (const InsertValueInst *IVI = dyn_cast(this))
247 return IVI->getIndices() == cast(I)->getIndices();
248 if (const ExtractValueInst *EVI = dyn_cast(this))
249 return EVI->getIndices() == cast(I)->getIndices();
274250
275251 return true;
276252 }
13851385 // InsertValueInst Class
13861386 //===----------------------------------------------------------------------===//
13871387
1388 void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1389 unsigned NumIdx, const Twine &Name) {
1388 void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef Idxs,
1389 const Twine &Name) {
13901390 assert(NumOperands == 2 && "NumOperands not initialized?");
1391 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1391
1392 // There's no fundamental reason why we require at least one index
1393 // (other than weirdness with &*IdxBegin being invalid; see
1394 // getelementptr's init routine for example). But there's no
1395 // present need to support it.
1396 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1397
1398 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
13921399 Val->getType() && "Inserted value must match indexed type!");
13931400 Op<0>() = Agg;
13941401 Op<1>() = Val;
13951402
1396 Indices.append(Idx, Idx + NumIdx);
1397 setName(Name);
1398 }
1399
1400 void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1401 const Twine &Name) {
1402 assert(NumOperands == 2 && "NumOperands not initialized?");
1403 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1404 && "Inserted value must match indexed type!");
1405 Op<0>() = Agg;
1406 Op<1>() = Val;
1407
1408 Indices.push_back(Idx);
1403 Indices.append(Idxs.begin(), Idxs.end());
14091404 setName(Name);
14101405 }
14111406
14181413 SubclassOptionalData = IVI.SubclassOptionalData;
14191414 }
14201415
1421 InsertValueInst::InsertValueInst(Value *Agg,
1422 Value *Val,
1423 unsigned Idx,
1424 const Twine &Name,
1425 Instruction *InsertBefore)
1426 : Instruction(Agg->getType(), InsertValue,
1427 OperandTraits::op_begin(this),
1428 2, InsertBefore) {
1429 init(Agg, Val, Idx, Name);
1430 }
1431
1432 InsertValueInst::InsertValueInst(Value *Agg,
1433 Value *Val,
1434 unsigned Idx,
1435 const Twine &Name,
1436 BasicBlock *InsertAtEnd)
1437 : Instruction(Agg->getType(), InsertValue,
1438 OperandTraits::op_begin(this),
1439 2, InsertAtEnd) {
1440 init(Agg, Val, Idx, Name);
1441 }
1442
14431416 //===----------------------------------------------------------------------===//
14441417 // ExtractValueInst Class
14451418 //===----------------------------------------------------------------------===//
14461419
1447 void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
1448 const Twine &Name) {
1420 void ExtractValueInst::init(ArrayRef Idxs, const Twine &Name) {
14491421 assert(NumOperands == 1 && "NumOperands not initialized?");
14501422
1451 Indices.append(Idx, Idx + NumIdx);
1452 setName(Name);
1453 }
1454
1455 void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
1456 assert(NumOperands == 1 && "NumOperands not initialized?");
1457
1458 Indices.push_back(Idx);
1423 // There's no fundamental reason why we require at least one index.
1424 // But there's no present need to support it.
1425 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
1426
1427 Indices.append(Idxs.begin(), Idxs.end());
14591428 setName(Name);
14601429 }
14611430
14721441 // pointer type.
14731442 //
14741443 Type *ExtractValueInst::getIndexedType(const Type *Agg,
1475 const unsigned *Idxs,
1476 unsigned NumIdx) {
1477 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
1444 ArrayRef Idxs) {
1445 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
14781446 unsigned Index = Idxs[CurIdx];
14791447 // We can't use CompositeType::indexValid(Index) here.
14801448 // indexValid() always returns true for arrays because getelementptr allows
14961464 Agg = cast(Agg)->getTypeAtIndex(Index);
14971465 }
14981466 return const_cast(Agg);
1499 }
1500
1501 Type *ExtractValueInst::getIndexedType(const Type *Agg, unsigned Idx) {
1502 return getIndexedType(Agg, &Idx, 1);
15031467 }
15041468
15051469 //===----------------------------------------------------------------------===//
13171317
13181318 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
13191319 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
1320 EVI.idx_begin(), EVI.idx_end()) ==
1320 EVI.getIndices()) ==
13211321 EVI.getType(),
13221322 "Invalid ExtractValueInst operands!", &EVI);
13231323
13261326
13271327 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
13281328 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
1329 IVI.idx_begin(), IVI.idx_end()) ==
1329 IVI.getIndices()) ==
13301330 IVI.getOperand(1)->getType(),
13311331 "Invalid InsertValueInst operands!", &IVI);
13321332