[llvm-commits] [llvm] r51834 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/apint-call-cast-target.ll

Duncan Sands baldrick at free.fr
Sun Jun 1 00:38:43 PDT 2008


Author: baldrick
Date: Sun Jun  1 02:38:42 2008
New Revision: 51834

URL: http://llvm.org/viewvc/llvm-project?rev=51834&view=rev
Log:
When simplifying a call to a bitcast function, tighten up
the conditions for performing the transform when only the
function declaration is available: no longer allow turning
i32 into i64 for example.  Only allow changing between
pointer types, and between pointer types and integers of
the same size.  For return values ptr -> intptr was already
allowed; I added ptr -> ptr and intptr -> ptr while there.
As shown by a recent objc testcase, changing the way
parameters/return values are passed can be fatal when calling
code written in assembler that directly manipulates call
arguments and return values unless the transform has no
impact on the way they are passed at the codegen level.
While it is possible to imagine an ABI that treats integers
of pointer size differently to pointers, I don't think LLVM
supports any so the transform should now be safe while still
being useful.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/apint-call-cast-target.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=51834&r1=51833&r2=51834&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sun Jun  1 02:38:42 2008
@@ -9450,27 +9450,28 @@
   //
   const FunctionType *FT = Callee->getFunctionType();
   const Type *OldRetTy = Caller->getType();
+  const Type *NewRetTy = FT->getReturnType();
 
-  if (isa<StructType>(FT->getReturnType()))
+  if (isa<StructType>(NewRetTy))
     return false; // TODO: Handle multiple return values.
 
   // Check to see if we are changing the return type...
-  if (OldRetTy != FT->getReturnType()) {
+  if (OldRetTy != NewRetTy) {
     if (Callee->isDeclaration() &&
-        // Conversion is ok if changing from pointer to int of same size.
-        !(isa<PointerType>(FT->getReturnType()) &&
-          TD->getIntPtrType() == OldRetTy))
+        // Conversion is ok if changing from one pointer type to another or from
+        // a pointer to an integer of the same size.
+        !((isa<PointerType>(OldRetTy) || OldRetTy == TD->getIntPtrType()) &&
+          isa<PointerType>(NewRetTy) || NewRetTy == TD->getIntPtrType()))
       return false;   // Cannot transform this return value.
 
     if (!Caller->use_empty() &&
         // void -> non-void is handled specially
-        FT->getReturnType() != Type::VoidTy &&
-        !CastInst::isCastable(FT->getReturnType(), OldRetTy))
+        NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy))
       return false;   // Cannot transform this return value.
 
     if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
       ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0);
-      if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType()))
+      if (RAttrs & ParamAttr::typeIncompatible(NewRetTy))
         return false;   // Attribute not compatible with transformed value.
     }
 
@@ -9502,15 +9503,11 @@
     if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy))
       return false;   // Attribute not compatible with transformed value.
 
-    ConstantInt *c = dyn_cast<ConstantInt>(*AI);
-    // Some conversions are safe even if we do not have a body.
-    // Either we can cast directly, or we can upconvert the argument
+    // Converting from one pointer type to another or between a pointer and an
+    // integer of the same size is safe even if we do not have a body.
     bool isConvertible = ActTy == ParamTy ||
-      (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) ||
-      (ParamTy->isInteger() && ActTy->isInteger() &&
-       ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) ||
-      (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()
-       && c->getValue().isStrictlyPositive());
+      ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) &&
+       (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType()));
     if (Callee->isDeclaration() && !isConvertible) return false;
   }
 
@@ -9543,7 +9540,7 @@
 
   // If the return value is not being used, the type may not be compatible
   // with the existing attributes.  Wipe out any problematic attributes.
-  RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType());
+  RAttrs &= ~ParamAttr::typeIncompatible(NewRetTy);
 
   // Add the new return attributes.
   if (RAttrs)
@@ -9598,7 +9595,7 @@
     }
   }
 
-  if (FT->getReturnType() == Type::VoidTy)
+  if (NewRetTy == Type::VoidTy)
     Caller->setName("");   // Void type should not have a name.
 
   const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end());

Modified: llvm/trunk/test/Transforms/InstCombine/apint-call-cast-target.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/apint-call-cast-target.ll?rev=51834&r1=51833&r2=51834&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/apint-call-cast-target.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/apint-call-cast-target.ll Sun Jun  1 02:38:42 2008
@@ -10,4 +10,8 @@
 	ret i32 %tmp
 }
 
-declare i7* @ctime(i999*)
+define i7* @ctime(i999*) {
+entry:
+	%tmp = call i7* bitcast (i32 ()* @main to i7* ()*)( )
+	ret i7* %tmp
+}





More information about the llvm-commits mailing list