Revert 122011, 122012, 122013, 122023 adding back an important optimization.
authorRafael Espindola <rafael.espindola@gmail.com>
Sat, 18 Dec 2010 03:57:21 +0000 (03:57 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Sat, 18 Dec 2010 03:57:21 +0000 (03:57 +0000)
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

include/llvm/MC/MCExpr.h
lib/MC/MCExpr.cpp
lib/MC/MCObjectStreamer.cpp

index 55e3f46a897684bbda1eab1a24952419b897e39b..da3cebe414e623e93d78fe5bfad0ce9241605857 100644 (file)
@@ -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;
 
   /// @}
 
index 41feaa837573b129049fb0c4d435751156978904..47b81407d99e4282d68ad93defa6205542d3aa50 100644 (file)
@@ -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<MCUnaryExpr>(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<MCBinaryExpr>(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);
index 3a79d1921c2983e973c8c14a6e347ae780aa838a..cbc66f1cf9b8415b99b53f9a8e375091d08e9f5a 100644 (file)
@@ -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;
   }