Skip to content

Commit

Permalink
with recent changes, ConstantArray is never a "string". Remove the as…
Browse files Browse the repository at this point in the history
…sociated

methods and constant fold the clients to false.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149362 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lattner committed Jan 31, 2012
1 parent ca012b8 commit 1b2f643
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 256 deletions.
25 changes: 0 additions & 25 deletions include/llvm/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,31 +373,6 @@ class ConstantArray : public Constant {
return reinterpret_cast<ArrayType*>(Value::getType());
}

// FIXME: String methods will eventually be removed.


/// isString - This method returns true if the array is an array of i8 and
/// the elements of the array are all ConstantInt's.
bool isString() const;

/// isCString - This method returns true if the array is a string (see
/// @verbatim
/// isString) and it ends in a null byte \0 and does not contains any other
/// @endverbatim
/// null bytes except its terminator.
bool isCString() const;

/// getAsString - If this array is isString(), then this method converts the
/// array to an std::string and returns it. Otherwise, it asserts out.
///
std::string getAsString() const;

/// getAsCString - If this array is isCString(), then this method converts the
/// array (without the trailing null byte) to an std::string and returns it.
/// Otherwise, it asserts out.
///
std::string getAsCString() const;

virtual void destroyConstant();
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);

Expand Down
26 changes: 0 additions & 26 deletions lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,32 +845,6 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
} else {
assert (0 && "Unknown FP type!");
}
} else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
const ConstantArray *CA = cast<ConstantArray>(C);
// Emit constant strings specially.
unsigned NumOps = CA->getNumOperands();
// If this is a null-terminated string, use the denser CSTRING encoding.
if (CA->getOperand(NumOps-1)->isNullValue()) {
Code = bitc::CST_CODE_CSTRING;
--NumOps; // Don't encode the null, which isn't allowed by char6.
} else {
Code = bitc::CST_CODE_STRING;
AbbrevToUse = String8Abbrev;
}
bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
for (unsigned i = 0; i != NumOps; ++i) {
unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue();
Record.push_back(V);
isCStr7 &= (V & 128) == 0;
if (isCStrChar6)
isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
}

