diff options
author | Dan Gohman <gohman@apple.com> | 2009-07-20 21:19:07 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2009-07-20 21:19:07 +0000 |
commit | 1224c386981f7948f298ed9ad444c40609570f2e (patch) | |
tree | 69577cb79bc2b30c8f801de5c52a374189fdb7a4 /lib/Bitcode | |
parent | 33d0474bf5d5783cf9690bcab3eabd513d918fc5 (diff) |
Assembly and Bitcode support for unsigned/signed overflow flags and
exact sdiv flags.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76475 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bitcode')
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 27 | ||||
-rw-r--r-- | lib/Bitcode/Writer/BitcodeWriter.cpp | 38 |
2 files changed, 61 insertions, 4 deletions
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index bc1eba01e2..95276b1aec 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -20,6 +20,7 @@ #include "llvm/LLVMContext.h" #include "llvm/MDNode.h" #include "llvm/Module.h" +#include "llvm/Operator.h" #include "llvm/AutoUpgrade.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" @@ -747,6 +748,18 @@ bool BitcodeReader::ResolveGlobalAndAliasInits() { return false; } +static void SetOptimizationFlags(Value *V, uint64_t Flags) { + if (OverflowingBinaryOperator *OBO = + dyn_cast<OverflowingBinaryOperator>(V)) { + if (Flags & (1 << bitc::OBO_NO_SIGNED_OVERFLOW)) + OBO->setHasNoSignedOverflow(true); + if (Flags & (1 << bitc::OBO_NO_UNSIGNED_OVERFLOW)) + OBO->setHasNoUnsignedOverflow(true); + } else if (SDivOperator *Div = dyn_cast<SDivOperator>(V)) { + if (Flags & (1 << bitc::SDIV_EXACT)) + Div->setIsExact(true); + } +} bool BitcodeReader::ParseConstants() { if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) @@ -778,7 +791,8 @@ bool BitcodeReader::ParseConstants() { // Read a record. Record.clear(); Value *V = 0; - switch (Stream.ReadRecord(Code, Record)) { + unsigned BitCode = Stream.ReadRecord(Code, Record); + switch (BitCode) { default: // Default behavior: unknown constant case bitc::CST_CODE_UNDEF: // UNDEF V = Context.getUndef(CurTy); @@ -899,6 +913,8 @@ bool BitcodeReader::ParseConstants() { Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); V = Context.getConstantExpr(Opc, LHS, RHS); } + if (Record.size() >= 4) + SetOptimizationFlags(V, Record[3]); break; } case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] @@ -1451,7 +1467,8 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { // Read a record. Record.clear(); Instruction *I = 0; - switch (Stream.ReadRecord(Code, Record)) { + unsigned BitCode = Stream.ReadRecord(Code, Record); + switch (BitCode) { default: // Default behavior: reject return Error("Unknown instruction"); case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] @@ -1469,12 +1486,14 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { Value *LHS, *RHS; if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || getValue(Record, OpNum, LHS->getType(), RHS) || - OpNum+1 != Record.size()) + OpNum+1 > Record.size()) return Error("Invalid BINOP record"); - int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType()); + int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); if (Opc == -1) return Error("Invalid BINOP record"); I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); + if (OpNum < Record.size()) + SetOptimizationFlags(I, Record[3]); break; } case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index d7acd14a80..d245e5bcb1 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -21,6 +21,7 @@ #include "llvm/Instructions.h" #include "llvm/MDNode.h" #include "llvm/Module.h" +#include "llvm/Operator.h" #include "llvm/TypeSymbolTable.h" #include "llvm/ValueSymbolTable.h" #include "llvm/Support/ErrorHandling.h" @@ -50,6 +51,7 @@ enum { // FUNCTION_BLOCK abbrev id's. FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, FUNCTION_INST_BINOP_ABBREV, + FUNCTION_INST_BINOP_FLAGS_ABBREV, FUNCTION_INST_CAST_ABBREV, FUNCTION_INST_RET_VOID_ABBREV, FUNCTION_INST_RET_VAL_ABBREV, @@ -454,6 +456,22 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, } } +static uint64_t GetOptimizationFlags(const Value *V) { + uint64_t Flags = 0; + + if (const OverflowingBinaryOperator *OBO = + dyn_cast<OverflowingBinaryOperator>(V)) { + if (OBO->hasNoSignedOverflow()) + Flags |= 1 << bitc::OBO_NO_SIGNED_OVERFLOW; + if (OBO->hasNoUnsignedOverflow()) + Flags |= 1 << bitc::OBO_NO_UNSIGNED_OVERFLOW; + } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(V)) { + if (Div->isExact()) + Flags |= 1 << bitc::SDIV_EXACT; + } + + return Flags; +} static void WriteConstants(unsigned FirstVal, unsigned LastVal, const ValueEnumerator &VE, @@ -641,6 +659,9 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); Record.push_back(VE.getValueID(C->getOperand(0))); Record.push_back(VE.getValueID(C->getOperand(1))); + uint64_t Flags = GetOptimizationFlags(CE); + if (Flags != 0) + Record.push_back(Flags); } break; case Instruction::GetElementPtr: @@ -778,6 +799,12 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; Vals.push_back(VE.getValueID(I.getOperand(1))); Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); + uint64_t Flags = GetOptimizationFlags(&I); + if (Flags != 0) { + if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) + AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; + Vals.push_back(Flags); + } } break; @@ -1225,6 +1252,17 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { Abbv) != FUNCTION_INST_BINOP_ABBREV) llvm_unreachable("Unexpected abbrev ordering!"); } + { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags + if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, + Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) + llvm_unreachable("Unexpected abbrev ordering!"); + } { // INST_CAST abbrev for FUNCTION_BLOCK. BitCodeAbbrev *Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); |