From b9657234ee8b1951db5977a8ffb55a2e5df6d76c Mon Sep 17 00:00:00 2001 From: Karl Schimpf Date: Thu, 1 Aug 2013 07:12:23 -0700 Subject: Remove the inttoptr dependency from load instructions. Elides inttoptr casts used (exclusively) in load instructions when PNaClVersion=2. This is an incremental start on removing the inttoptr instruction from the PNaCl wire format (See issue 3544 for more information on the strategy of removing ptrtoint). Also modifies PNaCl bitcode reader/writer to accept PNaClVersion=1 as supported, and PNaClVersion=2 as unsupported but readable (allowing pnacl-freeze and pnacl-thaw to work on such files). Also allows command-line option --pnacl-version for setting PNaClVersion in the PNaCl bitcode writer. Also fixes some problems on PNaCl bitcode headers, using common support to determine when the read/written PNaCl bitcode file is valid. BUG= https://code.google.com/p/nativeclient/issues/detail?id=3544 R=jvoung@chromium.org Review URL: https://codereview.chromium.org/5812155903377408 --- lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 41 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp') diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp index 9f3626557b..ac2a6a0acf 100644 --- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp @@ -811,8 +811,9 @@ static void pushValue(const Value *V, unsigned InstID, SmallVector &Vals, NaClValueEnumerator &VE, NaClBitstreamWriter &Stream) { - EmitFnForwardTypeRef(V, InstID, VE, Stream); - unsigned ValID = VE.getValueID(V); + const Value *VElided = VE.ElideCasts(V); + EmitFnForwardTypeRef(VElided, InstID, VE, Stream); + unsigned ValID = VE.getValueID(VElided); // Make encoding relative to the InstID. Vals.push_back(InstID - ValID); } @@ -821,8 +822,9 @@ static void pushValue64(const Value *V, unsigned InstID, SmallVector &Vals, NaClValueEnumerator &VE, NaClBitstreamWriter &Stream) { - EmitFnForwardTypeRef(V, InstID, VE, Stream); - uint64_t ValID = VE.getValueID(V); + const Value *VElided = VE.ElideCasts(V); + EmitFnForwardTypeRef(VElided, InstID, VE, Stream); + uint64_t ValID = VE.getValueID(VElided); Vals.push_back(InstID - ValID); } @@ -830,14 +832,16 @@ static void pushValueSigned(const Value *V, unsigned InstID, SmallVector &Vals, NaClValueEnumerator &VE, NaClBitstreamWriter &Stream) { - EmitFnForwardTypeRef(V, InstID, VE, Stream); - unsigned ValID = VE.getValueID(V); + const Value *VElided = VE.ElideCasts(V); + EmitFnForwardTypeRef(VElided, InstID, VE, Stream); + unsigned ValID = VE.getValueID(VElided); int64_t diff = ((int32_t)InstID - (int32_t)ValID); emitSignedInt64(Vals, diff); } /// WriteInstruction - Emit an instruction to the specified stream. -static void WriteInstruction(const Instruction &I, unsigned InstID, +/// Returns true if instruction actually emitted. +static bool WriteInstruction(const Instruction &I, unsigned InstID, NaClValueEnumerator &VE, NaClBitstreamWriter &Stream, SmallVector &Vals) { @@ -848,6 +852,8 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, default: if (Instruction::isCast(I.getOpcode())) { // CAST: [opval, destty, castopc] + if (VE.IsElidedCast(&I)) + return false; Code = naclbitc::FUNC_CODE_INST_CAST; AbbrevToUse = FUNCTION_INST_CAST_ABBREV; pushValue(I.getOperand(0), InstID, Vals, VE, Stream); @@ -965,7 +971,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, // Also do expected action - clear external Vals collection: Vals.clear(); - return; + return true; } break; case Instruction::Unreachable: @@ -988,7 +994,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, // Emit a Vals64 vector and exit. Stream.EmitRecord(Code, Vals64, AbbrevToUse); Vals64.clear(); - return; + return true; } case Instruction::Alloca: @@ -998,13 +1004,17 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, pushValue(I.getOperand(0), InstID, Vals, VE, Stream); // size. Vals.push_back(Log2_32(cast(I).getAlignment())+1); break; - case Instruction::Load: + // PNaCl Version 1: LOAD: [op, align, vol] + // PNaCl Version 2: LOAD: [op, align, vol, ty] Code = naclbitc::FUNC_CODE_INST_LOAD; - pushValue(I.getOperand(0), InstID, Vals, VE, Stream); // ptr + pushValue(I.getOperand(0), InstID, Vals, VE, Stream); AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; Vals.push_back(Log2_32(cast(I).getAlignment())+1); Vals.push_back(cast(I).isVolatile()); + if (PNaClVersion == 2) { + Vals.push_back(VE.getTypeID(I.getType())); + } break; case Instruction::Store: Code = naclbitc::FUNC_CODE_INST_STORE; @@ -1047,6 +1057,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, Stream.EmitRecord(Code, Vals, AbbrevToUse); Vals.clear(); + return true; } // Emit names for globals/functions etc. @@ -1133,9 +1144,8 @@ static void WriteFunction(const Function &F, NaClValueEnumerator &VE, for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { - WriteInstruction(*I, InstID, VE, Stream, Vals); - - if (!I->getType()->isVoidTy()) + if (WriteInstruction(*I, InstID, VE, Stream, Vals) && + !I->getType()->isVoidTy()) ++InstID; } @@ -1233,6 +1243,9 @@ static void WriteBlockInfo(const NaClValueEnumerator &VE, Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 6)); // Ptr Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 4)); // Align Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::Fixed, 1)); // volatile + if (PNaClVersion == 2) { + Abbv->Add(NaClBitCodeAbbrevOp(NaClBitCodeAbbrevOp::VBR, 4)); // Typecast + } if (Stream.EmitBlockInfoAbbrev(naclbitc::FUNCTION_BLOCK_ID, Abbv) != FUNCTION_INST_LOAD_ABBREV) llvm_unreachable("Unexpected abbrev ordering!"); -- cgit v1.2.3-18-g5258