Instruction.hpp 10.9 KB
Newer Older
lxq's avatar
lxq committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#pragma once

#include "Type.hpp"
#include "User.hpp"

#include <cstdint>
#include <llvm/ADT/ilist_node.h>

class BasicBlock;
class Function;

class Instruction : public User, public llvm::ilist_node<Instruction> {
  public:
    enum OpID : uint32_t {
        // Terminator Instructions
        ret,
        br,
        // Standard binary operators
        add,
        sub,
        mul,
        sdiv,
        // float binary operators
        fadd,
        fsub,
        fmul,
        fdiv,
        // Memory operators
        alloca,
        load,
        store,
        // Int compare operators
        ge,
        gt,
        le,
        lt,
        eq,
        ne,
        // Float compare operators
        fge,
        fgt,
        fle,
        flt,
        feq,
        fne,
        // Other operators
        phi,
        call,
        getelementptr,
        zext, // zero extend
        fptosi,
        sitofp
        // float binary operators Logical operators

    };
    /* @parent: if parent!=nullptr, auto insert to bb
     * @ty: result type */
    Instruction(Type *ty, OpID id, BasicBlock *parent = nullptr);
    Instruction(const Instruction &) = delete;
    virtual ~Instruction() = default;

    BasicBlock *get_parent() { return parent_; }
    const BasicBlock *get_parent() const { return parent_; }
    void set_parent(BasicBlock *parent) { this->parent_ = parent; }

    // Return the function this instruction belongs to.
    Function *get_function();
    Module *get_module();

    OpID get_instr_type() const { return op_id_; }
    std::string get_instr_op_name() const;

    bool is_void() {
        return ((op_id_ == ret) || (op_id_ == br) || (op_id_ == store) ||
                (op_id_ == call && this->get_type()->is_void_type()));
    }

    bool is_phi() const { return op_id_ == phi; }
    bool is_store() const { return op_id_ == store; }
    bool is_alloca() const { return op_id_ == alloca; }
    bool is_ret() const { return op_id_ == ret; }
    bool is_load() const { return op_id_ == load; }
    bool is_br() const { return op_id_ == br; }

    bool is_add() const { return op_id_ == add; }
    bool is_sub() const { return op_id_ == sub; }
    bool is_mul() const { return op_id_ == mul; }
    bool is_div() const { return op_id_ == sdiv; }

    bool is_fadd() const { return op_id_ == fadd; }
    bool is_fsub() const { return op_id_ == fsub; }
    bool is_fmul() const { return op_id_ == fmul; }
    bool is_fdiv() const { return op_id_ == fdiv; }
    bool is_fp2si() const { return op_id_ == fptosi; }
    bool is_si2fp() const { return op_id_ == sitofp; }

    bool is_cmp() const { return ge <= op_id_ and op_id_ <= ne; }
    bool is_fcmp() const { return fge <= op_id_ and op_id_ <= fne; }

    bool is_call() const { return op_id_ == call; }
    bool is_gep() const { return op_id_ == getelementptr; }
    bool is_zext() const { return op_id_ == zext; }

    bool isBinary() const {
        return (is_add() || is_sub() || is_mul() || is_div() || is_fadd() ||
                is_fsub() || is_fmul() || is_fdiv()) &&
               (get_num_operand() == 2);
    }

    bool isTerminator() const { return is_br() || is_ret(); }

  private:
    OpID op_id_;
    BasicBlock *parent_;
};

template <typename Inst> class BaseInst : public Instruction {
  protected:
    template <typename... Args> static Inst *create(Args &&...args) {
        return new Inst(std::forward<Args>(args)...);
    }

    template <typename... Args>
    BaseInst(Args &&...args) : Instruction(std::forward<Args>(args)...) {}
};

class IBinaryInst : public BaseInst<IBinaryInst> {
    friend BaseInst<IBinaryInst>;

  private:
    IBinaryInst(OpID id, Value *v1, Value *v2, BasicBlock *bb);

  public:
    static IBinaryInst *create_add(Value *v1, Value *v2, BasicBlock *bb);
    static IBinaryInst *create_sub(Value *v1, Value *v2, BasicBlock *bb);
    static IBinaryInst *create_mul(Value *v1, Value *v2, BasicBlock *bb);
    static IBinaryInst *create_sdiv(Value *v1, Value *v2, BasicBlock *bb);

