llvm.org GIT mirror llvm / 91766fe
Revert Chris' commits up to r149348 that started causing VMCoreTests unit test to fail. These are: r149348 r149351 r149352 r149354 r149356 r149357 r149361 r149362 r149364 r149365 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149470 91177308-0d34-0410-b5e6-96231b3b80d8 Argyrios Kyrtzidis 7 years ago
19 changed file(s) with 543 addition(s) and 280 deletion(s). Raw diff Collapse all Expand all
1616
1717 #include "llvm/ADT/ArrayRef.h"
1818 #include "llvm/Support/DataTypes.h"
19 #include
1920
2021 namespace llvm {
22 template class SmallVectorImpl;
2123 class Value;
2224 class Instruction;
2325 class APInt;
2426 class TargetData;
25 class StringRef;
2627
2728 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
2829 /// known to be either zero or one and return them in the KnownZero/KnownOne
123124 return GetPointerBaseWithConstantOffset(const_cast(Ptr), Offset,TD);
124125 }
125126
126 /// getConstantStringInfo - This function computes the length of a
127 /// GetConstantStringInfo - This function computes the length of a
127128 /// null-terminated C string pointed to by V. If successful, it returns true
128 /// and returns the string in Str. If unsuccessful, it returns false. This
129 /// does not include the trailing nul character.
130 bool getConstantStringInfo(const Value *V, StringRef &Str,
131 uint64_t Offset = 0);
132
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
133137 /// GetStringLength - If we can compute the length of the string pointed to by
134138 /// the specified pointer, return 'len+1'. If we can't, return 0.
135139 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
354365 /// Transparently provide more efficient getOperand methods.
355366 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
356367
360371 inline ArrayType *getType() const {
361372 return reinterpret_cast(Value::getType());
362373 }
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;
363399
364400 virtual void destroyConstant();
365401 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 StringRef Str;
479 if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) {
480 unsigned StrLen = Str.size();
478 std::string Str;
479 if (TD && GetConstantStringInfo(CE, Str) && !Str.empty()) {
480 unsigned StrLen = Str.length();
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 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);
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));
13761378 if (!Val)
13771379 return 0;
13781380
1379 for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
1380 if (CA->getElementAsConstant(I) != Elt)
1381 for (unsigned I = 1, E = CA->getNumOperands(); I != E; ++I)
1382 if (CA->getOperand(I-1) != CA->getOperand(I))
13811383 return 0;
13821384
13831385 return Val;
13841386 }
13851387
1388 // FIXME: Vector types (e.g., <4 x i32> ).
1389
13861390 // Conceptually, we could handle things like:
13871391 // %a = zext i8 %X to i16
13881392 // %b = shl i16 %a, 8
16021606 }
16031607
16041608
1605 /// getConstantStringInfo - This function computes the length of a
1609 /// GetConstantStringInfo - This function computes the length of a
16061610 /// null-terminated C string pointed to by V. If successful, it returns true
16071611 /// and returns the string in Str. If unsuccessful, it returns false.
1608 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
1609 uint64_t Offset) {
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)) {
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) {
16181636 // Make sure the GEP has exactly three arguments.
16191637 if (GEP->getNumOperands() != 3)
16201638 return false;
16391657 StartIdx = CI->getZExtValue();
16401658 else
16411659 return false;
1642 return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset);
1660 return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
1661 StopAtNul);
16431662 }
16441663
16451664 // The GEP instruction, constant or instruction, must reference a global
16461665 // variable that is a constant and is initialized. The referenced constant
16471666 // initializer is the array that we'll use for optimization.
1648 const GlobalVariable *GV = dyn_cast(V);
1667 const GlobalVariable* GV = dyn_cast(V);
16491668 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
16501669 return false;
1651
1670 const Constant *GlobalInit = GV->getInitializer();
1671
16521672 // Handle the all-zeros case
1653 if (GV->getInitializer()->isNullValue()) {
1673 if (GlobalInit->isNullValue()) {
16541674 // This is a degenerate case. The initializer is constant zero so the
16551675 // length of the string must be zero.
1656 Str = "";
1676 Str.clear();
16571677 return true;
16581678 }
16591679
16601680 // Must be a Constant Array
1661 const ConstantDataArray *Array =
1662 dyn_cast(GV->getInitializer());
1663 if (Array == 0 || !Array->isString())
1681 const ConstantArray *Array = dyn_cast(GlobalInit);
1682 if (Array == 0 || !Array->getType()->getElementType()->isIntegerTy(8))
16641683 return false;
16651684
16661685 // Get the number of elements in the array
1667 uint64_t NumElts = Array->getType()->getArrayNumElements();
1668
1669 // Start out with the entire array in the StringRef.
1670 Str = Array->getAsString();
1671
1686 uint64_t NumElts = Array->getType()->getNumElements();
1687
16721688 if (Offset > NumElts)
16731689 return false;
16741690
1675 // Skip over 'offset' bytes.
1676 Str = Str.substr(Offset);
1677 // Trim off the \0 and anything after it. If the array is not nul terminated,
1678 // we just return the whole end of string. The client may know some other way
1679 // that the string is length-bound.
1680 Str = Str.substr(0, Str.find('\0'));
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.
16811705 return true;
16821706 }
16831707
16891713 /// the specified pointer, return 'len+1'. If we can't, return 0.
16901714 static uint64_t GetStringLengthH(Value *V, SmallPtrSet &PHIs) {
16911715 // Look through noop bitcast instructions.
1692 V = V->stripPointerCasts();
1716 if (BitCastInst *BCI = dyn_cast(V))
1717 return GetStringLengthH(BCI->getOperand(0), PHIs);
16931718
16941719 // If this is a PHI node, there are two cases: either we have already seen it
16951720 // or we haven't.
17251750 if (Len1 != Len2) return 0;
17261751 return Len1;
17271752 }
1728
1729 // Otherwise, see if we can read the string.
1730 StringRef StrData;
1731 if (!getConstantStringInfo(V, StrData))
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;
17321759 return 0;
1733
1734 return StrData.size()+1;
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'.
17351830 }
17361831
17371832 /// 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 = ConstantDataArray::getString(Context, Lex.getStrVal(),
2021 false);
2020 ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
20222021 if (ParseToken(lltok::StringConstant, "expected string")) return true;
20232022 ID.Kind = ValID::t_Constant;
20242023 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;
847873 } else if (isa(C) &&
848874 cast(C)->isString()) {
849875 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.
323327 } else if (C->getNumOperands()) {
324328 // If a constant has operands, enumerate them. This makes sure that if a
325329 // constant has uses (for example an array of const ints), that they are
16741674
16751675 static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
16761676 AsmPrinter &AP) {
1677 // See if we can aggregate some values. Make sure it can be
1678 // represented as a series of bytes of the constant value.
1679 int Value = isRepeatedByteSequence(CA, AP.TM);
1680
1681 if (Value != -1) {
1682 uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
1683 AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
1684 }
1685 else {
1686 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1687 EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
1688 }
1677 if (AddrSpace != 0 || !CA->isString()) {
1678 // Not a string. Print the values in successive locations.
1679
1680 // See if we can aggregate some values. Make sure it can be
1681 // represented as a series of bytes of the constant value.
1682 int Value = isRepeatedByteSequence(CA, AP.TM);
1683
1684 if (Value != -1) {
1685 uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
1686 AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
1687 }
1688 else {
1689 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1690 EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
1691 }
1692 return;
1693 }
1694
1695 // Otherwise, it can be emitted as .ascii.
1696 SmallVector TmpVec;
1697 TmpVec.reserve(CA->getNumOperands());
1698 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1699 TmpVec.push_back(cast(CA->getOperand(i))->getZExtValue());
1700
1701 AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace);
16891702 }
16901703
16911704 static void EmitGlobalConstantVector(const ConstantVector *CV,
32983298 /// string ptr.
32993299 static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
33003300 const TargetLowering &TLI,
3301 StringRef Str, unsigned Offset) {
3301 std::string &Str, unsigned Offset) {
33023302 // Handle vector with all elements zero.
33033303 if (Str.empty()) {
33043304 if (VT.isInteger())
33223322 if (TLI.isLittleEndian())
33233323 Offset = Offset + MSB - 1;
33243324 for (unsigned i = 0; i != MSB; ++i) {
3325 Val = (Val << 8);
3326
3327 if (Offset < Str.size())
3328 Val |= (unsigned char)Str[Offset];
3325 Val = (Val << 8) | (unsigned char)Str[Offset];
33293326 Offset += TLI.isLittleEndian() ? -1 : 1;
33303327 }
33313328 return DAG.getConstant(Val, VT);
33423339
33433340 /// isMemSrcFromString - Returns true if memcpy source is a string constant.
33443341 ///
3345 static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
3342 static bool isMemSrcFromString(SDValue Src, std::string &Str) {
33463343 unsigned SrcDelta = 0;
33473344 GlobalAddressSDNode *G = NULL;
33483345 if (Src.getOpcode() == ISD::GlobalAddress)
33563353 if (!G)
33573354 return false;
33583355
3359 if (const GlobalVariable *GV = dyn_cast(G->getGlobal()))
3360 if (getConstantStringInfo(GV, Str, SrcDelta))
3361 return true;
3356 const GlobalVariable *GV = dyn_cast(G->getGlobal());
3357 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3358 return true;
33623359
33633360 return false;
33643361 }
34633460 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
34643461 if (Align > SrcAlign)
34653462 SrcAlign = Align;
3466 StringRef Str;
3463 std::string Str;
34673464 bool CopyFromStr = isMemSrcFromString(Src, Str);
34683465 bool isZeroStr = CopyFromStr && Str.empty();
34693466 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
557557 }
558558
559559 void CWriter::printConstantArray(ConstantArray *CPA, bool Static) {
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 << " }";
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 }
567616 }
568617
569618 void CWriter::printConstantVector(ConstantVector *CP, bool Static) {
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);
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 }
575627 }
576628 Out << " }";
577629 }
697697 printCFP(CFP);
698698 Out << ";";
699699 } else if (const ConstantArray *CA = dyn_cast(CV)) {
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);";
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 }
711730 } else if (const ConstantStruct *CS = dyn_cast(CV)) {
712731 Out << "std::vector " << constName << "_fields;";
713732 nl(Out);
720739 }
721740 Out << "Constant* " << constName << " = ConstantStruct::get("
722741 << typeName << ", " << constName << "_fields);";
723 } else if (const ConstantVector *CV = dyn_cast(CV)) {
742 } else if (const ConstantVector *CP = dyn_cast(CV)) {
724743 Out << "std::vector " << constName << "_elems;";
725744 nl(Out);
726 unsigned N = CV->getNumOperands();
745 unsigned N = CP->getNumOperands();
727746 for (unsigned i = 0; i < N; ++i) {
728 printConstant(CV->getOperand(i));
747 printConstant(CP->getOperand(i));
729748 Out << constName << "_elems.push_back("
730 << getCppName(CV->getOperand(i)) << ");";
749 << getCppName(CP->getOperand(i)) << ");";
731750 nl(Out);
732751 }
733752 Out << "Constant* " << constName << " = ConstantVector::get("
740759 if (CDS->isString()) {
741760 Out << "Constant *" << constName <<
742761 " = ConstantDataArray::getString(mod->getContext(), \"";
743 StringRef Str = CDS->getAsString();
762 StringRef Str = CA->getAsString();
744763 bool nullTerminate = false;
745764 if (Str.back() == 0) {
746765 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 = ConstantDataArray::getString(M.getContext(), Str);
215 Constant *StrConst = ConstantArray::get(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 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.
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.
270271 return Constant::getNullValue(CI->getType());
271272
272273 // strchr(s+n,c) -> gep(s+n+i,c)
294295 if (!CharC)
295296 return 0;
296297
297 StringRef Str;
298 if (!getConstantStringInfo(SrcStr, Str)) {
298 std::string Str;
299 if (!GetConstantStringInfo(SrcStr, Str)) {
299300 // strrchr(s, 0) -> strchr(s, 0)
300301 if (TD && CharC->isZero())
301302 return EmitStrChr(SrcStr, '\0', B, TD);
302303 return 0;
303304 }
304305
306 // strrchr can find the nul character.
307 Str += '\0';
308
305309 // Compute the offset.
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.
310 size_t I = Str.rfind(CharC->getSExtValue());
311 if (I == std::string::npos) // Didn't find the char. Return null.
309312 return Constant::getNullValue(CI->getType());
310313
311314 // strrchr(s+n,c) -> gep(s+n+i,c)
330333 if (Str1P == Str2P) // strcmp(x,x) -> 0
331334 return ConstantInt::get(CI->getType(), 0);
332335
333 StringRef Str1, Str2;
334 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
335 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
336 std::string Str1, Str2;
337 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
338 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
336339
337340 // strcmp(x, y) -> cnst (if both x and y are constant strings)
338341 if (HasStr1 && HasStr2)
339 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
342 return ConstantInt::get(CI->getType(),
343 StringRef(Str1).compare(Str2));
340344
341345 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
342346 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
392396 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
393397 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
394398
395 StringRef Str1, Str2;
396 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
397 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
399 std::string Str1, Str2;
400 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
401 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
398402
399403 // strncmp(x, y) -> cnst (if both x and y are constant strings)
400404 if (HasStr1 && HasStr2) {
401 StringRef SubStr1 = Str1.substr(0, Length);
402 StringRef SubStr2 = Str2.substr(0, Length);
405 StringRef SubStr1 = StringRef(Str1).substr(0, Length);
406 StringRef SubStr2 = StringRef(Str2).substr(0, Length);
403407 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
404408 }
405409
544548 FT->getReturnType() != FT->getParamType(0))
545549 return 0;
546550
547 StringRef S1, S2;
548 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
549 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
551 std::string S1, S2;
552 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
553 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
550554
551555 // strpbrk(s, "") -> NULL
552556 // strpbrk("", s) -> NULL
604608 !FT->getReturnType()->isIntegerTy())
605609 return 0;
606610
607 StringRef S1, S2;
608 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
609 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
611 std::string S1, S2;
612 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
613 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
610614
611615 // strspn(s, "") -> 0
612616 // strspn("", s) -> 0
614618 return Constant::getNullValue(CI->getType());
615619
616620 // Constant folding.
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 }
621 if (HasS1 && HasS2)
622 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
622623
623624 return 0;
624625 }
636637 !FT->getReturnType()->isIntegerTy())
637638 return 0;
638639
639 StringRef S1, S2;
640 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
641 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
640 std::string S1, S2;
641 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
642 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
642643
643644 // strcspn("", s) -> 0
644645 if (HasS1 && S1.empty())
645646 return Constant::getNullValue(CI->getType());
646647
647648 // Constant folding.
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 }
649 if (HasS1 && HasS2)
650 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
653651
654652 // strcspn(s, "") -> strlen(s)
655653 if (TD && HasS2 && S2.empty())
693691 }
694692
695693 // See if either input string is a constant string.
696 StringRef SearchStr, ToFindStr;
697 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
698 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
694 std::string SearchStr, ToFindStr;
695 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
696 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
699697
700698 // fold strstr(x, "") -> x.
701699 if (HasStr2 && ToFindStr.empty())
705703 if (HasStr1 && HasStr2) {
706704 std::string::size_type Offset = SearchStr.find(ToFindStr);
707705
708 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
706 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
709707 return Constant::getNullValue(CI->getType());
710708
711709 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
757755 }
758756
759757 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
760 StringRef LHSStr, RHSStr;
761 if (getConstantStringInfo(LHS, LHSStr) &&
762 getConstantStringInfo(RHS, RHSStr)) {
758 std::string LHSStr, RHSStr;
759 if (GetConstantStringInfo(LHS, LHSStr) &&
760 GetConstantStringInfo(RHS, RHSStr)) {
763761 // Make sure we're not reading out-of-bounds memory.
764 if (Len > LHSStr.size() || Len > RHSStr.size())
762 if (Len > LHSStr.length() || Len > RHSStr.length())
765763 return 0;
766764 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
767765 return ConstantInt::get(CI->getType(), Ret);
11171115 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
11181116 IRBuilder<> &B) {
11191117 // Check for a fixed format string.
1120 StringRef FormatStr;
1121 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1118 std::string FormatStr;
1119 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
11221120 return 0;
11231121
11241122 // Empty format string -> noop.
11441142 FormatStr.find('%') == std::string::npos) { // no format characters.
11451143 // Create a string literal with no \n on it. We expect the constant merge
11461144 // pass to be run after this pass, to merge duplicate strings.
1147 FormatStr = FormatStr.drop_back();
1145 FormatStr.erase(FormatStr.end()-1);
11481146 Value *GV = B.CreateGlobalString(FormatStr, "str");
11491147 EmitPutS(GV, B, TD);
11501148 return CI->use_empty() ? (Value*)CI :
12041202 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
12051203 IRBuilder<> &B) {
12061204 // Check for a fixed format string.
1207 StringRef FormatStr;
1208 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1205 std::string FormatStr;
1206 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
12091207 return 0;
12101208
12111209 // If we just have a format string (nothing else crazy) transform it.
13591357 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
13601358 IRBuilder<> &B) {
13611359 // All the optimizations depend on the format string.
1362 StringRef FormatStr;
1363 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1360 std::string FormatStr;
1361 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
13641362 return 0;
13651363
13661364 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
14431441 return 0;
14441442
14451443 // Check for a constant string.
1446 StringRef Str;
1447 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1444 std::string Str;
1445 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
14481446 return 0;
14491447
14501448 if (Str.empty() && CI->use_empty()) {
24142412 // * stpcpy(str, "literal") ->
24152413 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
24162414 //
2417 // strchr:
2418 // * strchr(p, 0) -> strlen(p)
24192415 // tan, tanf, tanl:
24202416 // * tan(atan(x)) -> x
24212417 //
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 //
829832 Type *ETy = CA->getType()->getElementType();
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 << ", ";
833 if (CA->isString()) {
834 Out << "c\"";
835 PrintEscapedString(CA->getAsString(), Out);
836 Out << '"';
837 } else { // Cannot output in string format...
838 Out << '[';
838839 TypePrinter.print(ETy, Out);
839840 Out << ' ';
840 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
841 WriteAsOperandInternal(Out, CA->getOperand(0),
842 &TypePrinter, Machine,
841843 Context);
842 }
843 Out << ']';
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 }
844853 return;
845854 }
846855
175175 return UV->getElementValue(Elt);
176176
177177 if (const ConstantDataSequential *CDS =dyn_cast(this))
178 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) : 0;
178 return CDS->getElementAsConstant(Elt);
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 }
675668
676669 ConstantArray::ConstantArray(ArrayType *T, ArrayRef V)
677670 : Constant(T, ConstantArrayVal,
686679 }
687680
688681 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef V) {
689 // Empty arrays are canonicalized to ConstantAggregateZero.
690 if (V.empty())
691 return ConstantAggregateZero::get(Ty);
692
693682 for (unsigned i = 0, e = V.size(); i != e; ++i) {
694683 assert(V[i]->getType() == Ty->getElementType() &&
695684 "Wrong type in array element initializer");
696685 }
697686 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
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))
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)
705 return ConstantAggregateZero::get(Ty);
706 if (isUndef)
704707 return UndefValue::get(Ty);
705
706 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
707 return ConstantAggregateZero::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.
779708 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);
780730 }
781731
782732 /// getTypeForElements - Return an anonymous struct type to use for a constant
888838
889839 // Check to see if all of the elements are ConstantFP or ConstantInt and if
890840 // the element type is compatible with ConstantDataVector. If so, use it.
891 if (ConstantDataSequential::isElementTypeCompatible(C->getType())) {
841 if (ConstantDataSequential::isElementTypeCompatible(C->getType()) &&
842 (isa(C) || isa(C))) {
892843 // We speculatively build the elements here even if it turns out that there
893844 // is a constantexpr or something else weird in the array, since it is so
894845 // uncommon for that to happen.
11941145 destroyConstantImpl();
11951146 }
11961147
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
11971211
11981212 //---- ConstantStruct::get() implementation...
11991213 //
633633 LLVMBool DontNullTerminate) {
634634 /* Inverted the sense of AddNull because ', 0)' is a
635635 better mnemonic for null termination than ', 1)'. */
636 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
637 DontNullTerminate == 0));
636 return wrap(ConstantArray::get(*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 = ConstantDataArray::getString(Context, Str);
26 Constant *StrConstant = ConstantArray::get(Context, Str, true);
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 =
823 ConstantDataArray::getString(F->getContext(), F->getName());
822 Constant *InitArray = ConstantArray::get(F->getContext(), F->getName());
824823 GlobalVariable *funcName =
825824 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
826825 GlobalValue::InternalLinkage, InitArray,
189189 Constant *op = ce->getOperand(0);
190190 if (GlobalVariable *gvn = dyn_cast(op)) {
191191 Constant *cn = gvn->getInitializer();
192 if (ConstantDataArray *ca = dyn_castArray>(cn)) {
192 if (ConstantArray *ca = dyn_castArray>(cn)) {
193193 if (ca->isCString()) {
194 name = ".objc_class_name_" + ca->getAsCString().str();
194 name = ".objc_class_name_" + ca->getAsCString();
195195 return true;
196196 }
197197 }