[Coverity] Fix explicit null dereferences
authorAkshay Khadse <akshayskhadse@gmail.com>
Sun, 23 Apr 2023 04:06:54 +0000 (12:06 +0800)
committerAkshay Khadse <akshayskhadse@gmail.com>
Sun, 23 Apr 2023 04:07:11 +0000 (12:07 +0800)
This change fixes static code analysis errors

Reviewed By: skan

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

llvm/include/llvm/ADT/SparseSet.h
llvm/include/llvm/CodeGen/PBQP/CostAllocator.h
llvm/include/llvm/CodeGen/SelectionDAGNodes.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/CodeGen/ScheduleDAG.cpp
llvm/lib/CodeGen/ValueTypes.cpp
llvm/lib/IR/Mangler.cpp
llvm/lib/TableGen/Record.cpp
llvm/lib/Target/X86/X86InstrInfo.cpp
llvm/utils/TableGen/GlobalISelEmitter.cpp

index c9895d7..4a999e6 100644 (file)
@@ -203,6 +203,7 @@ public:
   ///
   iterator findIndex(unsigned Idx) {
     assert(Idx < Universe && "Key out of range");
+    assert(Sparse != nullptr && "Invalid sparse type");
     const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
     for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
       const unsigned FoundIdx = ValIndexOf(Dense[i]);
index 0d6d8a3..7a8ee69 100644 (file)
@@ -100,7 +100,7 @@ public:
 
     auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
     EntrySet.insert(P.get());
-    return PoolRef(std::move(P), &P->getValue());
+    return PoolRef(P, &P->getValue());
   }
 };
 
index 620fdfc..62083b6 100644 (file)
@@ -1135,6 +1135,7 @@ inline SDValue::SDValue(SDNode *node, unsigned resno)
 }
 
 inline unsigned SDValue::getOpcode() const {
+  assert(Node != nullptr && "Invalid Node");
   return Node->getOpcode();
 }
 
index 0a803d8..71207ed 100644 (file)
@@ -1273,6 +1273,7 @@ AsmPrinter::getFunctionCFISectionType(const Function &F) const {
       F.needsUnwindTableEntry())
     return CFISection::EH;
 
+  assert(MMI != nullptr && "Invalid machine module info");
   if (MMI->hasDebugInfo() || TM.Options.ForceDwarfFrameSection)
     return CFISection::Debug;
 
index ef886ad..14ec419 100644 (file)
@@ -724,6 +724,8 @@ void ScheduleDAGTopologicalSort::AddSUnitWithoutPredecessors(const SUnit *SU) {
 
 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
                                              const SUnit *TargetSU) {
+  assert(TargetSU != nullptr && "Invalid target SUnit");
+  assert(SU != nullptr && "Invalid SUnit");
   FixOrder();
   // If insertion of the edge SU->TargetSU would create a cycle
   // then there is a path from TargetSU to SU.
index 1b317bd..d514e16 100644 (file)
@@ -571,6 +571,7 @@ Type *EVT::getTypeForEVT(LLVMContext &Context) const {
 /// pointers as MVT::iPTR.  If HandleUnknown is true, unknown types are returned
 /// as Other, otherwise they are invalid.
 MVT MVT::getVT(Type *Ty, bool HandleUnknown){
+  assert(Ty != nullptr && "Invalid type");
   switch (Ty->getTypeID()) {
   default:
     if (HandleUnknown) return MVT(MVT::Other);
index 93d5639..8d9880e 100644 (file)
@@ -119,6 +119,7 @@ static void addByteCountSuffix(raw_ostream &OS, const Function *F,
 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
                                 bool CannotUsePrivateLabel) const {
   ManglerPrefixTy PrefixTy = Default;
+  assert(GV != nullptr && "Invalid Global Value");
   if (GV->hasPrivateLinkage()) {
     if (CannotUsePrivateLabel)
       PrefixTy = LinkerPrivate;
index df1a4d4..07776cb 100644 (file)
@@ -318,8 +318,11 @@ RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
       return resolveRecordTypes(RecTy1, RecTy2);
   }
 
+  assert(T1 != nullptr && "Invalid record type");
   if (T1->typeIsConvertibleTo(T2))
     return T2;
+
+  assert(T2 != nullptr && "Invalid record type");
   if (T2->typeIsConvertibleTo(T1))
     return T1;
 
index 004104c..7458df7 100644 (file)
@@ -3603,6 +3603,7 @@ static unsigned getLoadStoreRegOpcode(Register Reg,
   bool HasAVX512 = STI.hasAVX512();
   bool HasVLX = STI.hasVLX();
 
+  assert(RC != nullptr && "Invalid target register class");
   switch (STI.getRegisterInfo()->getSpillSize(*RC)) {
   default:
     llvm_unreachable("Unknown spill size");
index fc32ebc..84c5559 100644 (file)
@@ -3958,6 +3958,7 @@ Expected<InstructionMatcher &> GlobalISelEmitter::addBuiltinPredicates(
     }
   }
 
+  assert(SrcGIEquivOrNull != nullptr && "Invalid SrcGIEquivOrNull value");
   // No check required. We already did it by swapping the opcode.
   if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") &&
       Predicate.isSignExtLoad())