1- // ===---------------- BPFAdjustOpt .cpp - Adjust Optimization ------ --------===//
1+ // ===--------- BPFCheckUnreachableIR .cpp - Issue Unreachable Error --------===//
22//
33// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44// See https://llvm.org/LICENSE.txt for license information.
55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===----------------------------------------------------------------------===//
88//
9- // Check 'unreachable' IRs and issue proper warnings .
9+ // Check 'unreachable' IRs and issue proper errors .
1010//
1111// ===----------------------------------------------------------------------===//
1212
1919#include " llvm/IR/Value.h"
2020#include " llvm/Pass.h"
2121
22- #define DEBUG_TYPE " bpf-check-undef -ir"
22+ #define DEBUG_TYPE " bpf-check-unreachable -ir"
2323
2424using namespace llvm ;
2525
26+ static cl::opt<bool >
27+ DisableCheckUnreachableIR (" bpf-disable-check-unreachable-ir" , cl::Hidden,
28+ cl::desc (" BPF: Disable Checking Unreachable IR" ),
29+ cl::init(false ));
30+
2631namespace {
2732
28- class BPFCheckUndefIR final : public ModulePass {
33+ class BPFCheckUnreachableIR final : public ModulePass {
2934 bool runOnModule (Module &F) override ;
3035
3136public:
3237 static char ID;
33- BPFCheckUndefIR () : ModulePass(ID) {}
38+ BPFCheckUnreachableIR () : ModulePass(ID) {}
3439
3540private:
36- void BPFCheckUndefIRImpl (Function &F);
41+ void BPFCheckUnreachableIRImpl (Function &F);
3742 void BPFCheckInst (Function &F, BasicBlock &BB, Instruction &I);
3843 void HandleUnreachableInsn (Function &F, BasicBlock &BB, Instruction &I);
3944};
4045} // End anonymous namespace
4146
42- char BPFCheckUndefIR ::ID = 0 ;
43- INITIALIZE_PASS (BPFCheckUndefIR , DEBUG_TYPE, " BPF Check Undef IRs" , false ,
44- false )
47+ char BPFCheckUnreachableIR ::ID = 0 ;
48+ INITIALIZE_PASS (BPFCheckUnreachableIR , DEBUG_TYPE, " BPF Check Unreachable IRs" ,
49+ false , false )
4550
46- ModulePass *llvm::createBPFCheckUndefIR() { return new BPFCheckUndefIR (); }
51+ ModulePass *llvm::createBPFCheckUnreachableIR() {
52+ return new BPFCheckUnreachableIR ();
53+ }
4754
48- void BPFCheckUndefIR ::HandleUnreachableInsn (Function &F, BasicBlock &BB,
49- Instruction &I) {
55+ void BPFCheckUnreachableIR ::HandleUnreachableInsn (Function &F, BasicBlock &BB,
56+ Instruction &I) {
5057 // LLVM may create a switch statement with default to a 'unreachable' basic
5158 // block. Do not warn for such cases.
5259 unsigned NumNoSwitches = 0 , NumSwitches = 0 ;
@@ -73,17 +80,18 @@ void BPFCheckUndefIR::HandleUnreachableInsn(Function &F, BasicBlock &BB,
7380 F.getContext ().diagnose (
7481 DiagnosticInfoGeneric (Twine (" unreachable in func " )
7582 .concat (F.getName ())
76- .concat (" , due to uninitialized variable?" ),
77- DS_Warning));
83+ .concat (" , due to uninitialized variable?" )
84+ .concat (" try -Wuninitialized?" ),
85+ DS_Error));
7886}
7987
80- void BPFCheckUndefIR ::BPFCheckInst (Function &F, BasicBlock &BB,
81- Instruction &I) {
88+ void BPFCheckUnreachableIR ::BPFCheckInst (Function &F, BasicBlock &BB,
89+ Instruction &I) {
8290 if (I.getOpcode () == Instruction::Unreachable)
8391 HandleUnreachableInsn (F, BB, I);
8492}
8593
86- void BPFCheckUndefIR::BPFCheckUndefIRImpl (Function &F) {
94+ void BPFCheckUnreachableIR::BPFCheckUnreachableIRImpl (Function &F) {
8795 // A 'unreachable' will be added to the end of naked function.
8896 // Let ignore these naked functions.
8997 if (F.hasFnAttribute (Attribute::Naked))
@@ -95,8 +103,10 @@ void BPFCheckUndefIR::BPFCheckUndefIRImpl(Function &F) {
95103 }
96104}
97105
98- bool BPFCheckUndefIR::runOnModule (Module &M) {
106+ bool BPFCheckUnreachableIR::runOnModule (Module &M) {
107+ if (DisableCheckUnreachableIR)
108+ return false ;
99109 for (Function &F : M)
100- BPFCheckUndefIRImpl (F);
110+ BPFCheckUnreachableIRImpl (F);
101111 return false ;
102112}
0 commit comments