llvm.org GIT mirror llvm / a9d0c9d
When transforming a call to a bitcast function into a direct call with cast parameters and cast return value (if any), instcombine was prepared to cast any non-void return value into any other, whether castable or not. Add a new predicate for testing whether casting is valid, and check it both for the return value and (as a cleanup) for the parameters. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45657 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 11 years ago
5 changed file(s) with 105 addition(s) and 47 deletion(s). Raw diff Collapse all Expand all
397397 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
398398 );
399399
400 /// @brief Check whether it is valid to call getCastOpcode for these types.
401 static bool isCastable(
402 const Type *SrcTy, ///< The Type from which the value should be cast.
403 const Type *DestTy ///< The Type to which the value should be cast.
404 );
405
400406 /// Returns the opcode necessary to cast Val into Ty using usual casting
401 /// rules.
407 /// rules.
402408 /// @brief Infer the opcode for cast operand and type
403409 static Instruction::CastOps getCastOpcode(
404410 const Value *Val, ///< The value to cast
80818081 const FunctionType *FT = Callee->getFunctionType();
80828082 const Type *OldRetTy = Caller->getType();
80838083
8084 const ParamAttrsList* CallerPAL = 0;
8085 if (CallInst *CallerCI = dyn_cast(Caller))
8086 CallerPAL = CallerCI->getParamAttrs();
8087 else if (InvokeInst *CallerII = dyn_cast(Caller))
8088 CallerPAL = CallerII->getParamAttrs();
8084 const ParamAttrsList* CallerPAL = CS.getParamAttrs();
80898085
80908086 // If the parameter attributes are not compatible, don't do the xform. We
80918087 // don't want to lose an sret attribute or something.
80988094 // Conversion is ok if changing from pointer to int of same size.
80998095 !(isa(FT->getReturnType()) &&
81008096 TD->getIntPtrType() == OldRetTy))
8097 return false; // Cannot transform this return value.
8098
8099 if (!Caller->use_empty() &&
8100 !CastInst::isCastable(FT->getReturnType(), OldRetTy) &&
8101 // void -> non-void is handled specially
8102 FT->getReturnType() != Type::VoidTy)
81018103 return false; // Cannot transform this return value.
81028104
81038105 // If the callsite is an invoke instruction, and the return value is used by
81218123 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
81228124 const Type *ParamTy = FT->getParamType(i);
81238125 const Type *ActTy = (*AI)->getType();
8126
8127 if (!CastInst::isCastable(ActTy, ParamTy))
8128 return false;
8129
81248130 ConstantInt *c = dyn_cast(*AI);
8125 //Some conversions are safe even if we do not have a body.
8126 //Either we can cast directly, or we can upconvert the argument
8131 // Some conversions are safe even if we do not have a body.
8132 // Either we can cast directly, or we can upconvert the argument
81278133 bool isConvertible = ActTy == ParamTy ||
81288134 (isa(ParamTy) && isa(ActTy)) ||
81298135 (ParamTy->isInteger() && ActTy->isInteger() &&
81318137 (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()
81328138 && c->getValue().isStrictlyPositive());
81338139 if (Callee->isDeclaration() && !isConvertible) return false;
8134
8135 // Most other conversions can be done if we have a body, even if these
8136 // lose information, e.g. int->short.
8137 // Some conversions cannot be done at all, e.g. float to pointer.
8138 // Logic here parallels CastInst::getCastOpcode (the design there
8139 // requires legality checks like this be done before calling it).
8140 if (ParamTy->isInteger()) {
8141 if (const VectorType *VActTy = dyn_cast(ActTy)) {
8142 if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits())
8143 return false;
8144 }
8145 if (!ActTy->isInteger() && !ActTy->isFloatingPoint() &&
8146 !isa(ActTy))
8147 return false;
8148 } else if (ParamTy->isFloatingPoint()) {
8149 if (const VectorType *VActTy = dyn_cast(ActTy)) {
8150 if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits())
8151 return false;
8152 }
8153 if (!ActTy->isInteger() && !ActTy->isFloatingPoint())
8154 return false;
8155 } else if (const VectorType *VParamTy = dyn_cast(ParamTy)) {
8156 if (const VectorType *VActTy = dyn_cast(ActTy)) {
8157 if (VActTy->getBitWidth() != VParamTy->getBitWidth())
8158 return false;
8159 }
8160 if (VParamTy->getBitWidth() != ActTy->getPrimitiveSizeInBits())
8161 return false;
8162 } else if (isa(ParamTy)) {
8163 if (!ActTy->isInteger() && !isa(ActTy))
8164 return false;
8165 } else {
8166 return false;
8167 }
81688140 }
81698141
81708142 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
82378209
82388210 // Insert a cast of the return type as necessary.
82398211 Value *NV = NC;
8240 if (Caller->getType() != NV->getType() && !Caller->use_empty()) {
8212 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
82418213 if (NV->getType() != Type::VoidTy) {
8242 const Type *CallerTy = Caller->getType();
82438214 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
8244 CallerTy, false);
8245 NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp");
8215 OldRetTy, false);
8216 NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp");
82468217
82478218 // If this is an invoke instruction, we should insert it after the first
82488219 // non-phi, instruction in the normal successor block.
18941894 return create(opcode, C, Ty, Name, InsertAtEnd);
18951895 }
18961896
1897 // Check whether it is valid to call getCastOpcode for these types.
1898 // This routine must be kept in sync with getCastOpcode.
1899 bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1900 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1901 return false;
1902
1903 if (SrcTy == DestTy)
1904 return true;
1905
1906 // Get the bit sizes, we'll need these
1907 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1908 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1909
1910 // Run through the possibilities ...
1911 if (DestTy->isInteger()) { // Casting to integral
1912 if (SrcTy->isInteger()) { // Casting from integral
1913 return true;
1914 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1915 return true;
1916 } else if (const VectorType *PTy = dyn_cast(SrcTy)) {
1917 // Casting from vector
1918 return DestBits == PTy->getBitWidth();
1919 } else { // Casting from something else
1920 return isa(SrcTy);
1921 }
1922 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1923 if (SrcTy->isInteger()) { // Casting from integral
1924 return true;
1925 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1926 return true;
1927 } else if (const VectorType *PTy = dyn_cast(SrcTy)) {
1928 // Casting from vector
1929 return DestBits == PTy->getBitWidth();
1930 } else { // Casting from something else
1931 return false;
1932 }
1933 } else if (const VectorType *DestPTy = dyn_cast(DestTy)) {
1934 // Casting to vector
1935 if (const VectorType *SrcPTy = dyn_cast(SrcTy)) {
1936 // Casting from vector
1937 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1938 } else { // Casting from something else
1939 return DestPTy->getBitWidth() == SrcBits;
1940 }
1941 } else if (isa(DestTy)) { // Casting to pointer
1942 if (isa(SrcTy)) { // Casting from pointer
1943 return true;
1944 } else if (SrcTy->isInteger()) { // Casting from integral
1945 return true;
1946 } else { // Casting from something else
1947 return false;
1948 }
1949 } else { // Casting to something else
1950 return false;
1951 }
1952 }
1953
18971954 // Provide a way to get a "cast" where the cast opcode is inferred from the
18981955 // types and size of the operand. This, basically, is a parallel of the
18991956 // logic in the castIsValid function below. This axiom should hold:
19001957 // castIsValid( getCastOpcode(Val, Ty), Val, Ty)
19011958 // should not assert in castIsValid. In other words, this produces a "correct"
19021959 // casting opcode for the arguments passed to it.
1960 // This routine must be kept in sync with isCastable.
19031961 Instruction::CastOps
19041962 CastInst::getCastOpcode(
19051963 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
19071965 const Type *SrcTy = Src->getType();
19081966 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
19091967 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1968
1969 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1970 "Only first class types are castable!");
19101971
19111972 // Run through the possibilities ...
19121973 if (DestTy->isInteger()) { // Casting to integral
20492110 if (isa(SrcTy) != isa(DstTy))
20502111 return false;
20512112
2052 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
2113 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
20532114 // these cases, the cast is okay if the source and destination bit widths
20542115 // are identical.
20552116 return SrcBitSize == DstBitSize;
0 ; RUN: llvm-as < %s | opt -instcombine -disable-output
1
2 define <2 x i32> @f() {
3 ret <2 x i32> undef
4 }
5
6 define i32 @g() {
7 %x = call i32 bitcast (<2 x i32> ()* @f to i32 ()*)( ) ; [#uses=1]
8 ret i32 %x
9 }
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep bitcast
1
2 define void @f(i16 %y) {
3 ret void
4 }
5
6 define i32 @g(i32 %y) {
7 %x = call i32 bitcast (void (i16)* @f to i32 (i32)*)( i32 %y ) ; [#uses=1]
8 ret i32 %x
9 }