[llvm] d77b7ca - [BPF] Avoid checking for intrinsics using string comparisons. NFC

Alex Richardson via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 25 03:35:44 PST 2022


Author: Alex Richardson
Date: 2022-11-25T11:34:55Z
New Revision: d77b7cac27afd2a14e7c5244d01d761f634cc71d

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

LOG: [BPF] Avoid checking for intrinsics using string comparisons. NFC

Use a dyn_cast<> to IntrinsicInst and an enum compare instead.
While touching this code also re-generate the test to use positive check
lines instead of negative ones and remove some unneeded metadata.

Reviewed By: yonghong-song

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

Added: 
    

Modified: 
    llvm/lib/Target/BPF/BPFIRPeephole.cpp
    llvm/test/CodeGen/BPF/vla.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/BPF/BPFIRPeephole.cpp b/llvm/lib/Target/BPF/BPFIRPeephole.cpp
index d6a70012dca04..283b3687f7ccc 100644
--- a/llvm/lib/Target/BPF/BPFIRPeephole.cpp
+++ b/llvm/lib/Target/BPF/BPFIRPeephole.cpp
@@ -14,6 +14,7 @@
 #include "BPF.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Type.h"
@@ -56,37 +57,32 @@ static bool BPFIRPeepholeImpl(Function &F) {
         ToErase = nullptr;
       }
 
-      if (auto *Call = dyn_cast<CallInst>(&I)) {
-        if (auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand())) {
-          if (!GV->getName().equals("llvm.stacksave"))
-            continue;
-          if (!Call->hasOneUser())
-            continue;
-          auto *Inst = cast<Instruction>(*Call->user_begin());
-          LLVM_DEBUG(dbgs() << "Remove:"; I.dump());
-          LLVM_DEBUG(dbgs() << "Remove:"; Inst->dump(); dbgs() << '\n');
-          Changed = true;
-          Inst->eraseFromParent();
-          ToErase = &I;
-        }
+      if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
+        if (II->getIntrinsicID() != Intrinsic::stacksave)
+          continue;
+        if (!II->hasOneUser())
+          continue;
+        auto *Inst = cast<Instruction>(*II->user_begin());
+        LLVM_DEBUG(dbgs() << "Remove:"; I.dump());
+        LLVM_DEBUG(dbgs() << "Remove:"; Inst->dump(); dbgs() << '\n');
+        Changed = true;
+        Inst->eraseFromParent();
+        ToErase = &I;
         continue;
       }
 
       if (auto *LD = dyn_cast<LoadInst>(&I)) {
         if (!LD->hasOneUser())
           continue;
-        auto *Call = dyn_cast<CallInst>(*LD->user_begin());
-        if (!Call)
-          continue;
-        auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
-        if (!GV)
+        auto *II = dyn_cast<IntrinsicInst>(*LD->user_begin());
+        if (!II)
           continue;
-        if (!GV->getName().equals("llvm.stackrestore"))
+        if (II->getIntrinsicID() != Intrinsic::stackrestore)
           continue;
         LLVM_DEBUG(dbgs() << "Remove:"; I.dump());
-        LLVM_DEBUG(dbgs() << "Remove:"; Call->dump(); dbgs() << '\n');
+        LLVM_DEBUG(dbgs() << "Remove:"; II->dump(); dbgs() << '\n');
         Changed = true;
-        Call->eraseFromParent();
+        II->eraseFromParent();
         ToErase = &I;
       }
     }

diff  --git a/llvm/test/CodeGen/BPF/vla.ll b/llvm/test/CodeGen/BPF/vla.ll
index 71eedb1d223bd..6f8537fa91f2a 100644
--- a/llvm/test/CodeGen/BPF/vla.ll
+++ b/llvm/test/CodeGen/BPF/vla.ll
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt --bpf-ir-peephole -mtriple=bpf-pc-linux -S %s | FileCheck %s
 ; Source:
 ;   #define AA 40
@@ -27,13 +28,26 @@ target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
 target triple = "bpf"
 
 ; Function Attrs: nounwind
-define dso_local i32 @test1() #0 {
+define dso_local i32 @test1() {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    [[SAVED_STACK:%.*]] = alloca i8*, align 8
+; CHECK-NEXT:    [[TMP0:%.*]] = bitcast i32* [[A]] to i8*
+; CHECK-NEXT:    call void @llvm.lifetime.start.p0i8(i64 4, i8* [[TMP0]])
+; CHECK-NEXT:    store i32 8, i32* [[A]], align 4
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 68, align 1
+; CHECK-NEXT:    call void @foo(i8* [[VLA]])
+; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i32* [[A]] to i8*
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0i8(i64 4, i8* [[TMP1]])
+; CHECK-NEXT:    ret i32 0
+;
 entry:
   %a = alloca i32, align 4
   %saved_stack = alloca i8*, align 8
   %0 = bitcast i32* %a to i8*
-  call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #4
-  store i32 8, i32* %a, align 4, !tbaa !3
+  call void @llvm.lifetime.start.p0i8(i64 4, i8* %0)
+  store i32 8, i32* %a, align 4
   %1 = call i8* @llvm.stacksave()
   store i8* %1, i8** %saved_stack, align 8
   %vla = alloca i8, i64 68, align 1
@@ -41,42 +55,51 @@ entry:
   %2 = load i8*, i8** %saved_stack, align 8
   call void @llvm.stackrestore(i8* %2)
   %3 = bitcast i32* %a to i8*
-  call void @llvm.lifetime.end.p0i8(i64 4, i8* %3) #4
+  call void @llvm.lifetime.end.p0i8(i64 4, i8* %3)
   ret i32 0
 }
 
