Calling isLandingPad on an empty BasicBlock can lead to undefined behaviour, e.g. see the following program:
int main(void) { // Start with a LLVM context. LLVMContext TheContext; // Make a module. Module *TheModule = new Module("mymod", TheContext); // Make a function std::vector<Type*> NoArgs = {}; Type *u32 = Type::getInt32Ty(TheContext); FunctionType *FT = FunctionType::get(u32, NoArgs, false); Function *F = Function::Create(FT, Function::ExternalLinkage, "main", TheModule); // Make an empty block IRBuilder<> Builder(TheContext); BasicBlock *BB = BasicBlock::Create(TheContext, "entry", F); Builder.SetInsertPoint(BB); auto fnp = BB->getFirstNonPHI(); assert(fnp == nullptr); // This can lead to UB! if (BB->isLandingPad()) { // do something } return 0; }
This patch fixes this by using isa_and_nonnull instead of isa, which would result in isLandingPad always returning false when called on an empty BasicBlock. If there is a reason (e.g. performance) for not using isa_and_nonnull we could alternatively document that isLandingPad should not be called without checking getFirstNonPHI != nullptr first, which would avoid people making the same mistake I made.