@@ -824,23 +824,25 @@ namespace {
824824/// - Get the operands for the interchangeable form (getOperand)
825825class InterchangeableInstruction {
826826protected:
827- Instruction *const MainOp;
827+ Instruction *const MainOp = nullptr ;
828828
829829public:
830830 InterchangeableInstruction(Instruction *MainOp) : MainOp(MainOp) {}
831831 virtual bool isSame(Instruction *I) {
832832 return MainOp->getOpcode() == I->getOpcode();
833833 }
834- virtual unsigned getOpcode() { return MainOp->getOpcode(); }
835- virtual SmallVector<Value *> getOperand(Instruction *I) {
836- assert(MainOp->getOpcode() == I->getOpcode());
834+ virtual unsigned getOpcode() const { return MainOp->getOpcode(); }
835+ virtual SmallVector<Value *> getOperand(Instruction *I) const {
836+ assert(MainOp->getOpcode() == I->getOpcode() &&
837+ "Cannot convert the instruction.");
837838 return SmallVector<Value *>(MainOp->operands());
838839 }
839840 virtual ~InterchangeableInstruction() = default;
840841};
841842
842843class InterchangeableBinOp final : public InterchangeableInstruction {
843844 using MaskType = std::uint_fast8_t;
845+ // Sort SupportedOp because it is used by binary_search.
844846 constexpr static std::initializer_list<unsigned> SupportedOp = {
845847 Instruction::Add, Instruction::Sub, Instruction::Mul, Instruction::Shl,
846848 Instruction::AShr, Instruction::And, Instruction::Or, Instruction::Xor};
@@ -934,7 +936,7 @@ class InterchangeableBinOp final : public InterchangeableInstruction {
934936 }
935937 return tryAnd(opcodeToMask(Opcode));
936938 }
937- unsigned getOpcode() override {
939+ unsigned getOpcode() const override {
938940 MaskType Candidate = Mask & SeenBefore;
939941 if (Candidate & 0b1)
940942 return Instruction::Shl;
@@ -954,7 +956,7 @@ class InterchangeableBinOp final : public InterchangeableInstruction {
954956 return Instruction::Xor;
955957 llvm_unreachable("Cannot find interchangeable instruction.");
956958 }
957- SmallVector<Value *> getOperand(Instruction *I) override {
959+ SmallVector<Value *> getOperand(Instruction *I) const override {
958960 unsigned ToOpcode = I->getOpcode();
959961 assert(binary_search(SupportedOp, ToOpcode) && "Unsupported opcode.");
960962 unsigned FromOpcode = MainOp->getOpcode();
@@ -997,8 +999,9 @@ class InterchangeableBinOp final : public InterchangeableInstruction {
997999 ToCIValue = APInt::getZero(FromCIValueBitWidth);
9981000 break;
9991001 }
1000- auto LHS = MainOp->getOperand(1 - Pos);
1001- auto RHS = ConstantInt::get(MainOp->getOperand(Pos)->getType(), ToCIValue);
1002+ Value *LHS = MainOp->getOperand(1 - Pos);
1003+ Constant *RHS =
1004+ ConstantInt::get(MainOp->getOperand(Pos)->getType(), ToCIValue);
10021005 if (Pos == 1)
10031006 return SmallVector<Value *>({LHS, RHS});
10041007 return SmallVector<Value *>({RHS, LHS});
@@ -1363,7 +1366,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
13631366 for (Value *V : VL) {
13641367 if (isa<PoisonValue>(V))
13651368 continue;
1366- Instruction *Inst = cast<Instruction>(V);
1369+ auto *Inst = cast<Instruction>(V);
13671370 if (Inst->getOpcode() == InterchangeableInstructionOpcode)
13681371 return Inst;
13691372 }
0 commit comments