-; CHECK:       define dso_local i32 @test1
-; CHECK-NOT:   %[[#]] = call i8* @llvm.stacksave()
-; CHECK-NOT:   store i8* %[[#]], i8** %saved_stack, align 8
-; CHECK-NOT:   %[[#]] = load i8*, i8** %saved_stack, align 8
-; CHECK-NOT:   call void @llvm.stackrestore(i8* %[[#]])
-
-; Function Attrs: argmemonly nofree nosync nounwind willreturn
-declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
+declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
 
-; Function Attrs: nofree nosync nounwind willreturn
-declare i8* @llvm.stacksave() #2
+declare i8* @llvm.stacksave()
 
-declare dso_local void @foo(i8*) #3
+declare dso_local void @foo(i8*)
 
-; Function Attrs: nofree nosync nounwind willreturn
-declare void @llvm.stackrestore(i8*) #2
+declare void @llvm.stackrestore(i8*)
 
-; Function Attrs: argmemonly nofree nosync nounwind willreturn
-declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1
+declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
 
-; Function Attrs: nounwind
-define dso_local i32 @test2(i32 %b) #0 {
+define dso_local i32 @test2(i32 %b) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[B_ADDR:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    [[SAVED_STACK:%.*]] = alloca i8*, align 8
+; CHECK-NEXT:    [[__VLA_EXPR0:%.*]] = alloca i64, align 8
+; CHECK-NEXT:    store i32 [[B:%.*]], i32* [[B_ADDR]], align 4
+; CHECK-NEXT:    [[TMP0:%.*]] = bitcast i32* [[A]] to i8*
+; CHECK-NEXT:    call void @llvm.lifetime.start.p0i8(i64 4, i8* [[TMP0]])
+; CHECK-NEXT:    store i32 8, i32* [[A]], align 4
+; CHECK-NEXT:    [[TMP1:%.*]] = load i32, i32* [[B_ADDR]], align 4
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 8, [[TMP1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = zext i32 [[ADD]] to i64
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP2]], align 1
+; CHECK-NEXT:    store i64 [[TMP2]], i64* [[__VLA_EXPR0]], align 8
+; CHECK-NEXT:    call void @foo(i8* [[VLA]])
+; CHECK-NEXT:    [[TMP3:%.*]] = bitcast i32* [[A]] to i8*
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0i8(i64 4, i8* [[TMP3]])
+; CHECK-NEXT:    ret i32 0
+;
 entry:
   %b.addr = alloca i32, align 4
   %a = alloca i32, align 4
   %saved_stack = alloca i8*, align 8
   %__vla_expr0 = alloca i64, align 8
-  store i32 %b, i32* %b.addr, align 4, !tbaa !3
+  store i32 %b, i32* %b.addr, align 4
   %0 = bitcast i32* %a to i8*
-  call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #4
-  store i32 8, i32* %a, align 4, !tbaa !3
-  %1 = load i32, i32* %b.addr, align 4, !tbaa !3
+  call void @llvm.lifetime.start.p0i8(i64 4, i8* %0)
+  store i32 8, i32* %a, align 4
+  %1 = load i32, i32* %b.addr, align 4
   %add = add nsw i32 8, %1
   %2 = zext i32 %add to i64
   %3 = call i8* @llvm.stacksave()
@@ -87,29 +110,6 @@ entry:
   %4 = load i8*, i8** %saved_stack, align 8
   call void @llvm.stackrestore(i8* %4)
   %5 = bitcast i32* %a to i8*
-  call void @llvm.lifetime.end.p0i8(i64 4, i8* %5) #4
+  call void @llvm.lifetime.end.p0i8(i64 4, i8* %5)
   ret i32 0
 }
-
-; CHECK:       define dso_local i32 @test2
-; CHECK-NOT:   %[[#]] = call i8* @llvm.stacksave()
-; CHECK-NOT:   store i8* %[[#]], i8** %saved_stack, align 8
-; CHECK-NOT:   %[[#]] = load i8*, i8** %saved_stack, align 8
-; CHECK-NOT:   call void @llvm.stackrestore(i8* %[[#]])
-
-attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #1 = { argmemonly nofree nosync nounwind willreturn }
-attributes #2 = { nofree nosync nounwind willreturn }
-attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #4 = { nounwind }
-
-!llvm.module.flags = !{!0, !1}
-!llvm.ident = !{!2}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 7, !"frame-pointer", i32 2}
-!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 64c5d5c671fb5b5f25c464652a4eec2cf743af0d)"}
-!3 = !{!4, !4, i64 0}
-!4 = !{!"int", !5, i64 0}
-!5 = !{!"omnipotent char", !6, i64 0}
-!6 = !{!"Simple C/C++ TBAA"}


        


More information about the llvm-commits mailing list