changeset 14937:78e1457c5bf5

Remove some macros from pt-jit.h and pt-jit.cc
author Max Brister <max@2bass.com>
date Tue, 05 Jun 2012 16:28:09 -0500
parents 32deb562ae77
children bab44e3ee291
files src/pt-jit.cc src/pt-jit.h
diffstat 2 files changed, 36 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/src/pt-jit.cc	Mon Jun 04 17:18:47 2012 -0500
+++ b/src/pt-jit.cc	Tue Jun 05 16:28:09 2012 -0500
@@ -771,7 +771,7 @@
   for (iterator iter = begin (); iter != end (); ++iter)
     {
       jit_instruction *temp = *iter;
-      if (! temp->is_phi ())
+      if (! isa<jit_phi> (temp))
         {
           insert_before (iter, instr);
           return instr;
@@ -813,7 +813,7 @@
     return 0;
 
   jit_instruction *last = instructions.back ();
-  return last->to_terminator ();
+  return dynamic_cast<jit_terminator *> (last);
 }
 
 jit_block *
@@ -1127,10 +1127,8 @@
   // more interesting
   for (jit_block::iterator iter = entry_block->begin ();
        iter != entry_block->end (); ++iter)
-    {
-      if (jit_extract_argument *extract = (*iter)->to_extract_argument ())
-        arguments.push_back (std::make_pair (extract->name (), true));
-    }
+    if (jit_extract_argument *extract = dynamic_cast<jit_extract_argument *> (*iter))
+      arguments.push_back (std::make_pair (extract->name (), true));
 
   convert_llvm to_llvm;
   function = to_llvm.convert (module, arguments, blocks);
@@ -1795,14 +1793,12 @@
   for (jit_block::iterator iter = block.begin (); iter != block.end (); ++iter)
     {
       jit_instruction *instr = *iter;
-      if (! instr->is_phi ())
+      if (! isa<jit_phi> (instr))
         {
           for (size_t i = 0; i < instr->argument_count (); ++i)
             {
-              jit_variable *var;
-              var = instr->argument_variable (i);
-              assert (var == instr->argument (i)->to_variable ());
-              assert (var == dynamic_cast<jit_variable *> (instr->argument (i)));
+              jit_value *arg = instr->argument (i);
+              jit_variable *var = dynamic_cast<jit_variable *> (arg);
               if (var)
                 instr->stash_argument (i, var->top ());
             }
@@ -1818,7 +1814,7 @@
       size_t pred_idx = finish->pred_index (&block);
 
       for (jit_block::iterator iter = finish->begin (); iter != finish->end ()
-             && (*iter)->is_phi (); ++iter)
+             && isa<jit_phi> (*iter); ++iter)
         {
           jit_instruction *phi = *iter;
           jit_variable *var = phi->tag ();
@@ -1854,7 +1850,8 @@
       jit_instruction *instr = *iter;
       for (size_t i = 0; i < instr->argument_count (); ++i)
         {
-          jit_instruction *arg = instr->argument_instruction (i);
+          jit_value *varg = instr->argument (i);
+          jit_instruction *arg = dynamic_cast<jit_instruction *> (varg);
           if (arg && arg->tag ())
             {
               jit_variable *tag = arg->tag ();
@@ -1863,7 +1860,7 @@
         }
 
       jit_variable *tag = instr->tag ();
-      if (tag && ! (instr->is_phi () || instr->is_store_argument ())
+      if (tag && ! (isa<jit_phi> (instr) || isa<jit_store_argument> (instr))
           && tag->has_top ())
         {
           jit_instruction *last_use = tag->last_use ();
@@ -1871,7 +1868,7 @@
                                                         tag->top ());
           release->infer ();
           if (last_use && last_use->parent () == &block
-              && ! last_use->is_phi ())
+              && ! isa<jit_phi> (last_use))
             block.insert_after (last_use->location (), release);
           else
             block.prepend_after_phi (release);
@@ -1931,7 +1928,7 @@
         {
           jit_block& block = **biter;
           for (jit_block::iterator piter = block.begin ();
-               piter != block.end () && (*piter)->is_phi (); ++piter)
+               piter != block.end () && isa<jit_phi> (*piter); ++piter)
             {
               jit_instruction *phi = *piter;
               finish_phi (phi);
@@ -1960,7 +1957,7 @@
   if (! can_remove && llvm_phi->hasOneUse () && phi->use_count () == 1)
     {
       jit_instruction *user = phi->first_use ()->user ();
-      can_remove = user->is_call (); // must be a remove
+      can_remove = isa<jit_call> (user); // must be a remove
     }
 
   if (can_remove)
--- a/src/pt-jit.h	Mon Jun 04 17:18:47 2012 -0500
+++ b/src/pt-jit.h	Tue Jun 05 16:28:09 2012 -0500
@@ -62,12 +62,11 @@
 //
 //
 // TODO:
-// 1. Rename symbol_table::symbol_record_ref -> symbol_table::symbol_reference
+// 1. Support error cases
 // 2. Support some simple matrix case (and cleanup Octave low level IR)
-// 3. Support error cases
-// 4. Fix memory leaks in JIT
-// 5. Cleanup/documentation
-// 6. ...
+// 3. Fix memory leaks in JIT
+// 4. Cleanup/documentation
+// 5. ...
 // ---------------------------------------------------------
 
 
@@ -91,6 +90,14 @@
 class octave_value;
 class tree;
 
+// Use like: isa<jit_phi> (value)
+// basically just a short cut type typing dyanmic_cast.
+template <typename T, typename U>
+bool isa (U *value)
+{
+  return dynamic_cast<T *> (value);
+}
+
 // jit_range is compatable with the llvm range structure
 struct
 jit_range
@@ -533,28 +540,22 @@
   JIT_METH(const_string)                        \
   JIT_METH(const_range)
 
-#define JIT_VISIT_IR_ABSTRACT                   \
-  JIT_METH(instruction)                         \
-  JIT_METH(terminator)
-
 #define JIT_VISIT_IR_CLASSES                    \
   JIT_VISIT_IR_NOTEMPLATE                       \
   JIT_VISIT_IR_CONST
 
-#define JIT_VISIT_IR_ALL                        \
-  JIT_VISIT_IR_CLASSES                          \
-  JIT_VISIT_IR_ABSTRACT
-
 // forward declare all ir classes
 #define JIT_METH(cname)                         \
   class jit_ ## cname;
 
-JIT_VISIT_IR_ABSTRACT
 JIT_VISIT_IR_NOTEMPLATE
 
 #undef JIT_METH
 
-// constants are typedefs from jit_const
+// ABCs which aren't included in  JIT_VISIT_IR_ALL
+class jit_instruction;
+class jit_terminator;
+
 template <typename T, jit_type *(*EXTRACT_T)(void), typename PASS_T = T,
           bool QUOTE=false>
 class jit_const;
@@ -630,21 +631,6 @@
     llvm_value = compiled;
   }
 
-#define JIT_METH(cname)                                         \
-  virtual bool is_ ## cname (void) const                        \
-  { return false; }                                             \
-                                                                \
-  virtual jit_ ## cname *to_ ## cname (void)                    \
-  { return 0; }                                                 \
-                                                                \
-  virtual const jit_ ## cname *to_ ## cname (void) const        \
-  { return 0; }
-
-JIT_VISIT_IR_NOTEMPLATE
-JIT_VISIT_IR_ABSTRACT
-
-#undef JIT_METH
-
 protected:
   std::ostream& print_indent (std::ostream& os, size_t indent) const
   {
@@ -662,16 +648,6 @@
 
 std::ostream& operator<< (std::ostream& os, const jit_value& value);
 
-#define JIT_GEN_CASTS(cname)                                    \
-  virtual bool is_ ## cname (void) const                        \
-  { return true; }                                              \
-                                                                \
-  virtual jit_ ## cname *to_ ## cname (void)                    \
-  { return this; }                                              \
-                                                                \
-  virtual const jit_ ## cname *to_ ## cname (void) const        \
-  { return this; }
-
 class
 jit_use
 {
@@ -835,20 +811,6 @@
     return argument_type (i)->to_llvm ();
   }
 
-  // generate functions of form argument_type where type is any subclass of
-  // jit_value
-#define JIT_METH(cname)                                 \
-  jit_ ## cname *argument_ ## cname (size_t i) const    \
-  {                                                     \
-    jit_value *arg = argument (i);                      \
-    return arg ? arg->to_ ## cname () : 0;              \
-  }
-
-JIT_VISIT_IR_ABSTRACT
-JIT_VISIT_IR_NOTEMPLATE
-
-#undef JIT_METH
-
   std::ostream& print_argument (std::ostream& os, size_t i) const
   {
     if (argument (i))
@@ -914,8 +876,6 @@
   jit_variable *tag (void) const;
 
   void stash_tag (jit_variable *atag);
-
-  JIT_GEN_CASTS (instruction)
 protected:
   std::vector<jit_type *> already_infered;
 private:
@@ -1155,7 +1115,6 @@
   llvm::BasicBlock *to_llvm (void) const;
 
   JIT_VALUE_ACCEPT (block)
-  JIT_GEN_CASTS (block)
 private:
   void compute_df (size_t visit_count);
 
@@ -1288,7 +1247,6 @@
   }
 
   JIT_VALUE_ACCEPT (variable)
-  JIT_GEN_CASTS (variable)
 private:
   std::string mname;
   std::stack<jit_value *> value_stack;
@@ -1357,7 +1315,6 @@
   }
 
   JIT_VALUE_ACCEPT (phi);
-  JIT_GEN_CASTS (phi)
 };
 
 class
@@ -1388,8 +1345,6 @@
   }
 
   virtual size_t sucessor_count (void) const = 0;
-
-  JIT_GEN_CASTS (terminator)
 };
 
 class
@@ -1413,7 +1368,6 @@
   }
 
   JIT_VALUE_ACCEPT (break)
-  JIT_GEN_CASTS (break)
 };
 
 class
@@ -1452,7 +1406,6 @@
   }
 
   JIT_VALUE_ACCEPT (cond_break)
-  JIT_GEN_CASTS (cond_break)
 };
 
 class
@@ -1518,7 +1471,6 @@
   virtual bool infer (void);
 
   JIT_VALUE_ACCEPT (call)
-  JIT_GEN_CASTS (call)
 private:
   const jit_function& mfunction;
 };
@@ -1553,7 +1505,6 @@
   }
 
   JIT_VALUE_ACCEPT (extract_argument)
-  JIT_GEN_CASTS (extract_argument)
 };
 
 class
@@ -1600,7 +1551,6 @@
   }
 
   JIT_VALUE_ACCEPT (store_argument)
-  JIT_GEN_CASTS (store_argument)
 };
 
 class
@@ -1952,5 +1902,11 @@
   std::vector<std::pair<std::string, bool> > arguments;
   type_bound_vector bounds;
 };
+
+// some #defines we use in the header, but not the cc file
+#undef JIT_VISIT_IR_CLASSES
+#undef JIT_VISIT_IR_CONST
+#undef JIT_VALUE_ACCEPT
+
 #endif
 #endif