if (isCStrChar6)
AbbrevToUse = CString6Abbrev;
else if (isCStr7)
AbbrevToUse = CString7Abbrev;
} else if (isa<ConstantDataSequential>(C) &&
cast<ConstantDataSequential>(C)->isString()) {
const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
Expand Down
4 changes: 0 additions & 4 deletions lib/Bitcode/Writer/ValueEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ void ValueEnumerator::EnumerateValue(const Value *V) {
if (const Constant *C = dyn_cast<Constant>(V)) {
if (isa<GlobalValue>(C)) {
// Initializers for globals are handled explicitly elsewhere.
} else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
// Do not enumerate the initializers for an array of simple characters.
// The initializers just pollute the value table, and we emit the strings
// specially.
} else if (C->getNumOperands()) {
// If a constant has operands, enumerate them. This makes sure that if a
// constant has uses (for example an array of const ints), that they are
Expand Down
33 changes: 10 additions & 23 deletions lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,31 +1675,18 @@ static void EmitGlobalConstantDataSequential(const ConstantDataSequential *CDS,

static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
AsmPrinter &AP) {
if (AddrSpace != 0 || !CA->isString()) {
// Not a string. Print the values in successive locations.
// See if we can aggregate some values. Make sure it can be
// represented as a series of bytes of the constant value.
int Value = isRepeatedByteSequence(CA, AP.TM);

// See if we can aggregate some values. Make sure it can be
// represented as a series of bytes of the constant value.
int Value = isRepeatedByteSequence(CA, AP.TM);

if (Value != -1) {
uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
}
else {
for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
}
return;
if (Value != -1) {
uint64_t Bytes = AP.TM.getTargetData()->getTypeAllocSize(CA->getType());
AP.OutStreamer.EmitFill(Bytes, Value, AddrSpace);
}
else {
for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
}

// Otherwise, it can be emitted as .ascii.
SmallVector<char, 128> TmpVec;
TmpVec.reserve(CA->getNumOperands());
for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
TmpVec.push_back(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());

AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace);
}

static void EmitGlobalConstantVector(const ConstantVector *CV,
Expand Down
74 changes: 11 additions & 63 deletions lib/Target/CBackend/CBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,73 +558,21 @@ raw_ostream &CWriter::printType(raw_ostream &Out, Type *Ty,
}

void CWriter::printConstantArray(ConstantArray *CPA, bool Static) {
// As a special case, print the array as a string if it is an array of
// ubytes or an array of sbytes with positive values.
//
if (CPA->isCString()) {
Out << '\"';
// Keep track of whether the last number was a hexadecimal escape.
bool LastWasHex = false;

// Do not include the last character, which we know is null
for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getZExtValue();

// Print it out literally if it is a printable character. The only thing
// to be careful about is when the last letter output was a hex escape
// code, in which case we have to be careful not to print out hex digits
// explicitly (the C compiler thinks it is a continuation of the previous
// character, sheesh...)
//
if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
LastWasHex = false;
if (C == '"' || C == '\\')
Out << "\\" << (char)C;
else
Out << (char)C;
} else {
LastWasHex = false;
switch (C) {
case '\n': Out << "\\n"; break;
case '\t': Out << "\\t"; break;
case '\r': Out << "\\r"; break;
case '\v': Out << "\\v"; break;
case '\a': Out << "\\a"; break;
case '\"': Out << "\\\""; break;
case '\'': Out << "\\\'"; break;
default:
Out << "\\x";
Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
LastWasHex = true;
break;
}
}
}
Out << '\"';
} else {
Out << '{';
if (CPA->getNumOperands()) {
Out << ' ';
printConstant(cast<Constant>(CPA->getOperand(0)), Static);
for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
Out << ", ";
printConstant(cast<Constant>(CPA->getOperand(i)), Static);
}
}
Out << " }";
Out << "{ ";
printConstant(cast<Constant>(CPA->getOperand(0)), Static);
for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
Out << ", ";
printConstant(cast<Constant>(CPA->getOperand(i)), Static);
}
Out << " }";
}

void CWriter::printConstantVector(ConstantVector *CP, bool Static) {
Out << '{';
if (CP->getNumOperands()) {
Out << ' ';
printConstant(cast<Constant>(CP->getOperand(0)), Static);
for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Out << ", ";
printConstant(cast<Constant>(CP->getOperand(i)), Static);
}
Out << "{ ";
printConstant(cast<Constant>(CP->getOperand(0)), Static);
for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Out << ", ";
printConstant(cast<Constant>(CP->getOperand(i)), Static);
}
Out << " }";
}
Expand Down
47 changes: 14 additions & 33 deletions lib/Target/CppBackend/CPPBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,36 +698,17 @@ void CppWriter::printConstant(const Constant *CV) {
printCFP(CFP);
Out << ";";
} else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
if (CA->isString()) {
Out << "Constant* " << constName <<
" = ConstantArray::get(mod->getContext(), \"";
std::string tmp = CA->getAsString();
bool nullTerminate = false;
if (tmp[tmp.length()-1] == 0) {
tmp.erase(tmp.length()-1);
nullTerminate = true;
}
printEscapedString(tmp);
// Determine if we want null termination or not.
if (nullTerminate)
Out << "\", true"; // Indicate that the null terminator should be
// added.
else
Out << "\", false";// No null terminator
Out << ");";
} else {
Out << "std::vector<Constant*> " << constName << "_elems;";
Out << "std::vector<Constant*> " << constName << "_elems;";
nl(Out);
unsigned N = CA->getNumOperands();
for (unsigned i = 0; i < N; ++i) {
printConstant(CA->getOperand(i)); // recurse to print operands
Out << constName << "_elems.push_back("
<< getCppName(CA->getOperand(i)) << ");";
nl(Out);
unsigned N = CA->getNumOperands();
for (unsigned i = 0; i < N; ++i) {
printConstant(CA->getOperand(i)); // recurse to print operands
Out << constName << "_elems.push_back("
<< getCppName(CA->getOperand(i)) << ");";
nl(Out);
}
Out << "Constant* " << constName << " = ConstantArray::get("
<< typeName << ", " << constName << "_elems);";
}
Out << "Constant* " << constName << " = ConstantArray::get("
<< typeName << ", " << constName << "_elems);";
} else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Out << "std::vector<Constant*> " << constName << "_fields;";
nl(Out);
Expand All @@ -740,14 +721,14 @@ void CppWriter::printConstant(const Constant *CV) {
}
Out << "Constant* " << constName << " = ConstantStruct::get("
<< typeName << ", " << constName << "_fields);";
} else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
} else if (const ConstantVector *CV = dyn_cast<ConstantVector>(CV)) {
Out << "std::vector<Constant*> " << constName << "_elems;";
nl(Out);
unsigned N = CP->getNumOperands();
unsigned N = CV->getNumOperands();
for (unsigned i = 0; i < N; ++i) {
printConstant(CP->getOperand(i));
printConstant(CV->getOperand(i));
Out << constName << "_elems.push_back("
<< getCppName(CP->getOperand(i)) << ");";
<< getCppName(CV->getOperand(i)) << ");";
nl(Out);
}
Out << "Constant* " << constName << " = ConstantVector::get("
Expand All @@ -760,7 +741,7 @@ void CppWriter::printConstant(const Constant *CV) {
if (CDS->isString()) {
Out << "Constant *" << constName <<
" = ConstantDataArray::getString(mod->getContext(), \"";
StringRef Str = CA->getAsString();
StringRef Str = CDS->getAsString();
bool nullTerminate = false;
if (Str.back() == 0) {
Str = Str.drop_back();
Expand Down
29 changes: 10 additions & 19 deletions lib/VMCore/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,30 +827,21 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
}

if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
// As a special case, print the array as a string if it is an array of
// i8 with ConstantInt values.
//
Type *ETy = CA->getType()->getElementType();
if (CA->isString()) {
Out << "c\"";
PrintEscapedString(CA->getAsString(), Out);
Out << '"';
} else { // Cannot output in string format...
Out << '[';
Out << '[';
TypePrinter.print(ETy, Out);
Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(0),
&TypePrinter, Machine,
Context);
for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
Out << ", ";
TypePrinter.print(ETy, Out);
Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(0),
&TypePrinter, Machine,
WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Context);
for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
Out << ", ";
TypePrinter.print(ETy, Out);
Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Context);
}
Out << ']';
}
Out << ']';
return;
}

