diff options
author | Mark Seaborn <mseaborn@chromium.org> | 2013-09-05 09:41:53 -0700 |
---|---|---|
committer | Mark Seaborn <mseaborn@chromium.org> | 2013-09-05 09:41:53 -0700 |
commit | cd4a14419f49fb236d65e5b69d871d6fc61fcb99 (patch) | |
tree | ddb320f736845f4aa93ef3ab42400f1fbf69584e /lib | |
parent | 1180f259c88b1eb1000d0aaf5753b3da9f8e4e51 (diff) |
PNaCl bitcode: Indirect calls: Store return type instead of function type
For indirect call instructions (INST_CALL_INDIRECT), it's not
necessary to store the full function type. The argument types are
already known from the arguments in the instruction. We only need to
store the return type to be able to reconstruct the full function
type.
Storing only the return type ID will make the bitcode a little more
compact. Return type IDs will be frequently-used scalar types, which
can be given smaller type IDs than function types, which are less
frequently used.
This potentially makes the writer simpler: In principle, the writer no
longer needs to make a pass across all functions' bodies to determine
which function types are used in order to build the type table.
BUG=https://code.google.com/p/nativeclient/issues/detail?id=3544
TEST=*.ll tests
Review URL: https://codereview.chromium.org/23521005
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 31 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 6 |
2 files changed, 20 insertions, 17 deletions
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp index eaaa85a164..29a842c948 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp @@ -1626,16 +1626,11 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { // Build function type for call. FunctionType *FTy = 0; + Type *ReturnType = 0; if (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT) { // Callee type has been elided, add back in. - Type *Type = getTypeByID(Record[2]); + ReturnType = getTypeByID(Record[2]); ++OpNum; - if (FunctionType *FcnType = dyn_cast<FunctionType>(Type)) { - FTy = FcnType; - Callee = ConvertOpToType(Callee, FcnType->getPointerTo(), CurBBNo); - } else { - return Error("Invalid type for CALL_INDIRECT record"); - } } else { // Get type signature from callee. if (PointerType *OpTy = dyn_cast<PointerType>(Callee->getType())) { @@ -1646,7 +1641,7 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { } unsigned NumParams = Record.size() - OpNum; - if (NumParams != FTy->getNumParams()) + if (FTy && NumParams != FTy->getNumParams()) return Error("Invalid CALL record"); // Process call arguments. @@ -1655,14 +1650,26 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { Value *Arg; if (popValue(Record, &OpNum, NextValueNo, &Arg)) Error("Invalid argument in CALL record"); - if (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT && - FTy->getParamType(Index)->isPointerTy()) { - return Error("Pointer arguments not allowed for indirect calls"); + if (FTy) { + // Add a cast, to a pointer type if necessary, in case this + // is an intrinsic call that takes a pointer argument. + Arg = ConvertOpToType(Arg, FTy->getParamType(Index), CurBBNo); + } else { + Arg = ConvertOpToScalar(Arg, CurBBNo); } - Arg = ConvertOpToType(Arg, FTy->getParamType(Index), CurBBNo); Args.push_back(Arg); } + if (FTy == 0) { + // Reconstruct the function type and cast the function pointer + // to it. + SmallVector<Type*, 6> ArgTypes; + for (unsigned Index = 0; Index < NumParams; ++Index) + ArgTypes.push_back(Args[Index]->getType()); + FTy = FunctionType::get(ReturnType, ArgTypes, false); + Callee = ConvertOpToType(Callee, FTy->getPointerTo(), CurBBNo); + } + // Construct call. I = CallInst::Create(Callee, Args); cast<CallInst>(I)->setCallingConv(GetDecodedCallingConv(CCInfo>>1)); diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp index 166a95bb23..4288aceb06 100644 --- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp @@ -893,11 +893,7 @@ static bool WriteInstruction(const Instruction &I, unsigned InstID, // not known. Hence, we must send the type signature to the // reader. Code = naclbitc::FUNC_CODE_INST_CALL_INDIRECT; - PointerType *FcnPtrType = - cast<PointerType>(Callee->getType()); - FunctionType *FcnType = - cast<FunctionType>(FcnPtrType->getElementType()); - Vals.push_back(VE.getTypeID(FcnType)); + Vals.push_back(VE.getTypeID(I.getType())); } } |