Two changes:
  * In promote32, if we can just promote a constant value, do so instead of
    promoting a constant dynamically.
  * In visitReturn inst, actually USE the promote32 argument that takes a
    Value*

The end result of this is that we now generate this:

test:
        mov %EAX, 0
        ret

instead of...

test:
        mov %AX, 0
        movzx %EAX, %AX
        ret

for:

ushort %test() {
        ret ushort 0
}


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12679 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp
index ad52353..25f67ec 100644
--- a/lib/Target/X86/X86ISelSimple.cpp
+++ b/lib/Target/X86/X86ISelSimple.cpp
@@ -1107,10 +1107,18 @@
 void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
   bool isUnsigned = VR.Ty->isUnsigned();
 
-  // Make sure we have the register number for this value...
-  unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg;
+  Value *Val = VR.Val;
+  const Type *Ty = VR.Ty;
+  if (Val)
+    if (Constant *C = dyn_cast<Constant>(Val)) {
+      Val = ConstantExpr::getCast(C, Type::IntTy);
+      Ty = Type::IntTy;
+    }
 
-  switch (getClassB(VR.Ty)) {
+  // Make sure we have the register number for this value...
+  unsigned Reg = Val ? getReg(Val) : VR.Reg;
+
+  switch (getClassB(Ty)) {
   case cByte:
     // Extend value into target register (8->32)
     if (isUnsigned)
@@ -1152,27 +1160,30 @@
   }
 
   Value *RetVal = I.getOperand(0);
-  unsigned RetReg = getReg(RetVal);
   switch (getClassB(RetVal->getType())) {
   case cByte:   // integral return values: extend or move into EAX and return
   case cShort:
   case cInt:
-    promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType()));
+    promote32(X86::EAX, ValueRecord(RetVal));
     // Declare that EAX is live on exit
     BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
     break;
-  case cFP:                   // Floats & Doubles: Return in ST(0)
+  case cFP: {                  // Floats & Doubles: Return in ST(0)
+    unsigned RetReg = getReg(RetVal);
     BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
     // Declare that top-of-stack is live on exit
     BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
     break;
-  case cLong:
+  }
+  case cLong: {
+    unsigned RetReg = getReg(RetVal);
     BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
     BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
     // Declare that EAX & EDX are live on exit
     BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
       .addReg(X86::ESP);
     break;
+  }
   default:
     visitInstruction(I);
   }