diff options
author | Karl Schimpf <kschimpf@google.com> | 2013-08-14 14:57:21 -0700 |
---|---|---|
committer | Karl Schimpf <kschimpf@google.com> | 2013-08-14 14:57:21 -0700 |
commit | 1d5f6749707a8821be66ad7bad1b48ad67257110 (patch) | |
tree | 1aed5bd3e31b35a51d48b6aad4e16b0118d51693 /lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | |
parent | 1a563f0e433442f3f7aa60b636cc6a95d1c22c29 (diff) |
Remove ptrtoint instructions from the PNaCl bitcode file.
Removes ptrtoint instructions when applicable (currently only in stores), and add them back just before their use.
Note: This code does not handle ptrtoint casts for calls and phi nodes, binary operators, etc. because handling of casts for these instructions has not been added yet.
BUG= https://code.google.com/p/nativeclient/issues/detail?id=3544
R=jvoung@chromium.org
Review URL: https://codereview.chromium.org/22633002
Diffstat (limited to 'lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp index f9d479767b..1bbbf4516a 100644 --- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp @@ -1314,6 +1314,15 @@ bool NaClBitcodeReader::InstallInstruction( return false; } +Value *NaClBitcodeReader::ConvertOpToScalar(Value *Op, BasicBlock *BB) { + if (Op->getType()->isPointerTy()) { + Instruction *Conversion = new PtrToIntInst(Op, IntPtrType); + InstallInstruction(BB, Conversion); + return Conversion; + } + return Op; +} + Value *NaClBitcodeReader::ConvertOpToType(Value *Op, Type *T, BasicBlock *BB) { // Note: Currently only knows how to add inttoptr and bitcast type // conversions for non-phi nodes, since these are the only elided @@ -1326,7 +1335,7 @@ Value *NaClBitcodeReader::ConvertOpToType(Value *Op, Type *T, BasicBlock *BB) { if (OpTy->isPointerTy()) { Conversion = new BitCastInst(Op, T); - } else if (OpTy->isIntegerTy()) { + } else if (OpTy == IntPtrType) { Conversion = new IntToPtrInst(Op, T); } @@ -1341,6 +1350,10 @@ Value *NaClBitcodeReader::ConvertOpToType(Value *Op, Type *T, BasicBlock *BB) { return Conversion; } +Type *NaClBitcodeReader::ConvertTypeToScalarType(Type *T) { + return T->isPointerTy() ? IntPtrType : T; +} + /// ParseFunctionBody - Lazily parse the specified function body block. bool NaClBitcodeReader::ParseFunctionBody(Function *F) { DEBUG(dbgs() << "-> ParseFunctionBody\n"); @@ -1427,6 +1440,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { OpNum+1 > Record.size()) return Error("Invalid BINOP record"); + LHS = ConvertOpToScalar(LHS, CurBB); + RHS = ConvertOpToScalar(RHS, CurBB); + int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); if (Opc == -1) return Error("Invalid BINOP record"); I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); @@ -1475,6 +1491,24 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { int Opc = GetDecodedCastOpcode(Record[OpNum+1]); if (Opc == -1 || ResTy == 0) return Error("Invalid CAST record"); + + if (GetPNaClVersion() == 2) { + // If a ptrtoint cast was elided on the argument of the cast, + // add it back. Note: The casts allowed here should match the + // casts in NaClValueEnumerator::ExpectsScalarValue. + switch (Opc) { + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + Op = ConvertOpToScalar(Op, CurBB); + break; + default: + break; + } + } + I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy); break; } @@ -1489,6 +1523,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, &OpNum, NextValueNo, &Cond)) return Error("Invalid SELECT record"); + TrueVal = ConvertOpToScalar(TrueVal, CurBB); + FalseVal = ConvertOpToScalar(FalseVal, CurBB); + // expect i1 if (Cond->getType() != Type::getInt1Ty(Context)) return Error("Invalid SELECT condition type"); @@ -1507,6 +1544,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { OpNum+1 != Record.size()) return Error("Invalid CMP record"); + LHS = ConvertOpToScalar(LHS, CurBB); + RHS = ConvertOpToScalar(RHS, CurBB); + if (LHS->getType()->isFPOrFPVectorTy()) I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); else @@ -1612,6 +1652,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { Type *Ty = getTypeByID(Record[0]); if (!Ty) return Error("Invalid PHI record"); + // TODO(kschimpf): Fix handling of converting types for values, + // to handle elided casts, once the bitcode writer knows how. + PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { @@ -1684,6 +1727,7 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { case 2: if (OpNum+1 != Record.size()) return Error("Invalid STORE record"); + Val = ConvertOpToScalar(Val, CurBB); Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBB); I = new StoreInst(Val, Ptr, false, (1 << Record[OpNum]) >> 1); break; @@ -1695,6 +1739,9 @@ bool NaClBitcodeReader::ParseFunctionBody(Function *F) { if (Record.size() < 2) return Error("Invalid CALL record"); + // TODO(kschimpf): Fix handling of type conversion to arguments for PNaCl, + // to handle elided casts, once the bitcode writer knows how. + unsigned CCInfo = Record[0]; unsigned OpNum = 1; |