Home | History | Annotate | Download | only in src
      1 //===- subzero/src/IceInstrumentation.h - ICE instrumentation ---*- C++ -*-===//
      2 //
      3 //                        The Subzero Code Generator
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief Declares the Ice::Instrumentation class.
     12 ///
     13 /// Instrumentation is an abstract class used to drive the instrumentation
     14 /// process for tools such as AddressSanitizer and MemorySanitizer. It uses a
     15 /// LoweringContext to enable the insertion of new instructions into a given
     16 /// Cfg. Although Instrumentation is an abstract class, each of its virtual
     17 /// functions has a trivial default implementation to make subclasses more
     18 /// succinct.
     19 ///
     20 /// If instrumentation is required by the command line arguments, a single
     21 /// Instrumentation subclass is instantiated and installed in the
     22 /// GlobalContext. If multiple types of instrumentation are requested, a single
     23 /// subclass is still responsible for driving the instrumentation, but it can
     24 /// use other Instrumentation subclasses however it needs to.
     25 ///
     26 //===----------------------------------------------------------------------===//
     27 
     28 #ifndef SUBZERO_SRC_ICEINSTRUMENTATION_H
     29 #define SUBZERO_SRC_ICEINSTRUMENTATION_H
     30 
     31 #include "IceDefs.h"
     32 
     33 #include <condition_variable>
     34 
     35 namespace Ice {
     36 
     37 class LoweringContext;
     38 
     39 class Instrumentation {
     40   Instrumentation() = delete;
     41   Instrumentation(const Instrumentation &) = delete;
     42   Instrumentation &operator=(const Instrumentation &) = delete;
     43 
     44 public:
     45   Instrumentation(GlobalContext *Ctx) : Ctx(Ctx) {}
     46   virtual ~Instrumentation() = default;
     47   virtual void instrumentGlobals(VariableDeclarationList &) {}
     48   void instrumentFunc(Cfg *Func);
     49   void setHasSeenGlobals();
     50 
     51 protected:
     52   virtual void instrumentInst(LoweringContext &Context);
     53   LockedPtr<VariableDeclarationList> getGlobals();
     54 
     55 private:
     56   virtual bool isInstrumentable(Cfg *) { return true; }
     57   virtual void instrumentFuncStart(LoweringContext &) {}
     58   virtual void instrumentAlloca(LoweringContext &, class InstAlloca *) {}
     59   virtual void instrumentArithmetic(LoweringContext &, class InstArithmetic *) {
     60   }
     61   virtual void instrumentBr(LoweringContext &, class InstBr *) {}
     62   virtual void instrumentCall(LoweringContext &, class InstCall *) {}
     63   virtual void instrumentCast(LoweringContext &, class InstCast *) {}
     64   virtual void instrumentExtractElement(LoweringContext &,
     65                                         class InstExtractElement *) {}
     66   virtual void instrumentFcmp(LoweringContext &, class InstFcmp *) {}
     67   virtual void instrumentIcmp(LoweringContext &, class InstIcmp *) {}
     68   virtual void instrumentInsertElement(LoweringContext &,
     69                                        class InstInsertElement *) {}
     70   virtual void instrumentIntrinsicCall(LoweringContext &,
     71                                        class InstIntrinsicCall *) {}
     72   virtual void instrumentLoad(LoweringContext &, class InstLoad *) {}
     73   virtual void instrumentPhi(LoweringContext &, class InstPhi *) {}
     74   virtual void instrumentRet(LoweringContext &, class InstRet *) {}
     75   virtual void instrumentSelect(LoweringContext &, class InstSelect *) {}
     76   virtual void instrumentStore(LoweringContext &, class InstStore *) {}
     77   virtual void instrumentSwitch(LoweringContext &, class InstSwitch *) {}
     78   virtual void instrumentUnreachable(LoweringContext &,
     79                                      class InstUnreachable *) {}
     80   virtual void instrumentStart(Cfg *) {}
     81   virtual void instrumentLocalVars(Cfg *) {}
     82   virtual void finishFunc(Cfg *) {}
     83 
     84 protected:
     85   GlobalContext *Ctx;
     86 
     87 private:
     88   bool HasSeenGlobals = false;
     89   std::mutex GlobalsSeenMutex;
     90   std::condition_variable GlobalsSeenCV;
     91 };
     92 
     93 } // end of namespace Ice
     94 
     95 #endif // SUBZERO_SRC_ICEINSTRUMENTATION_H
     96