diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -3677,9 +3677,12 @@ APInt FoldConst = C1->getValue().lshr(C2->getValue()); FoldConst ^= C3->getValue(); // Prepare the two operands. - auto *Opnd0 = cast(Builder.CreateLShr(X, C2)); + Value *Opnd0 = Builder.CreateLShr(X, C2); Opnd0->takeName(cast(Op0)); - Opnd0->setDebugLoc(I.getDebugLoc()); + // When X is a constant, CreateLShr() returns a constant instead + // of an instruction. + if (Instruction *Opnd0Inst = dyn_cast(Opnd0)) + Opnd0Inst->setDebugLoc(I.getDebugLoc()); return BinaryOperator::CreateXor(Opnd0, ConstantInt::get(Ty, FoldConst)); } }