From 0eeb913aa17a68b1f2963b02ca1d68f09dba0b78 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 28 Oct 2009 05:14:34 +0000 Subject: [PATCH] Previously, all operands to Constant were themselves constant. In the new world order, BlockAddress can have a BasicBlock operand. This doesn't permute much, because if you have a ConstantExpr (or anything more specific than Constant) we still know the operand has to be a Constant. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85375 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Constant.h | 19 +++++++------- include/llvm/Constants.h | 4 +-- lib/Bitcode/Writer/BitcodeWriter.cpp | 7 ++--- lib/Target/MSIL/MSILWriter.cpp | 2 +- lib/Transforms/IPO/StripSymbols.cpp | 9 +++---- lib/VMCore/ConstantFold.cpp | 39 ++++++++++++++++------------ lib/VMCore/Constants.cpp | 7 ++--- 7 files changed, 48 insertions(+), 39 deletions(-) diff --git a/include/llvm/Constant.h b/include/llvm/Constant.h index a42c7d43717..bcaa56e3902 100644 --- a/include/llvm/Constant.h +++ b/include/llvm/Constant.h @@ -48,6 +48,10 @@ protected: : User(ty, vty, Ops, NumOps) {} void destroyConstantImpl(); + + void setOperand(unsigned i, Value *V) { + User::setOperand(i, V); + } public: /// isNullValue - Return true if this is the value that would be returned by /// getNullValue. @@ -83,16 +87,13 @@ public: /// FIXME: This really should not be in VMCore. PossibleRelocationsTy getRelocationInfo() const; - // Specialize get/setOperand for Constants as their operands are always - // constants as well. - Constant *getOperand(unsigned i) { - return static_cast(User::getOperand(i)); - } - const Constant *getOperand(unsigned i) const { - return static_cast(User::getOperand(i)); + // Specialize get/setOperand for Users as their operands are always + // constants or BasicBlocks as well. + User *getOperand(unsigned i) { + return static_cast(User::getOperand(i)); } - void setOperand(unsigned i, Constant *C) { - User::setOperand(i, C); + const User *getOperand(unsigned i) const { + return static_cast(User::getOperand(i)); } /// getVectorElements - This method, which is only valid on constant of vector diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 0b881dc6889..99928d9b855 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -564,7 +564,7 @@ public: static BlockAddress *get(BasicBlock *BB); /// Transparently provide more efficient getOperand methods. - DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); Function *getFunction() const { return (Function*)Op<0>().get(); } BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); } @@ -587,7 +587,7 @@ template <> struct OperandTraits : public FixedNumOperandTraits<2> { }; -DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Constant) +DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value) //===----------------------------------------------------------------------===// /// ConstantExpr - a constant value that is initialized with an expression using diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 692c289b833..98f782ff5ba 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -751,10 +751,11 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, assert (0 && "Unknown FP type!"); } } else if (isa(C) && cast(C)->isString()) { + const ConstantArray *CA = cast(C); // Emit constant strings specially. - unsigned NumOps = C->getNumOperands(); + unsigned NumOps = CA->getNumOperands(); // If this is a null-terminated string, use the denser CSTRING encoding. - if (C->getOperand(NumOps-1)->isNullValue()) { + if (CA->getOperand(NumOps-1)->isNullValue()) { Code = bitc::CST_CODE_CSTRING; --NumOps; // Don't encode the null, which isn't allowed by char6. } else { @@ -764,7 +765,7 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, 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(C->getOperand(i))->getZExtValue(); + unsigned char V = cast(CA->getOperand(i))->getZExtValue(); Record.push_back(V); isCStr7 &= (V & 128) == 0; if (isCStrChar6) diff --git a/lib/Target/MSIL/MSILWriter.cpp b/lib/Target/MSIL/MSILWriter.cpp index f12ee78ccb4..949b91020fd 100644 --- a/lib/Target/MSIL/MSILWriter.cpp +++ b/lib/Target/MSIL/MSILWriter.cpp @@ -1529,7 +1529,7 @@ void MSILWriter::printStaticConstant(const Constant* C, uint64_t& Offset) { case Type::StructTyID: for (unsigned I = 0, E = C->getNumOperands(); IgetOperand(I),Offset); + printStaticConstant(cast(C->getOperand(I)), Offset); } break; case Type::PointerTyID: diff --git a/lib/Transforms/IPO/StripSymbols.cpp b/lib/Transforms/IPO/StripSymbols.cpp index 57aaf43c65a..369f0fc0c82 100644 --- a/lib/Transforms/IPO/StripSymbols.cpp +++ b/lib/Transforms/IPO/StripSymbols.cpp @@ -112,11 +112,11 @@ static bool OnlyUsedBy(Value *V, Value *Usr) { static void RemoveDeadConstant(Constant *C) { assert(C->use_empty() && "Constant is not dead!"); - SmallPtrSet Operands; + SmallPtrSet Operands; for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) if (isa(C->getOperand(i)->getType()) && OnlyUsedBy(C->getOperand(i), C)) - Operands.insert(C->getOperand(i)); + Operands.insert(cast(C->getOperand(i))); if (GlobalVariable *GV = dyn_cast(C)) { if (!GV->hasLocalLinkage()) return; // Don't delete non static globals. GV->eraseFromParent(); @@ -126,7 +126,7 @@ static void RemoveDeadConstant(Constant *C) { C->destroyConstant(); // If the constant referenced anything, see if we can delete it as well. - for (SmallPtrSet::iterator OI = Operands.begin(), + for (SmallPtrSet::iterator OI = Operands.begin(), OE = Operands.end(); OI != OE; ++OI) RemoveDeadConstant(*OI); } @@ -305,8 +305,7 @@ bool StripDebugDeclare::runOnModule(Module &M) { if (GlobalVariable *GV = dyn_cast(C)) { if (GV->hasLocalLinkage()) RemoveDeadConstant(GV); - } - else + } else RemoveDeadConstant(C); } diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 2c0a67f1d04..7f713d15c67 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -215,7 +215,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, switch (CE->getOpcode()) { default: return 0; case Instruction::Or: { - Constant *RHS = ExtractConstantBytes(C->getOperand(1), ByteStart, ByteSize); + Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); if (RHS == 0) return 0; @@ -224,13 +224,13 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, if (RHSC->isAllOnesValue()) return RHSC; - Constant *LHS = ExtractConstantBytes(C->getOperand(0), ByteStart, ByteSize); + Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); if (LHS == 0) return 0; return ConstantExpr::getOr(LHS, RHS); } case Instruction::And: { - Constant *RHS = ExtractConstantBytes(C->getOperand(1), ByteStart, ByteSize); + Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); if (RHS == 0) return 0; @@ -238,7 +238,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, if (RHS->isNullValue()) return RHS; - Constant *LHS = ExtractConstantBytes(C->getOperand(0), ByteStart, ByteSize); + Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); if (LHS == 0) return 0; return ConstantExpr::getAnd(LHS, RHS); @@ -259,7 +259,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, ByteSize*8)); // If the extract is known to be fully in the input, extract it. if (ByteStart+ByteSize+ShAmt <= CSize) - return ExtractConstantBytes(C->getOperand(0), ByteStart+ShAmt, ByteSize); + return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize); // TODO: Handle the 'partially zero' case. return 0; @@ -281,7 +281,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, ByteSize*8)); // If the extract is known to be fully in the input, extract it. if (ByteStart >= ShAmt) - return ExtractConstantBytes(C->getOperand(0), ByteStart-ShAmt, ByteSize); + return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize); // TODO: Handle the 'partially zero' case. return 0; @@ -289,7 +289,7 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, case Instruction::ZExt: { unsigned SrcBitSize = - cast(C->getOperand(0)->getType())->getBitWidth(); + cast(CE->getOperand(0)->getType())->getBitWidth(); // If extracting something that is completely zero, return 0. if (ByteStart*8 >= SrcBitSize) @@ -298,18 +298,18 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, // If exactly extracting the input, return it. if (ByteStart == 0 && ByteSize*8 == SrcBitSize) - return C->getOperand(0); + return CE->getOperand(0); // If extracting something completely in the input, if if the input is a // multiple of 8 bits, recurse. if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize) - return ExtractConstantBytes(C->getOperand(0), ByteStart, ByteSize); + return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize); // Otherwise, if extracting a subset of the input, which is not multiple of // 8 bits, do a shift and trunc to get the bits. if ((ByteStart+ByteSize)*8 < SrcBitSize) { assert((SrcBitSize&7) && "Shouldn't get byte sized case here"); - Constant *Res = C->getOperand(0); + Constant *Res = CE->getOperand(0); if (ByteStart) Res = ConstantExpr::getLShr(Res, ConstantInt::get(Res->getType(), ByteStart*8)); @@ -634,7 +634,15 @@ Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context, Idxs + NumIdx)); // Otherwise recurse. - return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs), + if (ConstantStruct *CS = dyn_cast(Agg)) + return ConstantFoldExtractValueInstruction(Context, CS->getOperand(*Idxs), + Idxs+1, NumIdx-1); + + if (ConstantArray *CA = dyn_cast(Agg)) + return ConstantFoldExtractValueInstruction(Context, CA->getOperand(*Idxs), + Idxs+1, NumIdx-1); + ConstantVector *CV = cast(Agg); + return ConstantFoldExtractValueInstruction(Context, CV->getOperand(*Idxs), Idxs+1, NumIdx-1); } @@ -714,11 +722,10 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, // Insertion of constant into aggregate constant. std::vector Ops(Agg->getNumOperands()); for (unsigned i = 0; i < Agg->getNumOperands(); ++i) { - Constant *Op = - (*Idxs == i) ? - ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i), - Val, Idxs+1, NumIdx-1) : - Agg->getOperand(i); + Constant *Op = cast(Agg->getOperand(i)); + if (*Idxs == i) + Op = ConstantFoldInsertValueInstruction(Context, Op, + Val, Idxs+1, NumIdx-1); Ops[i] = Op; } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 31d850cfc0e..0d7fabacea7 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -140,7 +140,7 @@ bool Constant::canTrap() const { // ConstantExpr traps if any operands can trap. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i)->canTrap()) + if (CE->getOperand(i)->canTrap()) return true; // Otherwise, only specific operations can trap. @@ -154,7 +154,7 @@ bool Constant::canTrap() const { case Instruction::SRem: case Instruction::FRem: // Div and rem can trap if the RHS is not known to be non-zero. - if (!isa(getOperand(1)) || getOperand(1)->isNullValue()) + if (!isa(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue()) return true; return false; } @@ -187,7 +187,8 @@ Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { PossibleRelocationsTy Result = NoRelocation; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - Result = std::max(Result, getOperand(i)->getRelocationInfo()); + Result = std::max(Result, + cast(getOperand(i))->getRelocationInfo()); return Result; } -- 2.34.1