[llvm] r344153 - Relax trivial cast requirements in CallPromotionUtils

Scott Linder via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 10 09:35:47 PDT 2018


Author: scott.linder
Date: Wed Oct 10 09:35:47 2018
New Revision: 344153

URL: http://llvm.org/viewvc/llvm-project?rev=344153&view=rev
Log:
Relax trivial cast requirements in CallPromotionUtils

Differential Revision: https://reviews.llvm.org/D52792

Added:
    llvm/trunk/test/Transforms/Util/call-promotion-utils-ptrcast.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/CallPromotionUtils.cpp

Modified: llvm/trunk/lib/Transforms/Utils/CallPromotionUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CallPromotionUtils.cpp?rev=344153&r1=344152&r2=344153&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/CallPromotionUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/CallPromotionUtils.cpp Wed Oct 10 09:35:47 2018
@@ -177,8 +177,8 @@ static void createRetBitCast(CallSite CS
     InsertBefore = &*std::next(CS.getInstruction()->getIterator());
 
   // Bitcast the return value to the correct type.
-  auto *Cast = CastInst::Create(Instruction::BitCast, CS.getInstruction(),
-                                RetTy, "", InsertBefore);
+  auto *Cast = CastInst::CreateBitOrPointerCast(CS.getInstruction(), RetTy, "",
+                                                InsertBefore);
   if (RetBitCast)
     *RetBitCast = Cast;
 
@@ -321,12 +321,14 @@ bool llvm::isLegalToPromote(CallSite CS,
                             const char **FailureReason) {
   assert(!CS.getCalledFunction() && "Only indirect call sites can be promoted");
 
+  auto &DL = Callee->getParent()->getDataLayout();
+
   // Check the return type. The callee's return value type must be bitcast
   // compatible with the call site's type.
   Type *CallRetTy = CS.getInstruction()->getType();
   Type *FuncRetTy = Callee->getReturnType();
   if (CallRetTy != FuncRetTy)
-    if (!CastInst::isBitCastable(FuncRetTy, CallRetTy)) {
+    if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) {
       if (FailureReason)
         *FailureReason = "Return type mismatch";
       return false;
@@ -351,7 +353,7 @@ bool llvm::isLegalToPromote(CallSite CS,
     Type *ActualTy = CS.getArgument(I)->getType();
     if (FormalTy == ActualTy)
       continue;
-    if (!CastInst::isBitCastable(ActualTy, FormalTy)) {
+    if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
       if (FailureReason)
         *FailureReason = "Argument type mismatch";
       return false;
@@ -396,8 +398,8 @@ Instruction *llvm::promoteCall(CallSite
     Type *FormalTy = CalleeType->getParamType(ArgNo);
     Type *ActualTy = Arg->getType();
     if (FormalTy != ActualTy) {
-      auto *Cast = CastInst::Create(Instruction::BitCast, Arg, FormalTy, "",
-                                    CS.getInstruction());
+      auto *Cast = CastInst::CreateBitOrPointerCast(Arg, FormalTy, "",
+                                                    CS.getInstruction());
       CS.setArgument(ArgNo, Cast);
     }
   }

Added: llvm/trunk/test/Transforms/Util/call-promotion-utils-ptrcast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Util/call-promotion-utils-ptrcast.ll?rev=344153&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Util/call-promotion-utils-ptrcast.ll (added)
+++ llvm/trunk/test/Transforms/Util/call-promotion-utils-ptrcast.ll Wed Oct 10 09:35:47 2018
@@ -0,0 +1,50 @@
+; RUN: opt -S -pgo-icall-prom -icp-total-percent-threshold=0 -icp-max-prom=4 < %s 2>&1 | FileCheck %s
+
+; Test that CallPromotionUtils will promote calls which require pointer casts.
+
+ at foo = common global i64 (i64)* null, align 8
+
+; Check ptrcast arguments.
+define i64 @func1(i8* %a) {
+  ret i64 undef
+}
+
+; Check ptrcast return.
+define i8* @func2(i64 %a) {
+  ret i8* undef
+}
+
+; Check ptrcast arguments and return.
+define i8* @func3(i8 *%a) {
+  ret i8* undef
+}
+
+; Check mixed ptrcast and bitcast.
+define i8* @func4(double %f) {
+  ret i8* undef
+}
+
+define i64 @bar() {
+  %tmp = load i64 (i64)*, i64 (i64)** @foo, align 8
+
+; CHECK: [[ARG:%[0-9]+]] = bitcast i64 1 to double
+; CHECK-NEXT: [[RET:%[0-9]+]] = call i8* @func4(double [[ARG]])
+; CHECK-NEXT: ptrtoint i8* [[RET]] to i64
+
+; CHECK: [[RET:%[0-9]+]] = call i8* @func2(i64 1)
+; CHECK-NEXT: ptrtoint i8* [[RET]] to i64
+
+; CHECK: [[ARG:%[0-9]+]] = inttoptr i64 1 to i8*
+; CHECK-NEXT: [[RET:%[0-9]+]] = call i8* @func3(i8* [[ARG]])
+; CHECK-NEXT: ptrtoint i8* [[RET]] to i64
+
+; CHECK: [[ARG:%[0-9]+]] = inttoptr i64 1 to i8*
+; CHECK-NEXT: call i64 @func1(i8* [[ARG]])
+; CHECK-NOT: ptrtoint
+; CHECK-NOT: bitcast
+
+  %call = call i64 %tmp(i64 1), !prof !1
+  ret i64 %call
+}
+
+!1 = !{!"VP", i32 0, i64 1600, i64 7651369219802541373, i64 1030, i64 -4377547752858689819, i64 410, i64 -6929281286627296573, i64 150, i64 -2545542355363006406, i64 10}




More information about the llvm-commits mailing list