Instruction.cpp 12.8 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 360 361 362 363 364
#include "Instruction.hpp"
#include "BasicBlock.hpp"
#include "Function.hpp"
#include "IRprinter.hpp"
#include "Module.hpp"
#include "Type.hpp"

#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <string>
#include <vector>

Instruction::Instruction(Type *ty, OpID id, BasicBlock *parent)
    : User(ty, ""), op_id_(id), parent_(parent) {
    if (parent)
        parent->add_instruction(this);
}

Function *Instruction::get_function() { return parent_->get_parent(); }
Module *Instruction::get_module() { return parent_->get_module(); }

std::string Instruction::get_instr_op_name() const {
    return print_instr_op_name(op_id_);
}

IBinaryInst::IBinaryInst(OpID id, Value *v1, Value *v2, BasicBlock *bb)
    : BaseInst<IBinaryInst>(bb->get_module()->get_int32_type(), id, bb) {
    assert(v1->get_type()->is_int32_type() && v2->get_type()->is_int32_type() &&
           "IBinaryInst operands are not both i32");
    add_operand(v1);
    add_operand(v2);
}

IBinaryInst *IBinaryInst::create_add(Value *v1, Value *v2, BasicBlock *bb) {
    return create(add, v1, v2, bb);
}
IBinaryInst *IBinaryInst::create_sub(Value *v1, Value *v2, BasicBlock *bb) {
    return create(sub, v1, v2, bb);
}
IBinaryInst *IBinaryInst::create_mul(Value *v1, Value *v2, BasicBlock *bb) {
    return create(mul, v1, v2, bb);
}
IBinaryInst *IBinaryInst::create_sdiv(Value *v1, Value *v2, BasicBlock *bb) {
    return create(sdiv, v1, v2, bb);
}

FBinaryInst::FBinaryInst(OpID id, Value *v1, Value *v2, BasicBlock *bb)
    : BaseInst<FBinaryInst>(bb->get_module()->get_float_type(), id, bb) {
    assert(v1->get_type()->is_float_type() && v2->get_type()->is_float_type() &&
           "FBinaryInst operands are not both float");
    add_operand(v1);
    add_operand(v2);
}

FBinaryInst *FBinaryInst::create_fadd(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fadd, v1, v2, bb);
}
FBinaryInst *FBinaryInst::create_fsub(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fsub, v1, v2, bb);
}
FBinaryInst *FBinaryInst::create_fmul(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fmul, v1, v2, bb);
}
FBinaryInst *FBinaryInst::create_fdiv(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fdiv, v1, v2, bb);
}

ICmpInst::ICmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
    : BaseInst<ICmpInst>(bb->get_module()->get_int1_type(), id, bb) {
    assert(lhs->get_type()->is_int32_type() &&
           rhs->get_type()->is_int32_type() &&
           "CmpInst operands are not both i32");
    add_operand(lhs);
    add_operand(rhs);
}

ICmpInst *ICmpInst::create_ge(Value *v1, Value *v2, BasicBlock *bb) {
    return create(ge, v1, v2, bb);
}
ICmpInst *ICmpInst::create_gt(Value *v1, Value *v2, BasicBlock *bb) {
    return create(gt, v1, v2, bb);
}
ICmpInst *ICmpInst::create_le(Value *v1, Value *v2, BasicBlock *bb) {
    return create(le, v1, v2, bb);
}
ICmpInst *ICmpInst::create_lt(Value *v1, Value *v2, BasicBlock *bb) {
    return create(lt, v1, v2, bb);
}
ICmpInst *ICmpInst::create_eq(Value *v1, Value *v2, BasicBlock *bb) {
    return create(eq, v1, v2, bb);
}
ICmpInst *ICmpInst::create_ne(Value *v1, Value *v2, BasicBlock *bb) {
    return create(ne, v1, v2, bb);
}

FCmpInst::FCmpInst(OpID id, Value *lhs, Value *rhs, BasicBlock *bb)
    : BaseInst<FCmpInst>(bb->get_module()->get_int1_type(), id, bb) {
    assert(lhs->get_type()->is_float_type() &&
           rhs->get_type()->is_float_type() &&
           "FCmpInst operands are not both float");
    add_operand(lhs);
    add_operand(rhs);
}

FCmpInst *FCmpInst::create_fge(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fge, v1, v2, bb);
}
FCmpInst *FCmpInst::create_fgt(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fgt, v1, v2, bb);
}
FCmpInst *FCmpInst::create_fle(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fle, v1, v2, bb);
}
FCmpInst *FCmpInst::create_flt(Value *v1, Value *v2, BasicBlock *bb) {
    return create(flt, v1, v2, bb);
}
FCmpInst *FCmpInst::create_feq(Value *v1, Value *v2, BasicBlock *bb) {
    return create(feq, v1, v2, bb);
}
FCmpInst *FCmpInst::create_fne(Value *v1, Value *v2, BasicBlock *bb) {
    return create(fne, v1, v2, bb);
}

