PR13578: Teach MachineCSE that instructions that use a constant register can be CSE...
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 11 Aug 2012 19:05:13 +0000 (19:05 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 11 Aug 2012 19:05:13 +0000 (19:05 +0000)
This is common e.g. when doing rip-relative addressing on x86_64.

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

lib/CodeGen/MachineCSE.cpp
test/CodeGen/X86/lsr-loop-exit-cond.ll
test/CodeGen/X86/machine-cse.ll

index 5153abbc4e1160589ca47bc47923408d3b5ed4a2..993975ef0ece2b320a5c6ec4b0c1207887881736 100644 (file)
@@ -215,8 +215,11 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
     if (MO.isDef() &&
         (MO.isDead() || isPhysDefTriviallyDead(Reg, I, MBB->end())))
       continue;
-    for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
-      PhysRefs.insert(*AI);
+    for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
+      // Reading constant physregs is ok.
+      if (!MRI->isConstantPhysReg(*AI, *MBB->getParent()))
+        PhysRefs.insert(*AI);
+    }
     if (MO.isDef())
       PhysDefs.push_back(Reg);
   }
index 6e8d0bfdfe31b0c9d834bf819d83043c0217715f..8a81f70a8a2a0005a72c5ff35d6d82415bb9902b 100644 (file)
@@ -3,11 +3,11 @@
 
 ; CHECK: t:
 ; CHECK: decq
-; CHECK-NEXT: movl (%r11,%rax,4), %eax
+; CHECK-NEXT: movl (%r9,%rax,4), %eax
 ; CHECK-NEXT: jne
 
 ; ATOM: t:
-; ATOM: movl (%r10,%rax,4), %eax
+; ATOM: movl (%r9,%rax,4), %eax
 ; ATOM-NEXT: decq
 ; ATOM-NEXT: jne
 
index 33bef70444a82ca5aaa1d5da6342f2c9eb7151f1..d171fd5f1d9fa1b53b354a8ce402e66d058f5577 100644 (file)
@@ -134,3 +134,25 @@ return:
   %retval.0 = phi i8* [ null, %entry ], [ null, %do.cond ], [ %p.0, %do.body ]
   ret i8* %retval.0
 }
+
+; PR13578
+@t2_global = external global i32
+
+declare i1 @t2_func()
+
+define i32 @t2() {
+  store i32 42, i32* @t2_global
+  %c = call i1 @t2_func()
+  br i1 %c, label %a, label %b
+
+a:
+  %l = load i32* @t2_global
+  ret i32 %l
+
+b:
+  ret i32 0
+
+; CHECK: t2:
+; CHECK: t2_global@GOTPCREL(%rip)
+; CHECK-NOT: t2_global@GOTPCREL(%rip)
+}