From cd4a14419f49fb236d65e5b69d871d6fc61fcb99 Mon Sep 17 00:00:00 2001 From: Mark Seaborn Date: Thu, 5 Sep 2013 09:41:53 -0700 Subject: 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 --- lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 31 ++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp') 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(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(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 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(I)->setCallingConv(GetDecodedCallingConv(CCInfo>>1)); -- cgit v1.2.3-18-g5258