CallInst::CallInst(Function *func, std::vector<Value *> args, BasicBlock *bb)
    : BaseInst<CallInst>(func->get_return_type(), call, bb) {
    assert(func->get_type()->is_function_type() && "Not a function");
    assert((func->get_num_of_args() == args.size()) && "Wrong number of args");
    add_operand(func);
    auto func_type = static_cast<FunctionType *>(func->get_type());
    for (unsigned i = 0; i < args.size(); i++) {
        assert(func_type->get_param_type(i) == args[i]->get_type() &&
               "CallInst: Wrong arg type");
        add_operand(args[i]);
    }
}

CallInst *CallInst::create_call(Function *func, std::vector<Value *> args,
                                BasicBlock *bb) {
    return create(func, args, bb);
}

FunctionType *CallInst::get_function_type() const {
    return static_cast<FunctionType *>(get_operand(0)->get_type());
}

BranchInst::BranchInst(Value *cond, BasicBlock *if_true, BasicBlock *if_false,
                       BasicBlock *bb)
    : BaseInst<BranchInst>(bb->get_module()->get_void_type(), br, bb) {
    if (cond == nullptr) { // conditionless jump
        assert(if_false == nullptr && "Given false-bb on conditionless jump");
        add_operand(if_true);
        // prev/succ
        if_true->add_pre_basic_block(bb);
        bb->add_succ_basic_block(if_true);
    } else {
        assert(cond->get_type()->is_int1_type() &&
               "BranchInst condition is not i1");
        add_operand(cond);
        add_operand(if_true);
        add_operand(if_false);
        // prev/succ
        if_true->add_pre_basic_block(bb);
        if_false->add_pre_basic_block(bb);
        bb->add_succ_basic_block(if_true);
        bb->add_succ_basic_block(if_false);
    }
}

BranchInst::~BranchInst() {
    std::list<BasicBlock *> succs;
    if (is_cond_br()) {
        succs.push_back(static_cast<BasicBlock *>(get_operand(1)));
        succs.push_back(static_cast<BasicBlock *>(get_operand(2)));
    } else {
        succs.push_back(static_cast<BasicBlock *>(get_operand(0)));
    }
    for (auto succ_bb : succs) {
        if (succ_bb) {
            succ_bb->remove_pre_basic_block(get_parent());
            get_parent()->remove_succ_basic_block(succ_bb);
        }
    }
}

BranchInst *BranchInst::create_cond_br(Value *cond, BasicBlock *if_true,
                                       BasicBlock *if_false, BasicBlock *bb) {
    return create(cond, if_true, if_false, bb);
}

BranchInst *BranchInst::create_br(BasicBlock *if_true, BasicBlock *bb) {
    return create(nullptr, if_true, nullptr, bb);
}

ReturnInst::ReturnInst(Value *val, BasicBlock *bb)
    : BaseInst<ReturnInst>(bb->get_module()->get_void_type(), ret, bb) {
    if (val == nullptr) {
        assert(bb->get_parent()->get_return_type()->is_void_type());
    } else {
        assert(!bb->get_parent()->get_return_type()->is_void_type() &&
               "Void function returning a value");
        assert(bb->get_parent()->get_return_type() == val->get_type() &&
               "ReturnInst type is different from function return type");
        add_operand(val);
    }
}

ReturnInst *ReturnInst::create_ret(Value *val, BasicBlock *bb) {
    return create(val, bb);
}
ReturnInst *ReturnInst::create_void_ret(BasicBlock *bb) {
    return create(nullptr, bb);
}

bool ReturnInst::is_void_ret() const { return get_num_operand() == 0; }

GetElementPtrInst::GetElementPtrInst(Value *ptr, std::vector<Value *> idxs,
                                     BasicBlock *bb)
    : BaseInst<GetElementPtrInst>(PointerType::get(get_element_type(ptr, idxs)),
                                  getelementptr, bb) {
    add_operand(ptr);
    for (unsigned i = 0; i < idxs.size(); i++) {
        Value *idx = idxs[i];
        assert(idx->get_type()->is_integer_type() && "Index is not integer");
        add_operand(idx);
    }
}

Type *GetElementPtrInst::get_element_type(Value *ptr,
                                          std::vector<Value *> idxs) {
    assert(ptr->get_type()->is_pointer_type() &&
           "GetElementPtrInst ptr is not a pointer");

    Type *ty = ptr->get_type()->get_pointer_element_type();
    assert(
        "GetElementPtrInst ptr is wrong type" &&
        (ty->is_array_type() || ty->is_integer_type() || ty->is_float_type()));
    if (ty->is_array_type()) {
        ArrayType *arr_ty = static_cast<ArrayType *>(ty);
        for (unsigned i = 1; i < idxs.size(); i++) {
            ty = arr_ty->get_element_type();
            if (i < idxs.size() - 1) {
                assert(ty->is_array_type() && "Index error!");
            }
            if (ty->is_array_type()) {
                arr_ty = static_cast<ArrayType *>(ty);
            }
        }
    }
    return ty;
}

