[llvm] 07d1642 - [Attributor][FIX] Do not try to cast if a cast is not required

Johannes Doerfert via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 22:54:50 PDT 2019


Author: Johannes Doerfert
Date: 2019-11-02T00:54:00-05:00
New Revision: 07d16424f28482a852a35bd817189d4dfb1701ef

URL: https://github.com/llvm/llvm-project/commit/07d16424f28482a852a35bd817189d4dfb1701ef
DIFF: https://github.com/llvm/llvm-project/commit/07d16424f28482a852a35bd817189d4dfb1701ef.diff

LOG: [Attributor][FIX] Do not try to cast if a cast is not required

When we replace constant returns at the call site we did issue a cast in
the hopes it would be a no-op if the types are equal. Turns out that is
not the case and we have to check it ourselves first.

Reused an IPConstantProp test for coverage. No functional change to the
test wrt. IPConstantProp.

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/Attributor.cpp
    llvm/test/Transforms/IPConstantProp/PR43857.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp
index 448a2d4b6c1e..5082aa26dba3 100644
--- a/llvm/lib/Transforms/IPO/Attributor.cpp
+++ b/llvm/lib/Transforms/IPO/Attributor.cpp
@@ -998,14 +998,18 @@ ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
         if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
           if (CB->isCallee(&U)) {
             Constant *RVCCast =
-                ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
+                CB->getType() == RVC->getType()
+                    ? RVC
+                    : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
             Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
           }
     } else {
       assert(isa<CallBase>(AnchorValue) &&
              "Expcected a function or call base anchor!");
       Constant *RVCCast =
-          ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
+          AnchorValue.getType() == RVC->getType()
+              ? RVC
+              : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
       Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
     }
     if (Changed == ChangeStatus::CHANGED)

diff  --git a/llvm/test/Transforms/IPConstantProp/PR43857.ll b/llvm/test/Transforms/IPConstantProp/PR43857.ll
index d098d552b3fc..b1a9760abad2 100644
--- a/llvm/test/Transforms/IPConstantProp/PR43857.ll
+++ b/llvm/test/Transforms/IPConstantProp/PR43857.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -S -ipconstprop | FileCheck %s
+; RUN: opt < %s -S -ipconstprop | FileCheck %s --check-prefixes=ALL,IPCP
+; RUN: opt -S -passes=attributor -attributor-disable=false -attributor-max-iterations-verify -attributor-max-iterations=1 < %s | FileCheck %s --check-prefixes=ALL,ATTRIBUTOR
 
 %struct.wobble = type { i32 }
 %struct.zot = type { %struct.wobble, %struct.wobble, %struct.wobble }
@@ -7,20 +8,25 @@
 declare dso_local fastcc float @bar(%struct.wobble* noalias, <8 x i32>) unnamed_addr
 
 define %struct.zot @widget(<8 x i32> %arg) local_unnamed_addr {
-; CHECK-LABEL: define {{[^@]+}}@widget(
-; CHECK-NEXT:  bb:
-; CHECK-NEXT:    ret [[STRUCT_ZOT:%.*]] undef
+; ALL-LABEL: @widget(
+; ALL-NEXT:  bb:
+; ALL-NEXT:    ret [[STRUCT_ZOT:%.*]] undef
 ;
 bb:
   ret %struct.zot undef
 }
 
 define void @baz(<8 x i32> %arg) local_unnamed_addr {
-; CHECK-LABEL: define {{[^@]+}}@baz(
-; CHECK-NEXT:  bb:
-; CHECK-NEXT:    [[TMP:%.*]] = call [[STRUCT_ZOT:%.*]] @widget(<8 x i32> [[ARG:%.*]])
-; CHECK-NEXT:    [[TMP1:%.*]] = extractvalue [[STRUCT_ZOT]] %tmp, 0, 0
-; CHECK-NEXT:    ret void
+; IPCP-LABEL: @baz(
+; IPCP-NEXT:  bb:
+; IPCP-NEXT:    [[TMP:%.*]] = call [[STRUCT_ZOT:%.*]] @widget(<8 x i32> [[ARG:%.*]])
+; IPCP-NEXT:    [[TMP1:%.*]] = extractvalue [[STRUCT_ZOT]] %tmp, 0, 0
+; IPCP-NEXT:    ret void
+;
+; ATTRIBUTOR-LABEL: @baz(
+; ATTRIBUTOR-NEXT:  bb:
+; ATTRIBUTOR-NEXT:    [[TMP1:%.*]] = extractvalue [[STRUCT_ZOT:%.*]] undef, 0, 0
+; ATTRIBUTOR-NEXT:    ret void
 ;
 bb:
   %tmp = call %struct.zot @widget(<8 x i32> %arg)


        


More information about the llvm-commits mailing list