diff --git a/examples/matrix.cpp b/examples/matrix.cpp index 0c42165bc..09bbcb593 100644 --- a/examples/matrix.cpp +++ b/examples/matrix.cpp @@ -15,11 +15,14 @@ const char src[] = "\ void test(fp32 *A, fp32 *B, fp32 *C, int32 i){\ int32 j = 1;\ - int32 test[16, 16] = 0;\ - int32 test2[16, 16];\ - int32 test3[16, 16];\ int32 k;\ - test = test2 + test3;\ + i = i + j;\ + for(k = 0; k < 10; k = k+1){\ + int32 u = 1;\ + u = u + i;\ + if(k == 0)\ + u = u + 2;\ + }\ }\ "; diff --git a/include/ir/builder.h b/include/ir/builder.h index 4e1fd922c..3e0ba4293 100644 --- a/include/ir/builder.h +++ b/include/ir/builder.h @@ -1,6 +1,7 @@ #ifndef TDL_INCLUDE_IR_BUILDER_H #define TDL_INCLUDE_IR_BUILDER_H +#include #include #include #include "instructions.h" @@ -37,9 +38,12 @@ public: // Insert template InstTy* insert(InstTy *inst, const std::string &name = ""){ - if(block_) - block_->get_inst_list().insert(insert_point_, inst); + assert(block_); + block_->get_inst_list().insert(insert_point_, inst); + inst->set_parent(block_); inst->set_name(name); + insert_point_ = block_->end(); + return inst; } // terminator instructions value* create_br(basic_block *dest); diff --git a/include/ir/function.h b/include/ir/function.h index 456faf5f3..52b1579d7 100644 --- a/include/ir/function.h +++ b/include/ir/function.h @@ -39,8 +39,6 @@ public: arg_iterator arg_end() { return args_.end(); } const_arg_iterator arg_begin() const { return args_.begin(); } const_arg_iterator arg_end() const { return args_.end(); } - // Accessors - function_type* get_function_ty() const; // Factory methods static function *create(function_type *ty, linkage_types_t linkage, const std::string &name, module *mod); @@ -49,6 +47,7 @@ private: module *parent_; args_t args_; bool init_; + function_type *fn_ty_; }; } diff --git a/include/ir/instructions.h b/include/ir/instructions.h index 1d7ecffcd..53d437082 100644 --- a/include/ir/instructions.h +++ b/include/ir/instructions.h @@ -23,8 +23,9 @@ protected: public: // parent - const basic_block *get_parent() const { return parent_;} - basic_block *get_parent() { return parent_; } + void set_parent(basic_block *block) { parent_ = block; } + const basic_block *get_parent() const { return parent_; } + basic_block *get_parent() { return parent_; } private: basic_block *parent_; @@ -148,7 +149,7 @@ public: typedef llvm::CastInst::CastOps op_t; private: - bool is_valid(op_t op, value *arg, type *ty); + static bool is_valid(op_t op, value *arg, type *ty); public: // Factory methods diff --git a/include/ir/type.h b/include/ir/type.h index ebbbe5952..c089df4c2 100644 --- a/include/ir/type.h +++ b/include/ir/type.h @@ -2,6 +2,7 @@ #define TDL_INCLUDE_IR_TYPE_H #include +#include namespace tdl{ namespace ir{ @@ -34,7 +35,7 @@ public: public: //constructors - type(context &ctx, id_t id) : ctx_(ctx), id_(id) {} + type(context &ctx, id_t id) : ctx_(ctx), id_(id) { } //destructor virtual ~type(){} diff --git a/include/ir/value.h b/include/ir/value.h index bab034603..c8b162071 100644 --- a/include/ir/value.h +++ b/include/ir/value.h @@ -24,6 +24,7 @@ public: void add_use(use *arg); // name void set_name(const std::string &name); + const std::string &get_name() const { return name_; } type* get_type() const { return ty_; } private: diff --git a/lib/codegen.cpp b/lib/codegen.cpp index d6f41bba3..12732b1b4 100644 --- a/lib/codegen.cpp +++ b/lib/codegen.cpp @@ -310,7 +310,7 @@ inline void implicit_broadcast(ir::module *mod, ir::builder &builder, ir::value ir::value *binary_operator::llvm_op(ir::module *mod, ir::builder &builder, ir::value *lhs, ir::value *rhs, const std::string &name) const { bool is_float = false, is_ptr = false, is_int = false, is_signed = false; -// implicit_cast(builder, lhs, rhs, is_float, is_ptr, is_int, is_signed); + implicit_cast(builder, lhs, rhs, is_float, is_ptr, is_int, is_signed); // implicit_broadcast(mod, builder, lhs, rhs); if(op_==MUL && is_float) return builder.create_fmul(lhs, rhs, name); diff --git a/lib/ir/basic_block.cpp b/lib/ir/basic_block.cpp index 359c55d0c..29361b3b7 100644 --- a/lib/ir/basic_block.cpp +++ b/lib/ir/basic_block.cpp @@ -9,18 +9,22 @@ class phi_node; basic_block::basic_block(context &ctx, const std::string &name, function *parent): value(type::get_label_ty(ctx), name), ctx_(ctx), parent_(parent){ - } basic_block* basic_block::create(context &ctx, const std::string &name, function *parent){ return new basic_block(ctx, name, parent); } +void basic_block::add_predecessor(basic_block *pred) { + preds_.push_back(pred); +} + basic_block::iterator basic_block::get_first_non_phi(){ auto it = begin(); for(; it != end(); it++) - if(!dynamic_cast(*it)) + if(!dynamic_cast(*it)){ return it; + } return it; } diff --git a/lib/ir/builder.cpp b/lib/ir/builder.cpp index b26c1946e..a2c7ef809 100644 --- a/lib/ir/builder.cpp +++ b/lib/ir/builder.cpp @@ -35,16 +35,25 @@ value *builder::get_int32(unsigned val) { return constant_int::get(type::get_int32_ty(ctx_), val); } +type *builder::get_float_ty() +{ return type::get_float_ty(ctx_); } + +type *builder::get_double_ty() +{ return type::get_double_ty(ctx_); } + //===----------------------------------------------------------------------===// // terminator instructions //===----------------------------------------------------------------------===// value* builder::create_br(basic_block *dest){ + dest->add_predecessor(block_); return insert(branch_inst::create(dest)); } value* builder::create_cond_br(value *cond, basic_block *if_dest, basic_block *else_dest){ + if_dest->add_predecessor(block_); + else_dest->add_predecessor(block_); return insert(branch_inst::create(cond, if_dest, else_dest)); } diff --git a/lib/ir/function.cpp b/lib/ir/function.cpp index b7cc14df5..2def17325 100644 --- a/lib/ir/function.cpp +++ b/lib/ir/function.cpp @@ -18,16 +18,16 @@ argument *argument::create(type *ty, const std::string &name, /* function */ function::function(function_type *ty, linkage_types_t linkage, const std::string &name, module *parent) - : global_object(ty, 0, linkage, name), parent_(parent) { + : global_object(ty, 0, linkage, name), parent_(parent), fn_ty_(ty) { + unsigned num_params = fn_ty_->get_num_params(); + // skip if no parameter + if(num_params == 0) + return; // create arguments - function_type *fn_ty = get_function_ty(); - unsigned num_params = fn_ty->get_num_params(); - if(num_params > 0) { - args_.resize(num_params); - for(unsigned i = 0; i < num_params; i++){ - type *param_ty = fn_ty->get_param_ty(i); - args_.push_back(argument::create(param_ty, "", this, i)); - } + args_.resize(num_params); + for(unsigned i = 0; i < num_params; i++){ + type *param_ty = fn_ty_->get_param_ty(i); + args_[i] = argument::create(param_ty, "", this, i); } } @@ -38,10 +38,6 @@ function *function::create(function_type *ty, linkage_types_t linkage, } -function_type* function::get_function_ty() const -{ return static_cast(get_type()); } - - } } diff --git a/lib/ir/instructions.cpp b/lib/ir/instructions.cpp index d330e015d..92a42fb00 100644 --- a/lib/ir/instructions.cpp +++ b/lib/ir/instructions.cpp @@ -26,7 +26,7 @@ instruction::instruction(type *ty, unsigned num_ops, const std::string &name, in //===----------------------------------------------------------------------===// phi_node::phi_node(type *ty, unsigned num_reserved, std::string const &name, instruction *next) - : instruction(ty, num_reserved, name, next){ } + : instruction(ty, num_reserved, name, next), blocks_(num_reserved){ } // Set incoming value void phi_node::set_incoming_value(unsigned i, value *v){ @@ -152,6 +152,11 @@ unary_inst::unary_inst(type *ty, value *v, const std::string &name, instruction // cast_inst classes //===----------------------------------------------------------------------===// +// TODO +bool cast_inst::is_valid(op_t op, value *arg, type *ty) { + return true; +} + cast_inst *cast_inst::create(op_t op, value *arg, type *ty, const std::string &name, instruction *next){ assert(is_valid(op, arg, ty) && "Invalid cast!"); // Construct and return the appropriate CastInst subclass diff --git a/lib/ir/module.cpp b/lib/ir/module.cpp index c1979881b..d5ad53e26 100644 --- a/lib/ir/module.cpp +++ b/lib/ir/module.cpp @@ -1,5 +1,6 @@ #include "ir/basic_block.h" #include "ir/module.h" +#include "ir/type.h" namespace tdl{ namespace ir{ @@ -28,10 +29,10 @@ void module::set_value(const std::string& name, ir::value *value){ ir::phi_node* module::make_phi(ir::type *ty, unsigned num_values, ir::basic_block *block){ basic_block::iterator insert = block->get_first_non_phi(); - if(insert == block->end()) + if(*insert) builder_.set_insert_point(insert); ir::phi_node *res = builder_.create_phi(ty, num_values); - if(insert == block->end()) + if(*insert) builder_.set_insert_point(block); return res; }