    virtual std::string print() override;
};

class FBinaryInst : public BaseInst<FBinaryInst> {
    friend BaseInst<FBinaryInst>;

  private:
    FBinaryInst(OpID id, Value *v1, Value *v2, BasicBlock *bb);

  public:
    static FBinaryInst *create_fadd(Value *v1, Value *v2, BasicBlock *bb);
    static FBinaryInst *create_fsub(Value *v1, Value *v2, BasicBlock *bb);
    static FBinaryInst *create_fmul(Value *v1, Value *v2, BasicBlock *bb);
    static FBinaryInst *create_fdiv(Value *v1, Value *v2, BasicBlock *bb);

    virtual std::string print() override;
};

class ICmpInst : public BaseInst<ICmpInst> {
    friend BaseInst<ICmpInst>;

  private:
    ICmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb);

  public:
    static ICmpInst *create_ge(Value *v1, Value *v2, BasicBlock *bb);
    static ICmpInst *create_gt(Value *v1, Value *v2, BasicBlock *bb);
    static ICmpInst *create_le(Value *v1, Value *v2, BasicBlock *bb);
    static ICmpInst *create_lt(Value *v1, Value *v2, BasicBlock *bb);
    static ICmpInst *create_eq(Value *v1, Value *v2, BasicBlock *bb);
    static ICmpInst *create_ne(Value *v1, Value *v2, BasicBlock *bb);

    virtual std::string print() override;
};

class FCmpInst : public BaseInst<FCmpInst> {
    friend BaseInst<FCmpInst>;

  private:
    FCmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb);

  public:
    static FCmpInst *create_fge(Value *v1, Value *v2, BasicBlock *bb);
    static FCmpInst *create_fgt(Value *v1, Value *v2, BasicBlock *bb);
    static FCmpInst *create_fle(Value *v1, Value *v2, BasicBlock *bb);
    static FCmpInst *create_flt(Value *v1, Value *v2, BasicBlock *bb);
    static FCmpInst *create_feq(Value *v1, Value *v2, BasicBlock *bb);
    static FCmpInst *create_fne(Value *v1, Value *v2, BasicBlock *bb);

    virtual std::string print() override;
};

class CallInst : public BaseInst<CallInst> {
    friend BaseInst<CallInst>;

  protected:
    CallInst(Function *func, std::vector<Value *> args, BasicBlock *bb);

  public:
    static CallInst *create_call(Function *func, std::vector<Value *> args,
                                 BasicBlock *bb);
    FunctionType *get_function_type() const;

    virtual std::string print() override;
};

class BranchInst : public BaseInst<BranchInst> {
    friend BaseInst<BranchInst>;

  private:
    BranchInst(Value *cond, BasicBlock *if_true, BasicBlock *if_false,
               BasicBlock *bb);
    ~BranchInst();

  public:
    static BranchInst *create_cond_br(Value *cond, BasicBlock *if_true,
                                      BasicBlock *if_false, BasicBlock *bb);
    static BranchInst *create_br(BasicBlock *if_true, BasicBlock *bb);

    bool is_cond_br() const { return get_num_operand() == 3; }

    Value *get_condition() const { return get_operand(0); }

    virtual std::string print() override;
};

class ReturnInst : public BaseInst<ReturnInst> {
    friend BaseInst<ReturnInst>;

  private:
    ReturnInst(Value *val, BasicBlock *bb);

  public:
    static ReturnInst *create_ret(Value *val, BasicBlock *bb);
    static ReturnInst *create_void_ret(BasicBlock *bb);
    bool is_void_ret() const;

    virtual std::string print() override;
};

class GetElementPtrInst : public BaseInst<GetElementPtrInst> {
    friend BaseInst<GetElementPtrInst>;

  private:
    GetElementPtrInst(Value *ptr, std::vector<Value *> idxs, BasicBlock *bb);

  public:
    static Type *get_element_type(Value *ptr, std::vector<Value *> idxs);
    static GetElementPtrInst *create_gep(Value *ptr, std::vector<Value *> idxs,
                                         BasicBlock *bb);
    Type *get_element_type() const;

