diff options
Diffstat (limited to 'lib/Bitcode/NaCl/Writer')
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp | 41 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp | 57 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h | 9 |
3 files changed, 92 insertions, 15 deletions
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<unsigned, 64> &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<uint64_t, 128> &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<uint64_t, 128> &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<unsigned, 64> &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<AllocaInst>(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<LoadInst>(I).getAlignment())+1); Vals.push_back(cast<LoadInst>(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!"); diff --git a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp index 3ad224a04f..7da01f005e 100644 --- a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp +++ b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.cpp @@ -231,7 +231,11 @@ void NaClValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) EnumerateValue(VI->getValue()); } -void NaClValueEnumerator::EnumerateValue(const Value *V) { +void NaClValueEnumerator::EnumerateValue(const Value *VIn) { + // Skip over elided values. + const Value *V = ElideCasts(VIn); + if (V != VIn) return; + assert(!V->getType()->isVoidTy() && "Can't insert void values!"); assert(!isa<MDNode>(V) && !isa<MDString>(V) && "EnumerateValue doesn't handle Metadata!"); @@ -423,3 +427,54 @@ void NaClValueEnumerator::purgeFunction() { BasicBlocks.clear(); FnForwardTypeRefs.clear(); } + +// Returns true if the bitcode writer can assume that the given +// argument of the given operation can accept a normalized pointer. +// Note: This function is based on the concept of NormalizedPtr as +// defined in llvm/lib/Transforms/NaCl/ReplacePtrsWithInts.cpp. +static bool AllowsNormalizedPtr(const Value *V, const Instruction *Arg) { + const Instruction *I = dyn_cast<Instruction>(V); + if (I == 0) return false; + + // TODO(kschimpf) Expand this list to any operation that can handle + // normalized pointers. That is loads and stores, function calls, and + // instrinsic calls. + switch (I->getOpcode()) { + default: + return false; + case Instruction::Load: + return I->getOperand(0) == Arg; + } +} + +// Returns true if the bitcode reader and writer can assume that the +// uses of the given inttotpr I2P allow normalized pointers (as +// defined in llvm/lib/Transforms/NaCl/ReplacePtrsWithInts.cpp). +static bool IntToPtrUsesAllowEliding(const Instruction *I2P) { + for (Value::const_use_iterator u = I2P->use_begin(), e = I2P->use_end(); + u != e; ++u) { + if (!AllowsNormalizedPtr(cast<Value>(*u), I2P)) return false; + } + // If reached, either all uses have a normalized pointer (and hence + // we know how to automatically add it back), or there were no uses (and + // hence represents dead code). + return true; +} + +// Note: This function is based on the comments in +// llvm/lib/Transforms/NaCl/ReplacePtrsWithInts.cpp. +const Value *NaClValueEnumerator::ElideCasts(const Value *V) { + if (PNaClVersion == 1) return V; + // TODO(kschimpf): Expand this out to cover all cases. + if (const Instruction *I = dyn_cast<Instruction>(V)) { + switch (I->getOpcode()) { + default: + break; + case Instruction::IntToPtr: + if (IntToPtrUsesAllowEliding(I)) { + return ElideCasts(I->getOperand(0)); + } + } + } + return V; +} diff --git a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h index 3e71740f92..22de263c4b 100644 --- a/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h +++ b/lib/Bitcode/NaCl/Writer/NaClValueEnumerator.h @@ -135,6 +135,15 @@ public: void incorporateFunction(const Function &F); void purgeFunction(); + /// \brief Returns the value after elided (cast) operations have been + /// removed. Returns V if unable to elide the cast. + const Value *ElideCasts(const Value *V); + + /// \brief Returns true if value V is an elided (cast) operation. + bool IsElidedCast(const Value *V) { + return V != ElideCasts(V); + } + private: void OptimizeTypes(const Module *M); void OptimizeConstants(unsigned CstStart, unsigned CstEnd); |