diff --git a/llvm/lib/CodeGen/HardwareLoops.cpp b/llvm/lib/CodeGen/HardwareLoops.cpp --- a/llvm/lib/CodeGen/HardwareLoops.cpp +++ b/llvm/lib/CodeGen/HardwareLoops.cpp @@ -332,9 +332,12 @@ HWLoopInfo.CountType = IntegerType::get(Ctx, Opts.Bitwidth.value()); } - if (Opts.Decrement.has_value()) + if (Opts.Decrement.has_value()) { + assert(HWLoopInfo.CountType && + "HWLoopInfo.CountType could not be nullptr!"); HWLoopInfo.LoopDecrement = - ConstantInt::get(HWLoopInfo.CountType, Opts.Decrement.value()); + ConstantInt::get(HWLoopInfo.CountType, Opts.Decrement.value()); + } MadeChange |= TryConvertLoop(HWLoopInfo); return MadeChange && (!HWLoopInfo.IsNestingLegal && !Opts.ForceNested); @@ -342,9 +345,10 @@ bool HardwareLoopsImpl::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) { + assert(HWLoopInfo.CountType && "HWLoopInfo.CountType could not be nullptr!"); Loop *L = HWLoopInfo.L; LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L); - + if (!HWLoopInfo.isHardwareLoopCandidate(SE, LI, DT, Opts.getForceNested(), Opts.getForcePhi())) { // TODO: there can be many reasons a loop is not considered a @@ -534,6 +538,7 @@ } void HardwareLoop::InsertLoopDec() { + assert(LoopDecrement && "LoopDecrement can not be nullptr!"); IRBuilder<> CondBuilder(ExitBranch); Function *DecFunc = diff --git a/llvm/test/Transforms/HardwareLoops/input-error.ll b/llvm/test/Transforms/HardwareLoops/input-error.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/HardwareLoops/input-error.ll @@ -0,0 +1,26 @@ +; RUN: not --crash opt -passes='hardware-loops' -S %s 2> %t.err +; RUN: FileCheck --check-prefix=CHECK < %t.err %s +; RUN: not --crash opt -passes='hardware-loops' -S %s 2> %t.err +; RUN: FileCheck --check-prefix=CHECK-NODECREMENT < %t.err %s +; RUN: not --crash opt -passes='hardware-loops' -S %s 2> %t.err +; RUN: FileCheck --check-prefix=CHECK-NOBITWIDTH < %t.err %s + +define void @while_lt(i32 %i, i32 %N, ptr nocapture %A) { +; CHECK: HWLoopInfo.CountType could not be nullptr! +; CHECK-NODECREMENT: LoopDecrement can not be nullptr! +; CHECK-NOBITWIDTH: HWLoopInfo.CountType could not be nullptr! +entry: + %cmp4 = icmp ult i32 %i, %N + br i1 %cmp4, label %while.body, label %while.end + +while.body: + %i.addr.05 = phi i32 [ %inc, %while.body ], [ %i, %entry ] + %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i.addr.05 + store i32 %i.addr.05, ptr %arrayidx, align 4 + %inc = add nuw i32 %i.addr.05, 1 + %exitcond = icmp eq i32 %inc, %N + br i1 %exitcond, label %while.end, label %while.body + +while.end: + ret void +}