[llvm] [SandboxIR] Implement BitCastInst (PR #101227)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 12:58:50 PDT 2024


================
@@ -1386,6 +1390,26 @@ class PtrToIntInst final : public CastInst {
 #endif // NDEBUG
 };
 
+class BitCastInst : public CastInst {
+public:
+  static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
+                       BasicBlock *WhereBB, Context &Ctx,
+                       const Twine &Name = "");
+  static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
+                       Context &Ctx, const Twine &Name = "");
+  static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
+                       Context &Ctx, const Twine &Name = "");
+
+  static bool classof(const Value *From) {
+    return isa<Instruction>(From) &&
----------------
vporpo wrote:

`isa` + `cast` is pretty common for these exact cases where you want to early return if the instruction is of the wrong type. Just grep for it in `llvm/lib`, you will find lots of examples that look exactly like this.

The alternative would look like:
```
auto *I = dyn_cast<Instruction>(From);
return I == nullptr ? false : I->getOpcode() == Instruction::Opcode::BitCast;
```
which is not as elegant and probably slower because of the extra control flow within `dyn_cast`.

> "Note that you should not use an isa<> test followed by a cast<>, for that use the dyn_cast<> operator."

I think this is meant to discourage things like:
```
  Foo *I = nullptr;
  if (isa<Foo>(V))
     I = cast<Foo>(V);
``` 

https://github.com/llvm/llvm-project/pull/101227


More information about the llvm-commits mailing list