Index: lib/Transforms/Scalar/ConstantHoisting.cpp =================================================================== --- lib/Transforms/Scalar/ConstantHoisting.cpp +++ lib/Transforms/Scalar/ConstantHoisting.cpp @@ -135,9 +135,22 @@ assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!"); if (Idx != ~0U && isa(Inst)) return cast(Inst)->getIncomingBlock(Idx)->getTerminator(); - - BasicBlock *IDom = DT->getNode(Inst->getParent())->getIDom()->getBlock(); - return IDom->getTerminator(); + // We start with what we already know is an EHPad Block + auto EHPadBlock = Inst->getParent(); + + // As already noted, we can't insert before an ehpad instruction, + // so climb the tree to the next idom. + auto IDom = DT->getNode(EHPadBlock)->getIDom(); + + // It is possible that IDom is an EHPad as well (i.e catchswitch). + // Keep climbing until a non-EHPad is found. + while(IDom->getBlock()->isEHPad()) + { + assert(Entry != IDom->getBlock() && "landing pad in entry block"); + IDom = IDom->getIDom(); + } + + return IDom->getBlock()->getTerminator(); } /// \brief Find an insertion point that dominates all uses. Index: test/Transforms/ConstantHoisting/X86/ehpad.ll =================================================================== --- test/Transforms/ConstantHoisting/X86/ehpad.ll +++ test/Transforms/ConstantHoisting/X86/ehpad.ll @@ -0,0 +1,45 @@ +; RUN: opt -S -consthoist < %s +; CHECK that verifier succeeds + +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc" + +; Function Attrs: norecurse +define i32 @main(i32 %argc, i8** nocapture readnone %argv) local_unnamed_addr #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) { + %call = tail call i64 @fn(i64 0) + %call1 = tail call i64 @fn(i64 1) + %tobool = icmp eq i32 %argc, 0 + br i1 %tobool, label %2, label %1 + +;