@@ -31,6 +31,7 @@ static constexpr char FDIV_ERROR[] = "2.5";
3131PreservedAnalyses
3232SYCLSqrtFDivMaxErrorCleanUpPass::run (Module &M,
3333 ModuleAnalysisManager &MAM) {
34+ SmallVector<std::pair<Instruction*, Instruction*>, 16 > Replaces;
3435 SmallVector<IntrinsicInst *, 16 > WorkListSqrt;
3536 SmallVector<IntrinsicInst *, 16 > WorkListFDiv;
3637
@@ -122,10 +123,11 @@ SYCLSqrtFDivMaxErrorCleanUpPass::run(Module &M,
122123 Attrs = Attrs.removeFnAttribute (Sqrt->getContext (), " fpbuiltin-max-error" );
123124 NewSqrt->setAttributes (Attrs);
124125 NewSqrt->copyMetadata (*Sqrt);
125- FPMathOperator *FPOp = cast<FPMathOperator>(Sqrt);
126- FastMathFlags FMF = FPOp->getFastMathFlags ();
127- NewSqrt->setFastMathFlags (FMF);
128- Sqrt->replaceAllUsesWith (NewSqrt);
126+ if (FPMathOperator *FPOp = dyn_cast<FPMathOperator>(Sqrt)) {
127+ FastMathFlags FMF = FPOp->getFastMathFlags ();
128+ NewSqrt->setFastMathFlags (FMF);
129+ }
130+ Replaces.emplace_back (std::make_pair (Sqrt, NewSqrt));
129131 InstsToRemove.push_back (Sqrt);
130132 }
131133
@@ -140,13 +142,17 @@ SYCLSqrtFDivMaxErrorCleanUpPass::run(Module &M,
140142
141143 // Copy FP flags and metadata. Replace old call with a new instruction.
142144 cast<Instruction>(NewFDiv)->copyMetadata (*FDiv);
143- FPMathOperator *FPOp = cast<FPMathOperator>(FDiv);
144- FastMathFlags FMF = FPOp->getFastMathFlags ();
145- NewFDiv->setFastMathFlags (FMF);
146- FDiv->replaceAllUsesWith (NewFDiv);
145+ if (FPMathOperator *FPOp = dyn_cast<FPMathOperator>(FDiv)) {
146+ FastMathFlags FMF = FPOp->getFastMathFlags ();
147+ NewFDiv->setFastMathFlags (FMF);
148+ }
149+ Replaces.emplace_back (std::make_pair (FDiv, NewFDiv));
147150 InstsToRemove.push_back (FDiv);
148151 }
149152
153+ for (const auto &InstPair : Replaces)
154+ InstPair.first ->replaceAllUsesWith (InstPair.second );
155+
150156 // Clear instructions.
151157 for (auto *Inst : llvm::reverse (InstsToRemove)) {
152158 Inst->dropAllReferences ();
0 commit comments