Don't fold a load if the other operand is a TLS address.
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 10 Apr 2009 10:09:34 +0000 (10:09 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 10 Apr 2009 10:09:34 +0000 (10:09 +0000)
With this we generate

movl    %gs:0, %eax
leal    i@NTPOFF(%eax), %eax

instead of

movl    $i@NTPOFF, %eax
addl    %gs:0, %eax

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@68778 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/X86/X86ISelDAGToDAG.cpp
test/CodeGen/X86/tls10.ll
test/CodeGen/X86/tls15.ll [new file with mode: 0644]
test/CodeGen/X86/tls2.ll
test/CodeGen/X86/tls6.ll
test/CodeGen/X86/tls8.ll

index 2cd6f74f27e0ce18b4d56ff6ed8f8abe4fd09fee..1b9572cc41266fe55a1f9801df72373fbe2eea07 100644 (file)
@@ -322,6 +322,8 @@ bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
     case ISD::AND:
     case ISD::OR:
     case ISD::XOR: {
+      SDValue Op1 = U->getOperand(1);
+
       // If the other operand is a 8-bit immediate we should fold the immediate
       // instead. This reduces code size.
       // e.g.
@@ -332,9 +334,25 @@ bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
       // addl 4(%esp), %eax
       // The former is 2 bytes shorter. In case where the increment is 1, then
       // the saving can be 4 bytes (by using incl %eax).
-      if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1)))
+      if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
         if (Imm->getAPIntValue().isSignedIntN(8))
           return false;
+
+      // If the other operand is a TLS address, we should fold it instead.
+      // This produces
+      // movl    %gs:0, %eax
+      // leal    i@NTPOFF(%eax), %eax
+      // instead of
+      // movl    $i@NTPOFF, %eax
+      // addl    %gs:0, %eax
+      // if the block also has an access to a second TLS address this will save
+      // a load.
+      // FIXME: This is probably also true for non TLS addresses.
+      if (Op1.getOpcode() == X86ISD::Wrapper) {
+        SDValue Val = Op1.getOperand(0);
+        if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
+          return false;
+      }
     }
     }
 
@@ -1170,13 +1188,16 @@ bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
                                     SDValue &Base, SDValue &Scale,
                                     SDValue &Index, SDValue &Disp) {
   X86ISelAddressMode AM;
-  if (MatchAddress(N, AM))
-    return false;
 
-  //Is it better to set AM.Segment before calling MatchAddress to
-  //prevent it from adding a segment?
-  if (AM.Segment.getNode())
+  // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
+  // segments.
+  SDValue Copy = AM.Segment;
+  SDValue T = CurDAG->getRegister(0, MVT::i32);
+  AM.Segment = T;
+  if (MatchAddress(N, AM))
     return false;
+  assert (T == AM.Segment);
+  AM.Segment = Copy;
 
   MVT VT = N.getValueType();
   unsigned Complexity = 0;
index 5f022e3992baab052b6da7f7584bcb9831e91cda..a4f2fb1293b94d481a46e286404fbb7df1546247 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
-; RUN: grep {movl      \$i@NTPOFF, %eax} %t
-; RUN: grep {addl      %gs:0, %eax} %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
 
 @i = external hidden thread_local global i32
 
diff --git a/test/CodeGen/X86/tls15.ll b/test/CodeGen/X86/tls15.ll
new file mode 100644 (file)
index 0000000..5d3ee16
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
+; RUN: grep {movl      %gs:0, %eax} %t | count 1
+; RUN: grep {leal      i@NTPOFF(%eax), %ecx} %t
+; RUN: grep {leal      j@NTPOFF(%eax), %eax} %t
+
+@i = thread_local global i32 0
+@j = thread_local global i32 0
+
+define void @f(i32** %a, i32** %b) {
+entry:
+       store i32* @i, i32** %a, align 8
+       store i32* @j, i32** %b, align 8
+       ret void
+}
index 8edc64ffba493c217eda35024bf9db8ef88b1874..fb57ae1a3917073ac35ec4864e0a8b3b29600ae1 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
-; RUN: grep {movl      \$i@NTPOFF, %eax} %t
-; RUN: grep {addl      %gs:0, %eax} %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
 
 @i = thread_local global i32 15
 
index fc10a57cc1bdb55149018a844d63cc1e3bdc25d7..e0bcade8dd88512b55a0518f8a120c096237be43 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
-; RUN: grep {movl      \$i@NTPOFF, %eax} %t
-; RUN: grep {addl      %gs:0, %eax} %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
 
 @i = internal thread_local global i32 15
 
index fb570b0118eb0489886b60ef75ff95c252506ef9..4971fd23ba6b238f0a964f421c566b2503249be1 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu > %t
-; RUN: grep {movl      \$i@NTPOFF, %eax} %t
-; RUN: grep {addl      %gs:0, %eax} %t
+; RUN: grep {movl      %gs:0, %eax} %t
+; RUN: grep {leal      i@NTPOFF(%eax), %eax} %t
 
 @i = hidden thread_local global i32 15