]> granicus.if.org Git - llvm/commitdiff
[CGP] don't expand a memcmp with nobuiltin attribute
authorSanjay Patel <spatel@rotateright.com>
Thu, 8 Jun 2017 19:47:25 +0000 (19:47 +0000)
committerSanjay Patel <spatel@rotateright.com>
Thu, 8 Jun 2017 19:47:25 +0000 (19:47 +0000)
This matches the behavior used in the SDAG when expanding memcmp.

For reference, we're intentionally treating the earlier fortified call transforms differently after:
https://bugs.llvm.org/show_bug.cgi?id=23093
https://reviews.llvm.org/rL233776

One motivation for not transforming nobuiltin calls is that it can interfere with sanitizers:
https://reviews.llvm.org/D19781
https://reviews.llvm.org/D19801

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305007 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/TargetLibraryInfo.h
lib/CodeGen/CodeGenPrepare.cpp
test/CodeGen/PowerPC/memCmpUsedInZeroEqualityComparison.ll

index 0e3bdaa11c9aa3cc80032616a8d5366b41eb9d8b..d931560964f017d9a2cc91ab92acd3b326dbf10e 100644 (file)
@@ -13,6 +13,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/Triple.h"
+#include "llvm/IR/CallSite.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
@@ -239,6 +240,13 @@ public:
     return Impl->getLibFunc(FDecl, F);
   }
 
+  /// If a callsite does not have the 'nobuiltin' attribute, return if the
+  /// called function is a known library function and set F to that function.
+  bool getLibFunc(ImmutableCallSite CS, LibFunc & F) const {
+    return !CS.isNoBuiltin() && CS.getCalledFunction() &&
+           getLibFunc(*(CS.getCalledFunction()), F);
+  }
+
   /// Tests whether a library function is available.
   bool has(LibFunc F) const {
     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
index b9e0e2198e1219b21df4ca3d866504159c119144..708b45810e5b71f02b495b80f27ee25f5706835b 100644 (file)
@@ -2406,12 +2406,10 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool& ModifiedDT) {
   }
 
   LibFunc Func;
-  if (TLInfo->getLibFunc(*CI->getCalledFunction(), Func) &&
-      Func == LibFunc_memcmp) {
-    if (expandMemCmp(CI, TTI, TLI, DL)) {
-      ModifiedDT = true;
-      return true;
-    }
+  if (TLInfo->getLibFunc(ImmutableCallSite(CI), Func) &&
+      Func == LibFunc_memcmp && expandMemCmp(CI, TTI, TLI, DL)) {
+    ModifiedDT = true;
+    return true;
   }
   return false;
 }
index d6ce5688c66de17c49d7d385a5423826dc25ab0e..9e5a31d00b68850e55f06eced9c0ee2a0bbb59b7 100644 (file)
@@ -199,12 +199,21 @@ define signext i32 @zeroEqualityTest06() {
 define i1 @length2_eq_nobuiltin_attr(i8* %X, i8* %Y) {
 ; CHECK-LABEL: length2_eq_nobuiltin_attr:
 ; CHECK:       # BB#0:
-; CHECK-NEXT:    lhz 3, 0(3)
-; CHECK-NEXT:    lhz 4, 0(4)
-; CHECK-NEXT:    li 5, 0
-; CHECK-NEXT:    li 12, 1
-; CHECK-NEXT:    cmpw 3, 4
-; CHECK-NEXT:    isel 3, 12, 5, 2
+; CHECK-NEXT:    mflr 0
+; CHECK-NEXT:    std 0, 16(1)
+; CHECK-NEXT:    stdu 1, -32(1)
+; CHECK-NEXT:  .Lcfi0:
+; CHECK-NEXT:    .cfi_def_cfa_offset 32
+; CHECK-NEXT:  .Lcfi1:
+; CHECK-NEXT:    .cfi_offset lr, 16
+; CHECK-NEXT:    li 5, 2
+; CHECK-NEXT:    bl memcmp
+; CHECK-NEXT:    nop
+; CHECK-NEXT:    cntlzw 3, 3
+; CHECK-NEXT:    rlwinm 3, 3, 27, 31, 31
+; CHECK-NEXT:    addi 1, 1, 32
+; CHECK-NEXT:    ld 0, 16(1)
+; CHECK-NEXT:    mtlr 0
 ; CHECK-NEXT:    blr
   %m = tail call signext i32 @memcmp(i8* %X, i8* %Y, i64 2) nobuiltin
   %c = icmp eq i32 %m, 0