From bbd4b303e3792d94239640a1075b66dedeaa37fe Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 14 Oct 2002 03:33:02 +0000 Subject: [PATCH] There is no way to guarantee that constants are not forward referenced. Handle forward referenced constants in a general way. This fixes bug: Assembler/2002-10-13-ConstantEncodingProblem.llx and allows the SPEC 197.parser benchmark to be built git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4161 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bytecode/Reader/ConstantReader.cpp | 37 ++++++-------------------- lib/Bytecode/Reader/Reader.cpp | 28 +++++++++++++++++++ lib/Bytecode/Reader/ReaderInternals.h | 1 + 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/Bytecode/Reader/ConstantReader.cpp b/lib/Bytecode/Reader/ConstantReader.cpp index 3fb2530c119..e71441edd8a 100644 --- a/lib/Bytecode/Reader/ConstantReader.cpp +++ b/lib/Bytecode/Reader/ConstantReader.cpp @@ -202,28 +202,8 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf, << ArgValSlot << "\n"); // Get the arg value from its slot if it exists, otherwise a placeholder - Value *Val = getValue(ArgTy, ArgValSlot, false); - Constant *C; - if (Val) { - if (!(C = dyn_cast(Val))) return true; - BCR_TRACE(5, "Constant Found in ValueTable!\n"); - } else { // Nope... find or create a forward ref. for it - GlobalRefsType::iterator I = - GlobalRefs.find(make_pair(ArgTy, ArgValSlot)); - - if (I != GlobalRefs.end()) { - BCR_TRACE(5, "Previous forward ref found!\n"); - C = cast(I->second); - } else { - // Create a placeholder for the constant reference and - // keep track of the fact that we have a forward ref to recycle it - BCR_TRACE(5, "Creating new forward ref to a constant!\n"); - C = new ConstPHolder(ArgTy, ArgValSlot); - - // Keep track of the fact that we have a forward ref to recycle it - GlobalRefs.insert(make_pair(make_pair(ArgTy, ArgValSlot), C)); - } - } + Constant *C = getConstantValue(ArgTy, ArgValSlot); + if (C == 0) return true; ArgVec.push_back(C); } @@ -310,9 +290,9 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf, while (NumElements--) { // Read all of the elements of the constant. unsigned Slot; if (read_vbr(Buf, EndBuf, Slot)) return true; - Value *V = getValue(AT->getElementType(), Slot, false); - if (!V || !isa(V)) return true; - Elements.push_back(cast(V)); + Constant *C = getConstantValue(AT->getElementType(), Slot); + if (!C) return true; + Elements.push_back(C); } V = ConstantArray::get(AT, Elements); break; @@ -326,10 +306,9 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf, for (unsigned i = 0; i < ET.size(); ++i) { unsigned Slot; if (read_vbr(Buf, EndBuf, Slot)) return true; - Value *V = getValue(ET[i], Slot, false); - if (!V || !isa(V)) - return true; - Elements.push_back(cast(V)); + Constant *C = getConstantValue(ET[i], Slot); + if (!C) return true; + Elements.push_back(C); } V = ConstantStruct::get(ST, Elements); diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp index 9898af93187..9aa54552110 100644 --- a/lib/Bytecode/Reader/Reader.cpp +++ b/lib/Bytecode/Reader/Reader.cpp @@ -132,6 +132,34 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { return d; } +/// getConstantValue - Just like getValue, except that it returns a null pointer +/// only on error. It always returns a constant (meaning that if the value is +/// defined, but is not a constant, that is an error). If the specified +/// constant hasn't been parsed yet, a placeholder is defined and used. Later, +/// after the real value is parsed, the placeholder is eliminated. +/// +Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) { + if (Value *V = getValue(Ty, Slot, false)) + return dyn_cast(V); // If we already have the value parsed... + + GlobalRefsType::iterator I = GlobalRefs.find(make_pair(Ty, Slot)); + if (I != GlobalRefs.end()) { + BCR_TRACE(5, "Previous forward ref found!\n"); + return cast(I->second); + } else { + // Create a placeholder for the constant reference and + // keep track of the fact that we have a forward ref to recycle it + BCR_TRACE(5, "Creating new forward ref to a constant!\n"); + Constant *C = new ConstPHolder(Ty, Slot); + + // Keep track of the fact that we have a forward ref to recycle it + GlobalRefs.insert(make_pair(make_pair(Ty, Slot), C)); + return C; + } +} + + + bool BytecodeParser::postResolveValues(ValueTable &ValTab) { bool Error = false; for (unsigned ty = 0; ty < ValTab.size(); ++ty) { diff --git a/lib/Bytecode/Reader/ReaderInternals.h b/lib/Bytecode/Reader/ReaderInternals.h index c156f515b88..8f656778219 100644 --- a/lib/Bytecode/Reader/ReaderInternals.h +++ b/lib/Bytecode/Reader/ReaderInternals.h @@ -108,6 +108,7 @@ private: Value *getValue(const Type *Ty, unsigned num, bool Create = true); const Type *getType(unsigned ID); + Constant *getConstantValue(const Type *Ty, unsigned num); int insertValue(Value *D, std::vector &D); // -1 = Failure bool postResolveValues(ValueTable &ValTab); -- 2.34.1