[llvm-commits] CVS: llvm/lib/Target/X86/InstSelectSimple.cpp

Chris Lattner lattner at cs.uiuc.edu
Wed Jul 14 17:59:03 PDT 2004



Changes in directory llvm/lib/Target/X86:

InstSelectSimple.cpp updated: 1.269 -> 1.270

---
Log message:

Improve codegen for the LLVM offsetof/sizeof "operator".  Before we compiled
this LLVM function:

int %foo() {
        ret int cast (int** getelementptr (int** null, int 1) to int)
}

into:

foo:
        mov %EAX, 0
        lea %EAX, DWORD PTR [%EAX + 4]
        ret 

now we compile it into:

foo:
        mov %EAX, 4
        ret 

This sequence is frequently generated by the MSIL front-end, and soon the malloc lowering pass and
Java front-ends as well..

-Chris



---
Diffs of the changes:  (+15 -0)

Index: llvm/lib/Target/X86/InstSelectSimple.cpp
diff -u llvm/lib/Target/X86/InstSelectSimple.cpp:1.269 llvm/lib/Target/X86/InstSelectSimple.cpp:1.270
--- llvm/lib/Target/X86/InstSelectSimple.cpp:1.269	Mon Jun 28 19:14:38 2004
+++ llvm/lib/Target/X86/InstSelectSimple.cpp	Wed Jul 14 19:58:53 2004
@@ -3663,6 +3663,21 @@
   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
     Src = CPR->getValue();
 
+  // If this is a getelementptr null, with all constant integer indices, just
+  // replace it with TargetReg = 42.
+  if (isa<ConstantPointerNull>(Src)) {
+    User::op_iterator I = IdxBegin;
+    for (; I != IdxEnd; ++I)
+      if (!isa<ConstantInt>(*I))
+        break;
+    if (I == IdxEnd) {   // All constant indices
+      unsigned Offset = TD.getIndexedOffset(Src->getType(),
+                                         std::vector<Value*>(IdxBegin, IdxEnd));
+      BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
+      return;
+    }
+  }
+
   std::vector<Value*> GEPOps;
   GEPOps.resize(IdxEnd-IdxBegin+1);
   GEPOps[0] = Src;





More information about the llvm-commits mailing list