This is an archive of the discontinued LLVM Phabricator instance.

Remove BinaryOpsEnd case from ConstantFoldBinaryInstruction(...)
AbandonedPublic

Authored by cameron.mcinally on May 4 2019, 5:03 PM.

Details

Reviewers
spatel
arsenm
Summary

@spatel noticed this in another proposed patch. The isBinaryOp assert at the beginning of ConstantFoldBinaryInstruction(...) ensures that a BinaryOpsEnd will never reach this switch.

I've confirmed that all current BinaryOp Instructions are covered in this switch, so it should be safe to change this case to the default case.

Diff Detail

Repository
rL LLVM

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2019, 5:03 PM

Doesn't using BinaryOpsEnd ensure we'll get a warning if any enum value is missing from the switch in the future? Using a plain default would suppress that.

Doesn't using BinaryOpsEnd ensure we'll get a warning if any enum value is missing from the switch in the future? Using a plain default would suppress that.

There must be something subtle that I don't understand about this. Wouldn't a default case be even stronger -- it would catch those plus any non-BinaryOp that hits the switch?

At least in *+Asserts mode, we should never see a BinaryOpsEnd in this function. The assert on function entry is:

assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");

Here is isBinaryOp(...):

static inline bool isBinaryOp(unsigned Opcode) {
  return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
}

In *-Asserts mode, all bets are off. But I would think that a default case would be preferred there...

The type of the input is an enum type there would be a warning for a "fully covered switch default" if a default was placed in a switch the contained all enum values. The compiler assumes that the input will never take a value that isn't in the enum. You should get this warning if you add default without removing BinaryOpsEnd. There's also a warning that gets emitted when a enum value is missing from a switch. Putting a default in the switch will suppress that latter warning.

Oh, a proper warning at compile time. That makes sense. I'm okay with abandoning this patch if Sanjay is okay with it.

spatel added a comment.May 5 2019, 8:10 AM

Oh, a proper warning at compile time. That makes sense. I'm okay with abandoning this patch if Sanjay is okay with it.

That's fine; I wasn't thinking about compile-time warning vs. run-time assert.

cameron.mcinally abandoned this revision.May 5 2019, 8:41 AM

I'm noticing that lib/Analysis/InstructionSimplify.cpp has similar switches that use the default case instead of UnaryOpsEnd and BinaryOpsEnd. Is it worth taking the time to update those for compile-time warnings?

I haven't confirmed which of the switches are candidates yet, so that will need to be done if we move forward with this plan...

spatel added a comment.May 6 2019, 6:40 AM

I'm noticing that lib/Analysis/InstructionSimplify.cpp has similar switches that use the default case instead of UnaryOpsEnd and BinaryOpsEnd. Is it worth taking the time to update those for compile-time warnings?

It's a nicety, but IMO not worth your time. I'd rather get FP strict/fast IR optimization and codegen improvements. :)