Type *GetElementPtrInst::get_element_type() const {
    return get_type()->get_pointer_element_type();
}

GetElementPtrInst *GetElementPtrInst::create_gep(Value *ptr,
                                                 std::vector<Value *> idxs,
                                                 BasicBlock *bb) {
    return create(ptr, idxs, bb);
}

StoreInst::StoreInst(Value *val, Value *ptr, BasicBlock *bb)
    : BaseInst<StoreInst>(bb->get_module()->get_void_type(), store, bb) {
    assert((ptr->get_type()->get_pointer_element_type() == val->get_type()) &&
           "StoreInst ptr is not a pointer to val type");
    add_operand(val);
    add_operand(ptr);
}

StoreInst *StoreInst::create_store(Value *val, Value *ptr, BasicBlock *bb) {
    return create(val, ptr, bb);
}

LoadInst::LoadInst(Value *ptr, BasicBlock *bb)
    : BaseInst<LoadInst>(ptr->get_type()->get_pointer_element_type(), load,
                         bb) {
    assert((get_type()->is_integer_type() or get_type()->is_float_type() or
            get_type()->is_pointer_type()) &&
           "Should not load value with type except int/float");
    add_operand(ptr);
}

LoadInst *LoadInst::create_load(Value *ptr, BasicBlock *bb) {
    return create(ptr, bb);
}

AllocaInst::AllocaInst(Type *ty, BasicBlock *bb)
    : BaseInst<AllocaInst>(PointerType::get(ty), alloca, bb) {
    static const std::array allowed_alloc_type = {
        Type::IntegerTyID, Type::FloatTyID, Type::ArrayTyID, Type::PointerTyID};
    assert(std::find(allowed_alloc_type.begin(), allowed_alloc_type.end(),
                     ty->get_type_id()) != allowed_alloc_type.end() &&
           "Not allowed type for alloca");
}

AllocaInst *AllocaInst::create_alloca(Type *ty, BasicBlock *bb) {
    return create(ty, bb);
}

ZextInst::ZextInst(Value *val, Type *ty, BasicBlock *bb)
    : BaseInst<ZextInst>(ty, zext, bb) {
    assert(val->get_type()->is_integer_type() &&
           "ZextInst operand is not integer");
    assert(ty->is_integer_type() && "ZextInst destination type is not integer");
    assert((static_cast<IntegerType *>(val->get_type())->get_num_bits() <
            static_cast<IntegerType *>(ty)->get_num_bits()) &&
           "ZextInst operand bit size is not smaller than destination type bit "
           "size");
    add_operand(val);
}

ZextInst *ZextInst::create_zext(Value *val, Type *ty, BasicBlock *bb) {
    return create(val, ty, bb);
}
ZextInst *ZextInst::create_zext_to_i32(Value *val, BasicBlock *bb) {
    return create(val, bb->get_module()->get_int32_type(), bb);
}

FpToSiInst::FpToSiInst(Value *val, Type *ty, BasicBlock *bb)
    : BaseInst<FpToSiInst>(ty, fptosi, bb) {
    assert(val->get_type()->is_float_type() &&
           "FpToSiInst operand is not float");
    assert(ty->is_integer_type() &&
           "FpToSiInst destination type is not integer");
    add_operand(val);
}

FpToSiInst *FpToSiInst::create_fptosi(Value *val, Type *ty, BasicBlock *bb) {
    return create(val, ty, bb);
}
FpToSiInst *FpToSiInst::create_fptosi_to_i32(Value *val, BasicBlock *bb) {
    return create(val, bb->get_module()->get_int32_type(), bb);
}

SiToFpInst::SiToFpInst(Value *val, Type *ty, BasicBlock *bb)
    : BaseInst<SiToFpInst>(ty, sitofp, bb) {
    assert(val->get_type()->is_integer_type() &&
           "SiToFpInst operand is not integer");
    assert(ty->is_float_type() && "SiToFpInst destination type is not float");
    add_operand(val);
}

SiToFpInst *SiToFpInst::create_sitofp(Value *val, BasicBlock *bb) {
    return create(val, bb->get_module()->get_float_type(), bb);
}

PhiInst::PhiInst(Type *ty, std::vector<Value *> vals,
                 std::vector<BasicBlock *> val_bbs, BasicBlock *bb)
    : BaseInst<PhiInst>(ty, phi) {
    assert(vals.size() == val_bbs.size() && "Unmatched vals and bbs");
    for (unsigned i = 0; i < vals.size(); i++) {
        assert(ty == vals[i]->get_type() && "Bad type for phi");
        add_operand(vals[i]);
        add_operand(val_bbs[i]);
    }
    this->set_parent(bb);
}

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