llvm.org GIT mirror llvm / 18c7f80
reapply the patches reverted in r149470 that reenable ConstantDataArray, but with a critical fix to the SelectionDAG code that optimizes copies from strings into immediate stores: the previous code was stopping reading string data at the first nul. Address this by adding a new argument to llvm::getConstantStringInfo, preserving the behavior before the patch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149800 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 7 years ago
19 changed file(s) with 291 addition(s) and 554 deletion(s). Raw diff Collapse all Expand all
1616
1717 #include "llvm/ADT/ArrayRef.h"
1818 #include "llvm/Support/DataTypes.h"
19 #include
2019
2120 namespace llvm {
22 template class SmallVectorImpl;
2321 class Value;
2422 class Instruction;
2523 class APInt;
2624 class TargetData;
25 class StringRef;
2726
2827 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
2928 /// known to be either zero or one and return them in the KnownZero/KnownOne
124123 return GetPointerBaseWithConstantOffset(const_cast(Ptr), Offset,TD);
125124 }
126125
127 /// GetConstantStringInfo - This function computes the length of a
126 /// getConstantStringInfo - This function computes the length of a
128127 /// null-terminated C string pointed to by V. If successful, it returns true
129 /// and returns the string in Str. If unsuccessful, it returns false. If
130 /// StopAtNul is set to true (the default), the returned string is truncated
131 /// by a nul character in the global. If StopAtNul is false, the nul
132 /// character is included in the result string.
133 bool GetConstantStringInfo(const Value *V, std::string &Str,
134 uint64_t Offset = 0,
135 bool StopAtNul = true);
136
128 /// and returns the string in Str. If unsuccessful, it returns false. This
129 /// does not include the trailing nul character by default. If TrimAtNul is
130 /// set to false, then this returns any trailing nul characters as well as any
131 /// other characters that come after it.
132 bool getConstantStringInfo(const Value *V, StringRef &Str,
133 uint64_t Offset = 0, bool TrimAtNul = true);
134
137135 /// GetStringLength - If we can compute the length of the string pointed to by
138136 /// the specified pointer, return 'len+1'. If we can't, return 0.
139137 uint64_t GetStringLength(Value *V);
351351 // ConstantArray accessors
352352 static Constant *get(ArrayType *T, ArrayRef V);
353353
354 /// This method constructs a ConstantArray and initializes it with a text
355 /// string. The default behavior (AddNull==true) causes a null terminator to
356 /// be placed at the end of the array. This effectively increases the length
357 /// of the array by one (you've been warned). However, in some situations
358 /// this is not desired so if AddNull==false then the string is copied without
359 /// null termination.
360
361 // FIXME Remove this.
362 static Constant *get(LLVMContext &Context, StringRef Initializer,
363 bool AddNull = true);
364
365354 /// Transparently provide more efficient getOperand methods.
366355 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
367356
371360 inline ArrayType *getType() const {
372361 return reinterpret_cast(Value::getType());
373362 }
374
375 // FIXME: String methods will eventually be removed.
376
377
378 /// isString - This method returns true if the array is an array of i8 and
379 /// the elements of the array are all ConstantInt's.
380 bool isString() const;
381
382 /// isCString - This method returns true if the array is a string (see
383 /// @verbatim
384 /// isString) and it ends in a null byte \0 and does not contains any other
385 /// @endverbatim
386 /// null bytes except its terminator.
387 bool isCString() const;
388
389 /// getAsString - If this array is isString(), then this method converts the
390 /// array to an std::string and returns it. Otherwise, it asserts out.
391 ///
392 std::string getAsString() const;
393
394 /// getAsCString - If this array is isCString(), then this method converts the
395 /// array (without the trailing null byte) to an std::string and returns it.
396 /// Otherwise, it asserts out.
397 ///
398 std::string getAsCString() const;
399363
400364 virtual void destroyConstant();
401365 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
475475
476476 // Instead of loading constant c string, use corresponding integer value
477477 // directly if string length is small enough.
478 std::string Str;
479 if (TD && GetConstantStringInfo(CE, Str) && !Str.empty()) {
480 unsigned StrLen = Str.length();
478 StringRef Str;
479 if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) {
480 unsigned StrLen = Str.size();
481481 Type *Ty = cast(CE->getType())->getElementType();
482482 unsigned NumBits = Ty->getPrimitiveSizeInBits();
483483 // Replace load with immediate integer if the result is an integer or fp
13681368 }
13691369 }
13701370
1371 // A ConstantArray is splatable if all its members are equal and also
1372 // splatable.
1373 if (ConstantArray *CA = dyn_cast(V)) {
1374 if (CA->getNumOperands() == 0)
1375 return 0;
1376
1377 Value *Val = isBytewiseValue(CA->getOperand(0));
1371 // A ConstantDataArray/Vector is splatable if all its members are equal and
1372 // also splatable.
1373 if (ConstantDataSequential *CA = dyn_cast(V)) {
1374 Value *Elt = CA->getElementAsConstant(0);
1375 Value *Val = isBytewiseValue(Elt);
13781376 if (!Val)
13791377 return 0;
13801378
1381 for (unsigned I = 1, E = CA->getNumOperands(); I != E; ++I)
1382 if (CA->getOperand(I-1) != CA->getOperand(I))
1379 for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
1380 if (CA->getElementAsConstant(I) != Elt)
13831381 return 0;
13841382
13851383 return Val;
13861384 }
13871385
1388 // FIXME: Vector types (e.g., <4 x i32> ).
1389
13901386 // Conceptually, we could handle things like:
13911387 // %a = zext i8 %X to i16
13921388 // %b = shl i16 %a, 8
16061602 }
16071603
16081604
1609 /// GetConstantStringInfo - This function computes the length of a
1605 /// getConstantStringInfo - This function computes the length of a
16101606 /// null-terminated C string pointed to by V. If successful, it returns true
16111607 /// and returns the string in Str. If unsuccessful, it returns false.
1612 bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
1613 uint64_t Offset, bool StopAtNul) {
1614 // If V is NULL then return false;
1615 if (V == NULL) return false;
1616
1617 // Look through bitcast instructions.
1618 if (const BitCastInst *BCI = dyn_cast(V))
1619 return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul);
1620
1621 // If the value is not a GEP instruction nor a constant expression with a
1622 // GEP instruction, then return false because ConstantArray can't occur
1623 // any other way.
1624 const User *GEP = 0;
1625 if (const GetElementPtrInst *GEPI = dyn_cast(V)) {
1626 GEP = GEPI;
1627 } else if (const ConstantExpr *CE = dyn_cast(V)) {
1628 if (CE->getOpcode() == Instruction::BitCast)
1629 return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul);
1630 if (CE->getOpcode() != Instruction::GetElementPtr)
1631 return false;
1632 GEP = CE;
1633 }
1634
1635 if (GEP) {
1608 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
1609 uint64_t Offset, bool TrimAtNul) {
1610 assert(V);
1611
1612 // Look through bitcast instructions and geps.
1613 V = V->stripPointerCasts();
1614
1615 // If the value is a GEP instructionor constant expression, treat it as an
1616 // offset.
1617 if (const GEPOperator *GEP = dyn_cast(V)) {
16361618 // Make sure the GEP has exactly three arguments.
16371619 if (GEP->getNumOperands() != 3)
16381620 return false;
16571639 StartIdx = CI->getZExtValue();
16581640 else
16591641 return false;
1660 return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
1661 StopAtNul);
1642 return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset);
16621643 }
16631644
16641645 // The GEP instruction, constant or instruction, must reference a global
16651646 // variable that is a constant and is initialized. The referenced constant
16661647 // initializer is the array that we'll use for optimization.
1667 const GlobalVariable* GV = dyn_cast(V);
1648 const GlobalVariable *GV = dyn_cast(V);
16681649 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
16691650 return false;
1670 const Constant *GlobalInit = GV->getInitializer();
1671
1651
16721652 // Handle the all-zeros case
1673 if (GlobalInit->isNullValue()) {
1653 if (GV->getInitializer()->isNullValue()) {
16741654 // This is a degenerate case. The initializer is constant zero so the
16751655 // length of the string must be zero.
1676 Str.clear();
1656 Str = "";
16771657 return true;
16781658 }
16791659
16801660 // Must be a Constant Array
1681 const ConstantArray *Array = dyn_cast(GlobalInit);
1682 if (Array == 0 || !Array->getType()->getElementType()->isIntegerTy(8))
1661 const ConstantDataArray *Array =
1662 dyn_cast(GV->getInitializer());
1663 if (Array == 0 || !Array->isString())
16831664 return false;
16841665
16851666 // Get the number of elements in the array
1686 uint64_t NumElts = Array->getType()->getNumElements();
1687
1667 uint64_t NumElts = Array->getType()->getArrayNumElements();
1668
1669 // Start out with the entire array in the StringRef.
1670 Str = Array->getAsString();
1671
16881672 if (Offset > NumElts)
16891673 return false;
16901674
1691 // Traverse the constant array from 'Offset' which is the place the GEP refers
1692 // to in the array.
1693 Str.reserve(NumElts-Offset);
1694 for (unsigned i = Offset; i != NumElts; ++i) {
1695 const Constant *Elt = Array->getOperand(i);
1696 const ConstantInt *CI = dyn_cast(Elt);
1697 if (!CI) // This array isn't suitable, non-int initializer.
1698 return false;
1699 if (StopAtNul && CI->isZero())
1700 return true; // we found end of string, success!
1701 Str += (char)CI->getZExtValue();
1702 }
1703
1704 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
1675 // Skip over 'offset' bytes.
1676 Str = Str.substr(Offset);
1677
1678 if (TrimAtNul) {
1679 // Trim off the \0 and anything after it. If the array is not nul
1680 // terminated, we just return the whole end of string. The client may know
1681 // some other way that the string is length-bound.
1682 Str = Str.substr(0, Str.find('\0'));
1683 }
17051684 return true;
17061685 }
17071686
17131692 /// the specified pointer, return 'len+1'. If we can't, return 0.
17141693 static uint64_t GetStringLengthH(Value *V, SmallPtrSet &PHIs) {
17151694 // Look through noop bitcast instructions.
1716 if (BitCastInst *BCI = dyn_cast(V))
1717 return GetStringLengthH(BCI->getOperand(0), PHIs);
1695 V = V->stripPointerCasts();
17181696
17191697 // If this is a PHI node, there are two cases: either we have already seen it
17201698 // or we haven't.
17501728 if (Len1 != Len2) return 0;
17511729 return Len1;
17521730 }
1753
1754 // As a special-case, "@string = constant i8 0" is also a string with zero
1755 // length, not wrapped in a bitcast or GEP.
1756 if (GlobalVariable *GV = dyn_cast(V)) {
1757 if (GV->isConstant() && GV->hasDefinitiveInitializer())
1758 if (GV->getInitializer()->isNullValue()) return 1;
1731
1732 // Otherwise, see if we can read the string.
1733 StringRef StrData;
1734 if (!getConstantStringInfo(V, StrData))
17591735 return 0;
1760 }
1761
1762 // If the value is not a GEP instruction nor a constant expression with a
1763 // GEP instruction, then return unknown.
1764 User *GEP = 0;
1765 if (GetElementPtrInst *GEPI = dyn_cast(V)) {
1766 GEP = GEPI;
1767 } else if (ConstantExpr *CE = dyn_cast(V)) {
1768 if (CE->getOpcode() != Instruction::GetElementPtr)
1769 return 0;
1770 GEP = CE;
1771 } else {
1772 return 0;
1773 }
1774
1775 // Make sure the GEP has exactly three arguments.
1776 if (GEP->getNumOperands() != 3)
1777 return 0;
1778
1779 // Check to make sure that the first operand of the GEP is an integer and
1780 // has value 0 so that we are sure we're indexing into the initializer.
1781 if (ConstantInt *Idx = dyn_cast(GEP->getOperand(1))) {
1782 if (!Idx->isZero())
1783 return 0;
1784 } else
1785 return 0;
1786
1787 // If the second index isn't a ConstantInt, then this is a variable index
1788 // into the array. If this occurs, we can't say anything meaningful about
1789 // the string.
1790 uint64_t StartIdx = 0;
1791 if (ConstantInt *CI = dyn_cast(GEP->getOperand(2)))
1792 StartIdx = CI->getZExtValue();
1793 else
1794 return 0;
1795
1796 // The GEP instruction, constant or instruction, must reference a global
1797 // variable that is a constant and is initialized. The referenced constant
1798 // initializer is the array that we'll use for optimization.
1799 GlobalVariable* GV = dyn_cast(GEP->getOperand(0));
1800 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
1801 GV->mayBeOverridden())
1802 return 0;
1803 Constant *GlobalInit = GV->getInitializer();
1804
1805 // Handle the ConstantAggregateZero case, which is a degenerate case. The
1806 // initializer is constant zero so the length of the string must be zero.
1807 if (isa(GlobalInit))
1808 return 1; // Len = 0 offset by 1.
1809
1810 // Must be a Constant Array
1811 ConstantArray *Array = dyn_cast(GlobalInit);
1812 if (!Array || !Array->getType()->getElementType()->isIntegerTy(8))
1813 return false;
1814
1815 // Get the number of elements in the array
1816 uint64_t NumElts = Array->getType()->getNumElements();
1817
1818 // Traverse the constant array from StartIdx (derived above) which is
1819 // the place the GEP refers to in the array.
1820 for (unsigned i = StartIdx; i != NumElts; ++i) {
1821 Constant *Elt = Array->getOperand(i);
1822 ConstantInt *CI = dyn_cast(Elt);
1823 if (!CI) // This array isn't suitable, non-int initializer.
1824 return 0;
1825 if (CI->isZero())
1826 return i-StartIdx+1; // We found end of string, success!
1827 }
1828
1829 return 0; // The array isn't null terminated, conservatively return 'unknown'.
1736
1737 return StrData.size()+1;
18301738 }
18311739
18321740 /// GetStringLength - If we can compute the length of the string pointed to by
20172017 }
20182018 case lltok::kw_c: // c "foo"
20192019 Lex.Lex();
2020 ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
2020 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2021 false);
20212022 if (ParseToken(lltok::StringConstant, "expected string")) return true;
20222023 ID.Kind = ValID::t_Constant;
20232024 return false;
844844 } else {
845845 assert (0 && "Unknown FP type!");
846846 }
847 } else if (isa(C) && cast(C)->isString()) {
848 const ConstantArray *CA = cast(C);
849 // Emit constant strings specially.
850 unsigned NumOps = CA->getNumOperands();
851 // If this is a null-terminated string, use the denser CSTRING encoding.
852 if (CA->getOperand(NumOps-1)->isNullValue()) {
853 Code = bitc::CST_CODE_CSTRING;
854 --NumOps; // Don't encode the null, which isn't allowed by char6.
855 } else {
856 Code = bitc::CST_CODE_STRING;
857 AbbrevToUse = String8Abbrev;
858 }
859 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
860 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
861 for (unsigned i = 0; i != NumOps; ++i) {
862 unsigned char V = cast(CA->getOperand(i))->getZExtValue();
863 Record.push_back(V);
864 isCStr7 &= (V & 128) == 0;
865 if (isCStrChar6)
866 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
867 }
868
869 if (isCStrChar6)
870 AbbrevToUse = CString6Abbrev;
871 else if (isCStr7)
872 AbbrevToUse = CString7Abbrev;
873847 } else if (isa(C) &&
874848 cast(C)->isString()) {
875849 const ConstantDataSequential *Str = cast(C);
320320 if (const Constant *C = dyn_cast(V)) {
321321 if (isa(C)) {
322322 // Initializers for globals are handled explicitly elsewhere.
323 } else if (isa(C) && cast(C)->isString()) {
324 // Do not enumerate the initializers for an array of simple characters.
325 // The initializers just pollute the value table, and we emit the strings
326 // specially.
327323 } else if (C->getNumOperands()) {
328324 // If a constant has operands, enumerate them. This makes sure that if a
329325 // constant has uses (for example an array of const ints), that they are
16831683
16841684 static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
16851685 AsmPrinter &AP) {
1686 if (AddrSpace != 0 || !CA->isString()) {
1687 // Not a string. Print the values in successive locations.
1688
1689 // See if we can aggregate some values. Make sure it can be
1690 // represented as a series of bytes of the constant value.
1691 int Value = isRepeatedByteSequence(CA, AP.TM);
1692
1693 if (Value != -1) {
1694 uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
1695 AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
1696 }
1697 else {
1698 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1699 EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
1700 }
1701 return;
1702 }
1703
1704 // Otherwise, it can be emitted as .ascii.
1705 SmallVector TmpVec;
1706 TmpVec.reserve(CA->getNumOperands());
1707 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1708 TmpVec.push_back(cast(CA->getOperand(i))->getZExtValue());
1709
1710 AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace);
1686 // See if we can aggregate some values. Make sure it can be
1687 // represented as a series of bytes of the constant value.
1688 int Value = isRepeatedByteSequence(CA, AP.TM);
1689
1690 if (Value != -1) {
1691 uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
1692 AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
1693 }
1694 else {
1695 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1696 EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
1697 }
17111698 }
17121699
17131700 static void EmitGlobalConstantVector(const ConstantVector *CV,
32973297 /// used when a memcpy is turned into a memset when the source is a constant
32983298 /// string ptr.
32993299 static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
3300 const TargetLowering &TLI,
3301 std::string &Str, unsigned Offset) {
3300 const TargetLowering &TLI, StringRef Str) {
33023301 // Handle vector with all elements zero.
33033302 if (Str.empty()) {
33043303 if (VT.isInteger())
33163315 }
33173316
33183317 assert(!VT.isVector() && "Can't handle vector type here!");
3319 unsigned NumBits = VT.getSizeInBits();
3320 unsigned MSB = NumBits / 8;
3318 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3319 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3320
33213321 uint64_t Val = 0;
3322 if (TLI.isLittleEndian())
3323 Offset = Offset + MSB - 1;
3324 for (unsigned i = 0; i != MSB; ++i) {
3325 Val = (Val << 8) | (unsigned char)Str[Offset];
3326 Offset += TLI.isLittleEndian() ? -1 : 1;
3327 }
3322 if (TLI.isLittleEndian()) {
3323 for (unsigned i = 0; i != NumBytes; ++i)
3324 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3325 } else {
3326 for (unsigned i = 0; i != NumBytes; ++i)
3327 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
3328 }
3329
33283330 return DAG.getConstant(Val, VT);
33293331 }
33303332
33393341
33403342 /// isMemSrcFromString - Returns true if memcpy source is a string constant.
33413343 ///
3342 static bool isMemSrcFromString(SDValue Src, std::string &Str) {
3344 static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
33433345 unsigned SrcDelta = 0;
33443346 GlobalAddressSDNode *G = NULL;
33453347 if (Src.getOpcode() == ISD::GlobalAddress)
33533355 if (!G)
33543356 return false;
33553357
3356 const GlobalVariable *GV = dyn_cast(G->getGlobal());
3357 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3358 return true;
3359
3360 return false;
3358 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
33613359 }
33623360
33633361 /// FindOptimalMemOpLowering - Determines the optimial series memory ops
34603458 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
34613459 if (Align > SrcAlign)
34623460 SrcAlign = Align;
3463 std::string Str;
3461 StringRef Str;
34643462 bool CopyFromStr = isMemSrcFromString(Src, Str);
34653463 bool isZeroStr = CopyFromStr && Str.empty();
34663464 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
34973495 // We only handle zero vectors here.
34983496 // FIXME: Handle other cases where store of vector immediate is done in
34993497 // a single instruction.
3500 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3498 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
35013499 Store = DAG.getStore(Chain, dl, Value,
35023500 getMemBasePlusOffset(Dst, DstOff, DAG),
35033501 DstPtrInfo.getWithOffset(DstOff), isVol,
557557 }
558558
559559 void CWriter::printConstantArray(ConstantArray *CPA, bool Static) {
560 // As a special case, print the array as a string if it is an array of
561 // ubytes or an array of sbytes with positive values.
562 //
563 if (CPA->isCString()) {
564 Out << '\"';
565 // Keep track of whether the last number was a hexadecimal escape.
566 bool LastWasHex = false;
567
568 // Do not include the last character, which we know is null
569 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
570 unsigned char C = cast(CPA->getOperand(i))->getZExtValue();
571
572 // Print it out literally if it is a printable character. The only thing
573 // to be careful about is when the last letter output was a hex escape
574 // code, in which case we have to be careful not to print out hex digits
575 // explicitly (the C compiler thinks it is a continuation of the previous
576 // character, sheesh...)
577 //
578 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
579 LastWasHex = false;
580 if (C == '"' || C == '\\')
581 Out << "\\" << (char)C;
582 else
583 Out << (char)C;
584 } else {
585 LastWasHex = false;
586 switch (C) {
587 case '\n': Out << "\\n"; break;
588 case '\t': Out << "\\t"; break;
589 case '\r': Out << "\\r"; break;
590 case '\v': Out << "\\v"; break;
591 case '\a': Out << "\\a"; break;
592 case '\"': Out << "\\\""; break;
593 case '\'': Out << "\\\'"; break;
594 default:
595 Out << "\\x";
596 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
597 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
598 LastWasHex = true;
599 break;
600 }
601 }
602 }
603 Out << '\"';
604 } else {
605 Out << '{';
606 if (CPA->getNumOperands()) {
607 Out << ' ';
608 printConstant(cast(CPA->getOperand(0)), Static);
609 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
610 Out << ", ";
611 printConstant(cast(CPA->getOperand(i)), Static);
612 }
613 }
614 Out << " }";
615 }
560 Out << "{ ";
561 printConstant(cast(CPA->getOperand(0)), Static);
562 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
563 Out << ", ";
564 printConstant(cast(CPA->getOperand(i)), Static);
565 }
566 Out << " }";
616567 }
617568
618569 void CWriter::printConstantVector(ConstantVector *CP, bool Static) {
619 Out << '{';
620 if (CP->getNumOperands()) {
621 Out << ' ';
622 printConstant(cast(CP->getOperand(0)), Static);
623 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
624 Out << ", ";
625 printConstant(cast(CP->getOperand(i)), Static);
626 }
570 Out << "{ ";
571 printConstant(cast(CP->getOperand(0)), Static);
572 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
573 Out << ", ";
574 printConstant(cast(CP->getOperand(i)), Static);
627575 }
628576 Out << " }";
629577 }
697697 printCFP(CFP);
698698 Out << ";";
699699 } else if (const ConstantArray *CA = dyn_cast(CV)) {
700 if (CA->isString()) {
701 Out << "Constant* " << constName <<
702 " = ConstantArray::get(mod->getContext(), \"";
703 std::string tmp = CA->getAsString();
704 bool nullTerminate = false;
705 if (tmp[tmp.length()-1] == 0) {
706 tmp.erase(tmp.length()-1);
707 nullTerminate = true;
708 }
709 printEscapedString(tmp);
710 // Determine if we want null termination or not.
711 if (nullTerminate)
712 Out << "\", true"; // Indicate that the null terminator should be
713 // added.
714 else
715 Out << "\", false";// No null terminator
716 Out << ");";
717 } else {
718 Out << "std::vector " << constName << "_elems;";
719 nl(Out);
720 unsigned N = CA->getNumOperands();
721 for (unsigned i = 0; i < N; ++i) {
722 printConstant(CA->getOperand(i)); // recurse to print operands
723 Out << constName << "_elems.push_back("
724 << getCppName(CA->getOperand(i)) << ");";
725 nl(Out);
726 }
727 Out << "Constant* " << constName << " = ConstantArray::get("
728 << typeName << ", " << constName << "_elems);";
729 }
700 Out << "std::vector " << constName << "_elems;";
701 nl(Out);
702 unsigned N = CA->getNumOperands();
703 for (unsigned i = 0; i < N; ++i) {
704 printConstant(CA->getOperand(i)); // recurse to print operands
705 Out << constName << "_elems.push_back("
706 << getCppName(CA->getOperand(i)) << ");";
707 nl(Out);
708 }
709 Out << "Constant* " << constName << " = ConstantArray::get("
710 << typeName << ", " << constName << "_elems);";
730711 } else if (const ConstantStruct *CS = dyn_cast(CV)) {
731712 Out << "std::vector " << constName << "_fields;";
732713 nl(Out);
739720 }
740721 Out << "Constant* " << constName << " = ConstantStruct::get("
741722 << typeName << ", " << constName << "_fields);";
742 } else if (const ConstantVector *CP = dyn_cast(CV)) {
723 } else if (const ConstantVector *CV = dyn_cast(CV)) {
743724 Out << "std::vector " << constName << "_elems;";
744725 nl(Out);
745 unsigned N = CP->getNumOperands();
726 unsigned N = CV->getNumOperands();
746727 for (unsigned i = 0; i < N; ++i) {
747 printConstant(CP->getOperand(i));
728 printConstant(CV->getOperand(i));
748729 Out << constName << "_elems.push_back("
749 << getCppName(CP->getOperand(i)) << ");";
730 << getCppName(CV->getOperand(i)) << ");";
750731 nl(Out);
751732 }
752733 Out << "Constant* " << constName << " = ConstantVector::get("
759740 if (CDS->isString()) {
760741 Out << "Constant *" << constName <<
761742 " = ConstantDataArray::getString(mod->getContext(), \"";
762 StringRef Str = CA->getAsString();
743 StringRef Str = CDS->getAsString();
763744 bool nullTerminate = false;
764745 if (Str.back() == 0) {
765746 Str = Str.drop_back();
212212
213213 // Create a constant for Str so that we can pass it to the run-time lib.
214214 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
215 Constant *StrConst = ConstantArray::get(M.getContext(), Str);
215 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
216216 return new GlobalVariable(M, StrConst->getType(), true,
217217 GlobalValue::PrivateLinkage, StrConst, "");
218218 }
255255 ConstantInt::get(TD->getIntPtrType(*Context), Len),
256256 B, TD);
257257 }
258
258
259259 // Otherwise, the character is a constant, see if the first argument is
260260 // a string literal. If so, we can constant fold.
261 std::string Str;
262 if (!GetConstantStringInfo(SrcStr, Str))
263 return 0;
264
265 // strchr can find the nul character.
266 Str += '\0';
267
268 // Compute the offset.
269 size_t I = Str.find(CharC->getSExtValue());
270 if (I == std::string::npos) // Didn't find the char. strchr returns null.
261 StringRef Str;
262 if (!getConstantStringInfo(SrcStr, Str))
263 return 0;
264
265 // Compute the offset, make sure to handle the case when we're searching for
266 // zero (a weird way to spell strlen).
267 size_t I = CharC->getSExtValue() == 0 ?
268 Str.size() : Str.find(CharC->getSExtValue());
269 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
271270 return Constant::getNullValue(CI->getType());
272271
273272 // strchr(s+n,c) -> gep(s+n+i,c)
295294 if (!CharC)
296295 return 0;
297296
298 std::string Str;
299 if (!GetConstantStringInfo(SrcStr, Str)) {
297 StringRef Str;
298 if (!getConstantStringInfo(SrcStr, Str)) {
300299 // strrchr(s, 0) -> strchr(s, 0)
301300 if (TD && CharC->isZero())
302301 return EmitStrChr(SrcStr, '\0', B, TD);
303302 return 0;
304303 }
305304
306 // strrchr can find the nul character.
307 Str += '\0';
308
309305 // Compute the offset.
310 size_t I = Str.rfind(CharC->getSExtValue());
311 if (I == std::string::npos) // Didn't find the char. Return null.
306 size_t I = CharC->getSExtValue() == 0 ?
307 Str.size() : Str.rfind(CharC->getSExtValue());
308 if (I == StringRef::npos) // Didn't find the char. Return null.
312309 return Constant::getNullValue(CI->getType());
313310
314311 // strrchr(s+n,c) -> gep(s+n+i,c)
333330 if (Str1P == Str2P) // strcmp(x,x) -> 0
334331 return ConstantInt::get(CI->getType(), 0);
335332
336 std::string Str1, Str2;
337 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
338 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
333 StringRef Str1, Str2;
334 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
335 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
339336
340337 // strcmp(x, y) -> cnst (if both x and y are constant strings)
341338 if (HasStr1 && HasStr2)
342 return ConstantInt::get(CI->getType(),
343 StringRef(Str1).compare(Str2));
339 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
344340
345341 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
346342 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
396392 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
397393 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
398394
399 std::string Str1, Str2;
400 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
401 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
395 StringRef Str1, Str2;
396 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
397 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
402398
403399 // strncmp(x, y) -> cnst (if both x and y are constant strings)
404400 if (HasStr1 && HasStr2) {
405 StringRef SubStr1 = StringRef(Str1).substr(0, Length);
406 StringRef SubStr2 = StringRef(Str2).substr(0, Length);
401 StringRef SubStr1 = Str1.substr(0, Length);
402 StringRef SubStr2 = Str2.substr(0, Length);
407403 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
408404 }
409405
548544 FT->getReturnType() != FT->getParamType(0))
549545 return 0;
550546
551 std::string S1, S2;
552 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
553 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
547 StringRef S1, S2;
548 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
549 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
554550
555551 // strpbrk(s, "") -> NULL
556552 // strpbrk("", s) -> NULL
608604 !FT->getReturnType()->isIntegerTy())
609605 return 0;
610606
611 std::string S1, S2;
612 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
613 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
607 StringRef S1, S2;
608 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
609 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
614610
615611 // strspn(s, "") -> 0
616612 // strspn("", s) -> 0
618614 return Constant::getNullValue(CI->getType());
619615
620616 // Constant folding.
621 if (HasS1 && HasS2)
622 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
617 if (HasS1 && HasS2) {
618 size_t Pos = S1.find_first_not_of(S2);
619 if (Pos == StringRef::npos) Pos = S1.size();
620 return ConstantInt::get(CI->getType(), Pos);
621 }
623622
624623 return 0;
625624 }
637636 !FT->getReturnType()->isIntegerTy())
638637 return 0;
639638
640 std::string S1, S2;
641 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
642 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
639 StringRef S1, S2;
640 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
641 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
643642
644643 // strcspn("", s) -> 0
645644 if (HasS1 && S1.empty())
646645 return Constant::getNullValue(CI->getType());
647646
648647 // Constant folding.
649 if (HasS1 && HasS2)
650 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
648 if (HasS1 && HasS2) {
649 size_t Pos = S1.find_first_of(S2);
650 if (Pos == StringRef::npos) Pos = S1.size();
651 return ConstantInt::get(CI->getType(), Pos);
652 }
651653
652654 // strcspn(s, "") -> strlen(s)
653655 if (TD && HasS2 && S2.empty())
691693 }
692694
693695 // See if either input string is a constant string.
694 std::string SearchStr, ToFindStr;
695 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
696 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
696 StringRef SearchStr, ToFindStr;
697 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
698 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
697699
698700 // fold strstr(x, "") -> x.
699701 if (HasStr2 && ToFindStr.empty())
703705 if (HasStr1 && HasStr2) {
704706 std::string::size_type Offset = SearchStr.find(ToFindStr);
705707
706 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
708 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
707709 return Constant::getNullValue(CI->getType());
708710
709711 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
755757 }
756758
757759 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
758 std::string LHSStr, RHSStr;
759 if (GetConstantStringInfo(LHS, LHSStr) &&
760 GetConstantStringInfo(RHS, RHSStr)) {
760 StringRef LHSStr, RHSStr;
761 if (getConstantStringInfo(LHS, LHSStr) &&
762 getConstantStringInfo(RHS, RHSStr)) {
761763 // Make sure we're not reading out-of-bounds memory.
762 if (Len > LHSStr.length() || Len > RHSStr.length())
764 if (Len > LHSStr.size() || Len > RHSStr.size())
763765 return 0;
764766 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
765767 return ConstantInt::get(CI->getType(), Ret);
11151117 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
11161118 IRBuilder<> &B) {
11171119 // Check for a fixed format string.
1118 std::string FormatStr;
1119 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
1120 StringRef FormatStr;
1121 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
11201122 return 0;
11211123
11221124 // Empty format string -> noop.
11421144 FormatStr.find('%') == std::string::npos) { // no format characters.
11431145 // Create a string literal with no \n on it. We expect the constant merge
11441146 // pass to be run after this pass, to merge duplicate strings.
1145 FormatStr.erase(FormatStr.end()-1);
1147 FormatStr = FormatStr.drop_back();
11461148 Value *GV = B.CreateGlobalString(FormatStr, "str");
11471149 EmitPutS(GV, B, TD);
11481150 return CI->use_empty() ? (Value*)CI :
12021204 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
12031205 IRBuilder<> &B) {
12041206 // Check for a fixed format string.
1205 std::string FormatStr;
1206 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
1207 StringRef FormatStr;
1208 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
12071209 return 0;
12081210
12091211 // If we just have a format string (nothing else crazy) transform it.
13571359 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
13581360 IRBuilder<> &B) {
13591361 // All the optimizations depend on the format string.
1360 std::string FormatStr;
1361 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
1362 StringRef FormatStr;
1363 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
13621364 return 0;
13631365
13641366 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
14411443 return 0;
14421444
14431445 // Check for a constant string.
1444 std::string Str;
1445 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1446 StringRef Str;
1447 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
14461448 return 0;
14471449
14481450 if (Str.empty() && CI->use_empty()) {
24122414 // * stpcpy(str, "literal") ->
24132415 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
24142416 //
2417 // strchr:
2418 // * strchr(p, 0) -> strlen(p)
24152419 // tan, tanf, tanl:
24162420 // * tan(atan(x)) -> x
24172421 //
826826 }
827827
828828 if (const ConstantArray *CA = dyn_cast(CV)) {
829 // As a special case, print the array as a string if it is an array of
830 // i8 with ConstantInt values.
831 //
832829 Type *ETy = CA->getType()->getElementType();
833 if (CA->isString()) {
834 Out << "c\"";
835 PrintEscapedString(CA->getAsString(), Out);
836 Out << '"';
837 } else { // Cannot output in string format...
838 Out << '[';
830 Out << '[';
831 TypePrinter.print(ETy, Out);
832 Out << ' ';
833 WriteAsOperandInternal(Out, CA->getOperand(0),
834 &TypePrinter, Machine,
835 Context);
836 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
837 Out << ", ";
839838 TypePrinter.print(ETy, Out);
840839 Out << ' ';
841 WriteAsOperandInternal(Out, CA->getOperand(0),
842 &TypePrinter, Machine,
840 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
843841 Context);
844 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
845 Out << ", ";
846 TypePrinter.print(ETy, Out);
847 Out << ' ';
848 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
849 Context);
850 }
851 Out << ']';
852 }
842 }
843 Out << ']';
853844 return;
854845 }
855846
175175 return UV->getElementValue(Elt);
176176
177177 if (const ConstantDataSequential *CDS =dyn_cast(this))
178 return CDS->getElementAsConstant(Elt);
178 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) : 0;
179179 return 0;
180180 }
181181
665665 // ConstantXXX Classes
666666 //===----------------------------------------------------------------------===//
667667
668 template
669 static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
670 for (; Start != End; ++Start)
671 if (*Start != Elt)
672 return false;
673 return true;
674 }
668675
669676 ConstantArray::ConstantArray(ArrayType *T, ArrayRef V)
670677 : Constant(T, ConstantArrayVal,
679686 }
680687
681688 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef V) {
689 // Empty arrays are canonicalized to ConstantAggregateZero.
690 if (V.empty())
691 return ConstantAggregateZero::get(Ty);
692
682693 for (unsigned i = 0, e = V.size(); i != e; ++i) {
683694 assert(V[i]->getType() == Ty->getElementType() &&
684695 "Wrong type in array element initializer");
685696 }
686697 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
687 // If this is an all-zero array, return a ConstantAggregateZero object
688 bool isAllZero = true;
689 bool isUndef = false;
690 if (!V.empty()) {
691 Constant *C = V[0];
692 isAllZero = C->isNullValue();
693 isUndef = isa(C);
694
695 if (isAllZero || isUndef)
696 for (unsigned i = 1, e = V.size(); i != e; ++i)
697 if (V[i] != C) {
698 isAllZero = false;
699 isUndef = false;
700 break;
701 }
702 }
703
704 if (isAllZero)
698
699 // If this is an all-zero array, return a ConstantAggregateZero object. If
700 // all undef, return an UndefValue, if "all simple", then return a
701 // ConstantDataArray.
702 Constant *C = V[0];
703 if (isa(C) && rangeOnlyContains(V.begin(), V.end(), C))
704 return UndefValue::get(Ty);
705
706 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
705707 return ConstantAggregateZero::get(Ty);
706 if (isUndef)
707 return UndefValue::get(Ty);
708
709 // Check to see if all of the elements are ConstantFP or ConstantInt and if
710 // the element type is compatible with ConstantDataVector. If so, use it.
711 if (ConstantDataSequential::isElementTypeCompatible(C->getType())) {
712 // We speculatively build the elements here even if it turns out that there
713 // is a constantexpr or something else weird in the array, since it is so
714 // uncommon for that to happen.
715 if (ConstantInt *CI = dyn_cast(C)) {
716 if (CI->getType()->isIntegerTy(8)) {
717 SmallVector Elts;
718 for (unsigned i = 0, e = V.size(); i != e; ++i)
719 if (ConstantInt *CI = dyn_cast(V[i]))
720 Elts.push_back(CI->getZExtValue());
721 else
722 break;
723 if (Elts.size() == V.size())
724 return ConstantDataArray::get(C->getContext(), Elts);
725 } else if (CI->getType()->isIntegerTy(16)) {
726 SmallVector Elts;
727 for (unsigned i = 0, e = V.size(); i != e; ++i)
728 if (ConstantInt *CI = dyn_cast(V[i]))
729 Elts.push_back(CI->getZExtValue());
730 else
731 break;
732 if (Elts.size() == V.size())
733 return ConstantDataArray::get(C->getContext(), Elts);
734 } else if (CI->getType()->isIntegerTy(32)) {
735 SmallVector Elts;
736 for (unsigned i = 0, e = V.size(); i != e; ++i)
737 if (ConstantInt *CI = dyn_cast(V[i]))
738 Elts.push_back(CI->getZExtValue());
739 else
740 break;
741 if (Elts.size() == V.size())
742 return ConstantDataArray::get(C->getContext(), Elts);
743 } else if (CI->getType()->isIntegerTy(64)) {
744 SmallVector Elts;
745 for (unsigned i = 0, e = V.size(); i != e; ++i)
746 if (ConstantInt *CI = dyn_cast(V[i]))
747 Elts.push_back(CI->getZExtValue());
748 else
749 break;
750 if (Elts.size() == V.size())
751 return ConstantDataArray::get(C->getContext(), Elts);
752 }
753 }
754
755 if (ConstantFP *CFP = dyn_cast(C)) {
756 if (CFP->getType()->isFloatTy()) {
757 SmallVector Elts;
758 for (unsigned i = 0, e = V.size(); i != e; ++i)
759 if (ConstantFP *CFP = dyn_cast(V[i]))
760 Elts.push_back(CFP->getValueAPF().convertToFloat());
761 else
762 break;
763 if (Elts.size() == V.size())
764 return ConstantDataArray::get(C->getContext(), Elts);
765 } else if (CFP->getType()->isDoubleTy()) {
766 SmallVector Elts;
767 for (unsigned i = 0, e = V.size(); i != e; ++i)
768 if (ConstantFP *CFP = dyn_cast(V[i]))
769 Elts.push_back(CFP->getValueAPF().convertToDouble());
770 else
771 break;
772 if (Elts.size() == V.size())
773 return ConstantDataArray::get(C->getContext(), Elts);
774 }
775 }
776 }
777
778 // Otherwise, we really do want to create a ConstantArray.
708779 return pImpl->ArrayConstants.getOrCreate(Ty, V);
709 }
710
711 /// ConstantArray::get(const string&) - Return an array that is initialized to
712 /// contain the specified string. If length is zero then a null terminator is
713 /// added to the specified string so that it may be used in a natural way.
714 /// Otherwise, the length parameter specifies how much of the string to use
715 /// and it won't be null terminated.
716 ///
717 Constant *ConstantArray::get(LLVMContext &Context, StringRef Str,
718 bool AddNull) {
719 SmallVector ElementVals;
720 ElementVals.reserve(Str.size() + size_t(AddNull));
721 for (unsigned i = 0; i < Str.size(); ++i)
722 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
723
724 // Add a null terminator to the string...
725 if (AddNull)
726 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
727
728 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
729 return get(ATy, ElementVals);
730780 }
731781
732782 /// getTypeForElements - Return an anonymous struct type to use for a constant
838888
839889 // Check to see if all of the elements are ConstantFP or ConstantInt and if
840890 // the element type is compatible with ConstantDataVector. If so, use it.
841 if (ConstantDataSequential::isElementTypeCompatible(C->getType()) &&
842 (isa(C) || isa(C))) {
891 if (ConstantDataSequential::isElementTypeCompatible(C->getType())) {
843892 // We speculatively build the elements here even if it turns out that there
844893 // is a constantexpr or something else weird in the array, since it is so
845894 // uncommon for that to happen.
11451194 destroyConstantImpl();
11461195 }
11471196
1148 /// isString - This method returns true if the array is an array of i8, and
1149 /// if the elements of the array are all ConstantInt's.
1150 bool ConstantArray::isString() const {
1151 // Check the element type for i8...
1152 if (!getType()->getElementType()->isIntegerTy(8))
1153 return false;
1154 // Check the elements to make sure they are all integers, not constant
1155 // expressions.
1156 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1157 if (!isa(getOperand(i)))
1158 return false;
1159 return true;
1160 }
1161
1162 /// isCString - This method returns true if the array is a string (see
1163 /// isString) and it ends in a null byte \\0 and does not contains any other
1164 /// null bytes except its terminator.
1165 bool ConstantArray::isCString() const {
1166 // Check the element type for i8...
1167 if (!getType()->getElementType()->isIntegerTy(8))
1168 return false;
1169
1170 // Last element must be a null.
1171 if (!getOperand(getNumOperands()-1)->isNullValue())
1172 return false;
1173 // Other elements must be non-null integers.
1174 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1175 if (!isa(getOperand(i)))
1176 return false;
1177 if (getOperand(i)->isNullValue())
1178 return false;
1179 }
1180 return true;
1181 }
1182
1183
1184 /// convertToString - Helper function for getAsString() and getAsCString().
1185 static std::string convertToString(const User *U, unsigned len) {
1186 std::string Result;
1187 Result.reserve(len);
1188 for (unsigned i = 0; i != len; ++i)
1189 Result.push_back((char)cast(U->getOperand(i))->getZExtValue());
1190 return Result;
1191 }
1192
1193 /// getAsString - If this array is isString(), then this method converts the
1194 /// array to an std::string and returns it. Otherwise, it asserts out.
1195 ///
1196 std::string ConstantArray::getAsString() const {
1197 assert(isString() && "Not a string!");
1198 return convertToString(this, getNumOperands());
1199 }
1200
1201
1202 /// getAsCString - If this array is isCString(), then this method converts the
1203 /// array (without the trailing null byte) to an std::string and returns it.
1204 /// Otherwise, it asserts out.
1205 ///
1206 std::string ConstantArray::getAsCString() const {
1207 assert(isCString() && "Not a string!");
1208 return convertToString(this, getNumOperands() - 1);
1209 }
1210
12111197
12121198 //---- ConstantStruct::get() implementation...
12131199 //
633633 LLVMBool DontNullTerminate) {
634634 /* Inverted the sense of AddNull because ', 0)' is a
635635 better mnemonic for null termination than ', 1)'. */
636 return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
637 DontNullTerminate == 0));
636 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
637 DontNullTerminate == 0));
638638 }
639639 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
640640 LLVMValueRef *ConstantVals,
2323 /// specified. If Name is specified, it is the name of the global variable
2424 /// created.
2525 Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) {
26 Constant *StrConstant = ConstantArray::get(Context, Str, true);
26 Constant *StrConstant = ConstantDataArray::getString(Context, Str);
2727 Module &M = *BB->getParent()->getParent();
2828 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
2929 true, GlobalValue::PrivateLinkage,
819819 // Don't forward functions which are external in the test module too.
820820 if (TestFn && !TestFn->isDeclaration()) {
821821 // 1. Add a string constant with its name to the global file
822 Constant *InitArray = ConstantArray::get(F->getContext(), F->getName());
822 Constant *InitArray =
823 ConstantDataArray::getString(F->getContext(), F->getName());
823824 GlobalVariable *funcName =
824825 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
825826 GlobalValue::InternalLinkage, InitArray,
189189 Constant *op = ce->getOperand(0);
190190 if (GlobalVariable *gvn = dyn_cast(op)) {
191191 Constant *cn = gvn->getInitializer();
192 if (ConstantArray *ca = dyn_castArray>(cn)) {
192 if (ConstantDataArray *ca = dyn_castArray>(cn)) {
193193 if (ca->isCString()) {
194 name = ".objc_class_name_" + ca->getAsCString();
194 name = ".objc_class_name_" + ca->getAsCString().str();
195195 return true;
196196 }
197197 }