    virtual std::string print() override;
};

class StoreInst : public BaseInst<StoreInst> {
    friend BaseInst<StoreInst>;

  private:
    StoreInst(Value *val, Value *ptr, BasicBlock *bb);

  public:
    static StoreInst *create_store(Value *val, Value *ptr, BasicBlock *bb);

    Value *get_rval() { return this->get_operand(0); }
    Value *get_lval() { return this->get_operand(1); }

    virtual std::string print() override;
};

class LoadInst : public BaseInst<LoadInst> {
    friend BaseInst<LoadInst>;

  private:
    LoadInst(Value *ptr, BasicBlock *bb);

  public:
    static LoadInst *create_load(Value *ptr, BasicBlock *bb);

    Value *get_lval() const { return this->get_operand(0); }
    Type *get_load_type() const { return get_type(); };

    virtual std::string print() override;
};

class AllocaInst : public BaseInst<AllocaInst> {
    friend BaseInst<AllocaInst>;

  private:
    AllocaInst(Type *ty, BasicBlock *bb);

  public:
    static AllocaInst *create_alloca(Type *ty, BasicBlock *bb);

    Type *get_alloca_type() const {
        return get_type()->get_pointer_element_type();
    };

    virtual std::string print() override;
};

class ZextInst : public BaseInst<ZextInst> {
    friend BaseInst<ZextInst>;

  private:
    ZextInst(Value *val, Type *ty, BasicBlock *bb);

  public:
    static ZextInst *create_zext(Value *val, Type *ty, BasicBlock *bb);
    static ZextInst *create_zext_to_i32(Value *val, BasicBlock *bb);

    Type *get_dest_type() const { return get_type(); };

    virtual std::string print() override;
};

class FpToSiInst : public BaseInst<FpToSiInst> {
    friend BaseInst<FpToSiInst>;

  private:
    FpToSiInst(Value *val, Type *ty, BasicBlock *bb);

  public:
    static FpToSiInst *create_fptosi(Value *val, Type *ty, BasicBlock *bb);
    static FpToSiInst *create_fptosi_to_i32(Value *val, BasicBlock *bb);

    Type *get_dest_type() const { return get_type(); };

    virtual std::string print() override;
};

class SiToFpInst : public BaseInst<SiToFpInst> {
    friend BaseInst<SiToFpInst>;

  private:
    SiToFpInst(Value *val, Type *ty, BasicBlock *bb);

  public:
    static SiToFpInst *create_sitofp(Value *val, BasicBlock *bb);

    Type *get_dest_type() const { return get_type(); };

    virtual std::string print() override;
};

class PhiInst : public BaseInst<PhiInst> {
    friend BaseInst<PhiInst>;

  private:
    PhiInst(Type *ty, std::vector<Value *> vals,
            std::vector<BasicBlock *> val_bbs, BasicBlock *bb);

  public:
    static PhiInst *create_phi(Type *ty, BasicBlock *bb,
                               std::vector<Value *> vals = {},
                               std::vector<BasicBlock *> val_bbs = {});

    void add_phi_pair_operand(Value *val, Value *pre_bb) {
        this->add_operand(val);
        this->add_operand(pre_bb);
    }
刘睿博's avatar
刘睿博 committed
360 361 362 363 364 365 366 367 368 369 370

    void remove_phi_operand(Value *pre_bb) {
        for (unsigned i = 0; i < this->get_num_operand(); i += 2) {
            if (this->get_operand(i + 1) == pre_bb) {
                this->remove_operand(i);
                this->remove_operand(i);
                return;
            }
        }
    }

lxq's avatar
lxq committed
371 372 373 374 375 376 377 378 379 380
    std::vector<std::pair<Value *, BasicBlock *>> get_phi_pairs() {
        std::vector<std::pair<Value *, BasicBlock *>> res;
        for (size_t i = 0; i < get_num_operand(); i += 2) {
            res.push_back({this->get_operand(i),
                           this->get_operand(i + 1)->as<BasicBlock>()});
        }
        return res;
    }
    virtual std::string print() override;
};