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

Chris Lattner lattner at cs.uiuc.edu
Sun Apr 11 18:22:01 PDT 2004


Changes in directory llvm/lib/Target/X86:

InstSelectSimple.cpp updated: 1.229 -> 1.230

---
Log message:

On X86, casting an integer to floating point requires going through memory.
If the source of the cast is a load, we can just use the source memory location, 
without having to create a temporary stack slot entry.

Before we code generated this:

double %int(int* %P) {
        %V = load int* %P
        %V2 = cast int %V to double
        ret double %V2
}

into:

int:
        sub %ESP, 4
        mov %EAX, DWORD PTR [%ESP + 8]
        mov %EAX, DWORD PTR [%EAX]
        mov DWORD PTR [%ESP], %EAX
        fild DWORD PTR [%ESP]
        add %ESP, 4
        ret 

Now we produce this:

int:
        mov %EAX, DWORD PTR [%ESP + 4]
        fild DWORD PTR [%EAX]
        ret 

... which is nicer.



---
Diffs of the changes:  (+33 -5)

Index: llvm/lib/Target/X86/InstSelectSimple.cpp
diff -u llvm/lib/Target/X86/InstSelectSimple.cpp:1.229 llvm/lib/Target/X86/InstSelectSimple.cpp:1.230
--- llvm/lib/Target/X86/InstSelectSimple.cpp:1.229	Sun Apr 11 17:05:45 2004
+++ llvm/lib/Target/X86/InstSelectSimple.cpp	Sun Apr 11 18:21:26 2004
@@ -2621,20 +2621,40 @@
   // instruction, like add.  If so, we don't want to emit it.  Wouldn't a real
   // pattern matching instruction selector be nice?
   unsigned Class = getClassB(I.getType());
-  if (I.hasOneUse() && Class != cLong) {
+  if (I.hasOneUse()) {
     Instruction *User = cast<Instruction>(I.use_back());
     switch (User->getOpcode()) {
+    case Instruction::Cast:
+      // If this is a cast from a signed-integer type to a floating point type,
+      // fold the cast here.
+      if (getClass(User->getType()) == cFP &&
+          (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
+           I.getType() == Type::LongTy)) {
+        unsigned DestReg = getReg(User);
+        static const unsigned Opcode[] = {
+          0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
+        };
+
+        unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
+        getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
+        addFullAddress(BuildMI(BB, Opcode[Class], 5, DestReg),
+                       BaseReg, Scale, IndexReg, Disp);
+        return;
+      } else {
+        User = 0;
+      }
+      break;
     case Instruction::Add:
     case Instruction::Sub:
     case Instruction::And:
     case Instruction::Or:
     case Instruction::Xor:
+      if (Class == cLong) User = 0;
       break;
     case Instruction::Mul:
     case Instruction::Div:
-      if (Class == cFP)
-        break;  // Folding only implemented for floating point.
-      // fall through.
+      if (Class == cFP) User = 0;
+      break;  // Folding only implemented for floating point.
     default: User = 0; break;
     }
 
@@ -2771,10 +2791,18 @@
                              MachineBasicBlock::iterator IP,
                              Value *Src, const Type *DestTy,
                              unsigned DestReg) {
-  unsigned SrcReg = getReg(Src, BB, IP);
   const Type *SrcTy = Src->getType();
   unsigned SrcClass = getClassB(SrcTy);
   unsigned DestClass = getClassB(DestTy);
+
+  // If this cast converts a load from a short,int, or long integer to a FP
+  // value, we will have folded this cast away.
+  if (DestClass == cFP && isa<LoadInst>(Src) &&
+      (Src->getType() == Type::ShortTy || Src->getType() == Type::IntTy ||
+       Src->getType() == Type::LongTy))
+    return;
+
+  unsigned SrcReg = getReg(Src, BB, IP);
 
   // Implement casts to bool by using compare on the operand followed by set if
   // not zero on the result.





More information about the llvm-commits mailing list