From: Rafael Espindola Date: Sat, 18 Dec 2010 03:57:21 +0000 (+0000) Subject: Revert 122011, 122012, 122013, 122023 adding back an important optimization. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=d076482ab7e672d1d65a43809695e8d0d3995203;p=oota-llvm.git Revert 122011, 122012, 122013, 122023 adding back an important optimization. I added a note, but suggestions on how to add a test are really welcome. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122138 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/MC/MCExpr.h b/include/llvm/MC/MCExpr.h index 55e3f46a897..da3cebe414e 100644 --- a/include/llvm/MC/MCExpr.h +++ b/include/llvm/MC/MCExpr.h @@ -44,12 +44,14 @@ private: MCExpr(const MCExpr&); // DO NOT IMPLEMENT void operator=(const MCExpr&); // DO NOT IMPLEMENT - bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout, + bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, + const MCAsmLayout *Layout, const SectionAddrMap *Addrs) const; protected: explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {} - bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, + bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, + const MCAsmLayout *Layout, const SectionAddrMap *Addrs, bool InSet) const; public: @@ -76,16 +78,11 @@ public: /// values. If not given, then only non-symbolic expressions will be /// evaluated. /// @result - True on success. - bool EvaluateAsAbsolute(int64_t &Res) const { - return EvaluateAsAbsolute(Res, 0, 0); - } - bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const{ - return EvaluateAsAbsolute(Res, &Layout, 0); - } + bool EvaluateAsAbsolute(int64_t &Res) const; + bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const; + bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout, - const SectionAddrMap &Addrs) const { - return EvaluateAsAbsolute(Res, &Layout, &Addrs); - } + const SectionAddrMap &Addrs) const; /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable /// value, i.e. an expression of the fixed form (a - b + constant). @@ -93,10 +90,7 @@ public: /// @param Res - The relocatable value, if evaluation succeeds. /// @param Layout - The assembler layout object to use for evaluating values. /// @result - True on success. - bool EvaluateAsRelocatable(MCValue &Res, - const MCAsmLayout *Layout = 0) const { - return EvaluateAsRelocatableImpl(Res, Layout, 0, false); - } + bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout = 0) const; /// @} diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index 41feaa83757..47b81407d99 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -237,7 +237,27 @@ void MCTargetExpr::Anchor() {} /* *** */ -bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout, +bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const { + return EvaluateAsAbsolute(Res, 0, 0, 0); +} + +bool MCExpr::EvaluateAsAbsolute(int64_t &Res, + const MCAsmLayout &Layout) const { + return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0); +} + +bool MCExpr::EvaluateAsAbsolute(int64_t &Res, + const MCAsmLayout &Layout, + const SectionAddrMap &Addrs) const { + return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs); +} + +bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const { + return EvaluateAsAbsolute(Res, &Asm, 0, 0); +} + +bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, + const MCAsmLayout *Layout, const SectionAddrMap *Addrs) const { MCValue Value; @@ -247,8 +267,7 @@ bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout, return true; } - // FIXME: This use of Addrs is wrong, right? - if (!EvaluateAsRelocatableImpl(Value, Layout, Addrs, /*InSet=*/Addrs) || + if (!EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, Addrs) || !Value.isAbsolute()) { // EvaluateAsAbsolute is defined to return the "current value" of // the expression if we are given a Layout object, even in cases @@ -304,7 +323,13 @@ static void AttemptToFoldSymbolOffsetDifference(const MCAsmLayout *Layout, /// /// \returns True on success, false if the result is not representable in an /// MCValue. -static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, + +/// NOTE: It is really important to have both the Asm and Layout arguments. +/// They might look redundant, but this function can be used before layout +/// is done (see the object streamer for example) and having the Asm argument +/// lets us avoid relocations. +static bool EvaluateSymbolicAdd(const MCAssembler *Asm, + const MCAsmLayout *Layout, const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,const MCSymbolRefExpr *RHS_A, @@ -353,14 +378,17 @@ static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, // Absolutize symbol differences between defined symbols when we have a // layout object and the target requests it. - if (Layout && A && B) { - const MCAssembler &Asm = Layout->getAssembler(); + + assert((!Layout || Asm) && + "Must have an assembler object if layout is given!"); + + if (Asm && A && B) { const MCSymbol &SA = A->getSymbol(); const MCSymbol &SB = B->getSymbol(); - const MCObjectFormat &F = Asm.getBackend().getObjectFormat(); + const MCObjectFormat &F = Asm->getBackend().getObjectFormat(); if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) { - MCSymbolData &AD = Asm.getSymbolData(A->getSymbol()); - MCSymbolData &BD = Asm.getSymbolData(B->getSymbol()); + MCSymbolData &AD = Asm->getSymbolData(A->getSymbol()); + MCSymbolData &BD = Asm->getSymbolData(B->getSymbol()); if (AD.getFragment() == BD.getFragment()) { Res = MCValue::get(+ AD.getOffset() @@ -391,7 +419,17 @@ static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, return true; } +bool MCExpr::EvaluateAsRelocatable(MCValue &Res, + const MCAsmLayout *Layout) const { + if (Layout) + return EvaluateAsRelocatableImpl(Res, &Layout->getAssembler(), Layout, + 0, false); + else + return EvaluateAsRelocatableImpl(Res, 0, 0, 0, false); +} + bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, + const MCAssembler *Asm, const MCAsmLayout *Layout, const SectionAddrMap *Addrs, bool InSet) const { @@ -411,8 +449,10 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, // Evaluate recursively if this is a variable. if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) { - bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Layout, - Addrs, true); + bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm, + Layout, + Addrs, + true); // If we failed to simplify this to a constant, let the target // handle it. if (Ret && !Res.getSymA() && !Res.getSymB()) @@ -427,7 +467,7 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCUnaryExpr *AUE = cast(this); MCValue Value; - if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Layout, + if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, InSet)) return false; @@ -461,9 +501,9 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCBinaryExpr *ABE = cast(this); MCValue LHSValue, RHSValue; - if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Layout, + if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Addrs, InSet) || - !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Layout, + !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Addrs, InSet)) return false; @@ -475,13 +515,13 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, return false; case MCBinaryExpr::Sub: // Negate RHS and add. - return EvaluateSymbolicAdd(Layout, Addrs, InSet, LHSValue, + return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, RHSValue.getSymB(), RHSValue.getSymA(), -RHSValue.getConstant(), Res); case MCBinaryExpr::Add: - return EvaluateSymbolicAdd(Layout, Addrs, InSet, LHSValue, + return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, RHSValue.getSymA(), RHSValue.getSymB(), RHSValue.getConstant(), Res); diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 3a79d1921c2..cbc66f1cf9b 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -85,7 +85,7 @@ void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, // Avoid fixups when possible. int64_t AbsValue; - if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { + if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) { EmitIntValue(AbsValue, Size, AddrSpace); return; } @@ -116,7 +116,7 @@ void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) { int64_t IntValue; - if (Value->EvaluateAsAbsolute(IntValue)) { + if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { EmitULEB128IntValue(IntValue, AddrSpace); return; } @@ -126,7 +126,7 @@ void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value, void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) { int64_t IntValue; - if (Value->EvaluateAsAbsolute(IntValue)) { + if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { EmitSLEB128IntValue(IntValue, AddrSpace); return; } @@ -206,7 +206,7 @@ void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, MCBinaryExpr::Create(MCBinaryExpr::Sub, LabelRef, LastLabelRef, getContext()); int64_t Res; - if (AddrDelta->EvaluateAsAbsolute(Res)) { + if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { MCDwarfLineAddr::Emit(this, LineDelta, Res); return; }