Skip to main content

pliron_llvm/
interface_impls.rs

1//! Implementation of various op interfaces for LLVM IR instructions.
2
3use pliron::{
4    basic_block::BasicBlock,
5    context::{Context, Ptr},
6    derive::op_interface_impl,
7    opts::dce::{BlockArgRemoval, SideEffects},
8};
9
10use crate::ops::{
11    AShrOp, AddOp, AddressOfOp, AllocaOp, AndOp, BitcastOp, ConstantOp, ExtractElementOp,
12    ExtractValueOp, FAddOp, FCmpOp, FDivOp, FMulOp, FNegOp, FPExtOp, FPToSIOp, FPToUIOp, FPTruncOp,
13    FRemOp, FSubOp, FreezeOp, FuncOp, GetElementPtrOp, ICmpOp, InsertElementOp, InsertValueOp,
14    IntToPtrOp, LShrOp, MulOp, OrOp, PoisonOp, PtrToIntOp, SDivOp, SExtOp, SIToFPOp, SRemOp,
15    SelectOp, ShlOp, ShuffleVectorOp, SubOp, TruncOp, UDivOp, UIToFPOp, URemOp, UndefOp, XorOp,
16    ZExtOp, ZeroOp,
17};
18
19// Implement [SideEffects] with `has_side_effects` returning `false`
20macro_rules! impl_side_effects_false {
21  ($($op:ty),+ $(,)?) => {
22    $(
23      #[op_interface_impl]
24      impl SideEffects for $op {
25        fn has_side_effects(&self, _ctx: &Context) -> bool {
26          false
27        }
28      }
29    )+
30  };
31}
32
33// Pure value-producing ops with no memory/control side effects.
34// We don't need to implement [SideEffects] for the other ops,
35// because the assumption is that the absense of the interface
36// implies the presence of side effects, which is a safe default for DCE.
37impl_side_effects_false!(
38    AddOp,
39    SubOp,
40    MulOp,
41    ShlOp,
42    UDivOp,
43    SDivOp,
44    URemOp,
45    SRemOp,
46    AndOp,
47    OrOp,
48    XorOp,
49    LShrOp,
50    AShrOp,
51    ICmpOp,
52    AllocaOp,
53    BitcastOp,
54    IntToPtrOp,
55    PtrToIntOp,
56    UndefOp,
57    PoisonOp,
58    FreezeOp,
59    ConstantOp,
60    ZeroOp,
61    AddressOfOp,
62    SExtOp,
63    ZExtOp,
64    FPExtOp,
65    TruncOp,
66    FPTruncOp,
67    FPToSIOp,
68    FPToUIOp,
69    SIToFPOp,
70    UIToFPOp,
71    InsertValueOp,
72    ExtractValueOp,
73    InsertElementOp,
74    ExtractElementOp,
75    ShuffleVectorOp,
76    SelectOp,
77    FNegOp,
78    FAddOp,
79    FSubOp,
80    FMulOp,
81    FDivOp,
82    FRemOp,
83    FCmpOp,
84    GetElementPtrOp,
85);
86
87#[op_interface_impl]
88impl BlockArgRemoval for FuncOp {
89    fn can_remove_block_args(&self, ctx: &Context, block: Ptr<BasicBlock>) -> bool {
90        !matches!(self.get_entry_block(ctx), Some(entry) if entry == block)
91    }
92}