From dc024674ff96820d6020757b48d47f46d4c07db2 Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Tue, 27 Nov 2007 13:23:08 +0000 Subject: Fix PR1146: parameter attributes are longer part of the function type, instead they belong to functions and function calls. This is an updated and slightly corrected version of Reid Spencer's original patch. The only known problem is that auto-upgrading of bitcode files doesn't seem to work properly (see test/Bitcode/AutoUpgradeIntrinsics.ll). Hopefully a bitcode guru (who might that be? :) ) will fix it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44359 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bitcode/Reader/BitcodeReader.cpp | 31 ++++++++++++++-------------- lib/Bitcode/Writer/BitcodeWriter.cpp | 37 ++++++++++------------------------ lib/Bitcode/Writer/ValueEnumerator.cpp | 17 +++++++++++----- 3 files changed, 39 insertions(+), 46 deletions(-) (limited to 'lib/Bitcode') diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 119a583..93542f3 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -329,15 +329,15 @@ bool BitcodeReader::ParseTypeTable() { ResultTy = PointerType::get(getTypeByID(Record[0], true)); break; case bitc::TYPE_CODE_FUNCTION: { - // FUNCTION: [vararg, attrid, retty, paramty x N] - if (Record.size() < 3) + // FUNCTION: [vararg, retty, paramty x N] + if (Record.size() < 2) return Error("Invalid FUNCTION type record"); std::vector ArgTys; - for (unsigned i = 3, e = Record.size(); i != e; ++i) + for (unsigned i = 2, e = Record.size(); i != e; ++i) ArgTys.push_back(getTypeByID(Record[i], true)); - ResultTy = FunctionType::get(getTypeByID(Record[2], true), ArgTys, - Record[0], getParamAttrs(Record[1])); + ResultTy = FunctionType::get(getTypeByID(Record[1], true), ArgTys, + Record[0]); break; } case bitc::TYPE_CODE_STRUCT: { // STRUCT: [ispacked, eltty x N] @@ -1033,9 +1033,8 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) { Func->setCallingConv(Record[1]); bool isProto = Record[2]; Func->setLinkage(GetDecodedLinkage(Record[3])); - - assert(Func->getFunctionType()->getParamAttrs() == - getParamAttrs(Record[4])); + const ParamAttrsList *PAL = getParamAttrs(Record[4]); + Func->setParamAttrs(PAL); Func->setAlignment((1 << Record[5]) >> 1); if (Record[6]) { @@ -1360,8 +1359,10 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { break; } - case bitc::FUNC_CODE_INST_INVOKE: { // INVOKE: [cc,fnty, op0,op1,op2, ...] + case bitc::FUNC_CODE_INST_INVOKE: { + // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] if (Record.size() < 4) return Error("Invalid INVOKE record"); + const ParamAttrsList *PAL = getParamAttrs(Record[0]); unsigned CCInfo = Record[1]; BasicBlock *NormalBB = getBasicBlock(Record[2]); BasicBlock *UnwindBB = getBasicBlock(Record[3]); @@ -1380,8 +1381,6 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { Record.size() < OpNum+FTy->getNumParams()) return Error("Invalid INVOKE record"); - assert(FTy->getParamAttrs() == getParamAttrs(Record[0])); - SmallVector Ops; for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i))); @@ -1403,6 +1402,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { I = new InvokeInst(Callee, NormalBB, UnwindBB, Ops.begin(), Ops.end()); cast(I)->setCallingConv(CCInfo); + cast(I)->setParamAttrs(PAL); break; } case bitc::FUNC_CODE_INST_UNWIND: // UNWIND @@ -1482,10 +1482,12 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); break; } - case bitc::FUNC_CODE_INST_CALL: { // CALL: [cc, fnty, fnid, arg0, arg1...] - if (Record.size() < 2) + case bitc::FUNC_CODE_INST_CALL: { + // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] + if (Record.size() < 3) return Error("Invalid CALL record"); + const ParamAttrsList *PAL = getParamAttrs(Record[0]); unsigned CCInfo = Record[1]; unsigned OpNum = 2; @@ -1499,8 +1501,6 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { if (!FTy || Record.size() < FTy->getNumParams()+OpNum) return Error("Invalid CALL record"); - assert(FTy->getParamAttrs() == getParamAttrs(Record[0])); - SmallVector Args; // Read the fixed params. for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { @@ -1527,6 +1527,7 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { I = new CallInst(Callee, Args.begin(), Args.end()); cast(I)->setCallingConv(CCInfo>>1); cast(I)->setTailCall(CCInfo & 1); + cast(I)->setParamAttrs(PAL); break; } case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 6bf2712..9249c70 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -147,8 +147,6 @@ static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, - Log2_32_Ceil(VE.getParamAttrs().size()+1))); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(VE.getTypes().size()+1))); @@ -206,10 +204,9 @@ static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { case Type::FunctionTyID: { const FunctionType *FT = cast(T); - // FUNCTION: [isvararg, attrid, retty, paramty x N] + // FUNCTION: [isvararg, retty, paramty x N] Code = bitc::TYPE_CODE_FUNCTION; TypeVals.push_back(FT->isVarArg()); - TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs())); TypeVals.push_back(VE.getTypeID(FT->getReturnType())); for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); @@ -383,18 +380,13 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, // Emit the function proto information. for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { - // FUNCTION: [type, callingconv, isproto, linkage, alignment, section, - // visibility] + // FUNCTION: [type, callingconv, isproto, paramattr, + // linkage, alignment, section, visibility] Vals.push_back(VE.getTypeID(F->getType())); Vals.push_back(F->getCallingConv()); Vals.push_back(F->isDeclaration()); Vals.push_back(getEncodedLinkage(F)); - - // Note: we emit the param attr ID number for the function type of this - // function. In the future, we intend for attrs to be properties of - // functions, instead of on the type. This is to support this future work. - Vals.push_back(VE.getParamAttrID(F->getFunctionType()->getParamAttrs())); - + Vals.push_back(VE.getParamAttrID(F->getParamAttrs())); Vals.push_back(Log2_32(F->getAlignment())+1); Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); Vals.push_back(getEncodedVisibility(F)); @@ -760,12 +752,9 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, const FunctionType *FTy = cast(PTy->getElementType()); Code = bitc::FUNC_CODE_INST_INVOKE; - // Note: we emit the param attr ID number for the function type of this - // function. In the future, we intend for attrs to be properties of - // functions, instead of on the type. This is to support this future work. - Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs())); - - Vals.push_back(cast(I).getCallingConv()); + const InvokeInst *II = cast(&I); + Vals.push_back(VE.getParamAttrID(II->getParamAttrs())); + Vals.push_back(II->getCallingConv()); Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee @@ -837,14 +826,10 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, Code = bitc::FUNC_CODE_INST_CALL; - // Note: we emit the param attr ID number for the function type of this - // function. In the future, we intend for attrs to be properties of - // functions, instead of on the type. This is to support this future work. - Vals.push_back(VE.getParamAttrID(FTy->getParamAttrs())); - - Vals.push_back((cast(I).getCallingConv() << 1) | - unsigned(cast(I).isTailCall())); - PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee + const CallInst *CI = cast(&I); + Vals.push_back(VE.getParamAttrID(CI->getParamAttrs())); + Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall())); + PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee // Emit value #'s for the fixed parameters. for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index 6b3885e..21b0372 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -17,6 +17,7 @@ #include "llvm/Module.h" #include "llvm/TypeSymbolTable.h" #include "llvm/ValueSymbolTable.h" +#include "llvm/Instructions.h" #include using namespace llvm; @@ -44,8 +45,10 @@ ValueEnumerator::ValueEnumerator(const Module *M) { EnumerateValue(I); // Enumerate the functions. - for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) + for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { EnumerateValue(I); + EnumerateParamAttrs(cast(I)->getParamAttrs()); + } // Enumerate the aliases. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); @@ -86,6 +89,10 @@ ValueEnumerator::ValueEnumerator(const Module *M) { OI != E; ++OI) EnumerateOperandType(*OI); EnumerateType(I->getType()); + if (const CallInst *CI = dyn_cast(I)) + EnumerateParamAttrs(CI->getParamAttrs()); + else if (const InvokeInst *II = dyn_cast(I)) + EnumerateParamAttrs(II->getParamAttrs()); } } @@ -220,10 +227,6 @@ void ValueEnumerator::EnumerateType(const Type *Ty) { for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); I != E; ++I) EnumerateType(*I); - - // If this is a function type, enumerate the param attrs. - if (const FunctionType *FTy = dyn_cast(Ty)) - EnumerateParamAttrs(FTy->getParamAttrs()); } // Enumerate the types for the specified value. If the value is a constant, @@ -296,6 +299,10 @@ void ValueEnumerator::incorporateFunction(const Function &F) { // Optimize the constant layout. OptimizeConstants(FirstFuncConstantID, Values.size()); + // Add the function's parameter attributes so they are available for use in + // the function's instruction. + EnumerateParamAttrs(F.getParamAttrs()); + FirstInstID = Values.size(); // Add all of the instructions. -- cgit v1.1