Expand Down
63 changes: 0 additions & 63 deletions lib/VMCore/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,69 +1201,6 @@ void ConstantArray::destroyConstant() {
destroyConstantImpl();
}

/// isString - This method returns true if the array is an array of i8, and
/// if the elements of the array are all ConstantInt's.
bool ConstantArray::isString() const {
// Check the element type for i8...
if (!getType()->getElementType()->isIntegerTy(8))
return false;
// Check the elements to make sure they are all integers, not constant
// expressions.
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
if (!isa<ConstantInt>(getOperand(i)))
return false;
return true;
}

/// isCString - This method returns true if the array is a string (see
/// isString) and it ends in a null byte \\0 and does not contains any other
/// null bytes except its terminator.
bool ConstantArray::isCString() const {
// Check the element type for i8...
if (!getType()->getElementType()->isIntegerTy(8))
return false;

// Last element must be a null.
if (!getOperand(getNumOperands()-1)->isNullValue())
return false;
// Other elements must be non-null integers.
for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
if (!isa<ConstantInt>(getOperand(i)))
return false;
if (getOperand(i)->isNullValue())
return false;
}
return true;
}


/// convertToString - Helper function for getAsString() and getAsCString().
static std::string convertToString(const User *U, unsigned len) {
std::string Result;
Result.reserve(len);
for (unsigned i = 0; i != len; ++i)
Result.push_back((char)cast<ConstantInt>(U->getOperand(i))->getZExtValue());
return Result;
}

/// getAsString - If this array is isString(), then this method converts the
/// array to an std::string and returns it. Otherwise, it asserts out.
///
std::string ConstantArray::getAsString() const {
assert(isString() && "Not a string!");
return convertToString(this, getNumOperands());
}


/// getAsCString - If this array is isCString(), then this method converts the
/// array (without the trailing null byte) to an std::string and returns it.
/// Otherwise, it asserts out.
///
std::string ConstantArray::getAsCString() const {
assert(isCString() && "Not a string!");
return convertToString(this, getNumOperands() - 1);
}


//---- ConstantStruct::get() implementation...
//
Expand Down

0 comments on commit 1b2f643

Please sign in to comment.